base.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // Copyright 2002 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Boost.MultiArray Library
  6. // Authors: Ronald Garcia
  7. // Jeremy Siek
  8. // Andrew Lumsdaine
  9. // See http://www.boost.org/libs/multi_array for documentation.
  10. #ifndef BASE_RG071801_HPP
  11. #define BASE_RG071801_HPP
  12. //
  13. // base.hpp - some implementation base classes for from which
  14. // functionality is acquired
  15. //
  16. #include "boost/multi_array/extent_range.hpp"
  17. #include "boost/multi_array/extent_gen.hpp"
  18. #include "boost/multi_array/index_range.hpp"
  19. #include "boost/multi_array/index_gen.hpp"
  20. #include "boost/multi_array/storage_order.hpp"
  21. #include "boost/multi_array/types.hpp"
  22. #include "boost/config.hpp"
  23. #include "boost/multi_array/concept_checks.hpp" //for ignore_unused_...
  24. #include "boost/mpl/eval_if.hpp"
  25. #include "boost/mpl/if.hpp"
  26. #include "boost/mpl/size_t.hpp"
  27. #include "boost/iterator/reverse_iterator.hpp"
  28. #include "boost/static_assert.hpp"
  29. #include "boost/type.hpp"
  30. #include "boost/assert.hpp"
  31. #include <cstddef>
  32. #include <memory>
  33. namespace boost {
  34. /////////////////////////////////////////////////////////////////////////
  35. // class declarations
  36. /////////////////////////////////////////////////////////////////////////
  37. template<typename T, std::size_t NumDims,
  38. typename Allocator = std::allocator<T> >
  39. class multi_array;
  40. // This is a public interface for use by end users!
  41. namespace multi_array_types {
  42. typedef boost::detail::multi_array::size_type size_type;
  43. typedef std::ptrdiff_t difference_type;
  44. typedef boost::detail::multi_array::index index;
  45. typedef detail::multi_array::index_range<index,size_type> index_range;
  46. typedef detail::multi_array::extent_range<index,size_type> extent_range;
  47. typedef detail::multi_array::index_gen<0,0> index_gen;
  48. typedef detail::multi_array::extent_gen<0> extent_gen;
  49. }
  50. // boost::extents and boost::indices are now a part of the public
  51. // interface. That way users don't necessarily have to create their
  52. // own objects. On the other hand, one may not want the overhead of
  53. // object creation in small-memory environments. Thus, the objects
  54. // can be left undefined by defining BOOST_MULTI_ARRAY_NO_GENERATORS
  55. // before loading multi_array.hpp.
  56. #ifndef BOOST_MULTI_ARRAY_NO_GENERATORS
  57. namespace {
  58. multi_array_types::extent_gen extents;
  59. multi_array_types::index_gen indices;
  60. }
  61. #endif // BOOST_MULTI_ARRAY_NO_GENERATORS
  62. namespace detail {
  63. namespace multi_array {
  64. template <typename T, std::size_t NumDims>
  65. class sub_array;
  66. template <typename T, std::size_t NumDims, typename TPtr = const T*>
  67. class const_sub_array;
  68. template <typename T, typename TPtr, typename NumDims, typename Reference,
  69. typename IteratorCategory>
  70. class array_iterator;
  71. template <typename T, std::size_t NumDims, typename TPtr = const T*>
  72. class const_multi_array_view;
  73. template <typename T, std::size_t NumDims>
  74. class multi_array_view;
  75. /////////////////////////////////////////////////////////////////////////
  76. // class interfaces
  77. /////////////////////////////////////////////////////////////////////////
  78. class multi_array_base {
  79. public:
  80. typedef multi_array_types::size_type size_type;
  81. typedef multi_array_types::difference_type difference_type;
  82. typedef multi_array_types::index index;
  83. typedef multi_array_types::index_range index_range;
  84. typedef multi_array_types::extent_range extent_range;
  85. typedef multi_array_types::index_gen index_gen;
  86. typedef multi_array_types::extent_gen extent_gen;
  87. };
  88. //
  89. // value_accessor_n
  90. // contains the routines for accessing elements from
  91. // N-dimensional views.
  92. //
  93. template<typename T, std::size_t NumDims>
  94. class value_accessor_n : public multi_array_base {
  95. typedef multi_array_base super_type;
  96. public:
  97. typedef typename super_type::index index;
  98. //
  99. // public typedefs used by classes that inherit from this base
  100. //
  101. typedef T element;
  102. typedef boost::multi_array<T,NumDims-1> value_type;
  103. typedef sub_array<T,NumDims-1> reference;
  104. typedef const_sub_array<T,NumDims-1> const_reference;
  105. protected:
  106. // used by array operator[] and iterators to get reference types.
  107. template <typename Reference, typename TPtr>
  108. Reference access(boost::type<Reference>,index idx,TPtr base,
  109. const size_type* extents,
  110. const index* strides,
  111. const index* index_bases) const {
  112. BOOST_ASSERT(idx - index_bases[0] >= 0);
  113. BOOST_ASSERT(size_type(idx - index_bases[0]) < extents[0]);
  114. // return a sub_array<T,NDims-1> proxy object
  115. TPtr newbase = base + idx * strides[0];
  116. return Reference(newbase,extents+1,strides+1,index_bases+1);
  117. }
  118. value_accessor_n() { }
  119. ~value_accessor_n() { }
  120. };
  121. //
  122. // value_accessor_one
  123. // contains the routines for accessing reference elements from
  124. // 1-dimensional views.
  125. //
  126. template<typename T>
  127. class value_accessor_one : public multi_array_base {
  128. typedef multi_array_base super_type;
  129. public:
  130. typedef typename super_type::index index;
  131. //
  132. // public typedefs for use by classes that inherit it.
  133. //
  134. typedef T element;
  135. typedef T value_type;
  136. typedef T& reference;
  137. typedef T const& const_reference;
  138. protected:
  139. // used by array operator[] and iterators to get reference types.
  140. template <typename Reference, typename TPtr>
  141. Reference access(boost::type<Reference>,index idx,TPtr base,
  142. const size_type* extents,
  143. const index* strides,
  144. const index* index_bases) const {
  145. ignore_unused_variable_warning(index_bases);
  146. ignore_unused_variable_warning(extents);
  147. BOOST_ASSERT(idx - index_bases[0] >= 0);
  148. BOOST_ASSERT(size_type(idx - index_bases[0]) < extents[0]);
  149. return *(base + idx * strides[0]);
  150. }
  151. value_accessor_one() { }
  152. ~value_accessor_one() { }
  153. };
  154. /////////////////////////////////////////////////////////////////////////
  155. // choose value accessor begins
  156. //
  157. template <typename T, std::size_t NumDims>
  158. struct choose_value_accessor_n {
  159. typedef value_accessor_n<T,NumDims> type;
  160. };
  161. template <typename T>
  162. struct choose_value_accessor_one {
  163. typedef value_accessor_one<T> type;
  164. };
  165. template <typename T, typename NumDims>
  166. struct value_accessor_generator {
  167. BOOST_STATIC_CONSTANT(std::size_t, dimensionality = NumDims::value);
  168. typedef typename
  169. mpl::eval_if_c<(dimensionality == 1),
  170. choose_value_accessor_one<T>,
  171. choose_value_accessor_n<T,dimensionality>
  172. >::type type;
  173. };
  174. template <class T, class NumDims>
  175. struct associated_types
  176. : value_accessor_generator<T,NumDims>::type
  177. {};
  178. //
  179. // choose value accessor ends
  180. /////////////////////////////////////////////////////////////////////////
  181. // Due to some imprecision in the C++ Standard,
  182. // MSVC 2010 is broken in debug mode: it requires
  183. // that an Output Iterator have output_iterator_tag in its iterator_category if
  184. // that iterator is not bidirectional_iterator or random_access_iterator.
  185. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1600)
  186. struct mutable_iterator_tag
  187. : boost::random_access_traversal_tag, std::input_iterator_tag
  188. {
  189. operator std::output_iterator_tag() const {
  190. return std::output_iterator_tag();
  191. }
  192. };
  193. #endif
  194. ////////////////////////////////////////////////////////////////////////
  195. // multi_array_base
  196. ////////////////////////////////////////////////////////////////////////
  197. template <typename T, std::size_t NumDims>
  198. class multi_array_impl_base
  199. :
  200. public value_accessor_generator<T,mpl::size_t<NumDims> >::type
  201. {
  202. typedef associated_types<T,mpl::size_t<NumDims> > types;
  203. public:
  204. typedef typename types::index index;
  205. typedef typename types::size_type size_type;
  206. typedef typename types::element element;
  207. typedef typename types::index_range index_range;
  208. typedef typename types::value_type value_type;
  209. typedef typename types::reference reference;
  210. typedef typename types::const_reference const_reference;
  211. template <std::size_t NDims>
  212. struct subarray {
  213. typedef boost::detail::multi_array::sub_array<T,NDims> type;
  214. };
  215. template <std::size_t NDims>
  216. struct const_subarray {
  217. typedef boost::detail::multi_array::const_sub_array<T,NDims> type;
  218. };
  219. template <std::size_t NDims>
  220. struct array_view {
  221. typedef boost::detail::multi_array::multi_array_view<T,NDims> type;
  222. };
  223. template <std::size_t NDims>
  224. struct const_array_view {
  225. public:
  226. typedef boost::detail::multi_array::const_multi_array_view<T,NDims> type;
  227. };
  228. //
  229. // iterator support
  230. //
  231. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1600)
  232. // Deal with VC 2010 output_iterator_tag requirement
  233. typedef array_iterator<T,T*,mpl::size_t<NumDims>,reference,
  234. mutable_iterator_tag> iterator;
  235. #else
  236. typedef array_iterator<T,T*,mpl::size_t<NumDims>,reference,
  237. boost::random_access_traversal_tag> iterator;
  238. #endif
  239. typedef array_iterator<T,T const*,mpl::size_t<NumDims>,const_reference,
  240. boost::random_access_traversal_tag> const_iterator;
  241. typedef ::boost::reverse_iterator<iterator> reverse_iterator;
  242. typedef ::boost::reverse_iterator<const_iterator> const_reverse_iterator;
  243. BOOST_STATIC_CONSTANT(std::size_t, dimensionality = NumDims);
  244. protected:
  245. multi_array_impl_base() { }
  246. ~multi_array_impl_base() { }
  247. // Used by operator() in our array classes
  248. template <typename Reference, typename IndexList, typename TPtr>
  249. Reference access_element(boost::type<Reference>,
  250. const IndexList& indices,
  251. TPtr base,
  252. const size_type* extents,
  253. const index* strides,
  254. const index* index_bases) const {
  255. boost::function_requires<
  256. CollectionConcept<IndexList> >();
  257. ignore_unused_variable_warning(index_bases);
  258. ignore_unused_variable_warning(extents);
  259. #if !defined(NDEBUG) && !defined(BOOST_DISABLE_ASSERTS)
  260. for (size_type i = 0; i != NumDims; ++i) {
  261. BOOST_ASSERT(indices[i] - index_bases[i] >= 0);
  262. BOOST_ASSERT(size_type(indices[i] - index_bases[i]) < extents[i]);
  263. }
  264. #endif
  265. index offset = 0;
  266. {
  267. typename IndexList::const_iterator i = indices.begin();
  268. size_type n = 0;
  269. while (n != NumDims) {
  270. offset += (*i) * strides[n];
  271. ++n;
  272. ++i;
  273. }
  274. }
  275. return base[offset];
  276. }
  277. template <typename StrideList, typename ExtentList>
  278. void compute_strides(StrideList& stride_list, ExtentList& extent_list,
  279. const general_storage_order<NumDims>& storage)
  280. {
  281. // invariant: stride = the stride for dimension n
  282. index stride = 1;
  283. for (size_type n = 0; n != NumDims; ++n) {
  284. index stride_sign = +1;
  285. if (!storage.ascending(storage.ordering(n)))
  286. stride_sign = -1;
  287. // The stride for this dimension is the product of the
  288. // lengths of the ranks minor to it.
  289. stride_list[storage.ordering(n)] = stride * stride_sign;
  290. stride *= extent_list[storage.ordering(n)];
  291. }
  292. }
  293. // This calculates the offset to the array base pointer due to:
  294. // 1. dimensions stored in descending order
  295. // 2. non-zero dimension index bases
  296. template <typename StrideList, typename ExtentList, typename BaseList>
  297. index
  298. calculate_origin_offset(const StrideList& stride_list,
  299. const ExtentList& extent_list,
  300. const general_storage_order<NumDims>& storage,
  301. const BaseList& index_base_list)
  302. {
  303. return
  304. calculate_descending_dimension_offset(stride_list,extent_list,
  305. storage) +
  306. calculate_indexing_offset(stride_list,index_base_list);
  307. }
  308. // This calculates the offset added to the base pointer that are
  309. // caused by descending dimensions
  310. template <typename StrideList, typename ExtentList>
  311. index
  312. calculate_descending_dimension_offset(const StrideList& stride_list,
  313. const ExtentList& extent_list,
  314. const general_storage_order<NumDims>& storage)
  315. {
  316. index offset = 0;
  317. if (!storage.all_dims_ascending())
  318. for (size_type n = 0; n != NumDims; ++n)
  319. if (!storage.ascending(n))
  320. offset -= (extent_list[n] - 1) * stride_list[n];
  321. return offset;
  322. }
  323. // This is used to reindex array_views, which are no longer
  324. // concerned about storage order (specifically, whether dimensions
  325. // are ascending or descending) since the viewed array handled it.
  326. template <typename StrideList, typename BaseList>
  327. index
  328. calculate_indexing_offset(const StrideList& stride_list,
  329. const BaseList& index_base_list)
  330. {
  331. index offset = 0;
  332. for (size_type n = 0; n != NumDims; ++n)
  333. offset -= stride_list[n] * index_base_list[n];
  334. return offset;
  335. }
  336. // Slicing using an index_gen.
  337. // Note that populating an index_gen creates a type that encodes
  338. // both the number of dimensions in the current Array (NumDims), and
  339. // the Number of dimensions for the resulting view. This allows the
  340. // compiler to fail if the dimensions aren't completely accounted
  341. // for. For reasons unbeknownst to me, a BOOST_STATIC_ASSERT
  342. // within the member function template does not work. I should add a
  343. // note to the documentation specifying that you get a damn ugly
  344. // error message if you screw up in your slicing code.
  345. template <typename ArrayRef, int NDims, typename TPtr>
  346. ArrayRef
  347. generate_array_view(boost::type<ArrayRef>,
  348. const boost::detail::multi_array::
  349. index_gen<NumDims,NDims>& indices,
  350. const size_type* extents,
  351. const index* strides,
  352. const index* index_bases,
  353. TPtr base) const {
  354. boost::array<index,NDims> new_strides;
  355. boost::array<index,NDims> new_extents;
  356. index offset = 0;
  357. size_type dim = 0;
  358. for (size_type n = 0; n != NumDims; ++n) {
  359. // Use array specs and input specs to produce real specs.
  360. const index default_start = index_bases[n];
  361. const index default_finish = default_start+extents[n];
  362. const index_range& current_range = indices.ranges_[n];
  363. index start = current_range.get_start(default_start);
  364. index finish = current_range.get_finish(default_finish);
  365. index stride = current_range.stride();
  366. BOOST_ASSERT(stride != 0);
  367. // An index range indicates a half-open strided interval
  368. // [start,finish) (with stride) which faces upward when stride
  369. // is positive and downward when stride is negative,
  370. // RG: The following code for calculating length suffers from
  371. // some representation issues: if finish-start cannot be represented as
  372. // by type index, then overflow may result.
  373. index len;
  374. if ((finish - start) / stride < 0) {
  375. // [start,finish) is empty according to the direction imposed by
  376. // the stride.
  377. len = 0;
  378. } else {
  379. // integral trick for ceiling((finish-start) / stride)
  380. // taking into account signs.
  381. index shrinkage = stride > 0 ? 1 : -1;
  382. len = (finish - start + (stride - shrinkage)) / stride;
  383. }
  384. // start marks the closed side of the range, so it must lie
  385. // exactly in the set of legal indices
  386. // with a special case for empty arrays
  387. BOOST_ASSERT(index_bases[n] <= start &&
  388. ((start <= index_bases[n]+index(extents[n])) ||
  389. (start == index_bases[n] && extents[n] == 0)));
  390. #ifndef BOOST_DISABLE_ASSERTS
  391. // finish marks the open side of the range, so it can go one past
  392. // the "far side" of the range (the top if stride is positive, the bottom
  393. // if stride is negative).
  394. index bound_adjustment = stride < 0 ? 1 : 0;
  395. BOOST_ASSERT(((index_bases[n] - bound_adjustment) <= finish) &&
  396. (finish <= (index_bases[n] + index(extents[n]) - bound_adjustment)));
  397. ignore_unused_variable_warning(bound_adjustment);
  398. #endif // BOOST_DISABLE_ASSERTS
  399. // the array data pointer is modified to account for non-zero
  400. // bases during slicing (see [Garcia] for the math involved)
  401. offset += start * strides[n];
  402. if (!current_range.is_degenerate()) {
  403. // The stride for each dimension is included into the
  404. // strides for the array_view (see [Garcia] for the math involved).
  405. new_strides[dim] = stride * strides[n];
  406. // calculate new extents
  407. new_extents[dim] = len;
  408. ++dim;
  409. }
  410. }
  411. BOOST_ASSERT(dim == NDims);
  412. return
  413. ArrayRef(base+offset,
  414. new_extents,
  415. new_strides);
  416. }
  417. };
  418. } // namespace multi_array
  419. } // namespace detail
  420. } // namespace boost
  421. #endif // BASE_RG071801_HPP