array iterator to python

This commit is contained in:
Christopher Lackner 2016-11-15 18:12:51 +01:00
parent 8e97d91912
commit 0a9abc1acc
2 changed files with 5 additions and 0 deletions

View File

@ -60,6 +60,7 @@ namespace netgen
T operator*() const { return ar[ind]; } T operator*() const { return ar[ind]; }
T & operator*() { return ar[ind]; } T & operator*() { return ar[ind]; }
bool operator != (ArrayIterator d2) { return ind != d2.ind; } bool operator != (ArrayIterator d2) { return ind != d2.ind; }
bool operator == (ArrayIterator d2) { return ind == d2.ind; }
}; };

View File

@ -20,6 +20,7 @@ namespace netgen
template <typename T, int BASE = 0, typename TIND = int> template <typename T, int BASE = 0, typename TIND = int>
void ExportArray (py::module &m) void ExportArray (py::module &m)
{ {
using TA = Array<T,BASE,TIND>;
string name = string("Array_") + typeid(T).name(); string name = string("Array_") + typeid(T).name();
py::class_<Array<T,BASE,TIND>>(m, name.c_str()) py::class_<Array<T,BASE,TIND>>(m, name.c_str())
.def ("__len__", [] ( Array<T,BASE,TIND> &self ) { return self.Size(); } ) .def ("__len__", [] ( Array<T,BASE,TIND> &self ) { return self.Size(); } )
@ -31,6 +32,9 @@ void ExportArray (py::module &m)
return self[i]; return self[i];
}), }),
py::return_value_policy::reference) py::return_value_policy::reference)
.def("__iter__", [] ( TA & self) {
return py::make_iterator (self.begin(),self.end());
}, py::keep_alive<0,1>()) // keep array alive while iterator is used
; ;
} }