Chris@16
|
1 #ifndef BOOST_PYTHON_SLICE_JDB20040105_HPP
|
Chris@16
|
2 #define BOOST_PYTHON_SLICE_JDB20040105_HPP
|
Chris@16
|
3
|
Chris@16
|
4 // Copyright (c) 2004 Jonathan Brandmeyer
|
Chris@16
|
5 // Use, modification and distribution are subject to the
|
Chris@16
|
6 // Boost Software License, Version 1.0. (See accompanying file
|
Chris@16
|
7 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8
|
Chris@16
|
9 #include <boost/python/detail/prefix.hpp>
|
Chris@16
|
10 #include <boost/config.hpp>
|
Chris@16
|
11 #include <boost/python/object.hpp>
|
Chris@16
|
12 #include <boost/python/extract.hpp>
|
Chris@16
|
13 #include <boost/python/converter/pytype_object_mgr_traits.hpp>
|
Chris@16
|
14
|
Chris@16
|
15 #include <boost/iterator/iterator_traits.hpp>
|
Chris@16
|
16
|
Chris@16
|
17 #include <iterator>
|
Chris@16
|
18 #include <algorithm>
|
Chris@16
|
19
|
Chris@16
|
20 namespace boost { namespace python {
|
Chris@16
|
21
|
Chris@16
|
22 namespace detail
|
Chris@16
|
23 {
|
Chris@16
|
24 class BOOST_PYTHON_DECL slice_base : public object
|
Chris@16
|
25 {
|
Chris@16
|
26 public:
|
Chris@16
|
27 // Get the Python objects associated with the slice. In principle, these
|
Chris@16
|
28 // may be any arbitrary Python type, but in practice they are usually
|
Chris@16
|
29 // integers. If one or more parameter is ommited in the Python expression
|
Chris@16
|
30 // that created this slice, than that parameter is None here, and compares
|
Chris@16
|
31 // equal to a default-constructed boost::python::object.
|
Chris@16
|
32 // If a user-defined type wishes to support slicing, then support for the
|
Chris@16
|
33 // special meaning associated with negative indices is up to the user.
|
Chris@16
|
34 object start() const;
|
Chris@16
|
35 object stop() const;
|
Chris@16
|
36 object step() const;
|
Chris@16
|
37
|
Chris@16
|
38 protected:
|
Chris@16
|
39 explicit slice_base(PyObject*, PyObject*, PyObject*);
|
Chris@16
|
40
|
Chris@16
|
41 BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(slice_base, object)
|
Chris@16
|
42 };
|
Chris@16
|
43 }
|
Chris@16
|
44
|
Chris@16
|
45 class slice : public detail::slice_base
|
Chris@16
|
46 {
|
Chris@16
|
47 typedef detail::slice_base base;
|
Chris@16
|
48 public:
|
Chris@16
|
49 // Equivalent to slice(::)
|
Chris@16
|
50 slice() : base(0,0,0) {}
|
Chris@16
|
51
|
Chris@16
|
52 // Each argument must be slice_nil, or implicitly convertable to object.
|
Chris@16
|
53 // They should normally be integers.
|
Chris@16
|
54 template<typename Integer1, typename Integer2>
|
Chris@16
|
55 slice( Integer1 start, Integer2 stop)
|
Chris@16
|
56 : base( object(start).ptr(), object(stop).ptr(), 0 )
|
Chris@16
|
57 {}
|
Chris@16
|
58
|
Chris@16
|
59 template<typename Integer1, typename Integer2, typename Integer3>
|
Chris@16
|
60 slice( Integer1 start, Integer2 stop, Integer3 stride)
|
Chris@16
|
61 : base( object(start).ptr(), object(stop).ptr(), object(stride).ptr() )
|
Chris@16
|
62 {}
|
Chris@16
|
63
|
Chris@16
|
64 // The following algorithm is intended to automate the process of
|
Chris@16
|
65 // determining a slice range when you want to fully support negative
|
Chris@16
|
66 // indices and non-singular step sizes. Its functionallity is simmilar to
|
Chris@16
|
67 // PySlice_GetIndicesEx() in the Python/C API, but tailored for C++ users.
|
Chris@16
|
68 // This template returns a slice::range struct that, when used in the
|
Chris@16
|
69 // following iterative loop, will traverse a slice of the function's
|
Chris@16
|
70 // arguments.
|
Chris@16
|
71 // while (start != end) {
|
Chris@16
|
72 // do_foo(...);
|
Chris@16
|
73 // std::advance( start, step);
|
Chris@16
|
74 // }
|
Chris@16
|
75 // do_foo(...); // repeat exactly once more.
|
Chris@16
|
76
|
Chris@16
|
77 // Arguments: a [begin, end) pair of STL-conforming random-access iterators.
|
Chris@16
|
78
|
Chris@16
|
79 // Return: slice::range, where start and stop define a _closed_ interval
|
Chris@16
|
80 // that covers at most [begin, end-1] of the provided arguments, and a step
|
Chris@16
|
81 // that is non-zero.
|
Chris@16
|
82
|
Chris@16
|
83 // Throws: error_already_set() if any of the indices are neither None nor
|
Chris@16
|
84 // integers, or the slice has a step value of zero.
|
Chris@16
|
85 // std::invalid_argument if the resulting range would be empty. Normally,
|
Chris@16
|
86 // you should catch this exception and return an empty sequence of the
|
Chris@16
|
87 // appropriate type.
|
Chris@16
|
88
|
Chris@16
|
89 // Performance: constant time for random-access iterators.
|
Chris@16
|
90
|
Chris@16
|
91 // Rationale:
|
Chris@16
|
92 // closed-interval: If an open interval were used, then for a non-singular
|
Chris@16
|
93 // value for step, the required state for the end iterator could be
|
Chris@16
|
94 // beyond the one-past-the-end postion of the specified range. While
|
Chris@16
|
95 // probably harmless, the behavior of STL-conforming iterators is
|
Chris@16
|
96 // undefined in this case.
|
Chris@16
|
97 // exceptions on zero-length range: It is impossible to define a closed
|
Chris@16
|
98 // interval over an empty range, so some other form of error checking
|
Chris@16
|
99 // would have to be used by the user to prevent undefined behavior. In
|
Chris@16
|
100 // the case where the user fails to catch the exception, it will simply
|
Chris@16
|
101 // be translated to Python by the default exception handling mechanisms.
|
Chris@16
|
102
|
Chris@16
|
103 template<typename RandomAccessIterator>
|
Chris@16
|
104 struct range
|
Chris@16
|
105 {
|
Chris@16
|
106 RandomAccessIterator start;
|
Chris@16
|
107 RandomAccessIterator stop;
|
Chris@16
|
108 typename iterator_difference<RandomAccessIterator>::type step;
|
Chris@16
|
109 };
|
Chris@16
|
110
|
Chris@16
|
111 template<typename RandomAccessIterator>
|
Chris@16
|
112 slice::range<RandomAccessIterator>
|
Chris@16
|
113 get_indices( const RandomAccessIterator& begin,
|
Chris@16
|
114 const RandomAccessIterator& end) const
|
Chris@16
|
115 {
|
Chris@16
|
116 // This is based loosely on PySlice_GetIndicesEx(), but it has been
|
Chris@16
|
117 // carefully crafted to ensure that these iterators never fall out of
|
Chris@16
|
118 // the range of the container.
|
Chris@16
|
119 slice::range<RandomAccessIterator> ret;
|
Chris@16
|
120
|
Chris@16
|
121 typedef typename iterator_difference<RandomAccessIterator>::type difference_type;
|
Chris@16
|
122 difference_type max_dist = boost::detail::distance(begin, end);
|
Chris@16
|
123
|
Chris@16
|
124 object slice_start = this->start();
|
Chris@16
|
125 object slice_stop = this->stop();
|
Chris@16
|
126 object slice_step = this->step();
|
Chris@16
|
127
|
Chris@16
|
128 // Extract the step.
|
Chris@16
|
129 if (slice_step == object()) {
|
Chris@16
|
130 ret.step = 1;
|
Chris@16
|
131 }
|
Chris@16
|
132 else {
|
Chris@16
|
133 ret.step = extract<long>( slice_step);
|
Chris@16
|
134 if (ret.step == 0) {
|
Chris@16
|
135 PyErr_SetString( PyExc_IndexError, "step size cannot be zero.");
|
Chris@16
|
136 throw_error_already_set();
|
Chris@16
|
137 }
|
Chris@16
|
138 }
|
Chris@16
|
139
|
Chris@16
|
140 // Setup the start iterator.
|
Chris@16
|
141 if (slice_start == object()) {
|
Chris@16
|
142 if (ret.step < 0) {
|
Chris@16
|
143 ret.start = end;
|
Chris@16
|
144 --ret.start;
|
Chris@16
|
145 }
|
Chris@16
|
146 else
|
Chris@16
|
147 ret.start = begin;
|
Chris@16
|
148 }
|
Chris@16
|
149 else {
|
Chris@16
|
150 difference_type i = extract<long>( slice_start);
|
Chris@16
|
151 if (i >= max_dist && ret.step > 0)
|
Chris@16
|
152 throw std::invalid_argument( "Zero-length slice");
|
Chris@16
|
153 if (i >= 0) {
|
Chris@16
|
154 ret.start = begin;
|
Chris@16
|
155 BOOST_USING_STD_MIN();
|
Chris@16
|
156 std::advance( ret.start, min BOOST_PREVENT_MACRO_SUBSTITUTION(i, max_dist-1));
|
Chris@16
|
157 }
|
Chris@16
|
158 else {
|
Chris@16
|
159 if (i < -max_dist && ret.step < 0)
|
Chris@16
|
160 throw std::invalid_argument( "Zero-length slice");
|
Chris@16
|
161 ret.start = end;
|
Chris@16
|
162 // Advance start (towards begin) not farther than begin.
|
Chris@16
|
163 std::advance( ret.start, (-i < max_dist) ? i : -max_dist );
|
Chris@16
|
164 }
|
Chris@16
|
165 }
|
Chris@16
|
166
|
Chris@16
|
167 // Set up the stop iterator. This one is a little trickier since slices
|
Chris@16
|
168 // define a [) range, and we are returning a [] range.
|
Chris@16
|
169 if (slice_stop == object()) {
|
Chris@16
|
170 if (ret.step < 0) {
|
Chris@16
|
171 ret.stop = begin;
|
Chris@16
|
172 }
|
Chris@16
|
173 else {
|
Chris@16
|
174 ret.stop = end;
|
Chris@16
|
175 std::advance( ret.stop, -1);
|
Chris@16
|
176 }
|
Chris@16
|
177 }
|
Chris@16
|
178 else {
|
Chris@16
|
179 difference_type i = extract<long>(slice_stop);
|
Chris@16
|
180 // First, branch on which direction we are going with this.
|
Chris@16
|
181 if (ret.step < 0) {
|
Chris@16
|
182 if (i+1 >= max_dist || i == -1)
|
Chris@16
|
183 throw std::invalid_argument( "Zero-length slice");
|
Chris@16
|
184
|
Chris@16
|
185 if (i >= 0) {
|
Chris@16
|
186 ret.stop = begin;
|
Chris@16
|
187 std::advance( ret.stop, i+1);
|
Chris@16
|
188 }
|
Chris@16
|
189 else { // i is negative, but more negative than -1.
|
Chris@16
|
190 ret.stop = end;
|
Chris@16
|
191 std::advance( ret.stop, (-i < max_dist) ? i : -max_dist);
|
Chris@16
|
192 }
|
Chris@16
|
193 }
|
Chris@16
|
194 else { // stepping forward
|
Chris@16
|
195 if (i == 0 || -i >= max_dist)
|
Chris@16
|
196 throw std::invalid_argument( "Zero-length slice");
|
Chris@16
|
197
|
Chris@16
|
198 if (i > 0) {
|
Chris@16
|
199 ret.stop = begin;
|
Chris@16
|
200 std::advance( ret.stop, (std::min)( i-1, max_dist-1));
|
Chris@16
|
201 }
|
Chris@16
|
202 else { // i is negative, but not more negative than -max_dist
|
Chris@16
|
203 ret.stop = end;
|
Chris@16
|
204 std::advance( ret.stop, i-1);
|
Chris@16
|
205 }
|
Chris@16
|
206 }
|
Chris@16
|
207 }
|
Chris@16
|
208
|
Chris@16
|
209 // Now the fun part, handling the possibilites surrounding step.
|
Chris@16
|
210 // At this point, step has been initialized, ret.stop, and ret.step
|
Chris@16
|
211 // represent the widest possible range that could be traveled
|
Chris@16
|
212 // (inclusive), and final_dist is the maximum distance covered by the
|
Chris@16
|
213 // slice.
|
Chris@16
|
214 typename iterator_difference<RandomAccessIterator>::type final_dist =
|
Chris@16
|
215 boost::detail::distance( ret.start, ret.stop);
|
Chris@16
|
216
|
Chris@16
|
217 // First case, if both ret.start and ret.stop are equal, then step
|
Chris@16
|
218 // is irrelevant and we can return here.
|
Chris@16
|
219 if (final_dist == 0)
|
Chris@16
|
220 return ret;
|
Chris@16
|
221
|
Chris@16
|
222 // Second, if there is a sign mismatch, than the resulting range and
|
Chris@16
|
223 // step size conflict: std::advance( ret.start, ret.step) goes away from
|
Chris@16
|
224 // ret.stop.
|
Chris@16
|
225 if ((final_dist > 0) != (ret.step > 0))
|
Chris@16
|
226 throw std::invalid_argument( "Zero-length slice.");
|
Chris@16
|
227
|
Chris@16
|
228 // Finally, if the last step puts us past the end, we move ret.stop
|
Chris@16
|
229 // towards ret.start in the amount of the remainder.
|
Chris@16
|
230 // I don't remember all of the oolies surrounding negative modulii,
|
Chris@16
|
231 // so I am handling each of these cases separately.
|
Chris@16
|
232 if (final_dist < 0) {
|
Chris@16
|
233 difference_type remainder = -final_dist % -ret.step;
|
Chris@16
|
234 std::advance( ret.stop, remainder);
|
Chris@16
|
235 }
|
Chris@16
|
236 else {
|
Chris@16
|
237 difference_type remainder = final_dist % ret.step;
|
Chris@16
|
238 std::advance( ret.stop, -remainder);
|
Chris@16
|
239 }
|
Chris@16
|
240
|
Chris@16
|
241 return ret;
|
Chris@16
|
242 }
|
Chris@16
|
243
|
Chris@16
|
244 // Incorrect spelling. DO NOT USE. Only here for backward compatibility.
|
Chris@16
|
245 // Corrected 2011-06-14.
|
Chris@16
|
246 template<typename RandomAccessIterator>
|
Chris@16
|
247 slice::range<RandomAccessIterator>
|
Chris@16
|
248 get_indicies( const RandomAccessIterator& begin,
|
Chris@16
|
249 const RandomAccessIterator& end) const
|
Chris@16
|
250 {
|
Chris@16
|
251 return get_indices(begin, end);
|
Chris@16
|
252 }
|
Chris@16
|
253
|
Chris@16
|
254 public:
|
Chris@16
|
255 // This declaration, in conjunction with the specialization of
|
Chris@16
|
256 // object_manager_traits<> below, allows C++ functions accepting slice
|
Chris@16
|
257 // arguments to be called from from Python. These constructors should never
|
Chris@16
|
258 // be used in client code.
|
Chris@16
|
259 BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(slice, detail::slice_base)
|
Chris@16
|
260 };
|
Chris@16
|
261
|
Chris@16
|
262
|
Chris@16
|
263 namespace converter {
|
Chris@16
|
264
|
Chris@16
|
265 template<>
|
Chris@16
|
266 struct object_manager_traits<slice>
|
Chris@16
|
267 : pytype_object_manager_traits<&PySlice_Type, slice>
|
Chris@16
|
268 {
|
Chris@16
|
269 };
|
Chris@16
|
270
|
Chris@16
|
271 } // !namesapce converter
|
Chris@16
|
272
|
Chris@16
|
273 } } // !namespace ::boost::python
|
Chris@16
|
274
|
Chris@16
|
275
|
Chris@16
|
276 #endif // !defined BOOST_PYTHON_SLICE_JDB20040105_HPP
|