blob: 416c1d0103f2f7fc347d8faf6e9067844990647f [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
Wenzel Jakoba576e6a2015-07-29 17:51:54 +02002 example/example6.cpp -- supporting Pythons' sequence protocol, iterators,
3 etc.
Wenzel Jakob38bd7112015-07-05 20:05:44 +02004
5 Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
6
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"
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020012#include <pybind11/operators.h>
13#include <pybind11/stl.h>
Wenzel Jakob38bd7112015-07-05 20:05:44 +020014
15class Sequence {
16public:
17 Sequence(size_t size) : m_size(size) {
18 std::cout << "Value constructor: Creating a sequence with " << m_size << " entries" << std::endl;
19 m_data = new float[size];
20 memset(m_data, 0, sizeof(float) * size);
21 }
22
23 Sequence(const std::vector<float> &value) : m_size(value.size()) {
24 std::cout << "Value constructor: Creating a sequence with " << m_size << " entries" << std::endl;
25 m_data = new float[m_size];
26 memcpy(m_data, &value[0], sizeof(float) * m_size);
27 }
28
29 Sequence(const Sequence &s) : m_size(s.m_size) {
30 std::cout << "Copy constructor: Creating a sequence with " << m_size << " entries" << std::endl;
31 m_data = new float[m_size];
32 memcpy(m_data, s.m_data, sizeof(float)*m_size);
33 }
34
35 Sequence(Sequence &&s) : m_size(s.m_size), m_data(s.m_data) {
36 std::cout << "Move constructor: Creating a sequence with " << m_size << " entries" << std::endl;
37 s.m_size = 0;
38 s.m_data = nullptr;
39 }
40
41 ~Sequence() {
42 std::cout << "Freeing a sequence with " << m_size << " entries" << std::endl;
43 delete[] m_data;
44 }
45
46 Sequence &operator=(const Sequence &s) {
47 std::cout << "Assignment operator: Creating a sequence with " << s.m_size << " entries" << std::endl;
48 delete[] m_data;
49 m_size = s.m_size;
50 m_data = new float[m_size];
51 memcpy(m_data, s.m_data, sizeof(float)*m_size);
52 return *this;
53 }
54
55 Sequence &operator=(Sequence &&s) {
56 std::cout << "Move assignment operator: Creating a sequence with " << s.m_size << " entries" << std::endl;
57 if (&s != this) {
58 delete[] m_data;
59 m_size = s.m_size;
60 m_data = s.m_data;
61 s.m_size = 0;
62 s.m_data = nullptr;
63 }
64 return *this;
65 }
66
67 bool operator==(const Sequence &s) const {
68 if (m_size != s.size())
69 return false;
70 for (size_t i=0; i<m_size; ++i)
71 if (m_data[i] != s[i])
72 return false;
73 return true;
74 }
75
76 bool operator!=(const Sequence &s) const {
77 return !operator==(s);
78 }
79
80 float operator[](size_t index) const {
81 return m_data[index];
82 }
83
84 float &operator[](size_t index) {
85 return m_data[index];
86 }
87
88 bool contains(float v) const {
89 for (size_t i=0; i<m_size; ++i)
90 if (v == m_data[i])
91 return true;
92 return false;
93 }
94
95 Sequence reversed() const {
96 Sequence result(m_size);
97 for (size_t i=0; i<m_size; ++i)
98 result[m_size-i-1] = m_data[i];
99 return result;
100 }
101
102 size_t size() const { return m_size; }
103
104private:
105 size_t m_size;
106 float *m_data;
107};
108
109namespace {
110 // Special iterator data structure for python
111 struct PySequenceIterator {
112 PySequenceIterator(const Sequence &seq, py::object ref) : seq(seq), ref(ref) { }
113
114 float next() {
115 if (index == seq.size())
116 throw py::stop_iteration();
117 return seq[index++];
118 }
119
120 const Sequence &seq;
121 py::object ref; // keep a reference
122 size_t index = 0;
123 };
124};
125
126void init_ex6(py::module &m) {
127 py::class_<Sequence> seq(m, "Sequence");
128
129 seq.def(py::init<size_t>())
130 .def(py::init<const std::vector<float>&>())
131 /// Bare bones interface
132 .def("__getitem__", [](const Sequence &s, size_t i) {
133 if (i >= s.size())
134 throw py::index_error();
135 return s[i];
136 })
137 .def("__setitem__", [](Sequence &s, size_t i, float v) {
138 if (i >= s.size())
139 throw py::index_error();
140 s[i] = v;
141 })
142 .def("__len__", &Sequence::size)
143 /// Optional sequence protocol operations
144 .def("__iter__", [](py::object s) { return PySequenceIterator(s.cast<const Sequence &>(), s); })
145 .def("__contains__", [](const Sequence &s, float v) { return s.contains(v); })
146 .def("__reversed__", [](const Sequence &s) -> Sequence { return s.reversed(); })
147 /// Slicing protocol (optional)
148 .def("__getitem__", [](const Sequence &s, py::slice slice) -> Sequence* {
149 py::ssize_t start, stop, step, slicelength;
150 if (!slice.compute(s.size(), &start, &stop, &step, &slicelength))
151 throw py::error_already_set();
152 Sequence *seq = new Sequence(slicelength);
153 for (int i=0; i<slicelength; ++i) {
154 (*seq)[i] = s[start]; start += step;
155 }
156 return seq;
157 })
158 .def("__setitem__", [](Sequence &s, py::slice slice, const Sequence &value) {
159 py::ssize_t start, stop, step, slicelength;
160 if (!slice.compute(s.size(), &start, &stop, &step, &slicelength))
161 throw py::error_already_set();
162 if ((size_t) slicelength != value.size())
163 throw std::runtime_error("Left and right hand size of slice assignment have different sizes!");
164 for (int i=0; i<slicelength; ++i) {
165 s[start] = value[i]; start += step;
166 }
167 })
168 /// Comparisons
169 .def(py::self == py::self)
170 .def(py::self != py::self);
171 // Could also define py::self + py::self for concatenation, etc.
172
173 py::class_<PySequenceIterator>(seq, "Iterator")
174 .def("__iter__", [](PySequenceIterator &it) -> PySequenceIterator& { return it; })
175 .def("__next__", &PySequenceIterator::next);
176}