multi_array.xml 12 KB

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