[C++-sig] Re: Adding __len__ to range objects

Raoul Gough RaoulGough at yahoo.co.uk
Wed Sep 10 00:04:59 CEST 2003

"Joel de Guzman" <djowel at gmx.co.uk> writes:

>> Raoul Gough <RaoulGough at yahoo.co.uk> wrote:
>>> I already considered and had to reject an approach like this.
>>> Actually, I was thinking of using an iterator pair, but the problem
>>> would be the same. A slice on this basis will be in error as soon as
>>> an insertion or deletion occurs within the [from,to) range in the
>>> original container, since the value lookup is deferred until elements

> Upon deeper analysis, I think you are right. Let me meditate on this
> a bit more. I certainly hope there's a better solution.

I suspect there is no way around this. Of course, using a std::map for
the proxy storage becomes less and less efficient as more and more of
the container elements get proxies created for them. In the worst case
every value in the container has a corresponding shared_proxy_impl,
and it would be better to use std::vector or std::deque to store the
proxy pointers (and maintain an invariant that the proxy for the value
at index n is also at index n in the pointer store). I guess this is
purely a performance issue, and not one of semantics.

I've made some progress on integrating the traits and the python
implementations. It's somewhat messier than I was hoping, but it
supports (where appropriate):

  o  __getitem__ with and without slicing
  o  __setitem__ without slicing
  o  __iter__
  o  append

Works for vector, list, map and iterator_pair (see testsuite.cpp,
testmap.py, testvector.py and testarray.py for examples).

The suite could trivially support len(), but I forgot to add that at
first. There's still more work to be done, e.g. setitem for slices and
a traits and/or algorithms specialization for container_proxy.

Latest updates are in boost-sandbox/.../indexing, in particular
slice_handler.hpp. I went a fairly different way for the slice
support, adding a TypeWrapper for Python slice objects and using the
Boost.Python overload resolution to pick the right __getitem__
depending on argument types. There is a magic Python function
PySlice_GetIndices that does all the index handling for us, and it all
seems to work fairly well. This probably needs a configurable result
conversion policy when added elements to the returned list.

Raoul Gough.
(setq dabbrev-case-fold-search nil)

More information about the Cplusplus-sig mailing list