base.hpp 17 KB

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