File indexing completed on 2025-01-18 09:42:05
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef BOOST_MULTI_ARRAY_BASE_HPP
0014 #define BOOST_MULTI_ARRAY_BASE_HPP
0015
0016
0017
0018
0019
0020
0021 #include "boost/multi_array/extent_range.hpp"
0022 #include "boost/multi_array/extent_gen.hpp"
0023 #include "boost/multi_array/index_range.hpp"
0024 #include "boost/multi_array/index_gen.hpp"
0025 #include "boost/multi_array/storage_order.hpp"
0026 #include "boost/multi_array/types.hpp"
0027 #include "boost/config.hpp"
0028 #include "boost/multi_array/concept_checks.hpp" //for ignore_unused_...
0029 #include "boost/mpl/eval_if.hpp"
0030 #include "boost/mpl/if.hpp"
0031 #include "boost/mpl/size_t.hpp"
0032 #include "boost/iterator/reverse_iterator.hpp"
0033 #include "boost/static_assert.hpp"
0034 #include "boost/type.hpp"
0035 #include "boost/assert.hpp"
0036 #include <cstddef>
0037 #include <memory>
0038
0039 namespace boost {
0040
0041
0042
0043
0044
0045 template<typename T, std::size_t NumDims,
0046 typename Allocator = std::allocator<T> >
0047 class multi_array;
0048
0049
0050 namespace multi_array_types {
0051 typedef boost::detail::multi_array::size_type size_type;
0052 typedef std::ptrdiff_t difference_type;
0053 typedef boost::detail::multi_array::index index;
0054 typedef detail::multi_array::index_range<index,size_type> index_range;
0055 typedef detail::multi_array::extent_range<index,size_type> extent_range;
0056 typedef detail::multi_array::index_gen<0,0> index_gen;
0057 typedef detail::multi_array::extent_gen<0> extent_gen;
0058 }
0059
0060
0061
0062
0063
0064
0065
0066
0067 #ifndef BOOST_MULTI_ARRAY_NO_GENERATORS
0068 namespace {
0069 multi_array_types::extent_gen extents;
0070 multi_array_types::index_gen indices;
0071 }
0072 #endif
0073
0074 namespace detail {
0075 namespace multi_array {
0076
0077 template <typename T, std::size_t NumDims>
0078 class sub_array;
0079
0080 template <typename T, std::size_t NumDims, typename TPtr = const T*>
0081 class const_sub_array;
0082
0083 template <typename T, typename TPtr, typename NumDims, typename Reference,
0084 typename IteratorCategory>
0085 class array_iterator;
0086
0087 template <typename T, std::size_t NumDims, typename TPtr = const T*>
0088 class const_multi_array_view;
0089
0090 template <typename T, std::size_t NumDims>
0091 class multi_array_view;
0092
0093
0094
0095
0096
0097 class multi_array_base {
0098 public:
0099 typedef multi_array_types::size_type size_type;
0100 typedef multi_array_types::difference_type difference_type;
0101 typedef multi_array_types::index index;
0102 typedef multi_array_types::index_range index_range;
0103 typedef multi_array_types::extent_range extent_range;
0104 typedef multi_array_types::index_gen index_gen;
0105 typedef multi_array_types::extent_gen extent_gen;
0106 };
0107
0108
0109
0110
0111
0112
0113 template<typename T, std::size_t NumDims>
0114 class value_accessor_n : public multi_array_base {
0115 typedef multi_array_base super_type;
0116 public:
0117 typedef typename super_type::index index;
0118
0119
0120
0121
0122 typedef T element;
0123 typedef boost::multi_array<T,NumDims-1> value_type;
0124 typedef sub_array<T,NumDims-1> reference;
0125 typedef const_sub_array<T,NumDims-1> const_reference;
0126
0127 protected:
0128
0129 template <typename Reference, typename TPtr>
0130 Reference access(boost::type<Reference>,index idx,TPtr base,
0131 const size_type* extents,
0132 const index* strides,
0133 const index* index_bases) const {
0134
0135 BOOST_ASSERT(idx - index_bases[0] >= 0);
0136 BOOST_ASSERT(size_type(idx - index_bases[0]) < extents[0]);
0137
0138 TPtr newbase = base + idx * strides[0];
0139 return Reference(newbase,extents+1,strides+1,index_bases+1);
0140
0141 }
0142
0143 value_accessor_n() { }
0144 ~value_accessor_n() { }
0145 };
0146
0147
0148
0149
0150
0151
0152
0153
0154 template<typename T>
0155 class value_accessor_one : public multi_array_base {
0156 typedef multi_array_base super_type;
0157 public:
0158 typedef typename super_type::index index;
0159
0160
0161
0162 typedef T element;
0163 typedef T value_type;
0164 typedef T& reference;
0165 typedef T const& const_reference;
0166
0167 protected:
0168
0169 template <typename Reference, typename TPtr>
0170 Reference access(boost::type<Reference>,index idx,TPtr base,
0171 const size_type* extents,
0172 const index* strides,
0173 const index* index_bases) const {
0174
0175 ignore_unused_variable_warning(index_bases);
0176 ignore_unused_variable_warning(extents);
0177 BOOST_ASSERT(idx - index_bases[0] >= 0);
0178 BOOST_ASSERT(size_type(idx - index_bases[0]) < extents[0]);
0179 return *(base + idx * strides[0]);
0180 }
0181
0182 value_accessor_one() { }
0183 ~value_accessor_one() { }
0184 };
0185
0186
0187
0188
0189
0190
0191 template <typename T, std::size_t NumDims>
0192 struct choose_value_accessor_n {
0193 typedef value_accessor_n<T,NumDims> type;
0194 };
0195
0196 template <typename T>
0197 struct choose_value_accessor_one {
0198 typedef value_accessor_one<T> type;
0199 };
0200
0201 template <typename T, typename NumDims>
0202 struct value_accessor_generator {
0203 BOOST_STATIC_CONSTANT(std::size_t, dimensionality = NumDims::value);
0204
0205 typedef typename
0206 mpl::eval_if_c<(dimensionality == 1),
0207 choose_value_accessor_one<T>,
0208 choose_value_accessor_n<T,dimensionality>
0209 >::type type;
0210 };
0211
0212 template <class T, class NumDims>
0213 struct associated_types
0214 : value_accessor_generator<T,NumDims>::type
0215 {};
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1600)
0226 struct mutable_iterator_tag
0227 : boost::random_access_traversal_tag, std::input_iterator_tag
0228 {
0229 operator std::output_iterator_tag() const {
0230 return std::output_iterator_tag();
0231 }
0232 };
0233 #endif
0234
0235
0236
0237
0238 template <typename T, std::size_t NumDims>
0239 class multi_array_impl_base
0240 :
0241 public value_accessor_generator<T,mpl::size_t<NumDims> >::type
0242 {
0243 typedef associated_types<T,mpl::size_t<NumDims> > types;
0244 public:
0245 typedef typename types::index index;
0246 typedef typename types::size_type size_type;
0247 typedef typename types::element element;
0248 typedef typename types::index_range index_range;
0249 typedef typename types::value_type value_type;
0250 typedef typename types::reference reference;
0251 typedef typename types::const_reference const_reference;
0252
0253 template <std::size_t NDims>
0254 struct subarray {
0255 typedef boost::detail::multi_array::sub_array<T,NDims> type;
0256 };
0257
0258 template <std::size_t NDims>
0259 struct const_subarray {
0260 typedef boost::detail::multi_array::const_sub_array<T,NDims> type;
0261 };
0262
0263 template <std::size_t NDims>
0264 struct array_view {
0265 typedef boost::detail::multi_array::multi_array_view<T,NDims> type;
0266 };
0267
0268 template <std::size_t NDims>
0269 struct const_array_view {
0270 public:
0271 typedef boost::detail::multi_array::const_multi_array_view<T,NDims> type;
0272 };
0273
0274
0275
0276
0277 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1600)
0278
0279 typedef array_iterator<T,T*,mpl::size_t<NumDims>,reference,
0280 mutable_iterator_tag> iterator;
0281 #else
0282 typedef array_iterator<T,T*,mpl::size_t<NumDims>,reference,
0283 boost::random_access_traversal_tag> iterator;
0284 #endif
0285 typedef array_iterator<T,T const*,mpl::size_t<NumDims>,const_reference,
0286 boost::random_access_traversal_tag> const_iterator;
0287
0288 typedef ::boost::reverse_iterator<iterator> reverse_iterator;
0289 typedef ::boost::reverse_iterator<const_iterator> const_reverse_iterator;
0290
0291 BOOST_STATIC_CONSTANT(std::size_t, dimensionality = NumDims);
0292 protected:
0293
0294 multi_array_impl_base() { }
0295 ~multi_array_impl_base() { }
0296
0297
0298 template <typename Reference, typename IndexList, typename TPtr>
0299 Reference access_element(boost::type<Reference>,
0300 const IndexList& indices,
0301 TPtr base,
0302 const size_type* extents,
0303 const index* strides,
0304 const index* index_bases) const {
0305 boost::function_requires<
0306 CollectionConcept<IndexList> >();
0307 ignore_unused_variable_warning(index_bases);
0308 ignore_unused_variable_warning(extents);
0309 #if !defined(NDEBUG) && !defined(BOOST_DISABLE_ASSERTS)
0310 for (size_type i = 0; i != NumDims; ++i) {
0311 BOOST_ASSERT(indices[i] - index_bases[i] >= 0);
0312 BOOST_ASSERT(size_type(indices[i] - index_bases[i]) < extents[i]);
0313 }
0314 #endif
0315
0316 index offset = 0;
0317 {
0318 typename IndexList::const_iterator i = indices.begin();
0319 size_type n = 0;
0320 while (n != NumDims) {
0321 offset += (*i) * strides[n];
0322 ++n;
0323 ++i;
0324 }
0325 }
0326 return base[offset];
0327 }
0328
0329 template <typename StrideList, typename ExtentList>
0330 void compute_strides(StrideList& stride_list, ExtentList& extent_list,
0331 const general_storage_order<NumDims>& storage)
0332 {
0333
0334 index stride = 1;
0335 for (size_type n = 0; n != NumDims; ++n) {
0336 index stride_sign = +1;
0337
0338 if (!storage.ascending(storage.ordering(n)))
0339 stride_sign = -1;
0340
0341
0342
0343 stride_list[storage.ordering(n)] = stride * stride_sign;
0344
0345 stride *= extent_list[storage.ordering(n)];
0346 }
0347 }
0348
0349
0350
0351
0352 template <typename StrideList, typename ExtentList, typename BaseList>
0353 index
0354 calculate_origin_offset(const StrideList& stride_list,
0355 const ExtentList& extent_list,
0356 const general_storage_order<NumDims>& storage,
0357 const BaseList& index_base_list)
0358 {
0359 return
0360 calculate_descending_dimension_offset(stride_list,extent_list,
0361 storage) +
0362 calculate_indexing_offset(stride_list,index_base_list);
0363 }
0364
0365
0366
0367 template <typename StrideList, typename ExtentList>
0368 index
0369 calculate_descending_dimension_offset(const StrideList& stride_list,
0370 const ExtentList& extent_list,
0371 const general_storage_order<NumDims>& storage)
0372 {
0373 index offset = 0;
0374 if (!storage.all_dims_ascending())
0375 for (size_type n = 0; n != NumDims; ++n)
0376 if (!storage.ascending(n))
0377 offset -= (extent_list[n] - 1) * stride_list[n];
0378
0379 return offset;
0380 }
0381
0382
0383
0384
0385
0386 template <typename StrideList, typename BaseList>
0387 index
0388 calculate_indexing_offset(const StrideList& stride_list,
0389 const BaseList& index_base_list)
0390 {
0391 index offset = 0;
0392 for (size_type n = 0; n != NumDims; ++n)
0393 offset -= stride_list[n] * index_base_list[n];
0394 return offset;
0395 }
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406 template <typename ArrayRef, int NDims, typename TPtr>
0407 ArrayRef
0408 generate_array_view(boost::type<ArrayRef>,
0409 const boost::detail::multi_array::
0410 index_gen<NumDims,NDims>& indices,
0411 const size_type* extents,
0412 const index* strides,
0413 const index* index_bases,
0414 TPtr base) const {
0415
0416 boost::array<index,NDims> new_strides;
0417 boost::array<index,NDims> new_extents;
0418
0419 index offset = 0;
0420 size_type dim = 0;
0421 for (size_type n = 0; n != NumDims; ++n) {
0422
0423
0424 const index default_start = index_bases[n];
0425 const index default_finish = default_start+extents[n];
0426 const index_range& current_range = indices.ranges_[n];
0427 index start = current_range.get_start(default_start);
0428 index finish = current_range.get_finish(default_finish);
0429 index stride = current_range.stride();
0430 BOOST_ASSERT(stride != 0);
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440 index len;
0441 if ((finish - start) / stride < 0) {
0442
0443
0444 len = 0;
0445 } else {
0446
0447
0448 index shrinkage = stride > 0 ? 1 : -1;
0449 len = (finish - start + (stride - shrinkage)) / stride;
0450 }
0451
0452
0453
0454
0455 BOOST_ASSERT(index_bases[n] <= start &&
0456 ((start <= index_bases[n]+index(extents[n])) ||
0457 (start == index_bases[n] && extents[n] == 0)));
0458
0459 #ifndef BOOST_DISABLE_ASSERTS
0460
0461
0462
0463 index bound_adjustment = stride < 0 ? 1 : 0;
0464 BOOST_ASSERT(((index_bases[n] - bound_adjustment) <= finish) &&
0465 (finish <= (index_bases[n] + index(extents[n]) - bound_adjustment)));
0466 ignore_unused_variable_warning(bound_adjustment);
0467 #endif
0468
0469
0470
0471
0472 offset += start * strides[n];
0473
0474 if (!current_range.is_degenerate()) {
0475
0476
0477
0478 new_strides[dim] = stride * strides[n];
0479
0480
0481 new_extents[dim] = len;
0482 ++dim;
0483 }
0484 }
0485 BOOST_ASSERT(dim == NDims);
0486
0487 return
0488 ArrayRef(base+offset,
0489 new_extents,
0490 new_strides);
0491 }
0492
0493
0494 };
0495
0496 }
0497 }
0498
0499 }
0500
0501 #endif