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 BOOST_MULTI_ARRAY_ITERATOR_HPP
14 #define BOOST_MULTI_ARRAY_ITERATOR_HPP
17 // iterator.hpp - implementation of iterators for the
18 // multi-dimensional array class
21 #include "boost/multi_array/base.hpp"
22 #include "boost/iterator/iterator_facade.hpp"
29 namespace multi_array {
31 /////////////////////////////////////////////////////////////////////////
32 // iterator components
33 /////////////////////////////////////////////////////////////////////////
36 struct operator_arrow_proxy
38 operator_arrow_proxy(T const& px) : value_(px) {}
39 T* operator->() const { return &value_; }
40 // This function is needed for MWCW and BCC, which won't call operator->
41 // again automatically per 13.3.1.2 para 8
42 operator T*() const { return &value_; }
46 template <typename T, typename TPtr, typename NumDims, typename Reference,
47 typename IteratorCategory>
50 template <typename T, typename TPtr, typename NumDims, typename Reference,
51 typename IteratorCategory>
55 array_iterator<T,TPtr,NumDims,Reference,IteratorCategory>
56 , typename associated_types<T,NumDims>::value_type
61 value_accessor_generator<T,NumDims>::type
63 friend class ::boost::iterator_core_access;
64 typedef detail::multi_array::associated_types<T,NumDims> access_t;
66 typedef iterator_facade<
67 array_iterator<T,TPtr,NumDims,Reference,IteratorCategory>
68 , typename detail::multi_array::associated_types<T,NumDims>::value_type
69 , boost::random_access_traversal_tag
73 typedef typename access_t::index index;
74 typedef typename access_t::size_type size_type;
76 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
77 template <typename, typename, typename, typename, typename>
78 friend class array_iterator;
85 const size_type* extents_;
86 const index* strides_;
87 const index* index_base_;
90 // Typedefs to circumvent ambiguities between parent classes
91 typedef typename facade_type::reference reference;
92 typedef typename facade_type::value_type value_type;
93 typedef typename facade_type::difference_type difference_type;
97 array_iterator(index idx, TPtr base, const size_type* extents,
99 const index* index_base) :
100 idx_(idx), base_(base), extents_(extents),
101 strides_(strides), index_base_(index_base) { }
103 template <typename OPtr, typename ORef, typename Cat>
105 const array_iterator<T,OPtr,NumDims,ORef,Cat>& rhs
106 , typename boost::enable_if_convertible<OPtr,TPtr>::type* = 0
108 : idx_(rhs.idx_), base_(rhs.base_), extents_(rhs.extents_),
109 strides_(rhs.strides_), index_base_(rhs.index_base_) { }
112 // RG - we make our own operator->
113 operator_arrow_proxy<reference>
116 return operator_arrow_proxy<reference>(this->dereference());
120 reference dereference() const
122 typedef typename value_accessor_generator<T,NumDims>::type accessor;
123 return accessor::access(boost::type<reference>(),
131 void increment() { ++idx_; }
132 void decrement() { --idx_; }
134 template <class IteratorAdaptor>
135 bool equal(IteratorAdaptor& rhs) const {
136 const std::size_t N = NumDims::value;
137 return (idx_ == rhs.idx_) &&
138 (base_ == rhs.base_) &&
139 ( (extents_ == rhs.extents_) ||
140 std::equal(extents_,extents_+N,rhs.extents_) ) &&
141 ( (strides_ == rhs.strides_) ||
142 std::equal(strides_,strides_+N,rhs.strides_) ) &&
143 ( (index_base_ == rhs.index_base_) ||
144 std::equal(index_base_,index_base_+N,rhs.index_base_) );
147 template <class DifferenceType>
148 void advance(DifferenceType n) {
152 template <class IteratorAdaptor>
153 typename facade_type::difference_type
154 distance_to(IteratorAdaptor& rhs) const {
155 return rhs.idx_ - idx_;
161 } // namespace multi_array
162 } // namespace detail