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 SUBARRAY_RG071801_HPP
14 #define SUBARRAY_RG071801_HPP
17 // subarray.hpp - used to implement standard operator[] on
21 #include "boost/multi_array/base.hpp"
22 #include "boost/multi_array/concept_checks.hpp"
23 #include "boost/limits.hpp"
24 #include "boost/type.hpp"
31 namespace multi_array {
35 // multi_array's proxy class to allow multiple overloads of
36 // operator[] in order to provide a clean multi-dimensional array
38 template <typename T, std::size_t NumDims, typename TPtr>
39 class const_sub_array :
40 public boost::detail::multi_array::multi_array_impl_base<T,NumDims>
42 typedef boost::detail::multi_array::multi_array_impl_base<T,NumDims> super_type;
44 typedef typename super_type::value_type value_type;
45 typedef typename super_type::const_reference const_reference;
46 typedef typename super_type::const_iterator const_iterator;
47 typedef typename super_type::const_reverse_iterator const_reverse_iterator;
48 typedef typename super_type::element element;
49 typedef typename super_type::size_type size_type;
50 typedef typename super_type::difference_type difference_type;
51 typedef typename super_type::index index;
52 typedef typename super_type::extent_range extent_range;
55 template <std::size_t NDims>
56 struct const_array_view {
57 typedef boost::detail::multi_array::const_multi_array_view<T,NDims> type;
60 template <std::size_t NDims>
62 typedef boost::detail::multi_array::multi_array_view<T,NDims> type;
65 // Allow default copy constructor as well.
67 template <typename OPtr>
68 const_sub_array (const const_sub_array<T,NumDims,OPtr>& rhs) :
69 base_(rhs.base_), extents_(rhs.extents_), strides_(rhs.strides_),
70 index_base_(rhs.index_base_) {
73 // const_sub_array always returns const types, regardless of its own
75 const_reference operator[](index idx) const {
76 return super_type::access(boost::type<const_reference>(),
77 idx,base_,shape(),strides(),index_bases());
80 template <typename IndexList>
81 const element& operator()(const IndexList& indices) const {
82 return super_type::access_element(boost::type<const element&>(),
87 // see generate_array_view in base.hpp
88 #if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
91 template <int NumDims, int NDims> // else ICE
93 typename const_array_view<NDims>::type
94 operator[](const boost::detail::multi_array::
95 index_gen<NumDims,NDims>& indices)
97 typedef typename const_array_view<NDims>::type return_type;
99 super_type::generate_array_view(boost::type<return_type>(),
107 template <typename OPtr>
108 bool operator<(const const_sub_array<T,NumDims,OPtr>& rhs) const {
109 return std::lexicographical_compare(begin(),end(),rhs.begin(),rhs.end());
112 template <typename OPtr>
113 bool operator==(const const_sub_array<T,NumDims,OPtr>& rhs) const {
114 if(std::equal(shape(),shape()+num_dimensions(),rhs.shape()))
115 return std::equal(begin(),end(),rhs.begin());
119 template <typename OPtr>
120 bool operator!=(const const_sub_array<T,NumDims,OPtr>& rhs) const {
121 return !(*this == rhs);
124 template <typename OPtr>
125 bool operator>(const const_sub_array<T,NumDims,OPtr>& rhs) const {
129 template <typename OPtr>
130 bool operator<=(const const_sub_array<T,NumDims,OPtr>& rhs) const {
131 return !(*this > rhs);
134 template <typename OPtr>
135 bool operator>=(const const_sub_array<T,NumDims,OPtr>& rhs) const {
136 return !(*this < rhs);
139 const_iterator begin() const {
140 return const_iterator(*index_bases(),origin(),
141 shape(),strides(),index_bases());
144 const_iterator end() const {
145 return const_iterator(*index_bases()+(index)*shape(),origin(),
146 shape(),strides(),index_bases());
149 const_reverse_iterator rbegin() const {
150 return const_reverse_iterator(end());
153 const_reverse_iterator rend() const {
154 return const_reverse_iterator(begin());
157 TPtr origin() const { return base_; }
158 size_type size() const { return extents_[0]; }
159 size_type max_size() const { return num_elements(); }
160 bool empty() const { return size() == 0; }
161 size_type num_dimensions() const { return NumDims; }
162 const size_type* shape() const { return extents_; }
163 const index* strides() const { return strides_; }
164 const index* index_bases() const { return index_base_; }
166 size_type num_elements() const {
167 return std::accumulate(shape(),shape() + num_dimensions(),
168 size_type(1), std::multiplies<size_type>());
172 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
174 template <typename,std::size_t> friend class value_accessor_n;
175 template <typename,std::size_t,typename> friend class const_sub_array;
177 public: // Should be protected
180 const_sub_array (TPtr base,
181 const size_type* extents,
182 const index* strides,
183 const index* index_base) :
184 base_(base), extents_(extents), strides_(strides),
185 index_base_(index_base) {
189 const size_type* extents_;
190 const index* strides_;
191 const index* index_base_;
193 // const_sub_array cannot be assigned to (no deep copies!)
194 const_sub_array& operator=(const const_sub_array&);
200 // multi_array's proxy class to allow multiple overloads of
201 // operator[] in order to provide a clean multi-dimensional array
203 template <typename T, std::size_t NumDims>
204 class sub_array : public const_sub_array<T,NumDims,T*>
206 typedef const_sub_array<T,NumDims,T*> super_type;
208 typedef typename super_type::element element;
209 typedef typename super_type::reference reference;
210 typedef typename super_type::index index;
211 typedef typename super_type::size_type size_type;
212 typedef typename super_type::iterator iterator;
213 typedef typename super_type::reverse_iterator reverse_iterator;
214 typedef typename super_type::const_reference const_reference;
215 typedef typename super_type::const_iterator const_iterator;
216 typedef typename super_type::const_reverse_iterator const_reverse_iterator;
219 template <std::size_t NDims>
220 struct const_array_view {
221 typedef boost::detail::multi_array::const_multi_array_view<T,NDims> type;
224 template <std::size_t NDims>
226 typedef boost::detail::multi_array::multi_array_view<T,NDims> type;
229 // Assignment from other ConstMultiArray types.
230 template <typename ConstMultiArray>
231 sub_array& operator=(const ConstMultiArray& other) {
232 function_requires< boost::detail::multi_array::ConstMultiArrayConcept<
233 ConstMultiArray, NumDims> >();
235 // make sure the dimensions agree
236 assert(other.num_dimensions() == this->num_dimensions());
237 assert(std::equal(other.shape(),other.shape()+this->num_dimensions(),
239 // iterator-based copy
240 std::copy(other.begin(),other.end(),begin());
245 sub_array& operator=(const sub_array& other) {
246 if (&other != this) {
247 // make sure the dimensions agree
248 assert(other.num_dimensions() == this->num_dimensions());
249 assert(std::equal(other.shape(),other.shape()+this->num_dimensions(),
251 // iterator-based copy
252 std::copy(other.begin(),other.end(),begin());
257 T* origin() { return this->base_; }
258 const T* origin() const { return this->base_; }
260 reference operator[](index idx) {
261 return super_type::access(boost::type<reference>(),
262 idx,this->base_,this->shape(),this->strides(),
263 this->index_bases());
266 // see generate_array_view in base.hpp
267 #if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
270 template <int NumDims, int NDims> // else ICE
272 typename array_view<NDims>::type
273 operator[](const boost::detail::multi_array::
274 index_gen<NumDims,NDims>& indices) {
275 typedef typename array_view<NDims>::type return_type;
277 super_type::generate_array_view(boost::type<return_type>(),
285 template <class IndexList>
286 element& operator()(const IndexList& indices) {
287 return super_type::access_element(boost::type<element&>(),
289 indices,this->strides());
293 return iterator(*this->index_bases(),origin(),
294 this->shape(),this->strides(),this->index_bases());
298 return iterator(*this->index_bases()+(index)*this->shape(),origin(),
299 this->shape(),this->strides(),this->index_bases());
302 // RG - rbegin() and rend() written naively to thwart MSVC ICE.
303 reverse_iterator rbegin() {
304 reverse_iterator ri(end());
308 reverse_iterator rend() {
309 reverse_iterator ri(begin());
317 template <class IndexList>
318 const element& operator()(const IndexList& indices) const {
319 return super_type::operator()(indices);
322 const_reference operator[](index idx) const {
323 return super_type::operator[](idx);
326 // see generate_array_view in base.hpp
327 #if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
330 template <int NumDims, int NDims> // else ICE
332 typename const_array_view<NDims>::type
333 operator[](const boost::detail::multi_array::
334 index_gen<NumDims,NDims>& indices)
336 return super_type::operator[](indices);
339 const_iterator begin() const {
340 return super_type::begin();
343 const_iterator end() const {
344 return super_type::end();
347 const_reverse_iterator rbegin() const {
348 return super_type::rbegin();
351 const_reverse_iterator rend() const {
352 return super_type::rend();
355 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
357 template <typename,std::size_t> friend class value_accessor_n;
359 public: // should be private
363 const size_type* extents,
364 const index* strides,
365 const index* index_base) :
366 super_type(base,extents,strides,index_base) {
371 } // namespace multi_array
372 } // namespace detail
374 // traits classes to get sub_array types
376 template <typename Array, int N>
378 typedef typename Array::element element;
380 typedef boost::detail::multi_array::sub_array<element,N> type;
383 template <typename Array, int N>
384 class const_subarray_gen {
385 typedef typename Array::element element;
387 typedef boost::detail::multi_array::const_sub_array<element,N> type;
391 #endif // SUBARRAY_RG071801_HPP