From be40a4d3f130ac00dbd7284fb5df8e8be8385b3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20Sch=C3=B6berl?= Date: Fri, 6 Sep 2019 17:17:04 +0200 Subject: [PATCH] array setitem functionality --- libsrc/core/python_ngcore.hpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/libsrc/core/python_ngcore.hpp b/libsrc/core/python_ngcore.hpp index be3b84dd..45832009 100644 --- a/libsrc/core/python_ngcore.hpp +++ b/libsrc/core/python_ngcore.hpp @@ -47,6 +47,30 @@ namespace ngcore return self[i]; }, py::return_value_policy::reference) + .def ("__setitem__", + [](TFlat & self, TIND i, T val) -> T& + { + static constexpr int base = IndexBASE(); + if (i < base || i >= self.Size()+base) + throw py::index_error(); + self[i] = val; + return self[i]; + }, + py::return_value_policy::reference) + + .def ("__setitem__", + [](TFlat & self, py::slice slice, T val) + { + size_t start, stop, step, slicelength; + if (!slice.compute(self.Size(), &start, &stop, &step, &slicelength)) + throw py::error_already_set(); + static constexpr int base = IndexBASE(); + if (start < base || start+slicelength*step >= self.Size()+base) + throw py::index_error(); + for (size_t i = 0; i < slicelength; i++, start+=step) + self[start] = val; + }) + .def("__iter__", [] ( TFlat & self) { return py::make_iterator (self.begin(),self.end()); }, py::keep_alive<0,1>()) // keep array alive while iterator is used