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