|
|
|
|
@ -164,23 +164,38 @@ template<class T> void rectangularMatrix(py::class_<T>& c) {
|
|
|
|
|
.def(py::self != py::self, "Non-equality comparison") |
|
|
|
|
|
|
|
|
|
/* Set / get. Need to throw IndexError in order to allow iteration:
|
|
|
|
|
https://docs.python.org/3/reference/datamodel.html#object.__getitem__ */
|
|
|
|
|
https://docs.python.org/3/reference/datamodel.html#object.__getitem__
|
|
|
|
|
Using error_already_set is slightly faster than throwing index_error |
|
|
|
|
directly, but still much slower than not throwing at all. Waiting |
|
|
|
|
for https://github.com/pybind/pybind11/pull/1853 to get merged. */
|
|
|
|
|
.def("__setitem__", [](T& self, std::size_t i, const typename VectorTraits<T::Rows, typename T::Type>::Type& value) { |
|
|
|
|
if(i >= T::Cols) throw pybind11::index_error{}; |
|
|
|
|
if(i >= T::Cols) { |
|
|
|
|
PyErr_SetString(PyExc_IndexError, ""); |
|
|
|
|
throw pybind11::error_already_set{}; |
|
|
|
|
} |
|
|
|
|
self[i] = value; |
|
|
|
|
}, "Set a column at given position") |
|
|
|
|
.def("__getitem__", [](const T& self, std::size_t i) -> typename VectorTraits<T::Rows, typename T::Type>::Type { |
|
|
|
|
if(i >= T::Cols) throw pybind11::index_error{}; |
|
|
|
|
if(i >= T::Cols) { |
|
|
|
|
PyErr_SetString(PyExc_IndexError, ""); |
|
|
|
|
throw pybind11::error_already_set{}; |
|
|
|
|
} |
|
|
|
|
return self[i]; |
|
|
|
|
}, "Column at given position") |
|
|
|
|
/* Set / get for direct elements, because [a][b] = 2.5 won't work
|
|
|
|
|
without involving shared pointers */ |
|
|
|
|
.def("__setitem__", [](T& self, const std::pair<std::size_t, std::size_t>& i, typename T::Type value) { |
|
|
|
|
if(i.first >= T::Cols || i.second >= T::Rows) throw pybind11::index_error{}; |
|
|
|
|
if(i.first >= T::Cols || i.second >= T::Rows) { |
|
|
|
|
PyErr_SetString(PyExc_IndexError, ""); |
|
|
|
|
throw pybind11::error_already_set{}; |
|
|
|
|
} |
|
|
|
|
self[i.first][i.second] = value; |
|
|
|
|
}, "Set a value at given col/row") |
|
|
|
|
.def("__getitem__", [](const T& self, const std::pair<std::size_t, std::size_t>& i) { |
|
|
|
|
if(i.first >= T::Cols || i.second >= T::Rows) throw pybind11::index_error{}; |
|
|
|
|
if(i.first >= T::Cols || i.second >= T::Rows) { |
|
|
|
|
PyErr_SetString(PyExc_IndexError, ""); |
|
|
|
|
throw pybind11::error_already_set{}; |
|
|
|
|
} |
|
|
|
|
return self[i.first][i.second]; |
|
|
|
|
}, "Value at given col/row") |
|
|
|
|
|
|
|
|
|
|