• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<sect2 id="multi_array_class">
2<title><literal>multi_array</literal></title>
3
4<para>
5<literal>multi_array</literal> is a multi-dimensional container that
6supports random access iteration. Its number of dimensions is
7fixed at compile time, but its shape and the number of elements it
8contains are specified during its construction. The number of elements
9will remain fixed for the duration of a
10<literal>multi_array</literal>'s lifetime, but the shape of the container can
11be changed. A <literal>multi_array</literal> manages its data elements
12using a replaceable allocator.
13</para>
14
15
16<formalpara>
17      <title>Model Of.</title>
18<para>
19<link linkend="MultiArray">MultiArray</link>,
20<ulink url="../../../libs/utility/CopyConstructible.html">CopyConstructible</ulink>. Depending on the element type,
21it may also model <ulink url="https://www.boost.org/sgi/stl/EqualityComparable.html">EqualityComparable</ulink> and <ulink url="https://www.boost.org/sgi/stl/LessThanComparable.html">LessThanComparable</ulink>.
22</para>
23</formalpara>
24
25<formalpara>
26<title>Synopsis</title>
27
28<programlisting>
29<![CDATA[
30namespace boost {
31
32template <typename ValueType,
33          std::size_t NumDims,
34          typename Allocator = std::allocator<ValueType> >
35class multi_array {
36public:
37// types:
38  typedef ValueType                             element;
39  typedef *unspecified*                         value_type;
40  typedef *unspecified*                         reference;
41  typedef *unspecified*                         const_reference;
42  typedef *unspecified*                         difference_type;
43  typedef *unspecified*                         iterator;
44  typedef *unspecified*                         const_iterator;
45  typedef *unspecified*                         reverse_iterator;
46  typedef *unspecified*                         const_reverse_iterator;
47  typedef multi_array_types::size_type          size_type;
48  typedef multi_array_types::index              index;
49  typedef multi_array_types::index_gen          index_gen;
50  typedef multi_array_types::index_range        index_range;
51  typedef multi_array_types::extent_gen         extent_gen;
52  typedef multi_array_types::extent_range       extent_range;
53  typedef *unspecified*                         storage_order_type;
54
55
56  // template typedefs
57  template <std::size_t Dims> struct            subarray;
58  template <std::size_t Dims> struct            const_subarray;
59  template <std::size_t Dims> struct            array_view;
60  template <std::size_t Dims> struct            const_array_view;
61
62
63  static const std::size_t dimensionality = NumDims;
64
65
66  // constructors and destructors
67
68  multi_array(const Allocator& alloc = Allocator());
69
70  template <typename ExtentList>
71  explicit multi_array(const ExtentList& sizes,
72                       const storage_order_type& store = c_storage_order(),
73                       const Allocator& alloc = Allocator());
74  explicit multi_array(const extents_tuple& ranges,
75                       const storage_order_type& store = c_storage_order(),
76	               const Allocator& alloc = Allocator());
77  multi_array(const multi_array& x);
78  multi_array(const const_multi_array_ref<ValueType,NumDims>& x,
79              const Allocator& alloc = Allocator());
80  multi_array(const const_subarray<NumDims>::type& x,
81              const Allocator& alloc = Allocator());
82  multi_array(const const_array_view<NumDims>::type& x,
83              const Allocator& alloc = Allocator());
84
85  multi_array(const multi_array_ref<ValueType,NumDims>& x,
86              const Allocator& alloc = Allocator());
87  multi_array(const subarray<NumDims>::type& x,
88              const Allocator& alloc = Allocator());
89  multi_array(const array_view<NumDims>::type& x,
90              const Allocator& alloc = Allocator());
91
92  ~multi_array();
93
94  // modifiers
95
96  multi_array& operator=(const multi_array& x);
97  template <class Array> multi_array& operator=(const Array& x);
98
99  // iterators:
100  iterator				begin();
101  iterator				end();
102  const_iterator			begin() const;
103  const_iterator			end() const;
104  reverse_iterator			rbegin();
105  reverse_iterator			rend();
106  const_reverse_iterator		rbegin() const;
107  const_reverse_iterator		rend() const;
108
109  // capacity:
110  size_type				size() const;
111  size_type				num_elements() const;
112  size_type				num_dimensions() const;
113
114  // element access:
115  template <typename IndexList>
116    element&			operator()(const IndexList& indices);
117  template <typename IndexList>
118    const element&		operator()(const IndexList& indices) const;
119  reference			operator[](index i);
120  const_reference		operator[](index i) const;
121  array_view<Dims>::type	operator[](const indices_tuple& r);
122  const_array_view<Dims>::type	operator[](const indices_tuple& r) const;
123
124  // queries
125  element*			data();
126  const element*		data() const;
127  element*			origin();
128  const element*		origin() const;
129  const size_type*		shape() const;
130  const index*			strides() const;
131  const index*			index_bases() const;
132  const storage_order_type&     storage_order() const;
133
134  // comparators
135  bool operator==(const multi_array& rhs);
136  bool operator!=(const multi_array& rhs);
137  bool operator<(const multi_array& rhs);
138  bool operator>(const multi_array& rhs);
139  bool operator>=(const multi_array& rhs);
140  bool operator<=(const multi_array& rhs);
141
142  // modifiers:
143  template <typename InputIterator>
144    void			assign(InputIterator begin, InputIterator end);
145  template <typename SizeList>
146    void			reshape(const SizeList& sizes)
147  template <typename BaseList>	void reindex(const BaseList& values);
148    void			reindex(index value);
149  template <typename ExtentList>
150    multi_array&		resize(const ExtentList& extents);
151  multi_array&                  resize(extents_tuple& extents);
152};
153]]>
154</programlisting>
155</formalpara>
156
157<formalpara>
158<title>Constructors</title>
159
160<variablelist>
161<varlistentry>
162<term><programlisting>template &lt;typename ExtentList&gt;
163explicit multi_array(const ExtentList&amp; sizes,
164                     const storage_order_type&amp; store = c_storage_order(),
165                     const Allocator&amp; alloc = Allocator());
166</programlisting></term>
167<listitem>
168
169<para>
170This constructs a <literal>multi_array</literal> using the specified
171parameters.  <literal>sizes</literal> specifies the shape of the
172constructed <literal>multi_array</literal>.  <literal>store</literal>
173specifies the storage order or layout in memory of the array
174dimensions.  <literal>alloc</literal> is used to
175allocate the contained elements.
176</para>
177
178<formalpara><title><literal>ExtentList</literal> Requirements</title>
179<para>
180<literal>ExtentList</literal> must model <ulink url="../../utility/Collection.html">Collection</ulink>.
181</para>
182</formalpara>
183
184<formalpara><title>Preconditions</title>
185<para><literal>sizes.size() == NumDims;</literal></para>
186</formalpara>
187
188</listitem>
189</varlistentry>
190
191<varlistentry>
192<term>
193<programlisting><![CDATA[explicit multi_array(extent_gen::gen_type<NumDims>::type ranges,
194                     const storage_order_type& store = c_storage_order(),
195                     const Allocator& alloc = Allocator());]]>
196</programlisting></term>
197<listitem>
198<para>
199This constructs a <literal>multi_array</literal> using the specified
200    parameters.  <literal>ranges</literal> specifies the shape and
201index bases of the constructed multi_array. It is the result of
202<literal>NumDims</literal> chained calls to
203    <literal>extent_gen::operator[]</literal>. <literal>store</literal>
204specifies the storage order or layout in memory of the array
205dimensions.  <literal>alloc</literal> is the allocator used to
206allocate the memory used to store <literal>multi_array</literal>
207elements.
208</para>
209</listitem>
210</varlistentry>
211
212
213<varlistentry>
214<term><programlisting>
215<![CDATA[multi_array(const multi_array& x);
216multi_array(const const_multi_array_ref<ValueType,NumDims>& x,
217    const Allocator& alloc = Allocator());
218multi_array(const const_subarray<NumDims>::type& x,
219    const Allocator& alloc = Allocator());
220multi_array(const const_array_view<NumDims>::type& x,
221    const Allocator& alloc = Allocator());
222multi_array(const multi_array_ref<ValueType,NumDims>& x,
223    const Allocator& alloc = Allocator());
224multi_array(const subarray<NumDims>::type& x,
225    const Allocator& alloc = Allocator());
226multi_array(const array_view<NumDims>::type& x,
227    const Allocator& alloc = Allocator());]]>
228</programlisting></term>
229<listitem>
230<para>These constructors all constructs a <literal>multi_array</literal> and
231perform a deep copy of <literal>x</literal>.
232</para>
233
234<formalpara>
235<title>Complexity</title>
236<para> This performs O(<literal>x.num_elements()</literal>) calls to
237<literal>element</literal>'s copy
238constructor.
239</para></formalpara>
240</listitem>
241</varlistentry>
242
243<varlistentry>
244<term><programlisting>
245<![CDATA[multi_array();]]>
246</programlisting></term>
247<listitem>
248<para>This constructs a <literal>multi_array</literal> whose shape is (0,...,0) and contains no elements.
249</para>
250</listitem>
251</varlistentry>
252
253</variablelist>
254
255<formalpara><title>Note on Constructors</title>
256<para>
257The  <literal>multi_array</literal> construction expressions,
258<programlisting>
259     multi_array&lt;int,3&gt; A(boost::extents[5][4][3]);
260</programlisting>
261and
262<programlisting>
263     boost::array&lt;multi_array_base::index,3&gt; my_extents = {{5, 4, 3}};
264     multi_array&lt;int,3&gt; A(my_extents);
265</programlisting>
266are equivalent.
267</para>
268</formalpara>
269</formalpara>
270
271<formalpara>
272<title>Modifiers</title>
273
274<variablelist>
275
276<varlistentry>
277<term><programlisting>
278<![CDATA[multi_array& operator=(const multi_array& x);
279template <class Array> multi_array& operator=(const Array& x);]]>
280</programlisting>
281</term>
282
283<listitem>
284<para>This performs an element-wise copy of <literal>x</literal>
285into the current <literal>multi_array</literal>.</para>
286
287<formalpara>
288<title><literal>Array</literal> Requirements</title>
289<para><literal>Array</literal> must model MultiArray.
290</para></formalpara>
291
292<formalpara>
293<title>Preconditions</title>
294<para>
295<programlisting>std::equal(this->shape(),this->shape()+this->num_dimensions(),
296x.shape());</programlisting></para>
297</formalpara>
298
299<formalpara>
300<title>Postconditions</title>
301<para>
302<programlisting>(*.this) == x;</programlisting>
303</para>
304</formalpara>
305
306<formalpara>
307<title>Complexity</title>
308<para>The assignment operators perform
309O(<literal>x.num_elements()</literal>) calls to <literal>element</literal>'s
310copy constructor.</para></formalpara>
311</listitem>
312</varlistentry>
313
314<varlistentry>
315<term>
316<programlisting>
317<![CDATA[
318template <typename InputIterator>
319void assign(InputIterator begin, InputIterator end);]]>
320</programlisting>
321</term>
322
323<listitem>
324 <para>This copies the elements in the range
325<literal>[begin,end)</literal> into the array.  It is equivalent to
326<literal>std::copy(begin,end,this->data())</literal>.
327</para>
328
329<formalpara><title>Preconditions</title>
330<para><literal>std::distance(begin,end) == this->num_elements();</literal>
331</para>
332</formalpara>
333
334<formalpara>
335<title>Complexity</title>
336<para>
337The <literal>assign</literal> member function performs
338O(<literal>this->num_elements()</literal>) calls to
339<literal>ValueType</literal>'s copy constructor.
340</para>
341</formalpara>
342</listitem>
343</varlistentry>
344
345<varlistentry>
346<term>
347<programlisting><![CDATA[multi_array& resize(extent_gen::gen_type<NumDims>::type extents);
348template <typename ExtentList>
349  multi_array& resize(const ExtentList& extents);
350]]>
351</programlisting></term>
352<listitem>
353<para>
354This function resizes an array to the shape specified by
355<literal>extents</literal>, which is either a generated list of
356extents or a model of the <literal>Collection</literal> concept. The
357contents of the array are preserved whenever possible; if the new
358array size is smaller, then some data will be lost. Any new elements
359created by resizing the array are initialized with the
360<literal>element</literal> default constructor.
361</para>
362</listitem>
363</varlistentry>
364
365</variablelist>
366</formalpara>
367
368
369<formalpara>
370<title>Queries</title>
371
372<variablelist>
373
374<varlistentry>
375<term><programlisting>
376<![CDATA[storage_order_type& storage_order() const;]]>
377</programlisting>
378</term>
379
380<listitem>
381<para>This query returns the storage order object associated with the
382<literal>multi_array</literal> in question.  It can be used to construct a new array with the same storage order.</para>
383</listitem>
384</varlistentry>
385</variablelist>
386</formalpara>
387</sect2>
388