1// Copyright 2002 The Trustees of Indiana University.
2
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)
6
7// Boost.MultiArray Library
8// Authors: Ronald Garcia
9// Jeremy Siek
10// Andrew Lumsdaine
11// See http://www.boost.org/libs/multi_array for documentation.
12
13#ifndef ITERATOR_RG071801_HPP
14#define ITERATOR_RG071801_HPP
15
16//
17// iterator.hpp - implementation of iterators for the
18// multi-dimensional array class
19//
20
21#include "boost/multi_array/base.hpp"
22#include "boost/iterator/iterator_facade.hpp"
23#include <algorithm>
24#include <cstddef>
25#include <iterator>
26
27namespace boost {
28namespace detail {
29namespace multi_array {
30
31/////////////////////////////////////////////////////////////////////////
32// iterator components
33/////////////////////////////////////////////////////////////////////////
34
35template <class T>
36struct operator_arrow_proxy
37{
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_; }
43 mutable T value_;
44};
45
46template <typename T, typename TPtr, typename NumDims, typename Reference,
47 typename IteratorCategory>
48class array_iterator;
49
50template <typename T, typename TPtr, typename NumDims, typename Reference,
51 typename IteratorCategory>
52class array_iterator
53 : public
54 iterator_facade<
55 array_iterator<T,TPtr,NumDims,Reference,IteratorCategory>
56 , typename associated_types<T,NumDims>::value_type
57 , IteratorCategory
58 , Reference
59 >
60 , private
61 value_accessor_generator<T,NumDims>::type
62{
63 friend class ::boost::iterator_core_access;
64 typedef detail::multi_array::associated_types<T,NumDims> access_t;
65
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
70 , Reference
71 > facade_type;
72
73 typedef typename access_t::index index;
74 typedef typename access_t::size_type size_type;
75
76#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
77 template <typename, typename, typename, typename, typename>
78 friend class array_iterator;
79#else
80 public:
81#endif
82
83 index idx_;
84 TPtr base_;
85 const size_type* extents_;
86 const index* strides_;
87 const index* index_base_;
88
89public:
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;
94
95 array_iterator() {}
96
97 array_iterator(index idx, TPtr base, const size_type* extents,
98 const index* strides,
99 const index* index_base) :
100 idx_(idx), base_(base), extents_(extents),
101 strides_(strides), index_base_(index_base) { }
102
103 template <typename OPtr, typename ORef, typename Cat>
104 array_iterator(
105 const array_iterator<T,OPtr,NumDims,ORef,Cat>& rhs
106 , typename boost::enable_if_convertible<OPtr,TPtr>::type* = 0
107 )
108 : idx_(rhs.idx_), base_(rhs.base_), extents_(rhs.extents_),
109 strides_(rhs.strides_), index_base_(rhs.index_base_) { }
110
111
112 // RG - we make our own operator->
113 operator_arrow_proxy<reference>
114 operator->() const
115 {
116 return operator_arrow_proxy<reference>(this->dereference());
117 }
118
119
120 reference dereference() const
121 {
122 typedef typename value_accessor_generator<T,NumDims>::type accessor;
123 return accessor::access(boost::type<reference>(),
124 idx_,
125 base_,
126 extents_,
127 strides_,
128 index_base_);
129 }
130
131 void increment() { ++idx_; }
132 void decrement() { --idx_; }
133
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_) );
145 }
146
147 template <class DifferenceType>
148 void advance(DifferenceType n) {
149 idx_ += n;
150 }
151
152 template <class IteratorAdaptor>
153 typename facade_type::difference_type
154 distance_to(IteratorAdaptor& rhs) const {
155 return rhs.idx_ - idx_;
156 }
157
158
159};
160
161} // namespace multi_array
162} // namespace detail
163} // namespace boost
164
165#endif // ITERATOR_RG071801_HPP
166