bbref.xml 25 KB

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