Skip to content

Commit 8452e27

Browse files
author
Jonathan Brandmeyer
committed
New implementation, tests, and documentation for a PySliceObject
objectmanager. [SVN r22192]
1 parent 5326800 commit 8452e27

File tree

11 files changed

+685
-8
lines changed

11 files changed

+685
-8
lines changed

build/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ if [ check-python-config ]
3434
dict.cpp
3535
tuple.cpp
3636
str.cpp
37+
slice.cpp
3738

3839
aix_init_module.cpp
3940
converter/from_python.cpp

doc/v2/object.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,16 +208,19 @@ <h2><a name="types"></a>Types</h2>
208208

209209
<p><a name="slice_nil-spec"></a></p>
210210
<pre>
211-
enum slice_nil { _ };
211+
class slice_nil;
212+
static const _ = slice_nil();
212213
</pre>
213214
A type that can be used to get the effect of leaving out an index in a
214215
Python slice expression:
215216
<pre>
216217
&gt;&gt;&gt; x[:-1]
218+
&gt;&gt;&gt; x[::-1]
217219
</pre>
218220
C++ equivalent:
219221
<pre>
220222
x.slice(_,-1)
223+
x[slice(_,_,-1)]
221224
</pre>
222225

223226
<h2><a name="classes"></a>Classes</h2>

doc/v2/reference.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,19 @@ <h2><a name="object_wrappers">Object Wrappers</a></h2>
357357
</dd>
358358
</dl>
359359
</dd>
360+
<dt><a href="slice.html">slice.hpp</a></dt>
361+
362+
<dd>
363+
<dl class="index">
364+
<dt><a href="slice.html#classes">Classes</a></dt>
365+
366+
<dd>
367+
<dl class="index">
368+
<dt><a href="slice.html#slice-spec">slice</a></dt>
369+
</dl>
370+
</dd>
371+
</dl>
372+
</dd>
360373
</dl>
361374

362375
<h2><a name="invocation">Function Invocation and Creation</a></h2>

doc/v2/slice.html

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

include/boost/python/object_core.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# include <boost/python/call.hpp>
1414
# include <boost/python/handle_fwd.hpp>
1515
# include <boost/python/errors.hpp>
16-
# include <boost/python/slice_nil.hpp>
1716
# include <boost/python/refcount.hpp>
1817
# include <boost/python/detail/preprocessor.hpp>
1918
# include <boost/python/tag.hpp>
@@ -63,6 +62,7 @@ namespace api
6362
struct item_policies;
6463
struct const_slice_policies;
6564
struct slice_policies;
65+
class slice_nil;
6666

6767
typedef proxy<const_attribute_policies> const_object_attribute;
6868
typedef proxy<attribute_policies> object_attribute;
@@ -471,4 +471,6 @@ inline PyObject* get_managed_object(object const& x, tag_t)
471471

472472
}} // namespace boost::python
473473

474+
# include <boost/python/slice_nil.hpp>
475+
474476
#endif // OBJECT_CORE_DWA2002615_HPP

0 commit comments

Comments
 (0)