You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
8.6 KiB

  1. /*
  2. tests/test_sequences_and_iterators.cpp -- supporting Pythons' sequence protocol, iterators,
  3. etc.
  4. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  5. All rights reserved. Use of this source code is governed by a
  6. BSD-style license that can be found in the LICENSE file.
  7. */
  8. #include "pybind11_tests.h"
  9. #include "constructor_stats.h"
  10. #include <pybind11/operators.h>
  11. #include <pybind11/stl.h>
  12. class Sequence {
  13. public:
  14. Sequence(size_t size) : m_size(size) {
  15. print_created(this, "of size", m_size);
  16. m_data = new float[size];
  17. memset(m_data, 0, sizeof(float) * size);
  18. }
  19. Sequence(const std::vector<float> &value) : m_size(value.size()) {
  20. print_created(this, "of size", m_size, "from std::vector");
  21. m_data = new float[m_size];
  22. memcpy(m_data, &value[0], sizeof(float) * m_size);
  23. }
  24. Sequence(const Sequence &s) : m_size(s.m_size) {
  25. print_copy_created(this);
  26. m_data = new float[m_size];
  27. memcpy(m_data, s.m_data, sizeof(float)*m_size);
  28. }
  29. Sequence(Sequence &&s) : m_size(s.m_size), m_data(s.m_data) {
  30. print_move_created(this);
  31. s.m_size = 0;
  32. s.m_data = nullptr;
  33. }
  34. ~Sequence() {
  35. print_destroyed(this);
  36. delete[] m_data;
  37. }
  38. Sequence &operator=(const Sequence &s) {
  39. if (&s != this) {
  40. delete[] m_data;
  41. m_size = s.m_size;
  42. m_data = new float[m_size];
  43. memcpy(m_data, s.m_data, sizeof(float)*m_size);
  44. }
  45. print_copy_assigned(this);
  46. return *this;
  47. }
  48. Sequence &operator=(Sequence &&s) {
  49. if (&s != this) {
  50. delete[] m_data;
  51. m_size = s.m_size;
  52. m_data = s.m_data;
  53. s.m_size = 0;
  54. s.m_data = nullptr;
  55. }
  56. print_move_assigned(this);
  57. return *this;
  58. }
  59. bool operator==(const Sequence &s) const {
  60. if (m_size != s.size())
  61. return false;
  62. for (size_t i=0; i<m_size; ++i)
  63. if (m_data[i] != s[i])
  64. return false;
  65. return true;
  66. }
  67. bool operator!=(const Sequence &s) const {
  68. return !operator==(s);
  69. }
  70. float operator[](size_t index) const {
  71. return m_data[index];
  72. }
  73. float &operator[](size_t index) {
  74. return m_data[index];
  75. }
  76. bool contains(float v) const {
  77. for (size_t i=0; i<m_size; ++i)
  78. if (v == m_data[i])
  79. return true;
  80. return false;
  81. }
  82. Sequence reversed() const {
  83. Sequence result(m_size);
  84. for (size_t i=0; i<m_size; ++i)
  85. result[m_size-i-1] = m_data[i];
  86. return result;
  87. }
  88. size_t size() const { return m_size; }
  89. const float *begin() const { return m_data; }
  90. const float *end() const { return m_data+m_size; }
  91. private:
  92. size_t m_size;
  93. float *m_data;
  94. };
  95. class IntPairs {
  96. public:
  97. IntPairs(std::vector<std::pair<int, int>> data) : data_(std::move(data)) {}
  98. const std::pair<int, int>* begin() const { return data_.data(); }
  99. private:
  100. std::vector<std::pair<int, int>> data_;
  101. };
  102. // Interface of a map-like object that isn't (directly) an unordered_map, but provides some basic
  103. // map-like functionality.
  104. class StringMap {
  105. public:
  106. StringMap() = default;
  107. StringMap(std::unordered_map<std::string, std::string> init)
  108. : map(std::move(init)) {}
  109. void set(std::string key, std::string val) {
  110. map[key] = val;
  111. }
  112. std::string get(std::string key) const {
  113. return map.at(key);
  114. }
  115. size_t size() const {
  116. return map.size();
  117. }
  118. private:
  119. std::unordered_map<std::string, std::string> map;
  120. public:
  121. decltype(map.cbegin()) begin() const { return map.cbegin(); }
  122. decltype(map.cend()) end() const { return map.cend(); }
  123. };
  124. template<typename T>
  125. class NonZeroIterator {
  126. const T* ptr_;
  127. public:
  128. NonZeroIterator(const T* ptr) : ptr_(ptr) {}
  129. const T& operator*() const { return *ptr_; }
  130. NonZeroIterator& operator++() { ++ptr_; return *this; }
  131. };
  132. class NonZeroSentinel {};
  133. template<typename A, typename B>
  134. bool operator==(const NonZeroIterator<std::pair<A, B>>& it, const NonZeroSentinel&) {
  135. return !(*it).first || !(*it).second;
  136. }
  137. test_initializer sequences_and_iterators([](py::module &m) {
  138. py::class_<Sequence> seq(m, "Sequence");
  139. seq.def(py::init<size_t>())
  140. .def(py::init<const std::vector<float>&>())
  141. /// Bare bones interface
  142. .def("__getitem__", [](const Sequence &s, size_t i) {
  143. if (i >= s.size())
  144. throw py::index_error();
  145. return s[i];
  146. })
  147. .def("__setitem__", [](Sequence &s, size_t i, float v) {
  148. if (i >= s.size())
  149. throw py::index_error();
  150. s[i] = v;
  151. })
  152. .def("__len__", &Sequence::size)
  153. /// Optional sequence protocol operations
  154. .def("__iter__", [](const Sequence &s) { return py::make_iterator(s.begin(), s.end()); },
  155. py::keep_alive<0, 1>() /* Essential: keep object alive while iterator exists */)
  156. .def("__contains__", [](const Sequence &s, float v) { return s.contains(v); })
  157. .def("__reversed__", [](const Sequence &s) -> Sequence { return s.reversed(); })
  158. /// Slicing protocol (optional)
  159. .def("__getitem__", [](const Sequence &s, py::slice slice) -> Sequence* {
  160. size_t start, stop, step, slicelength;
  161. if (!slice.compute(s.size(), &start, &stop, &step, &slicelength))
  162. throw py::error_already_set();
  163. Sequence *seq = new Sequence(slicelength);
  164. for (size_t i=0; i<slicelength; ++i) {
  165. (*seq)[i] = s[start]; start += step;
  166. }
  167. return seq;
  168. })
  169. .def("__setitem__", [](Sequence &s, py::slice slice, const Sequence &value) {
  170. size_t start, stop, step, slicelength;
  171. if (!slice.compute(s.size(), &start, &stop, &step, &slicelength))
  172. throw py::error_already_set();
  173. if (slicelength != value.size())
  174. throw std::runtime_error("Left and right hand size of slice assignment have different sizes!");
  175. for (size_t i=0; i<slicelength; ++i) {
  176. s[start] = value[i]; start += step;
  177. }
  178. })
  179. /// Comparisons
  180. .def(py::self == py::self)
  181. .def(py::self != py::self);
  182. // Could also define py::self + py::self for concatenation, etc.
  183. py::class_<StringMap> map(m, "StringMap");
  184. map .def(py::init<>())
  185. .def(py::init<std::unordered_map<std::string, std::string>>())
  186. .def("__getitem__", [](const StringMap &map, std::string key) {
  187. try { return map.get(key); }
  188. catch (const std::out_of_range&) {
  189. throw py::key_error("key '" + key + "' does not exist");
  190. }
  191. })
  192. .def("__setitem__", &StringMap::set)
  193. .def("__len__", &StringMap::size)
  194. .def("__iter__", [](const StringMap &map) { return py::make_key_iterator(map.begin(), map.end()); },
  195. py::keep_alive<0, 1>())
  196. .def("items", [](const StringMap &map) { return py::make_iterator(map.begin(), map.end()); },
  197. py::keep_alive<0, 1>())
  198. ;
  199. py::class_<IntPairs>(m, "IntPairs")
  200. .def(py::init<std::vector<std::pair<int, int>>>())
  201. .def("nonzero", [](const IntPairs& s) {
  202. return py::make_iterator(NonZeroIterator<std::pair<int, int>>(s.begin()), NonZeroSentinel());
  203. }, py::keep_alive<0, 1>())
  204. .def("nonzero_keys", [](const IntPairs& s) {
  205. return py::make_key_iterator(NonZeroIterator<std::pair<int, int>>(s.begin()), NonZeroSentinel());
  206. }, py::keep_alive<0, 1>());
  207. #if 0
  208. // Obsolete: special data structure for exposing custom iterator types to python
  209. // kept here for illustrative purposes because there might be some use cases which
  210. // are not covered by the much simpler py::make_iterator
  211. struct PySequenceIterator {
  212. PySequenceIterator(const Sequence &seq, py::object ref) : seq(seq), ref(ref) { }
  213. float next() {
  214. if (index == seq.size())
  215. throw py::stop_iteration();
  216. return seq[index++];
  217. }
  218. const Sequence &seq;
  219. py::object ref; // keep a reference
  220. size_t index = 0;
  221. };
  222. py::class_<PySequenceIterator>(seq, "Iterator")
  223. .def("__iter__", [](PySequenceIterator &it) -> PySequenceIterator& { return it; })
  224. .def("__next__", &PySequenceIterator::next);
  225. On the actual Sequence object, the iterator would be constructed as follows:
  226. .def("__iter__", [](py::object s) { return PySequenceIterator(s.cast<const Sequence &>(), s); })
  227. #endif
  228. });