multi_array.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 BOOST_MULTI_ARRAY_RG071801_HPP
  11. #define BOOST_MULTI_ARRAY_RG071801_HPP
  12. //
  13. // multi_array.hpp - contains the multi_array class template
  14. // declaration and definition
  15. //
  16. #include "boost/multi_array/base.hpp"
  17. #include "boost/multi_array/collection_concept.hpp"
  18. #include "boost/multi_array/copy_array.hpp"
  19. #include "boost/multi_array/iterator.hpp"
  20. #include "boost/multi_array/subarray.hpp"
  21. #include "boost/multi_array/multi_array_ref.hpp"
  22. #include "boost/multi_array/algorithm.hpp"
  23. #include "boost/array.hpp"
  24. #include "boost/mpl/if.hpp"
  25. #include "boost/type_traits.hpp"
  26. #include <algorithm>
  27. #include <cstddef>
  28. #include <functional>
  29. #include <numeric>
  30. #include <vector>
  31. namespace boost {
  32. namespace detail {
  33. namespace multi_array {
  34. struct populate_index_ranges {
  35. multi_array_types::index_range
  36. // RG: underscore on extent_ to stifle strange MSVC warning.
  37. operator()(multi_array_types::index base,
  38. multi_array_types::size_type extent_) {
  39. return multi_array_types::index_range(base,base+extent_);
  40. }
  41. };
  42. #ifdef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  43. //
  44. // Compilers that don't support partial ordering may need help to
  45. // disambiguate multi_array's templated constructors. Even vc6/7 are
  46. // capable of some limited SFINAE, so we take the most-general version
  47. // out of the overload set with disable_multi_array_impl.
  48. //
  49. template <typename T, std::size_t NumDims, typename TPtr>
  50. char is_multi_array_impl_help(const_multi_array_view<T,NumDims,TPtr>&);
  51. template <typename T, std::size_t NumDims, typename TPtr>
  52. char is_multi_array_impl_help(const_sub_array<T,NumDims,TPtr>&);
  53. template <typename T, std::size_t NumDims, typename TPtr>
  54. char is_multi_array_impl_help(const_multi_array_ref<T,NumDims,TPtr>&);
  55. char ( &is_multi_array_impl_help(...) )[2];
  56. template <class T>
  57. struct is_multi_array_impl
  58. {
  59. static T x;
  60. BOOST_STATIC_CONSTANT(bool, value = sizeof((is_multi_array_impl_help)(x)) == 1);
  61. typedef mpl::bool_<value> type;
  62. };
  63. template <bool multi_array = false>
  64. struct disable_multi_array_impl_impl
  65. {
  66. typedef int type;
  67. };
  68. template <>
  69. struct disable_multi_array_impl_impl<true>
  70. {
  71. // forming a pointer to a reference triggers SFINAE
  72. typedef int& type;
  73. };
  74. template <class T>
  75. struct disable_multi_array_impl :
  76. disable_multi_array_impl_impl<is_multi_array_impl<T>::value>
  77. { };
  78. template <>
  79. struct disable_multi_array_impl<int>
  80. {
  81. typedef int type;
  82. };
  83. #endif
  84. } //namespace multi_array
  85. } // namespace detail
  86. template<typename T, std::size_t NumDims,
  87. typename Allocator>
  88. class multi_array :
  89. public multi_array_ref<T,NumDims>
  90. {
  91. typedef multi_array_ref<T,NumDims> super_type;
  92. public:
  93. typedef typename super_type::value_type value_type;
  94. typedef typename super_type::reference reference;
  95. typedef typename super_type::const_reference const_reference;
  96. typedef typename super_type::iterator iterator;
  97. typedef typename super_type::const_iterator const_iterator;
  98. typedef typename super_type::reverse_iterator reverse_iterator;
  99. typedef typename super_type::const_reverse_iterator const_reverse_iterator;
  100. typedef typename super_type::element element;
  101. typedef typename super_type::size_type size_type;
  102. typedef typename super_type::difference_type difference_type;
  103. typedef typename super_type::index index;
  104. typedef typename super_type::extent_range extent_range;
  105. template <std::size_t NDims>
  106. struct const_array_view {
  107. typedef boost::detail::multi_array::const_multi_array_view<T,NDims> type;
  108. };
  109. template <std::size_t NDims>
  110. struct array_view {
  111. typedef boost::detail::multi_array::multi_array_view<T,NDims> type;
  112. };
  113. explicit multi_array() :
  114. super_type((T*)initial_base_,c_storage_order(),
  115. /*index_bases=*/0, /*extents=*/0) {
  116. allocate_space();
  117. }
  118. template <class ExtentList>
  119. explicit multi_array(
  120. ExtentList const& extents
  121. #ifdef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  122. , typename mpl::if_<
  123. detail::multi_array::is_multi_array_impl<ExtentList>,
  124. int&,int>::type* = 0
  125. #endif
  126. ) :
  127. super_type((T*)initial_base_,extents) {
  128. boost::function_requires<
  129. detail::multi_array::CollectionConcept<ExtentList> >();
  130. allocate_space();
  131. }
  132. template <class ExtentList>
  133. explicit multi_array(ExtentList const& extents,
  134. const general_storage_order<NumDims>& so) :
  135. super_type((T*)initial_base_,extents,so) {
  136. boost::function_requires<
  137. detail::multi_array::CollectionConcept<ExtentList> >();
  138. allocate_space();
  139. }
  140. template <class ExtentList>
  141. explicit multi_array(ExtentList const& extents,
  142. const general_storage_order<NumDims>& so,
  143. Allocator const& alloc) :
  144. super_type((T*)initial_base_,extents,so), allocator_(alloc) {
  145. boost::function_requires<
  146. detail::multi_array::CollectionConcept<ExtentList> >();
  147. allocate_space();
  148. }
  149. explicit multi_array(const detail::multi_array
  150. ::extent_gen<NumDims>& ranges) :
  151. super_type((T*)initial_base_,ranges) {
  152. allocate_space();
  153. }
  154. explicit multi_array(const detail::multi_array
  155. ::extent_gen<NumDims>& ranges,
  156. const general_storage_order<NumDims>& so) :
  157. super_type((T*)initial_base_,ranges,so) {
  158. allocate_space();
  159. }
  160. explicit multi_array(const detail::multi_array
  161. ::extent_gen<NumDims>& ranges,
  162. const general_storage_order<NumDims>& so,
  163. Allocator const& alloc) :
  164. super_type((T*)initial_base_,ranges,so), allocator_(alloc) {
  165. allocate_space();
  166. }
  167. multi_array(const multi_array& rhs) :
  168. super_type(rhs), allocator_(rhs.allocator_) {
  169. allocate_space();
  170. boost::detail::multi_array::copy_n(rhs.base_,rhs.num_elements(),base_);
  171. }
  172. //
  173. // A multi_array is constructible from any multi_array_ref, subarray, or
  174. // array_view object. The following constructors ensure that.
  175. //
  176. // Due to limited support for partial template ordering,
  177. // MSVC 6&7 confuse the following with the most basic ExtentList
  178. // constructor.
  179. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  180. template <typename OPtr>
  181. multi_array(const const_multi_array_ref<T,NumDims,OPtr>& rhs,
  182. const general_storage_order<NumDims>& so = c_storage_order())
  183. : super_type(0,so,rhs.index_bases(),rhs.shape())
  184. {
  185. allocate_space();
  186. // Warning! storage order may change, hence the following copy technique.
  187. std::copy(rhs.begin(),rhs.end(),this->begin());
  188. }
  189. template <typename OPtr>
  190. multi_array(const detail::multi_array::
  191. const_sub_array<T,NumDims,OPtr>& rhs,
  192. const general_storage_order<NumDims>& so = c_storage_order())
  193. : super_type(0,so,rhs.index_bases(),rhs.shape())
  194. {
  195. allocate_space();
  196. std::copy(rhs.begin(),rhs.end(),this->begin());
  197. }
  198. template <typename OPtr>
  199. multi_array(const detail::multi_array::
  200. const_multi_array_view<T,NumDims,OPtr>& rhs,
  201. const general_storage_order<NumDims>& so = c_storage_order())
  202. : super_type(0,so,rhs.index_bases(),rhs.shape())
  203. {
  204. allocate_space();
  205. std::copy(rhs.begin(),rhs.end(),this->begin());
  206. }
  207. #else // BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  208. // More limited support for MSVC
  209. multi_array(const const_multi_array_ref<T,NumDims>& rhs)
  210. : super_type(0,c_storage_order(),rhs.index_bases(),rhs.shape())
  211. {
  212. allocate_space();
  213. // Warning! storage order may change, hence the following copy technique.
  214. std::copy(rhs.begin(),rhs.end(),this->begin());
  215. }
  216. multi_array(const const_multi_array_ref<T,NumDims>& rhs,
  217. const general_storage_order<NumDims>& so)
  218. : super_type(0,so,rhs.index_bases(),rhs.shape())
  219. {
  220. allocate_space();
  221. // Warning! storage order may change, hence the following copy technique.
  222. std::copy(rhs.begin(),rhs.end(),this->begin());
  223. }
  224. multi_array(const detail::multi_array::
  225. const_sub_array<T,NumDims>& rhs)
  226. : super_type(0,c_storage_order(),rhs.index_bases(),rhs.shape())
  227. {
  228. allocate_space();
  229. std::copy(rhs.begin(),rhs.end(),this->begin());
  230. }
  231. multi_array(const detail::multi_array::
  232. const_sub_array<T,NumDims>& rhs,
  233. const general_storage_order<NumDims>& so)
  234. : super_type(0,so,rhs.index_bases(),rhs.shape())
  235. {
  236. allocate_space();
  237. std::copy(rhs.begin(),rhs.end(),this->begin());
  238. }
  239. multi_array(const detail::multi_array::
  240. const_multi_array_view<T,NumDims>& rhs)
  241. : super_type(0,c_storage_order(),rhs.index_bases(),rhs.shape())
  242. {
  243. allocate_space();
  244. std::copy(rhs.begin(),rhs.end(),this->begin());
  245. }
  246. multi_array(const detail::multi_array::
  247. const_multi_array_view<T,NumDims>& rhs,
  248. const general_storage_order<NumDims>& so)
  249. : super_type(0,so,rhs.index_bases(),rhs.shape())
  250. {
  251. allocate_space();
  252. std::copy(rhs.begin(),rhs.end(),this->begin());
  253. }
  254. #endif // !BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  255. // Thes constructors are necessary because of more exact template matches.
  256. multi_array(const multi_array_ref<T,NumDims>& rhs)
  257. : super_type(0,c_storage_order(),rhs.index_bases(),rhs.shape())
  258. {
  259. allocate_space();
  260. // Warning! storage order may change, hence the following copy technique.
  261. std::copy(rhs.begin(),rhs.end(),this->begin());
  262. }
  263. multi_array(const multi_array_ref<T,NumDims>& rhs,
  264. const general_storage_order<NumDims>& so)
  265. : super_type(0,so,rhs.index_bases(),rhs.shape())
  266. {
  267. allocate_space();
  268. // Warning! storage order may change, hence the following copy technique.
  269. std::copy(rhs.begin(),rhs.end(),this->begin());
  270. }
  271. multi_array(const detail::multi_array::
  272. sub_array<T,NumDims>& rhs)
  273. : super_type(0,c_storage_order(),rhs.index_bases(),rhs.shape())
  274. {
  275. allocate_space();
  276. std::copy(rhs.begin(),rhs.end(),this->begin());
  277. }
  278. multi_array(const detail::multi_array::
  279. sub_array<T,NumDims>& rhs,
  280. const general_storage_order<NumDims>& so)
  281. : super_type(0,so,rhs.index_bases(),rhs.shape())
  282. {
  283. allocate_space();
  284. std::copy(rhs.begin(),rhs.end(),this->begin());
  285. }
  286. multi_array(const detail::multi_array::
  287. multi_array_view<T,NumDims>& rhs)
  288. : super_type(0,c_storage_order(),rhs.index_bases(),rhs.shape())
  289. {
  290. allocate_space();
  291. std::copy(rhs.begin(),rhs.end(),this->begin());
  292. }
  293. multi_array(const detail::multi_array::
  294. multi_array_view<T,NumDims>& rhs,
  295. const general_storage_order<NumDims>& so)
  296. : super_type(0,so,rhs.index_bases(),rhs.shape())
  297. {
  298. allocate_space();
  299. std::copy(rhs.begin(),rhs.end(),this->begin());
  300. }
  301. // Since assignment is a deep copy, multi_array_ref
  302. // contains all the necessary code.
  303. template <typename ConstMultiArray>
  304. multi_array& operator=(const ConstMultiArray& other) {
  305. super_type::operator=(other);
  306. return *this;
  307. }
  308. multi_array& operator=(const multi_array& other) {
  309. if (&other != this) {
  310. super_type::operator=(other);
  311. }
  312. return *this;
  313. }
  314. template <typename ExtentList>
  315. multi_array& resize(const ExtentList& extents) {
  316. boost::function_requires<
  317. detail::multi_array::CollectionConcept<ExtentList> >();
  318. typedef detail::multi_array::extent_gen<NumDims> gen_type;
  319. gen_type ranges;
  320. for (int i=0; i != NumDims; ++i) {
  321. typedef typename gen_type::range range_type;
  322. ranges.ranges_[i] = range_type(0,extents[i]);
  323. }
  324. return this->resize(ranges);
  325. }
  326. multi_array& resize(const detail::multi_array
  327. ::extent_gen<NumDims>& ranges) {
  328. // build a multi_array with the specs given
  329. multi_array new_array(ranges,this->storage_order());
  330. // build a view of tmp with the minimum extents
  331. // Get the minimum extents of the arrays.
  332. boost::array<size_type,NumDims> min_extents;
  333. const size_type& (*min)(const size_type&, const size_type&) =
  334. std::min;
  335. std::transform(new_array.extent_list_.begin(),new_array.extent_list_.end(),
  336. this->extent_list_.begin(),
  337. min_extents.begin(),
  338. min);
  339. // typedef boost::array<index,NumDims> index_list;
  340. // Build index_gen objects to create views with the same shape
  341. // these need to be separate to handle non-zero index bases
  342. typedef detail::multi_array::index_gen<NumDims,NumDims> index_gen;
  343. index_gen old_idxes;
  344. index_gen new_idxes;
  345. std::transform(new_array.index_base_list_.begin(),
  346. new_array.index_base_list_.end(),
  347. min_extents.begin(),new_idxes.ranges_.begin(),
  348. detail::multi_array::populate_index_ranges());
  349. std::transform(this->index_base_list_.begin(),
  350. this->index_base_list_.end(),
  351. min_extents.begin(),old_idxes.ranges_.begin(),
  352. detail::multi_array::populate_index_ranges());
  353. // Build same-shape views of the two arrays
  354. typename
  355. multi_array::BOOST_NESTED_TEMPLATE array_view<NumDims>::type view_old = (*this)[old_idxes];
  356. typename
  357. multi_array::BOOST_NESTED_TEMPLATE array_view<NumDims>::type view_new = new_array[new_idxes];
  358. // Set the right portion of the new array
  359. view_new = view_old;
  360. using std::swap;
  361. // Swap the internals of these arrays.
  362. swap(this->super_type::base_,new_array.super_type::base_);
  363. swap(this->storage_,new_array.storage_);
  364. swap(this->extent_list_,new_array.extent_list_);
  365. swap(this->stride_list_,new_array.stride_list_);
  366. swap(this->index_base_list_,new_array.index_base_list_);
  367. swap(this->origin_offset_,new_array.origin_offset_);
  368. swap(this->directional_offset_,new_array.directional_offset_);
  369. swap(this->num_elements_,new_array.num_elements_);
  370. swap(this->allocator_,new_array.allocator_);
  371. swap(this->base_,new_array.base_);
  372. swap(this->allocated_elements_,new_array.allocated_elements_);
  373. return *this;
  374. }
  375. ~multi_array() {
  376. deallocate_space();
  377. }
  378. private:
  379. void allocate_space() {
  380. typename Allocator::const_pointer no_hint=0;
  381. base_ = allocator_.allocate(this->num_elements(),no_hint);
  382. this->set_base_ptr(base_);
  383. allocated_elements_ = this->num_elements();
  384. std::uninitialized_fill_n(base_,allocated_elements_,T());
  385. }
  386. void deallocate_space() {
  387. if(base_) {
  388. for(T* i = base_; i != base_+allocated_elements_; ++i)
  389. allocator_.destroy(i);
  390. allocator_.deallocate(base_,allocated_elements_);
  391. }
  392. }
  393. typedef boost::array<size_type,NumDims> size_list;
  394. typedef boost::array<index,NumDims> index_list;
  395. Allocator allocator_;
  396. T* base_;
  397. size_type allocated_elements_;
  398. enum {initial_base_ = 0};
  399. };
  400. } // namespace boost
  401. #endif // BOOST_MULTI_ARRAY_RG071801_HPP