blob: 791a9292658f1a1b1b735c975334a957bb12a86f [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -04002 example/example-sequences-and-iterators.cpp -- supporting Pythons' sequence protocol, iterators,
Wenzel Jakoba576e6a2015-07-29 17:51:54 +02003 etc.
Wenzel Jakob38bd7112015-07-05 20:05:44 +02004
Wenzel Jakob8cb6cb32016-04-17 20:21:41 +02005 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob38bd7112015-07-05 20:05:44 +02006
7 All rights reserved. Use of this source code is governed by a
8 BSD-style license that can be found in the LICENSE file.
9*/
10
11#include "example.h"
Jason Rhinelander3f589372016-08-07 13:05:26 -040012#include "constructor-stats.h"
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020013#include <pybind11/operators.h>
14#include <pybind11/stl.h>
Wenzel Jakob38bd7112015-07-05 20:05:44 +020015
16class Sequence {
17public:
18 Sequence(size_t size) : m_size(size) {
Jason Rhinelander3f589372016-08-07 13:05:26 -040019 print_created(this, "of size", m_size);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020020 m_data = new float[size];
21 memset(m_data, 0, sizeof(float) * size);
22 }
23
24 Sequence(const std::vector<float> &value) : m_size(value.size()) {
Jason Rhinelander3f589372016-08-07 13:05:26 -040025 print_created(this, "of size", m_size, "from std::vector");
Wenzel Jakob38bd7112015-07-05 20:05:44 +020026 m_data = new float[m_size];
27 memcpy(m_data, &value[0], sizeof(float) * m_size);
28 }
29
30 Sequence(const Sequence &s) : m_size(s.m_size) {
Jason Rhinelander3f589372016-08-07 13:05:26 -040031 print_copy_created(this);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020032 m_data = new float[m_size];
33 memcpy(m_data, s.m_data, sizeof(float)*m_size);
34 }
35
36 Sequence(Sequence &&s) : m_size(s.m_size), m_data(s.m_data) {
Jason Rhinelander3f589372016-08-07 13:05:26 -040037 print_move_created(this);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020038 s.m_size = 0;
39 s.m_data = nullptr;
40 }
41
42 ~Sequence() {
Jason Rhinelander3f589372016-08-07 13:05:26 -040043 print_destroyed(this);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020044 delete[] m_data;
45 }
46
47 Sequence &operator=(const Sequence &s) {
Jason Rhinelander3f589372016-08-07 13:05:26 -040048 if (&s != this) {
49 delete[] m_data;
50 m_size = s.m_size;
51 m_data = new float[m_size];
52 memcpy(m_data, s.m_data, sizeof(float)*m_size);
53 }
54
55 print_copy_assigned(this);
56
Wenzel Jakob38bd7112015-07-05 20:05:44 +020057 return *this;
58 }
59
60 Sequence &operator=(Sequence &&s) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +020061 if (&s != this) {
62 delete[] m_data;
63 m_size = s.m_size;
64 m_data = s.m_data;
65 s.m_size = 0;
66 s.m_data = nullptr;
67 }
Jason Rhinelander3f589372016-08-07 13:05:26 -040068
69 print_move_assigned(this);
70
Wenzel Jakob38bd7112015-07-05 20:05:44 +020071 return *this;
72 }
73
74 bool operator==(const Sequence &s) const {
75 if (m_size != s.size())
76 return false;
77 for (size_t i=0; i<m_size; ++i)
78 if (m_data[i] != s[i])
79 return false;
80 return true;
81 }
82
83 bool operator!=(const Sequence &s) const {
84 return !operator==(s);
85 }
86
87 float operator[](size_t index) const {
88 return m_data[index];
89 }
90
91 float &operator[](size_t index) {
92 return m_data[index];
93 }
94
95 bool contains(float v) const {
96 for (size_t i=0; i<m_size; ++i)
97 if (v == m_data[i])
98 return true;
99 return false;
100 }
101
102 Sequence reversed() const {
103 Sequence result(m_size);
104 for (size_t i=0; i<m_size; ++i)
105 result[m_size-i-1] = m_data[i];
106 return result;
107 }
108
109 size_t size() const { return m_size; }
110
Wenzel Jakobb2825952016-04-13 23:33:00 +0200111 const float *begin() const { return m_data; }
112 const float *end() const { return m_data+m_size; }
113
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200114private:
115 size_t m_size;
116 float *m_data;
117};
118
Jason Rhinelander5aa85be2016-08-11 21:22:05 -0400119// Interface of a map-like object that isn't (directly) an unordered_map, but provides some basic
120// map-like functionality.
121class StringMap {
122public:
123 StringMap(std::unordered_map<std::string, std::string> init = {})
124 : map(std::move(init)) {}
125
126 void set(std::string key, std::string val) {
127 map[key] = val;
128 }
129
130 std::string get(std::string key) const {
131 return map.at(key);
132 }
133
134 size_t size() const {
135 return map.size();
136 }
137
138private:
139 std::unordered_map<std::string, std::string> map;
140
141public:
142 decltype(map.cbegin()) begin() const { return map.cbegin(); }
143 decltype(map.cend()) end() const { return map.cend(); }
144};
145
146
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -0400147void init_ex_sequences_and_iterators(py::module &m) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200148 py::class_<Sequence> seq(m, "Sequence");
149
150 seq.def(py::init<size_t>())
151 .def(py::init<const std::vector<float>&>())
152 /// Bare bones interface
153 .def("__getitem__", [](const Sequence &s, size_t i) {
154 if (i >= s.size())
155 throw py::index_error();
156 return s[i];
157 })
158 .def("__setitem__", [](Sequence &s, size_t i, float v) {
159 if (i >= s.size())
160 throw py::index_error();
161 s[i] = v;
162 })
163 .def("__len__", &Sequence::size)
164 /// Optional sequence protocol operations
Wenzel Jakobb2825952016-04-13 23:33:00 +0200165 .def("__iter__", [](const Sequence &s) { return py::make_iterator(s.begin(), s.end()); },
166 py::keep_alive<0, 1>() /* Essential: keep object alive while iterator exists */)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200167 .def("__contains__", [](const Sequence &s, float v) { return s.contains(v); })
168 .def("__reversed__", [](const Sequence &s) -> Sequence { return s.reversed(); })
169 /// Slicing protocol (optional)
170 .def("__getitem__", [](const Sequence &s, py::slice slice) -> Sequence* {
Wenzel Jakob0a078052016-05-29 13:40:40 +0200171 size_t start, stop, step, slicelength;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200172 if (!slice.compute(s.size(), &start, &stop, &step, &slicelength))
173 throw py::error_already_set();
174 Sequence *seq = new Sequence(slicelength);
Wenzel Jakob0a078052016-05-29 13:40:40 +0200175 for (size_t i=0; i<slicelength; ++i) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200176 (*seq)[i] = s[start]; start += step;
177 }
178 return seq;
179 })
180 .def("__setitem__", [](Sequence &s, py::slice slice, const Sequence &value) {
Wenzel Jakob0a078052016-05-29 13:40:40 +0200181 size_t start, stop, step, slicelength;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200182 if (!slice.compute(s.size(), &start, &stop, &step, &slicelength))
183 throw py::error_already_set();
Wenzel Jakob0a078052016-05-29 13:40:40 +0200184 if (slicelength != value.size())
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200185 throw std::runtime_error("Left and right hand size of slice assignment have different sizes!");
Wenzel Jakob0a078052016-05-29 13:40:40 +0200186 for (size_t i=0; i<slicelength; ++i) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200187 s[start] = value[i]; start += step;
188 }
189 })
190 /// Comparisons
191 .def(py::self == py::self)
192 .def(py::self != py::self);
193 // Could also define py::self + py::self for concatenation, etc.
194
Jason Rhinelander5aa85be2016-08-11 21:22:05 -0400195 py::class_<StringMap> map(m, "StringMap");
196
197 map .def(py::init<>())
198 .def(py::init<std::unordered_map<std::string, std::string>>())
199 .def("__getitem__", [](const StringMap &map, std::string key) {
200 try { return map.get(key); }
201 catch (const std::out_of_range&) {
202 throw py::key_error("key '" + key + "' does not exist");
203 }
204 })
205 .def("__setitem__", &StringMap::set)
206 .def("__len__", &StringMap::size)
207 .def("__iter__", [](const StringMap &map) { return py::make_key_iterator(map.begin(), map.end()); },
208 py::keep_alive<0, 1>())
209 .def("items", [](const StringMap &map) { return py::make_iterator(map.begin(), map.end()); },
210 py::keep_alive<0, 1>())
211 ;
212
213
Wenzel Jakobb2825952016-04-13 23:33:00 +0200214#if 0
215 // Obsolete: special data structure for exposing custom iterator types to python
216 // kept here for illustrative purposes because there might be some use cases which
217 // are not covered by the much simpler py::make_iterator
218
219 struct PySequenceIterator {
220 PySequenceIterator(const Sequence &seq, py::object ref) : seq(seq), ref(ref) { }
221
222 float next() {
223 if (index == seq.size())
224 throw py::stop_iteration();
225 return seq[index++];
226 }
227
228 const Sequence &seq;
229 py::object ref; // keep a reference
230 size_t index = 0;
231 };
232
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200233 py::class_<PySequenceIterator>(seq, "Iterator")
234 .def("__iter__", [](PySequenceIterator &it) -> PySequenceIterator& { return it; })
235 .def("__next__", &PySequenceIterator::next);
Wenzel Jakobb2825952016-04-13 23:33:00 +0200236
237 On the actual Sequence object, the iterator would be constructed as follows:
238 .def("__iter__", [](py::object s) { return PySequenceIterator(s.cast<const Sequence &>(), s); })
239#endif
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200240}