1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
---|
2 | <!-- Copyright David Abrahams 2006. Distributed under the Boost --> |
---|
3 | <!-- Software License, Version 1.0. (See accompanying --> |
---|
4 | <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) --> |
---|
5 | <html> |
---|
6 | <head> |
---|
7 | <meta name="generator" |
---|
8 | content="HTML Tidy for Windows (vers 1st August 2002), see www.w3.org"> |
---|
9 | <meta http-equiv="Content-Type" |
---|
10 | content="text/html; charset=iso-8859-1"> |
---|
11 | <link rel="stylesheet" type="text/css" href="../boost.css"> |
---|
12 | <title>Boost.Python - <boost/python/slice.hpp></title> |
---|
13 | </head> |
---|
14 | <body> |
---|
15 | <table border="0" cellpadding="7" cellspacing="0" width="100%" |
---|
16 | summary="header"> |
---|
17 | <tbody> |
---|
18 | <tr> |
---|
19 | <td valign="top" width="300"> |
---|
20 | <h3><a href="../../../../index.htm"><img height="86" width="277" |
---|
21 | alt="C++ Boost" src="../../../../boost.png" border="0"></a></h3> |
---|
22 | </td> |
---|
23 | <td valign="top"> |
---|
24 | <h1 align="center"><a href="../index.html">Boost.Python</a></h1> |
---|
25 | <h2 align="center">Header <boost/python/slice.hpp></h2> |
---|
26 | </td> |
---|
27 | </tr> |
---|
28 | </tbody> |
---|
29 | </table> |
---|
30 | <hr> |
---|
31 | <h2>Contents</h2> |
---|
32 | <dl class="page-index"> |
---|
33 | <dt><a href="#introduction">Introduction</a></dt> |
---|
34 | <dt><a href="#classes">Classes</a></dt> |
---|
35 | <dd> |
---|
36 | <dl class="page-index"> |
---|
37 | <dt><a href="#slice-spec">Class <code>slice</code></a></dt> |
---|
38 | <dd> |
---|
39 | <dl class="page-index"> |
---|
40 | <dt><a href="#slice-spec-synopsis">Class <code>slice</code> |
---|
41 | synopsis</a></dt> |
---|
42 | <dt><a href="#slice-spec-ctors">Class <code>slice</code> |
---|
43 | constructors</a></dt> |
---|
44 | <dt><a href="#slice-spec-observers">Class <code>slice</code> |
---|
45 | observer functions</a></dt> |
---|
46 | </dl> |
---|
47 | </dd> |
---|
48 | </dl> |
---|
49 | </dd> |
---|
50 | <dt><a href="#examples">Example(s)</a></dt> |
---|
51 | </dl> |
---|
52 | <hr> |
---|
53 | <h2><a name="introduction"></a>Introduction</h2> |
---|
54 | <p>Exposes a <a href="ObjectWrapper.html#TypeWrapper-concept">TypeWrapper</a> |
---|
55 | for the Python <a |
---|
56 | href="http://www.python.org/doc/2.3.3/api/slice-objects.html">slice</a> |
---|
57 | type.</p> |
---|
58 | <h2><a name="classes"></a>Classes</h2> |
---|
59 | <h3><a name="slice-spec"></a>Class <code>slice</code></h3> |
---|
60 | <p>Exposes the extended slicing protocol by wrapping the built-in slice |
---|
61 | type. The semantics of the constructors and member functions defined |
---|
62 | below can be fully understood by reading the <a |
---|
63 | href="ObjectWrapper.html#TypeWrapper-concept">TypeWrapper</a> concept |
---|
64 | definition. Since <code>slice</code> is publicly derived from <code><a |
---|
65 | href="object.html#object-spec">object</a></code>, the public object |
---|
66 | interface applies to <code>slice</code> instances as well.<br> |
---|
67 | </p> |
---|
68 | <h4><a name="slice-spec-synopsis"></a>Class <code>slice</code> synopsis</h4> |
---|
69 | <pre> |
---|
70 | namespace boost { namespace python |
---|
71 | { |
---|
72 | class slice : public object |
---|
73 | { |
---|
74 | public: |
---|
75 | slice(); // create an empty slice, equivalent to [::] |
---|
76 | |
---|
77 | template <typename Int1, typename Int2> |
---|
78 | slice(Int1 start, Int2 stop); |
---|
79 | |
---|
80 | template <typename Int1, typename Int2, typename Int3> |
---|
81 | slice(Int1 start, Int2 stop, Int3 step); |
---|
82 | |
---|
83 | // Access the parameters this slice was created with. |
---|
84 | object start(); |
---|
85 | object stop(); |
---|
86 | object step(); |
---|
87 | |
---|
88 | // The return type of slice::get_indicies() |
---|
89 | template <typename RandomAccessIterator> |
---|
90 | struct range |
---|
91 | { |
---|
92 | RandomAccessIterator start; |
---|
93 | RandomAccessIterator stop; |
---|
94 | int step; |
---|
95 | }; |
---|
96 | |
---|
97 | template <typename RandomAccessIterator> |
---|
98 | range<RandomAccessIterator> |
---|
99 | get_indicies( |
---|
100 | RandomAccessIterator const& begin, |
---|
101 | RandomAccessIterator const& end); |
---|
102 | }; |
---|
103 | }} |
---|
104 | </pre> |
---|
105 | <h4><a name="slice-spec-ctors"></a>Class <code>slice</code> |
---|
106 | constructors<br> |
---|
107 | </h4> |
---|
108 | <pre>slice();<br></pre> |
---|
109 | <dl class="function-semantics"> |
---|
110 | <dt><b>Effects:</b> constructs a <code>slice</code> with default stop, start, and |
---|
111 | step values. Equivalent to the slice object created as part of the Python |
---|
112 | expression <code>base[::].</code></dt> |
---|
113 | <dt><b>Throws:</b> nothing.</dt> |
---|
114 | </dl> |
---|
115 | <pre> |
---|
116 | template <typename Int1, typename Int2> |
---|
117 | slice(Int1 start, Int2 stop); |
---|
118 | </pre> |
---|
119 | <dl class="function-semantics"> |
---|
120 | <dt><b>Requires:</b> <code>start</code>, <code>stop</code>, and <code>step</code> |
---|
121 | are of type <code><a href="object.html#slice_nil-spec">slice_nil</a></code> |
---|
122 | or convertible to type <code>object</code>.</dt> |
---|
123 | <dt><b>Effects:</b> constructs a new slice with default step value |
---|
124 | and the provided start and stop values. Equivalent to the slice |
---|
125 | object |
---|
126 | created by the built-in Python function <code><a |
---|
127 | href="http://www.python.org/doc/current/lib/built-in-funcs.html#12h-62">slice(start,stop)</a></code>, |
---|
128 | or as part of the Python expression <code>base[start:stop]</code>.</dt> |
---|
129 | <dt><b>Throws:</b> <code>error_already_set</code> and sets a Python <code>TypeError</code> |
---|
130 | exception if no conversion is possible from the arguments to type <code>object</code>.</dt> |
---|
131 | </dl> |
---|
132 | <pre> |
---|
133 | template <typename Int1, typename Int2, typename Int3> |
---|
134 | slice(Int1 start, Int2 stop, Int3 step); |
---|
135 | </pre> |
---|
136 | <dt><b>Requires:</b> <code>start</code>, <code>stop</code>, and <code>step</code> are <code>slice_nil</code> or convertible to type <code>object</code>.</dt> |
---|
137 | <dt><b>Effects:</b> constructs a new slice with start stop and step |
---|
138 | values. Equivalent to the slice object created |
---|
139 | by the built-in Python function <code><a |
---|
140 | href="http://www.python.org/doc/current/lib/built-in-funcs.html">slice(start,stop,step)</a></code>, |
---|
141 | or as part of the Python expression <code>base[start:stop:step]</code>.</dt> |
---|
142 | <dt><b>Throws:</b> <code>error_already_set</code> and sets a Python <code>TypeError</code> |
---|
143 | exception if no conversion is possible from the arguments to type |
---|
144 | object.</dt> |
---|
145 | <h4><a name="slice-spec-observers"></a>Class <code>slice</code> |
---|
146 | observer functions<br> |
---|
147 | </h4> |
---|
148 | <pre> |
---|
149 | object slice::start() const; |
---|
150 | object slice::stop() const; |
---|
151 | object slice::step() const; |
---|
152 | </pre> |
---|
153 | <dl class="function-semantics"> |
---|
154 | <dt><b>Effects:</b> None.</dt> |
---|
155 | <dt><b>Throws:</b> nothing.</dt> |
---|
156 | <dt><b>Returns:</b>the parameter that |
---|
157 | the slice was created with. If the parameter was omitted or |
---|
158 | slice_nil was used when the slice was created, than that parameter will |
---|
159 | be a reference to PyNone and compare equal to a default-constructed |
---|
160 | object. In principal, any object may be used when creating a |
---|
161 | slice object, but in practice they are usually integers.</dt> |
---|
162 | </dl> |
---|
163 | <br> |
---|
164 | <pre> |
---|
165 | template <typename RandomAccessIterator> |
---|
166 | slice::range<RandomAccessIterator> |
---|
167 | slice::get_indicies( |
---|
168 | RandomAccessIterator const& begin, |
---|
169 | RandomAccessIterator const& end) const; |
---|
170 | </pre> |
---|
171 | <dl class="function-semantics"> |
---|
172 | <dt><b>Arguments:</b> A pair of STL-conforming Random Access |
---|
173 | Iterators that form a half-open range.</dt> |
---|
174 | <dt><b>Effects:</b> Create a RandomAccessIterator pair that defines a |
---|
175 | fully-closed range within the [begin,end) range of its arguments. |
---|
176 | This function translates this slice's indicies while accounting for the |
---|
177 | effects of any PyNone or negative indicies, and non-singular step sizes.</dt> |
---|
178 | <dt><b>Returns:</b> a slice::range |
---|
179 | that has been initialized with a non-zero value of step and a pair of |
---|
180 | RandomAccessIterators that point within the range of this functions |
---|
181 | arguments and define a closed interval.</dt> |
---|
182 | <dt><b>Throws:</b> <a href="definitions.html#raise">Raises</a> a Python <code>TypeError</code> exception if any of this slice's arguments |
---|
183 | are neither references to <code>PyNone</code> nor convertible to <code>int</code>. Throws |
---|
184 | <code>std::invalid_argument</code> if the resulting range would be empty. You |
---|
185 | should always wrap calls to <code>slice::get_indicies()</code> |
---|
186 | within <code>try { ...; } catch (std::invalid_argument) {}</code> to |
---|
187 | handle this case and take appropriate action.</dt> |
---|
188 | <dt><b>Rationale</b>: closed-interval: If |
---|
189 | an open interval were used, then for step |
---|
190 | size other than 1, the required state for the end iterator would point |
---|
191 | beyond the one-past-the-end position or before the beginning of the |
---|
192 | specified range.<br> |
---|
193 | exceptions on empty slice: It is impossible to define a closed interval |
---|
194 | over an empty range, so some other form of error checking would have to |
---|
195 | be used to prevent undefined behavior. In the case where the |
---|
196 | exception is not caught, it will simply be translated to Python by the |
---|
197 | default exception handling mechanisms. </dt> |
---|
198 | </dl> |
---|
199 | <h2><a name="examples"></a><b>Examples</b></h2> |
---|
200 | <pre> |
---|
201 | using namespace boost::python; |
---|
202 | |
---|
203 | // Perform an extended slice of a Python list. |
---|
204 | // Warning: extended slicing was not supported for built-in types prior |
---|
205 | // to Python 2.3 |
---|
206 | list odd_elements(list l) |
---|
207 | { |
---|
208 | return l[slice(_,_,2)]; |
---|
209 | } |
---|
210 | |
---|
211 | // Perform a multidimensional extended slice of a Numeric.array |
---|
212 | numeric::array even_columns(numeric::array arr) |
---|
213 | { |
---|
214 | // select every other column, starting with the second, of a 2-D array. |
---|
215 | // Equivalent to "return arr[:, 1::2]" in Python. |
---|
216 | return arr[make_tuple( slice(), slice(1,_,2))]; |
---|
217 | } |
---|
218 | |
---|
219 | // Perform a summation over a slice of a std::vector. |
---|
220 | double partial_sum(std::vector<double> const& Foo, const slice index) |
---|
221 | { |
---|
222 | slice::range<std::vector<double>::const_iterator> bounds; |
---|
223 | try { |
---|
224 | bounds = index.get_indicies<>(Foo.begin(), Foo.end()); |
---|
225 | } |
---|
226 | catch (std::invalid_argument) { |
---|
227 | return 0.0; |
---|
228 | } |
---|
229 | double sum = 0.0; |
---|
230 | while (bounds.start != bounds.stop) { |
---|
231 | sum += *bounds.start; |
---|
232 | std::advance( bounds.start, bounds.step); |
---|
233 | } |
---|
234 | sum += *bounds.start; |
---|
235 | return sum; |
---|
236 | } |
---|
237 | </pre> |
---|
238 | <p>Revised 07 Febuary, 2004</p> |
---|
239 | <p><i>© Copyright <a |
---|
240 | href="mailto:jbrandmeyer@users.sourceforge.net">Jonathan Brandmeyer</a>, |
---|
241 | 2004. Modification, copying and redistribution of this document |
---|
242 | is permitted under the terms and conditions of the Boost Software |
---|
243 | License, version 1.0.<br> |
---|
244 | </i></p> |
---|
245 | </body> |
---|
246 | </html> |
---|