[C++-SIG] How to use CXX...
Steve Harris
sharris at primus.com
Tue May 23 02:41:50 CEST 2000
"Barry Scott" <barry at scottb.demon.co.uk> writes:
[...]
> > 4. I haven't gotten to trying to wrap containers yet. What if your C++
> > type has a method like
> > const std::list<foo>& get_foos() const;
> > where the type exposes a nested container of some other type. That
> > looks like you could coax some Pythonic behavior out of the pattern,
> > but how would you go about wrapping it? Do you have to copy the whole
> > list for each request for the Python-wrapped sequence?
>
> No copy needed. You can implement the methods required for a
> sequence type and be a proxy between Python and the std::list.
Looking at the sequence methods more closely, it looks like Python
imposes random-access requirements on the underlying sequence. The
sequence_item() method allows the caller to fetch an element at a
particular index. Has anyone tried to adapt this to something like
std::list?
It seems like you _could_ find an item by iterating from begin() a
given number of times, making sure that you're not at end(), but
that's horribly inefficient. You could try to cache the last index
request and say, "If the requested index is just one higher than the
last one, then just bump my iterator forward by one." This could get
into some weird situations if the underlying list changed - like if
some elements got inserted before your "current" position. The list
iterators would be stable, but the indexes would not be.
An alternate idea is to copy the list iterators into a vector. That
could also be costly if the proxy sequence has to be constructed again
and again.
Has anyone tried to create such a Standard C++ container proxy?
[...]
--
Steven E. Harris
Primus Knowledge Solutions, Inc.
http://www.primus.com
More information about the Cplusplus-sig
mailing list