From 0a9abc1accb55fda767ddbf7018682fdc8695468 Mon Sep 17 00:00:00 2001 From: Christopher Lackner Date: Tue, 15 Nov 2016 18:12:51 +0100 Subject: [PATCH] array iterator to python --- libsrc/general/array.hpp | 1 + libsrc/meshing/python_mesh.cpp | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/libsrc/general/array.hpp b/libsrc/general/array.hpp index 1429f2c8..69cf2f30 100644 --- a/libsrc/general/array.hpp +++ b/libsrc/general/array.hpp @@ -60,6 +60,7 @@ namespace netgen T operator*() const { return ar[ind]; } T & operator*() { return ar[ind]; } bool operator != (ArrayIterator d2) { return ind != d2.ind; } + bool operator == (ArrayIterator d2) { return ind == d2.ind; } }; diff --git a/libsrc/meshing/python_mesh.cpp b/libsrc/meshing/python_mesh.cpp index 07ea777b..7c27092a 100644 --- a/libsrc/meshing/python_mesh.cpp +++ b/libsrc/meshing/python_mesh.cpp @@ -20,6 +20,7 @@ namespace netgen template void ExportArray (py::module &m) { + using TA = Array; string name = string("Array_") + typeid(T).name(); py::class_>(m, name.c_str()) .def ("__len__", [] ( Array &self ) { return self.Size(); } ) @@ -31,6 +32,9 @@ void ExportArray (py::module &m) return self[i]; }), 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 ; }