|
| 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 - <boost/python/slice.hpp></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 <boost/python/slice.hpp></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 <typename Int1, typename Int2> |
| 75 | + slice(Int1 start, Int2 stop); |
| 76 | + |
| 77 | + template <typename Int1, typename Int2, typename Int3> |
| 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 <typename RandomAccessIterator> |
| 87 | + struct range |
| 88 | + { |
| 89 | + RandomAccessIterator start; |
| 90 | + RandomAccessIterator stop; |
| 91 | + int step; |
| 92 | + }; |
| 93 | + |
| 94 | + template <typename RandomAccessIterator> |
| 95 | + range<RandomAccessIterator> |
| 96 | + get_indicies( |
| 97 | + RandomAccessIterator const& begin, |
| 98 | + RandomAccessIterator const& 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. 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 <typename Int1, typename Int2> |
| 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. 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 <typename Int1, typename Int2, typename Int3> |
| 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. 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. 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. 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 <typename RandomAccessIterator> |
| 162 | +slice::range<RandomAccessIterator> |
| 163 | +slice::get_indicies( |
| 164 | + RandomAccessIterator const& begin, RandomAccessIterator const& 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. |
| 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>. Throws |
| 179 | +<code>std::invalid_argument</code> if the resulting range would be empty. 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. 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<double> const& Foo, slice index) |
| 216 | +{ |
| 217 | + slice::range<std::vector<double>::const_iterator> bounds; |
| 218 | + try { |
| 219 | + bounds = index.get_indicies<>(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>© Copyright <a |
| 235 | + href="mailto:jbrandmeyer@users.sourceforge.net">Jonathan Brandmeyer</a>, |
| 236 | +2004. 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> |
0 commit comments