1 // Copyright 2002 The Trustees of Indiana University.
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // Boost.MultiArray Library
8 // Authors: Ronald Garcia
11 // See http://www.boost.org/libs/multi_array for documentation.
13 #ifndef BASE_RG071801_HPP
14 #define BASE_RG071801_HPP
17 // base.hpp - some implementation base classes for from which
18 // functionality is acquired
21 #include "boost/multi_array/extent_range.hpp"
22 #include "boost/multi_array/extent_gen.hpp"
23 #include "boost/multi_array/index_range.hpp"
24 #include "boost/multi_array/index_gen.hpp"
25 #include "boost/multi_array/storage_order.hpp"
26 #include "boost/multi_array/types.hpp"
27 #include "boost/config.hpp"
28 #include "boost/mpl/eval_if.hpp"
29 #include "boost/mpl/if.hpp"
30 #include "boost/mpl/size_t.hpp"
31 #include "boost/mpl/aux_/msvc_eti_base.hpp"
32 #include "boost/iterator/reverse_iterator.hpp"
33 #include "boost/static_assert.hpp"
34 #include "boost/type.hpp"
41 /////////////////////////////////////////////////////////////////////////
43 /////////////////////////////////////////////////////////////////////////
45 template<typename T, std::size_t NumDims,
46 typename Allocator = std::allocator<T> >
49 // This is a public interface for use by end users!
50 namespace multi_array_types {
51 typedef boost::detail::multi_array::size_type size_type;
52 typedef std::ptrdiff_t difference_type;
53 typedef boost::detail::multi_array::index index;
54 typedef detail::multi_array::index_range<index,size_type> index_range;
55 typedef detail::multi_array::extent_range<index,size_type> extent_range;
56 typedef detail::multi_array::index_gen<0,0> index_gen;
57 typedef detail::multi_array::extent_gen<0> extent_gen;
61 // boost::extents and boost::indices are now a part of the public
62 // interface. That way users don't necessarily have to create their
63 // own objects. On the other hand, one may not want the overhead of
64 // object creation in small-memory environments. Thus, the objects
65 // can be left undefined by defining BOOST_MULTI_ARRAY_NO_GENERATORS
66 // before loading multi_array.hpp.
67 #if !BOOST_MULTI_ARRAY_NO_GENERATORS
69 multi_array_types::extent_gen extents;
70 multi_array_types::index_gen indices;
72 #endif // BOOST_MULTI_ARRAY_NO_GENERATORS
75 namespace multi_array {
77 template <typename T, std::size_t NumDims>
80 template <typename T, std::size_t NumDims, typename TPtr = const T*>
81 class const_sub_array;
83 template <typename T, typename TPtr, typename NumDims, typename Reference>
86 template <typename T, std::size_t NumDims, typename TPtr = const T*>
87 class const_multi_array_view;
89 template <typename T, std::size_t NumDims>
90 class multi_array_view;
92 /////////////////////////////////////////////////////////////////////////
94 /////////////////////////////////////////////////////////////////////////
96 class multi_array_base {
98 typedef multi_array_types::size_type size_type;
99 typedef multi_array_types::difference_type difference_type;
100 typedef multi_array_types::index index;
101 typedef multi_array_types::index_range index_range;
102 typedef multi_array_types::extent_range extent_range;
103 typedef multi_array_types::index_gen index_gen;
104 typedef multi_array_types::extent_gen extent_gen;
109 // contains the routines for accessing elements from
110 // N-dimensional views.
112 template<typename T, std::size_t NumDims>
113 class value_accessor_n : public multi_array_base {
114 typedef multi_array_base super_type;
116 typedef typename super_type::index index;
119 // public typedefs used by classes that inherit from this base
122 typedef boost::multi_array<T,NumDims-1> value_type;
123 typedef sub_array<T,NumDims-1> reference;
124 typedef const_sub_array<T,NumDims-1> const_reference;
127 // used by array operator[] and iterators to get reference types.
128 template <typename Reference, typename TPtr>
129 Reference access(boost::type<Reference>,index idx,TPtr base,
130 const size_type* extents,
131 const index* strides,
132 const index* index_base) const {
134 // return a sub_array<T,NDims-1> proxy object
135 TPtr newbase = base + idx * strides[0];
136 return Reference(newbase,extents+1,strides+1,index_base+1);
140 value_accessor_n() { }
141 ~value_accessor_n() { }
147 // value_accessor_one
148 // contains the routines for accessing reference elements from
149 // 1-dimensional views.
152 class value_accessor_one : public multi_array_base {
153 typedef multi_array_base super_type;
155 typedef typename super_type::index index;
157 // public typedefs for use by classes that inherit it.
160 typedef T value_type;
161 typedef T& reference;
162 typedef T const& const_reference;
165 // used by array operator[] and iterators to get reference types.
166 template <typename Reference, typename TPtr>
167 Reference access(boost::type<Reference>,index idx,TPtr base,
169 const index* strides,
170 const index*) const {
171 return *(base + idx * strides[0]);
174 value_accessor_one() { }
175 ~value_accessor_one() { }
179 /////////////////////////////////////////////////////////////////////////
180 // choose value accessor begins
183 template <typename T, std::size_t NumDims>
184 struct choose_value_accessor_n {
185 typedef value_accessor_n<T,NumDims> type;
188 template <typename T>
189 struct choose_value_accessor_one {
190 typedef value_accessor_one<T> type;
193 template <typename T, typename NumDims>
194 struct value_accessor_generator {
195 BOOST_STATIC_CONSTANT(std::size_t, dimensionality = NumDims::value);
198 mpl::eval_if_c<(dimensionality == 1),
199 choose_value_accessor_one<T>,
200 choose_value_accessor_n<T,dimensionality>
204 #if BOOST_WORKAROUND(BOOST_MSVC, == 1200)
206 struct eti_value_accessor
209 typedef int size_type;
211 typedef int index_range;
212 typedef int value_type;
213 typedef int reference;
214 typedef int const_reference;
218 struct value_accessor_generator<int,int>
220 typedef eti_value_accessor type;
223 template <class T, class NumDims>
224 struct associated_types
225 : mpl::aux::msvc_eti_base<
226 typename value_accessor_generator<T,NumDims>::type
231 struct associated_types<int,int> : eti_value_accessor {};
235 template <class T, class NumDims>
236 struct associated_types
237 : value_accessor_generator<T,NumDims>::type
243 // choose value accessor ends
244 /////////////////////////////////////////////////////////////////////////
248 ////////////////////////////////////////////////////////////////////////
250 ////////////////////////////////////////////////////////////////////////
251 template <typename T, std::size_t NumDims>
252 class multi_array_impl_base
254 #if BOOST_WORKAROUND(BOOST_MSVC, == 1200)
255 public mpl::aux::msvc_eti_base<
256 typename value_accessor_generator<T,mpl::size_t<NumDims> >::type
259 public value_accessor_generator<T,mpl::size_t<NumDims> >::type
262 typedef associated_types<T,mpl::size_t<NumDims> > types;
264 typedef typename types::index index;
265 typedef typename types::size_type size_type;
266 typedef typename types::element element;
267 typedef typename types::index_range index_range;
268 typedef typename types::value_type value_type;
269 typedef typename types::reference reference;
270 typedef typename types::const_reference const_reference;
272 template <std::size_t NDims>
274 typedef boost::detail::multi_array::sub_array<T,NDims> type;
277 template <std::size_t NDims>
278 struct const_subarray {
279 typedef boost::detail::multi_array::const_sub_array<T,NDims> type;
282 template <std::size_t NDims>
284 typedef boost::detail::multi_array::multi_array_view<T,NDims> type;
287 template <std::size_t NDims>
288 struct const_array_view {
290 typedef boost::detail::multi_array::const_multi_array_view<T,NDims> type;
296 typedef array_iterator<T,T*,mpl::size_t<NumDims>,reference> iterator;
297 typedef array_iterator<T,T const*,mpl::size_t<NumDims>,const_reference> const_iterator;
299 typedef ::boost::reverse_iterator<iterator> reverse_iterator;
300 typedef ::boost::reverse_iterator<const_iterator> const_reverse_iterator;
302 BOOST_STATIC_CONSTANT(std::size_t, dimensionality = NumDims);
305 multi_array_impl_base() { }
306 ~multi_array_impl_base() { }
308 // Used by operator() in our array classes
309 template <typename Reference, typename IndexList, typename TPtr>
310 Reference access_element(boost::type<Reference>, TPtr base,
311 const IndexList& indices,
312 const index* strides) const {
314 for (size_type n = 0; n != NumDims; ++n)
315 offset += indices[n] * strides[n];
320 template <typename StrideList, typename ExtentList>
321 void compute_strides(StrideList& stride_list, ExtentList& extent_list,
322 const general_storage_order<NumDims>& storage)
324 // invariant: stride = the stride for dimension n
326 for (size_type n = 0; n != NumDims; ++n) {
327 index stride_sign = +1;
329 if (!storage.ascending(storage.ordering(n)))
332 // The stride for this dimension is the product of the
333 // lengths of the ranks minor to it.
334 stride_list[storage.ordering(n)] = stride * stride_sign;
336 stride *= extent_list[storage.ordering(n)];
340 // This calculates the offset to the array base pointer due to:
341 // 1. dimensions stored in descending order
342 // 2. non-zero dimension index bases
343 template <typename StrideList, typename ExtentList, typename BaseList>
345 calculate_origin_offset(const StrideList& stride_list,
346 const ExtentList& extent_list,
347 const general_storage_order<NumDims>& storage,
348 const BaseList& index_base_list)
351 calculate_descending_dimension_offset(stride_list,extent_list,
353 calculate_indexing_offset(stride_list,index_base_list);
356 // This calculates the offset added to the base pointer that are
357 // caused by descending dimensions
358 template <typename StrideList, typename ExtentList>
360 calculate_descending_dimension_offset(const StrideList& stride_list,
361 const ExtentList& extent_list,
362 const general_storage_order<NumDims>& storage)
365 if (!storage.all_dims_ascending())
366 for (size_type n = 0; n != NumDims; ++n)
367 if (!storage.ascending(n))
368 offset -= (extent_list[n] - 1) * stride_list[n];
373 // This is used to reindex array_views, which are no longer
374 // concerned about storage order (specifically, whether dimensions
375 // are ascending or descending) since the viewed array handled it.
377 template <typename StrideList, typename BaseList>
379 calculate_indexing_offset(const StrideList& stride_list,
380 const BaseList& index_base_list)
383 for (size_type n = 0; n != NumDims; ++n)
384 offset -= stride_list[n] * index_base_list[n];
388 // Slicing using an index_gen.
389 // Note that populating an index_gen creates a type that encodes
390 // both the number of dimensions in the current Array (NumDims), and
391 // the Number of dimensions for the resulting view. This allows the
392 // compiler to fail if the dimensions aren't completely accounted
393 // for. For reasons unbeknownst to me, a BOOST_STATIC_ASSERT
394 // within the member function template does not work. I should add a
395 // note to the documentation specifying that you get a damn ugly
396 // error message if you screw up in your slicing code.
397 template <typename ArrayRef, int NDims, typename TPtr>
399 generate_array_view(boost::type<ArrayRef>,
400 const boost::detail::multi_array::
401 index_gen<NumDims,NDims>& indices,
402 const size_type* extents,
403 const index* strides,
404 const index* index_bases,
407 boost::array<index,NDims> new_strides;
408 boost::array<index,NDims> new_extents;
412 for (size_type n = 0; n != NumDims; ++n) {
413 const index default_start = index_bases[n];
414 const index default_finish = default_start+extents[n];
415 const index_range& current_range = indices.ranges_[n];
416 index start = current_range.get_start(default_start);
417 index finish = current_range.get_finish(default_finish);
418 index index_factor = current_range.stride();
419 index len = (finish - start + (index_factor - 1)) / index_factor;
421 // the array data pointer is modified to account for non-zero
422 // bases during slicing (see [Garcia] for the math involved)
423 offset += start * strides[n];
425 if (!current_range.is_degenerate()) {
427 // The index_factor for each dimension is included into the
428 // strides for the array_view (see [Garcia] for the math involved).
429 new_strides[dim] = index_factor * strides[n];
431 // calculate new extents
432 new_extents[dim] = len;
436 assert (dim == NDims);
439 ArrayRef(base+offset,
447 } // namespace multi_array
448 } // namespace detail
452 #endif // BASE_RG071801_HPP