Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/python/doc/v2/slice.html @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 9.5 KB
Line 
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 - &lt;boost/python/slice.hpp&gt;</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 &lt;boost/python/slice.hpp&gt;</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>
41synopsis</a></dt>
42          <dt><a href="#slice-spec-ctors">Class <code>slice</code>
43constructors</a></dt>
44          <dt><a href="#slice-spec-observers">Class <code>slice</code>
45observer 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>
55for the Python <a
56 href="http://www.python.org/doc/2.3.3/api/slice-objects.html">slice</a>
57type.</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
61type. The semantics of the constructors and member functions defined
62below can be fully understood by reading the <a
63 href="ObjectWrapper.html#TypeWrapper-concept">TypeWrapper</a> concept
64definition. Since <code>slice</code> is publicly derived from <code><a
65 href="object.html#object-spec">object</a></code>, the public object
66interface 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>
70namespace boost { namespace python
71{
72  class slice : public object
73  {
74   public:
75      slice(); // create an empty slice, equivalent to [::]
76
77      template &lt;typename Int1, typename Int2&gt;
78      slice(Int1 start, Int2 stop);
79
80      template &lt;typename Int1, typename Int2, typename Int3&gt;
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 &lt;typename RandomAccessIterator&gt;
90      struct range
91      {
92          RandomAccessIterator start;
93          RandomAccessIterator stop;
94          int step;
95      };
96
97      template &lt;typename RandomAccessIterator&gt;
98      range&lt;RandomAccessIterator&gt;
99      get_indicies(
100          RandomAccessIterator const&amp; begin,
101          RandomAccessIterator const&amp; end);
102  };
103}}
104</pre>
105<h4><a name="slice-spec-ctors"></a>Class <code>slice</code>
106constructors<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
111step values.&nbsp; Equivalent to the slice object created as part of the Python
112expression <code>base[::].</code></dt>
113  <dt><b>Throws:</b> nothing.</dt>
114</dl>
115<pre>
116template &lt;typename Int1, typename Int2&gt;
117slice(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
124and the provided start and stop values.&nbsp; Equivalent to the slice
125object
126created 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>,
128or 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>
130exception if no conversion is possible from the arguments to type <code>object</code>.</dt>
131</dl>
132<pre>
133template &lt;typename Int1, typename Int2, typename Int3&gt;
134slice(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
138values.&nbsp; Equivalent to the slice object created
139by 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>,
141or 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>
143exception if no conversion is possible from the arguments to type
144object.</dt>
145<h4><a name="slice-spec-observers"></a>Class <code>slice</code>
146observer functions<br>
147</h4>
148<pre>
149object slice::start() const;
150object slice::stop() const;
151object 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
157the slice was created with.&nbsp;If the parameter was omitted or
158slice_nil was used when the slice was created, than that parameter will
159be a reference to PyNone and compare equal to a default-constructed
160object.&nbsp; In principal, any object may be used when creating a
161slice object, but in practice they are usually integers.</dt>
162</dl>
163<br>
164<pre>
165template &lt;typename RandomAccessIterator&gt;
166slice::range&lt;RandomAccessIterator&gt;
167slice::get_indicies(
168    RandomAccessIterator const&amp; begin,
169    RandomAccessIterator const&amp; end) const;
170</pre>
171<dl class="function-semantics">
172  <dt><b>Arguments:</b> A pair of STL-conforming Random Access
173Iterators that form a half-open range.</dt>
174  <dt><b>Effects:</b> Create a RandomAccessIterator pair that defines a
175fully-closed range within the [begin,end) range of its arguments.&nbsp;
176This function translates this slice's indicies while accounting for the
177effects of any PyNone or negative indicies, and non-singular step sizes.</dt>
178  <dt><b>Returns:</b> a slice::range
179that has been initialized with a non-zero value of step and a pair of
180RandomAccessIterators that point within the range of this functions
181arguments 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
183are neither references to <code>PyNone</code> nor convertible to <code>int</code>.&nbsp; Throws
184<code>std::invalid_argument</code> if the resulting range would be empty.&nbsp; You
185should always wrap calls to <code>slice::get_indicies()</code>
186within <code>try { ...; } catch (std::invalid_argument) {}</code> to
187handle this case and take appropriate action.</dt>
188  <dt><b>Rationale</b>: closed-interval: If
189an open interval were used, then for step
190size other than 1, the required state for the end iterator would point
191beyond the one-past-the-end position or before the beginning of the
192specified range.<br>
193exceptions on empty slice: It is impossible to define a closed interval
194over an empty range, so some other form of error checking would have to
195be used to prevent undefined behavior.&nbsp;In the case where the
196exception is not caught, it will simply be translated to Python by the
197default exception handling mechanisms. </dt>
198</dl>
199<h2><a name="examples"></a><b>Examples</b></h2>
200<pre>
201using 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
206list odd_elements(list l)
207{
208    return l[slice(_,_,2)];
209}
210
211// Perform a multidimensional extended slice of a Numeric.array
212numeric::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.
220double partial_sum(std::vector&lt;double&gt; const&amp; Foo, const slice index)
221{
222    slice::range&lt;std::vector&lt;double&gt;::const_iterator&gt; bounds;
223    try {
224        bounds = index.get_indicies&lt;&gt;(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>&copy; Copyright <a
240 href="mailto:jbrandmeyer@users.sourceforge.net">Jonathan Brandmeyer</a>,
2412004.&nbsp; Modification, copying and redistribution of this document
242is permitted under the terms and conditions of the Boost Software
243License, version 1.0.<br>
244</i></p>
245</body>
246</html>
Note: See TracBrowser for help on using the repository browser.