123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- #ifndef BOOST_MULTI_ARRAY_BASE_HPP
- #define BOOST_MULTI_ARRAY_BASE_HPP
- #include "boost/multi_array/extent_range.hpp"
- #include "boost/multi_array/extent_gen.hpp"
- #include "boost/multi_array/index_range.hpp"
- #include "boost/multi_array/index_gen.hpp"
- #include "boost/multi_array/storage_order.hpp"
- #include "boost/multi_array/types.hpp"
- #include "boost/config.hpp"
- #include "boost/multi_array/concept_checks.hpp"
- #include "boost/mpl/eval_if.hpp"
- #include "boost/mpl/if.hpp"
- #include "boost/mpl/size_t.hpp"
- #include "boost/iterator/reverse_iterator.hpp"
- #include "boost/static_assert.hpp"
- #include "boost/type.hpp"
- #include "boost/assert.hpp"
- #include <cstddef>
- #include <memory>
- namespace boost {
- template<typename T, std::size_t NumDims,
- typename Allocator = std::allocator<T> >
- class multi_array;
- namespace multi_array_types {
- typedef boost::detail::multi_array::size_type size_type;
- typedef std::ptrdiff_t difference_type;
- typedef boost::detail::multi_array::index index;
- typedef detail::multi_array::index_range<index,size_type> index_range;
- typedef detail::multi_array::extent_range<index,size_type> extent_range;
- typedef detail::multi_array::index_gen<0,0> index_gen;
- typedef detail::multi_array::extent_gen<0> extent_gen;
- }
- #ifndef BOOST_MULTI_ARRAY_NO_GENERATORS
- namespace {
- multi_array_types::extent_gen extents;
- multi_array_types::index_gen indices;
- }
- #endif
- namespace detail {
- namespace multi_array {
- template <typename T, std::size_t NumDims>
- class sub_array;
- template <typename T, std::size_t NumDims, typename TPtr = const T*>
- class const_sub_array;
- template <typename T, typename TPtr, typename NumDims, typename Reference,
- typename IteratorCategory>
- class array_iterator;
- template <typename T, std::size_t NumDims, typename TPtr = const T*>
- class const_multi_array_view;
- template <typename T, std::size_t NumDims>
- class multi_array_view;
- class multi_array_base {
- public:
- typedef multi_array_types::size_type size_type;
- typedef multi_array_types::difference_type difference_type;
- typedef multi_array_types::index index;
- typedef multi_array_types::index_range index_range;
- typedef multi_array_types::extent_range extent_range;
- typedef multi_array_types::index_gen index_gen;
- typedef multi_array_types::extent_gen extent_gen;
- };
- template<typename T, std::size_t NumDims>
- class value_accessor_n : public multi_array_base {
- typedef multi_array_base super_type;
- public:
- typedef typename super_type::index index;
-
-
-
- typedef T element;
- typedef boost::multi_array<T,NumDims-1> value_type;
- typedef sub_array<T,NumDims-1> reference;
- typedef const_sub_array<T,NumDims-1> const_reference;
- protected:
-
- template <typename Reference, typename TPtr>
- Reference access(boost::type<Reference>,index idx,TPtr base,
- const size_type* extents,
- const index* strides,
- const index* index_bases) const {
- BOOST_ASSERT(idx - index_bases[0] >= 0);
- BOOST_ASSERT(size_type(idx - index_bases[0]) < extents[0]);
-
- TPtr newbase = base + idx * strides[0];
- return Reference(newbase,extents+1,strides+1,index_bases+1);
- }
- value_accessor_n() { }
- ~value_accessor_n() { }
- };
- template<typename T>
- class value_accessor_one : public multi_array_base {
- typedef multi_array_base super_type;
- public:
- typedef typename super_type::index index;
-
-
-
- typedef T element;
- typedef T value_type;
- typedef T& reference;
- typedef T const& const_reference;
- protected:
-
- template <typename Reference, typename TPtr>
- Reference access(boost::type<Reference>,index idx,TPtr base,
- const size_type* extents,
- const index* strides,
- const index* index_bases) const {
- ignore_unused_variable_warning(index_bases);
- ignore_unused_variable_warning(extents);
- BOOST_ASSERT(idx - index_bases[0] >= 0);
- BOOST_ASSERT(size_type(idx - index_bases[0]) < extents[0]);
- return *(base + idx * strides[0]);
- }
- value_accessor_one() { }
- ~value_accessor_one() { }
- };
- template <typename T, std::size_t NumDims>
- struct choose_value_accessor_n {
- typedef value_accessor_n<T,NumDims> type;
- };
- template <typename T>
- struct choose_value_accessor_one {
- typedef value_accessor_one<T> type;
- };
- template <typename T, typename NumDims>
- struct value_accessor_generator {
- BOOST_STATIC_CONSTANT(std::size_t, dimensionality = NumDims::value);
-
- typedef typename
- mpl::eval_if_c<(dimensionality == 1),
- choose_value_accessor_one<T>,
- choose_value_accessor_n<T,dimensionality>
- >::type type;
- };
- template <class T, class NumDims>
- struct associated_types
- : value_accessor_generator<T,NumDims>::type
- {};
- #if BOOST_WORKAROUND(BOOST_MSVC, >= 1600)
- struct mutable_iterator_tag
- : boost::random_access_traversal_tag, std::input_iterator_tag
- {
- operator std::output_iterator_tag() const {
- return std::output_iterator_tag();
- }
- };
- #endif
- template <typename T, std::size_t NumDims>
- class multi_array_impl_base
- :
- public value_accessor_generator<T,mpl::size_t<NumDims> >::type
- {
- typedef associated_types<T,mpl::size_t<NumDims> > types;
- public:
- typedef typename types::index index;
- typedef typename types::size_type size_type;
- typedef typename types::element element;
- typedef typename types::index_range index_range;
- typedef typename types::value_type value_type;
- typedef typename types::reference reference;
- typedef typename types::const_reference const_reference;
- template <std::size_t NDims>
- struct subarray {
- typedef boost::detail::multi_array::sub_array<T,NDims> type;
- };
- template <std::size_t NDims>
- struct const_subarray {
- typedef boost::detail::multi_array::const_sub_array<T,NDims> type;
- };
- template <std::size_t NDims>
- struct array_view {
- typedef boost::detail::multi_array::multi_array_view<T,NDims> type;
- };
- template <std::size_t NDims>
- struct const_array_view {
- public:
- typedef boost::detail::multi_array::const_multi_array_view<T,NDims> type;
- };
-
-
-
- #if BOOST_WORKAROUND(BOOST_MSVC, >= 1600)
-
- typedef array_iterator<T,T*,mpl::size_t<NumDims>,reference,
- mutable_iterator_tag> iterator;
- #else
- typedef array_iterator<T,T*,mpl::size_t<NumDims>,reference,
- boost::random_access_traversal_tag> iterator;
- #endif
- typedef array_iterator<T,T const*,mpl::size_t<NumDims>,const_reference,
- boost::random_access_traversal_tag> const_iterator;
- typedef ::boost::reverse_iterator<iterator> reverse_iterator;
- typedef ::boost::reverse_iterator<const_iterator> const_reverse_iterator;
- BOOST_STATIC_CONSTANT(std::size_t, dimensionality = NumDims);
- protected:
- multi_array_impl_base() { }
- ~multi_array_impl_base() { }
-
- template <typename Reference, typename IndexList, typename TPtr>
- Reference access_element(boost::type<Reference>,
- const IndexList& indices,
- TPtr base,
- const size_type* extents,
- const index* strides,
- const index* index_bases) const {
- boost::function_requires<
- CollectionConcept<IndexList> >();
- ignore_unused_variable_warning(index_bases);
- ignore_unused_variable_warning(extents);
- #if !defined(NDEBUG) && !defined(BOOST_DISABLE_ASSERTS)
- for (size_type i = 0; i != NumDims; ++i) {
- BOOST_ASSERT(indices[i] - index_bases[i] >= 0);
- BOOST_ASSERT(size_type(indices[i] - index_bases[i]) < extents[i]);
- }
- #endif
- index offset = 0;
- {
- typename IndexList::const_iterator i = indices.begin();
- size_type n = 0;
- while (n != NumDims) {
- offset += (*i) * strides[n];
- ++n;
- ++i;
- }
- }
- return base[offset];
- }
- template <typename StrideList, typename ExtentList>
- void compute_strides(StrideList& stride_list, ExtentList& extent_list,
- const general_storage_order<NumDims>& storage)
- {
-
- index stride = 1;
- for (size_type n = 0; n != NumDims; ++n) {
- index stride_sign = +1;
-
- if (!storage.ascending(storage.ordering(n)))
- stride_sign = -1;
-
-
-
- stride_list[storage.ordering(n)] = stride * stride_sign;
-
- stride *= extent_list[storage.ordering(n)];
- }
- }
-
-
-
- template <typename StrideList, typename ExtentList, typename BaseList>
- index
- calculate_origin_offset(const StrideList& stride_list,
- const ExtentList& extent_list,
- const general_storage_order<NumDims>& storage,
- const BaseList& index_base_list)
- {
- return
- calculate_descending_dimension_offset(stride_list,extent_list,
- storage) +
- calculate_indexing_offset(stride_list,index_base_list);
- }
-
-
- template <typename StrideList, typename ExtentList>
- index
- calculate_descending_dimension_offset(const StrideList& stride_list,
- const ExtentList& extent_list,
- const general_storage_order<NumDims>& storage)
- {
- index offset = 0;
- if (!storage.all_dims_ascending())
- for (size_type n = 0; n != NumDims; ++n)
- if (!storage.ascending(n))
- offset -= (extent_list[n] - 1) * stride_list[n];
- return offset;
- }
-
-
-
- template <typename StrideList, typename BaseList>
- index
- calculate_indexing_offset(const StrideList& stride_list,
- const BaseList& index_base_list)
- {
- index offset = 0;
- for (size_type n = 0; n != NumDims; ++n)
- offset -= stride_list[n] * index_base_list[n];
- return offset;
- }
-
-
-
-
-
-
-
-
-
- template <typename ArrayRef, int NDims, typename TPtr>
- ArrayRef
- generate_array_view(boost::type<ArrayRef>,
- const boost::detail::multi_array::
- index_gen<NumDims,NDims>& indices,
- const size_type* extents,
- const index* strides,
- const index* index_bases,
- TPtr base) const {
- boost::array<index,NDims> new_strides;
- boost::array<index,NDims> new_extents;
- index offset = 0;
- size_type dim = 0;
- for (size_type n = 0; n != NumDims; ++n) {
-
- const index default_start = index_bases[n];
- const index default_finish = default_start+extents[n];
- const index_range& current_range = indices.ranges_[n];
- index start = current_range.get_start(default_start);
- index finish = current_range.get_finish(default_finish);
- index stride = current_range.stride();
- BOOST_ASSERT(stride != 0);
-
-
-
-
-
-
- index len;
- if ((finish - start) / stride < 0) {
-
-
- len = 0;
- } else {
-
-
- index shrinkage = stride > 0 ? 1 : -1;
- len = (finish - start + (stride - shrinkage)) / stride;
- }
-
-
-
- BOOST_ASSERT(index_bases[n] <= start &&
- ((start <= index_bases[n]+index(extents[n])) ||
- (start == index_bases[n] && extents[n] == 0)));
- #ifndef BOOST_DISABLE_ASSERTS
-
-
-
- index bound_adjustment = stride < 0 ? 1 : 0;
- BOOST_ASSERT(((index_bases[n] - bound_adjustment) <= finish) &&
- (finish <= (index_bases[n] + index(extents[n]) - bound_adjustment)));
- ignore_unused_variable_warning(bound_adjustment);
- #endif
-
-
- offset += start * strides[n];
- if (!current_range.is_degenerate()) {
-
-
- new_strides[dim] = stride * strides[n];
-
-
- new_extents[dim] = len;
- ++dim;
- }
- }
- BOOST_ASSERT(dim == NDims);
- return
- ArrayRef(base+offset,
- new_extents,
- new_strides);
- }
-
- };
- }
- }
- }
- #endif
|