reference.xml 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. <?xml version='1.0' encoding="ISO-Latin-1" ?>
  2. <!DOCTYPE article
  3. PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
  4. "http://www.oasis-open.org/docbook/xml/4.0/docbookx.dtd" [
  5. <!ENTITY concepts SYSTEM "MultiArray.xml">
  6. <!ENTITY multi_array SYSTEM "multi_array.xml">
  7. <!ENTITY multi_array_ref SYSTEM "multi_array_ref.xml">
  8. <!ENTITY const_multi_array_ref SYSTEM "const_multi_array_ref.xml">
  9. ]>
  10. <article>
  11. <articleinfo>
  12. <title>Boost.MultiArray Reference Manual</title>
  13. <author>
  14. <surname>Garcia</surname><firstname>Ronald</firstname>
  15. <affiliation>
  16. <orgname>Indiana University</orgname>
  17. <orgdiv>Open Systems Lab</orgdiv>
  18. </affiliation>
  19. </author>
  20. <orgname>BOOST</orgname>
  21. <copyright>
  22. <year>2002</year>
  23. <holder>The Trustees of Indiana University</holder>
  24. </copyright>
  25. </articleinfo>
  26. <para>Boost.MultiArray is composed of several components.
  27. The MultiArray concept defines a generic interface to multidimensional
  28. containers.
  29. <literal>multi_array</literal> is a general purpose container class
  30. that models MultiArray. <literal>multi_array_ref</literal>
  31. and <literal>const_multi_array_ref</literal> are adapter
  32. classes. Using them,
  33. you can manipulate any block of contiguous data as though it were a
  34. <literal>multi_array</literal>.
  35. <literal>const_multi_array_ref</literal> differs from
  36. <literal>multi_array_ref</literal> in that its elements cannot
  37. be modified through its interface. Finally, several auxiliary classes are used
  38. to create and specialize arrays and some global objects are defined as
  39. part of the library interface.</para>
  40. <sect1 id="synopsis">
  41. <title>Library Synopsis</title>
  42. <para>To use Boost.MultiArray, you must include the header
  43. <filename>boost/multi_array.hpp</filename> in your source. This file
  44. brings the following declarations into scope:</para>
  45. <programlisting>
  46. <![CDATA[namespace boost {
  47. namespace multi_array_types {
  48. typedef *unspecified* index;
  49. typedef *unspecified* size_type;
  50. typedef *unspecified* difference_type;
  51. typedef *unspecified* index_range;
  52. typedef *unspecified* extent_range;
  53. typedef *unspecified* index_gen;
  54. typedef *unspecified* extent_gen;
  55. }
  56. template <typename ValueType,
  57. std::size_t NumDims,
  58. typename Allocator = std::allocator<ValueType> >
  59. class multi_array;
  60. template <typename ValueType,
  61. std::size_t NumDims>
  62. class multi_array_ref;
  63. template <typename ValueType,
  64. std::size_t NumDims>
  65. class const_multi_array_ref;
  66. multi_array_types::extent_gen extents;
  67. multi_array_types::index_gen indices;
  68. template <typename Array, int N> class subarray_gen;
  69. template <typename Array, int N> class const_subarray_gen;
  70. template <typename Array, int N> class array_view_gen;
  71. template <typename Array, int N> class const_array_view_gen;
  72. class c_storage_order;
  73. class fortran_storage_order;
  74. template <std::size_t NumDims> class general_storage_order;
  75. }]]>
  76. </programlisting>
  77. </sect1>
  78. &concepts;
  79. <sect1 id="array_types">
  80. <title>Array Components</title>
  81. <para>
  82. Boost.MultiArray defines an array class,
  83. <literal>multi_array</literal>, and two adapter classes,
  84. <literal>multi_array_ref</literal> and
  85. <literal>const_multi_array_ref</literal>. The three classes model
  86. MultiArray and so they share a lot of functionality.
  87. <literal>multi_array_ref</literal> differs from
  88. <literal>multi_array</literal> in that the
  89. <literal>multi_array</literal> manages its own memory, while
  90. <literal>multi_array_ref</literal> is passed a block of memory that it
  91. expects to be externally managed.
  92. <literal>const_multi_array_ref</literal> differs from
  93. <literal>multi_array_ref</literal> in that the underlying elements it
  94. adapts cannot be modified through its interface, though some array
  95. properties, including the array shape and index bases, can be altered.
  96. Functionality the classes have in common is described
  97. below.
  98. </para>
  99. <formalpara>
  100. <title>Note: Preconditions, Effects, and Implementation</title>
  101. <para>
  102. Throughout the following sections, small pieces of C++ code are
  103. used to specify constraints such as preconditions, effects, and
  104. postconditions. These do not necessarily describe the underlying
  105. implementation of array components; rather, they describe the
  106. expected input to and
  107. behavior of the specified operations. Failure to meet
  108. preconditions results in undefined behavior. Not all effects
  109. (i.e. copy constructors, etc.) must be mimicked exactly. The code
  110. snippets for effects intend to capture the essence of the described
  111. operation.
  112. </para>
  113. </formalpara>
  114. <formalpara>
  115. <title>Queries</title>
  116. <variablelist>
  117. <varlistentry>
  118. <term><programlisting>element* data();
  119. const element* data() const;</programlisting></term>
  120. <listitem>
  121. <para>This returns a pointer to the beginning of the
  122. contiguous block that contains the array's data. If all dimensions of
  123. the array are 0-indexed and stored in ascending order, this is
  124. equivalent to <literal>origin()</literal>. Note that
  125. <literal>const_multi_array_ref</literal> only provides the const
  126. version of this function.
  127. </para>
  128. </listitem>
  129. </varlistentry>
  130. <varlistentry>
  131. <term><programlisting>element* origin();
  132. const element* origin() const;</programlisting></term>
  133. <listitem>
  134. <para>This returns the origin element of the
  135. <literal>multi_array</literal>. Note that
  136. <literal>const_multi_array_ref</literal> only provides the const
  137. version of this function. (Required by MultiArray)
  138. </para>
  139. </listitem>
  140. </varlistentry>
  141. <varlistentry>
  142. <term><function>const index* index_bases();</function></term>
  143. <listitem>
  144. <para>This returns the index bases for the
  145. <literal>multi_array</literal>. (Required by MultiArray)
  146. </para>
  147. </listitem>
  148. </varlistentry>
  149. <varlistentry>
  150. <term><function>const index* strides();</function></term>
  151. <listitem>
  152. <para>This returns the strides for the
  153. <literal>multi_array</literal>. (Required by MultiArray)
  154. </para>
  155. </listitem>
  156. </varlistentry>
  157. <varlistentry>
  158. <term><function>const size_type* shape();</function></term>
  159. <listitem>
  160. <para>This returns the shape of the
  161. <literal>multi_array</literal>. (Required by MultiArray)
  162. </para>
  163. </listitem>
  164. </varlistentry>
  165. </variablelist>
  166. </formalpara>
  167. <formalpara>
  168. <title>Comparators</title>
  169. <variablelist>
  170. <varlistentry>
  171. <term><programlisting><![CDATA[
  172. bool operator==(const *array-type*& rhs);
  173. bool operator!=(const *array-type*& rhs);
  174. bool operator<(const *array-type*& rhs);
  175. bool operator>(const *array-type*& rhs);
  176. bool operator>=(const *array-type*& rhs);
  177. bool operator<=(const *array-type*& rhs);]]></programlisting></term>
  178. <listitem>
  179. <para>Each comparator executes a lexicographical compare over
  180. the value types of the two arrays.
  181. (Required by MultiArray)
  182. </para>
  183. <formalpara>
  184. <title>Preconditions</title>
  185. <para><literal>element</literal> must support the
  186. comparator corresponding to that called on
  187. <literal>multi_array</literal>.</para>
  188. </formalpara>
  189. <formalpara>
  190. <title>Complexity</title>
  191. <para>O(<literal>num_elements()</literal>).</para>
  192. </formalpara>
  193. </listitem>
  194. </varlistentry>
  195. </variablelist>
  196. </formalpara>
  197. <formalpara>
  198. <title>Modifiers</title>
  199. <variablelist>
  200. <varlistentry>
  201. <term>
  202. <programlisting>
  203. <![CDATA[
  204. template <typename SizeList>
  205. void reshape(const SizeList& sizes)
  206. ]]>
  207. </programlisting>
  208. </term>
  209. <listitem>
  210. <para>This changes the shape of the <literal>multi_array</literal>. The
  211. number of elements and the index bases remain the same, but the number
  212. of values at each level of the nested container hierarchy may
  213. change.</para>
  214. <formalpara><title><literal>SizeList</literal> Requirements</title>
  215. <para><literal>SizeList</literal> must model
  216. <ulink url="../../utility/Collection.html">Collection</ulink>.</para>
  217. </formalpara>
  218. <formalpara><title>Preconditions</title>
  219. <para>
  220. <programlisting>
  221. <![CDATA[std::accumulate(sizes.begin(),sizes.end(),size_type(1),std::times<size_type>()) == this->num_elements();
  222. sizes.size() == NumDims;]]>
  223. </programlisting></para>
  224. </formalpara>
  225. <formalpara><title>Postconditions</title>
  226. <para>
  227. <literal>std::equal(sizes.begin(),sizes.end(),this->shape) == true;</literal>
  228. </para>
  229. </formalpara>
  230. </listitem>
  231. </varlistentry>
  232. <varlistentry>
  233. <term>
  234. <programlisting>
  235. <![CDATA[
  236. template <typename BaseList>
  237. void reindex(const BaseList& values);
  238. ]]>
  239. </programlisting>
  240. </term>
  241. <listitem>
  242. <para>This changes the index bases of the <literal>multi_array</literal> to
  243. correspond to the the values in <literal>values</literal>.</para>
  244. <formalpara>
  245. <title><literal>BaseList</literal> Requirements</title>
  246. <para><literal>BaseList</literal> must model
  247. <ulink url="../../utility/Collection.html">Collection</ulink>.</para>
  248. </formalpara>
  249. <formalpara>
  250. <title>Preconditions</title>
  251. <para><literal>values.size() == NumDims;</literal></para>
  252. </formalpara>
  253. <formalpara>
  254. <title>Postconditions</title>
  255. <para><literal>std::equal(values.begin(),values.end(),this->index_bases());
  256. </literal></para>
  257. </formalpara>
  258. </listitem>
  259. </varlistentry>
  260. <varlistentry>
  261. <term>
  262. <programlisting>
  263. <![CDATA[
  264. void reindex(index value);
  265. ]]>
  266. </programlisting>
  267. </term>
  268. <listitem>
  269. <para>This changes the index bases of all dimensions of the
  270. <literal>multi_array</literal> to <literal>value</literal>.</para>
  271. <formalpara>
  272. <title>Postconditions</title>
  273. <para>
  274. <programlisting>
  275. <![CDATA[
  276. std::count_if(this->index_bases(),this->index_bases()+this->num_dimensions(),
  277. std::bind_2nd(std::equal_to<index>(),value)) ==
  278. this->num_dimensions();
  279. ]]>
  280. </programlisting>
  281. </para>
  282. </formalpara>
  283. </listitem>
  284. </varlistentry>
  285. </variablelist>
  286. </formalpara>
  287. &multi_array;
  288. &multi_array_ref;
  289. &const_multi_array_ref;
  290. </sect1>
  291. <sect1 id="auxiliary">
  292. <title>Auxiliary Components</title>
  293. <sect2 id="multi_array_types">
  294. <title><literal>multi_array_types</literal></title>
  295. <programlisting>
  296. <![CDATA[namespace multi_array_types {
  297. typedef *unspecified* index;
  298. typedef *unspecified* size_type;
  299. typedef *unspecified* difference_type;
  300. typedef *unspecified* index_range;
  301. typedef *unspecified* extent_range;
  302. typedef *unspecified* index_gen;
  303. typedef *unspecified* extent_gen;
  304. }]]>
  305. </programlisting>
  306. <para>Namespace <literal>multi_array_types</literal> defines types
  307. associated with <literal>multi_array</literal>,
  308. <literal>multi_array_ref</literal>, and
  309. <literal>const_multi_array_ref</literal> that are not
  310. dependent upon template parameters. These types find common use with
  311. all Boost.Multiarray components. They are defined
  312. in a namespace from which they can be accessed conveniently.
  313. With the exception of <literal>extent_gen</literal> and
  314. <literal>extent_range</literal>, these types fulfill the roles of the
  315. same name required by MultiArray and are described in its
  316. concept definition. <literal>extent_gen</literal> and
  317. <literal>extent_range</literal> are described below.
  318. </para>
  319. </sect2>
  320. <sect2 id="extent_range">
  321. <title><classname>extent_range</classname></title>
  322. <para><classname>extent_range</classname> objects define half open
  323. intervals. They provide shape and index base information to
  324. <literal>multi_array</literal>, <literal>multi_array_ref</literal>,
  325. and <literal>const_multi_array_ref</literal> constructors.
  326. <classname>extent_range</classname>s are passed in
  327. aggregate to an array constructor (see
  328. <classname>extent_gen</classname> for more details).
  329. </para>
  330. <formalpara>
  331. <title>Synopsis</title>
  332. <programlisting><![CDATA[
  333. class extent_range {
  334. public:
  335. typedef multi_array_types::index index;
  336. typedef multi_array_types::size_type size_type;
  337. // Structors
  338. extent_range(index start, index finish);
  339. extent_range(index finish);
  340. ~extent_range();
  341. // Queries
  342. index start();
  343. index finish();
  344. size_type size();
  345. };]]></programlisting>
  346. </formalpara>
  347. <formalpara>
  348. <title>Model Of</title>
  349. <para>DefaultConstructible,CopyConstructible</para>
  350. </formalpara>
  351. <formalpara><title>Methods and Types</title>
  352. <variablelist>
  353. <varlistentry>
  354. <term><function>extent_range(index start, index finish)</function></term>
  355. <listitem>
  356. <para> This constructor defines the half open interval
  357. <literal>[start,finish)</literal>. The expression
  358. <literal>finish</literal> must be greater than <literal>start</literal>.
  359. </para>
  360. </listitem>
  361. </varlistentry>
  362. <varlistentry><term><function>extent_range(index finish)</function></term>
  363. <listitem>
  364. <para>This constructor defines the half open interval
  365. <literal>[0,finish)</literal>. The value of <literal>finish</literal>
  366. must be positive.</para>
  367. </listitem>
  368. </varlistentry>
  369. <varlistentry><term><function>index start()</function></term>
  370. <listitem>
  371. <para>This function returns the first index represented by the range</para>
  372. </listitem>
  373. </varlistentry>
  374. <varlistentry><term><function>index finish()</function></term>
  375. <listitem>
  376. <para>This function returns the upper boundary value of the half-open
  377. interval. Note that the range does not include this value.</para>
  378. </listitem>
  379. </varlistentry>
  380. <varlistentry>
  381. <term><function>size_type size()</function></term>
  382. <listitem>
  383. <para>This function returns the size of the specified range. It is
  384. equivalent to <literal>finish()-start()</literal>.</para>
  385. </listitem>
  386. </varlistentry>
  387. </variablelist>
  388. </formalpara>
  389. </sect2>
  390. <sect2 id="extent_gen">
  391. <title><classname>extent_gen</classname></title>
  392. <para>The <classname>extent_gen</classname> class defines an
  393. interface for aggregating array shape and indexing information to be
  394. passed to a <literal>multi_array</literal>,
  395. <literal>multi_array_ref</literal>, or <literal>const_multi_array_ref</literal>
  396. constructor. Its interface mimics
  397. the syntax used to declare built-in array types
  398. in C++. For example, while a 3-dimensional array of
  399. <classname>int</classname> values in C++ would be
  400. declared as:
  401. <programlisting>int A[3][4][5],</programlisting>
  402. a similar <classname>multi_array</classname> would be declared:
  403. <programlisting>multi_array&lt;int,3&gt; A(extents[3][4][5]).</programlisting>
  404. </para>
  405. <formalpara><title>Synopsis</title>
  406. <programlisting><![CDATA[
  407. template <std::size_t NumRanges>
  408. class *implementation_defined* {
  409. public:
  410. typedef multi_array_types::index index;
  411. typedef multi_array_types::size_type size_type;
  412. template <std::size_t NumRanges> class gen_type;
  413. gen_type<NumRanges+1>::type operator[](const range& a_range) const;
  414. gen_type<NumRanges+1>::type operator[](index idx) const;
  415. };
  416. typedef *implementation_defined*<0> extent_gen;
  417. ]]></programlisting>
  418. </formalpara>
  419. <formalpara><title>Methods and Types</title>
  420. <variablelist>
  421. <varlistentry>
  422. <term><function>template gen_type&lt;Ranges&gt;::type</function></term>
  423. <listitem>
  424. <para>This type generator is used to specify the result of
  425. <literal>Ranges</literal> chained calls to
  426. <literal>extent_gen::operator[].</literal> The types
  427. <classname>extent_gen</classname> and
  428. <classname>gen_type&lt;0&gt;::type</classname> are the same.</para>
  429. </listitem>
  430. </varlistentry>
  431. <varlistentry>
  432. <term><function>gen_type&lt;NumRanges+1&gt;::type
  433. operator[](const extent_range&amp; a_range) const;</function></term>
  434. <listitem>
  435. <para>This function returns a new object containing all previous
  436. <classname>extent_range</classname> objects in addition to
  437. <literal>a_range.</literal> <classname>extent_range</classname>
  438. objects are aggregated by chained calls to
  439. <function>operator[]</function>.</para>
  440. </listitem>
  441. </varlistentry>
  442. <varlistentry>
  443. <term><function>gen_type&lt;NumRanges+1&gt;::type
  444. operator[](index idx) const;</function></term>
  445. <listitem>
  446. <para>This function returns a new object containing all previous
  447. <classname>extent_range</classname> objects in addition to
  448. <literal>extent_range(0,idx).</literal> This function gives the array
  449. constructors a similar syntax to traditional C multidimensional array
  450. declaration.</para>
  451. </listitem>
  452. </varlistentry>
  453. </variablelist>
  454. </formalpara>
  455. </sect2>
  456. <sect2>
  457. <title>Global Objects</title>
  458. <para>For syntactic convenience, Boost.MultiArray defines two
  459. global objects as part of its
  460. interface. These objects play the role of object generators;
  461. expressions involving them create other objects of interest.
  462. </para>
  463. <para> Under some circumstances, the two global objects may be
  464. considered excessive overhead. Their construction can be prevented by
  465. defining the preprocessor symbol
  466. <literal>BOOST_MULTI_ARRAY_NO_GENERATORS</literal> before including
  467. <filename>boost/multi_array.hpp.</filename></para>
  468. <sect3 id="extents">
  469. <title><literal>extents</literal></title>
  470. <programlisting>
  471. <![CDATA[namespace boost {
  472. multi_array_base::extent_gen extents;
  473. }]]>
  474. </programlisting>
  475. <para>Boost.MultiArray's array classes use the
  476. <literal>extents</literal> global object to specify
  477. array shape during their construction.
  478. For example,
  479. a 3 by 3 by 3 <classname>multi_array</classname> is constructed as follows:
  480. <programlisting>multi_array&lt;int,3&gt; A(extents[3][3][3]);</programlisting>
  481. The same array could also be created by explicitly declaring an <literal>extent_gen</literal>
  482. object locally,, but the global object makes this declaration unnecessary.
  483. </para>
  484. </sect3>
  485. <sect3 id="indices">
  486. <title><literal>indices</literal></title>
  487. <programlisting>
  488. <![CDATA[namespace boost {
  489. multi_array_base::index_gen indices;
  490. }]]>
  491. </programlisting>
  492. <para>The MultiArray concept specifies an
  493. <literal>index_gen</literal> associated type that is used to
  494. create views.
  495. <literal>indices</literal> is a global object that serves the role of
  496. <literal>index_gen</literal> for all array components provided by this
  497. library and their associated subarrays and views.
  498. </para>
  499. <para>For example, using the <literal>indices</literal> object,
  500. a view of an array <literal>A</literal> is constructed as follows:
  501. <programlisting>
  502. A[indices[index_range(0,5)][2][index_range(2,4)]];
  503. </programlisting>
  504. </para>
  505. </sect3>
  506. </sect2>
  507. <sect2 id="generators">
  508. <title>View and SubArray Generators</title>
  509. <para>
  510. Boost.MultiArray provides traits classes, <literal>subarray_gen</literal>,
  511. <literal>const_subarray_gen</literal>,
  512. <literal>array_view_gen</literal>,
  513. and <literal>const_array_view_gen</literal>, for naming of
  514. array associated types within function templates.
  515. In general this is no more convenient to use than the nested
  516. type generators, but the library author found that some C++ compilers do not
  517. properly handle templates nested within function template parameter types.
  518. These generators constitute a workaround for this deficit.
  519. The following code snippet illustrates
  520. the correspondence between the <literal>array_view_gen</literal>
  521. traits class and the <literal>array_view</literal> type associated to
  522. an array:
  523. <programlisting>
  524. template &lt;typename Array&gt;
  525. void my_function() {
  526. typedef typename Array::template array_view&lt;3&gt;::type view1_t;
  527. typedef typename boost::array_view_gen&lt;Array,3&gt;::type view2_t;
  528. // ...
  529. }
  530. </programlisting>
  531. In the above example, <literal>view1_t</literal> and
  532. <literal>view2_t</literal> have the same type.
  533. </para>
  534. </sect2>
  535. <sect2 id="memory_layout">
  536. <title>Memory Layout Specifiers</title>
  537. <para>
  538. While a multidimensional array represents a hierarchy of containers of
  539. elements, at some point the elements must be laid out in
  540. memory. As a result, a single multidimensional array
  541. can be represented in memory more than one way.
  542. </para>
  543. <para>For example, consider the two dimensional array shown below in
  544. matrix notation:
  545. <graphic fileref="matrix.gif"/>
  546. Here is how the above array is expressed in C++:
  547. <programlisting>
  548. int a[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  549. </programlisting>
  550. This is an example of row-major storage, where elements of each row
  551. are stored contiguously.
  552. While C++ transparently handles accessing elements of an array, you
  553. can also manage the array and its indexing manually. One way that
  554. this may be expressed in memory is as follows:
  555. <programlisting>
  556. int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  557. int s[] = { 4, 1 };
  558. </programlisting>
  559. With the latter declaration of <literal>a</literal> and
  560. strides <literal>s</literal>, element <literal>a(i,j)</literal>
  561. of the array can be
  562. accessed using the expression
  563. <programlisting>*a+i*s[0]+j*s[1]</programlisting>.
  564. </para>
  565. <para>The same two dimensional array could be laid out by column as follows:
  566. <programlisting>
  567. int a[] = { 0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11 };
  568. int s[] = { 3, 1 };
  569. </programlisting>
  570. Notice that the strides here are different. As a result,
  571. The expression given above to access values will work with this pair
  572. of data and strides as well.
  573. </para>
  574. <para>In addition to dimension order, it is also possible to
  575. store any dimension in descending order. For example, returning to the
  576. first example, the first dimension of the example array, the
  577. rows, could be stored in
  578. reverse, resulting in the following:
  579. <programlisting>
  580. int data[] = { 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3 };
  581. int *a = data + 8;
  582. int s[] = { -4, 1 };
  583. </programlisting>
  584. Note that in this example <literal>a</literal> must be explicitly set
  585. to the origin. In the previous examples, the
  586. first element stored in memory was the origin; here this is no longer
  587. the case.
  588. </para>
  589. <para>
  590. Alternatively, the second dimension, or the columns, could be reversed
  591. and the rows stored in ascending order:
  592. <programlisting>
  593. int data[] = { 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8 };
  594. int *a = data + 3;
  595. int s[] = { 4, -1 };
  596. </programlisting>
  597. </para>
  598. <para>
  599. Finally, both dimensions could be stored in descending order:
  600. <programlisting>
  601. int data[] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
  602. int *a = data + 11;
  603. int s[] = { -4, -1 };
  604. </programlisting>
  605. <literal>
  606. </literal>
  607. </para>
  608. <para>
  609. All of the above arrays are equivalent. The expression
  610. given above for <literal>a(i,j)</literal> will yield the same value
  611. regardless of the memory layout.
  612. Boost.MultiArray arrays can be created with customized storage
  613. parameters as described above. Thus, existing data can be adapted
  614. (with <literal>multi_array_ref</literal> or
  615. <literal>const_multi_array_ref</literal>) as suited to the array
  616. abstraction. A common usage of this feature would be to wrap arrays
  617. that must interoperate with Fortran routines so they can be
  618. manipulated naturally at both the C++ and Fortran levels. The
  619. following sections describe the Boost.MultiArray components used to
  620. specify memory layout.
  621. </para>
  622. <sect3 id="c_storage_order">
  623. <title><literal>c_storage_order</literal></title>
  624. <programlisting>
  625. <![CDATA[class c_storage_order {
  626. c_storage_order();
  627. };]]>
  628. </programlisting>
  629. <para><literal>c_storage_order</literal> is used to specify that an
  630. array should store its elements using the same layout as that used by
  631. primitive C++ multidimensional arrays, that is, from last dimension
  632. to first. This is the default storage order for the arrays provided by
  633. this library.</para>
  634. </sect3>
  635. <sect3 id="fortran_storage_order">
  636. <title><literal>fortran_storage_order</literal></title>
  637. <programlisting>
  638. <![CDATA[class fortran_storage_order {
  639. fortran_storage_order();
  640. };]]>
  641. </programlisting>
  642. <para><literal>fortran_storage_order</literal> is used to specify that
  643. an array should store its elements using the same memory layout as a
  644. Fortran multidimensional array would, that is, from first dimension to
  645. last.</para>
  646. </sect3>
  647. <sect3 id="general_storage_order">
  648. <title><literal>general_storage_order</literal></title>
  649. <programlisting>
  650. <![CDATA[template <std::size_t NumDims>
  651. class general_storage_order {
  652. template <typename OrderingIter, typename AscendingIter>
  653. general_storage_order(OrderingIter ordering, AscendingIter ascending);
  654. };]]>
  655. </programlisting>
  656. <para><literal>general_storage_order</literal> allows the user to
  657. specify an arbitrary memory layout for the contents of an array. The
  658. constructed object is passed to the array constructor in order to
  659. specify storage order.</para>
  660. <para>
  661. <literal>OrderingIter</literal> and <literal>AscendingIter</literal>
  662. must model the <literal>InputIterator</literal> concept. Both
  663. iterators must refer to a range of <literal>NumDims</literal>
  664. elements. <literal>AscendingIter</literal> points to objects
  665. convertible to <literal>bool</literal>. A value of
  666. <literal>true</literal> means that a dimension is stored in ascending
  667. order while <literal>false</literal> means that a dimension is stored
  668. in descending order. <literal>OrderingIter</literal> specifies the
  669. order in which dimensions are stored.
  670. </para>
  671. </sect3>
  672. </sect2>
  673. <sect2 id="range_checking">
  674. <title>Range Checking</title>
  675. <para>
  676. By default, the array access methods <literal>operator()</literal> and
  677. <literal>operator[]</literal> perform range
  678. checking. If a supplied index is out of the range defined for an
  679. array, an assertion will abort the program. To disable range
  680. checking (for performance reasons in production releases), define
  681. the <literal>BOOST_DISABLE_ASSERTS</literal> preprocessor macro prior to
  682. including multi_array.hpp in an application.
  683. </para>
  684. </sect2>
  685. </sect1>
  686. </article>