Discussion:
[Swig-devel] wrapping forward iterators in Python
Kris Thielemans
2015-05-31 18:35:23 UTC
Permalink
Hi

I want to wrap some forward iterators (at present in Python). In my code, I
have n-dimensional arrays which have "flat" iterators (constructed using
begin_all(), end_all()) that go through all elements. Inspired by other
code, I did

%extend Array{
// add "flat" iterator, using begin_all()
%newobject flat(PyObject **PYTHON_SELF);
swig::SwigPyIterator* flat(PyObject **PYTHON_SELF) {
return swig::make_iterator(self->begin_all(), self->begin_all(),
self->end_all(), *PYTHON_SELF);
}

This fails, as my "flat" iterator is only a forward iterator at present.
I've solved this by (almost) copying SwigPyIteratorClosed_T from
pyiterator.swg to my own interface file to make a new type
SwigPyForwardIteratorClosed_T (which doesn't implement decr()).

I guess others could use this as well, but it then seems to make sense to
derive SwigPyIteratorClosed_T from SwigPyForwardIteratorClosed_T (and
similar for the open case).

Any opinions?

Kris



------------------------------------------------------------------------------
Chen Huang
2015-06-01 05:19:38 UTC
Permalink
Should a child class usually be more specialized than its parent? You
proposal seems to be the other way around.

Since PyIterator has a decr() interface, i think it assumes a bidirectional
iterator. So any of its child class should also be a bidirectional iterator.

Chen
Post by Kris Thielemans
Hi
I want to wrap some forward iterators (at present in Python). In my code, I
have n-dimensional arrays which have "flat" iterators (constructed using
begin_all(), end_all()) that go through all elements. Inspired by other
code, I did
%extend Array{
// add "flat" iterator, using begin_all()
%newobject flat(PyObject **PYTHON_SELF);
swig::SwigPyIterator* flat(PyObject **PYTHON_SELF) {
return swig::make_iterator(self->begin_all(), self->begin_all(),
self->end_all(), *PYTHON_SELF);
}
This fails, as my "flat" iterator is only a forward iterator at present.
I've solved this by (almost) copying SwigPyIteratorClosed_T from
pyiterator.swg to my own interface file to make a new type
SwigPyForwardIteratorClosed_T (which doesn't implement decr()).
I guess others could use this as well, but it then seems to make sense to
derive SwigPyIteratorClosed_T from SwigPyForwardIteratorClosed_T (and
similar for the open case).
Any opinions?
Kris
------------------------------------------------------------------------------
_______________________________________________
Swig-devel mailing list
https://lists.sourceforge.net/lists/listinfo/swig-devel
Loading...