Ticket #6554: iterator.hpp

File iterator.hpp, 5.2 KB (added by Bill Buklis, 11 years ago)

Merge with trunk version

Line 
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 "boost/mpl/aux_/msvc_eti_base.hpp"
24#include "boost/type_traits/remove_reference.hpp"
25#include <algorithm>
26#include <cstddef>
27#include <iterator>
28
29namespace boost {
30namespace detail {
31namespace multi_array {
32
33/////////////////////////////////////////////////////////////////////////
34// iterator components
35/////////////////////////////////////////////////////////////////////////
36
37template <class T>
38struct operator_arrow_proxy
39{
40 typedef typename remove_reference<T>::type* pointer;
41
42 operator_arrow_proxy(T const& px) : value_(px) {}
43 pointer operator->() const { return &value_; }
44 // This function is needed for MWCW and BCC, which won't call operator->
45 // again automatically per 13.3.1.2 para 8
46 operator pointer() const { return &value_; }
47 mutable T value_;
48};
49
50template <typename T, typename TPtr, typename NumDims, typename Reference,
51 typename IteratorCategory>
52class array_iterator;
53
54template <typename T, typename TPtr, typename NumDims, typename Reference,
55 typename IteratorCategory>
56class array_iterator
57 : public
58 iterator_facade<
59 array_iterator<T,TPtr,NumDims,Reference,IteratorCategory>
60 , typename associated_types<T,NumDims>::value_type
61 , IteratorCategory
62 , Reference
63 >
64 , private
65#if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
66 mpl::aux::msvc_eti_base<typename
67#endif
68 value_accessor_generator<T,NumDims>::type
69#if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
70 >::type
71#endif
72{
73 friend class iterator_core_access;
74 typedef detail::multi_array::associated_types<T,NumDims> access_t;
75
76 typedef iterator_facade<
77 array_iterator<T,TPtr,NumDims,Reference,IteratorCategory>
78 , typename detail::multi_array::associated_types<T,NumDims>::value_type
79 , boost::random_access_traversal_tag
80 , Reference
81 > facade_type;
82
83 typedef typename access_t::index index;
84 typedef typename access_t::size_type size_type;
85
86#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
87 template <typename, typename, typename, typename, typename>
88 friend class array_iterator;
89#else
90 public:
91#endif
92
93 index idx_;
94 TPtr base_;
95 const size_type* extents_;
96 const index* strides_;
97 const index* index_base_;
98
99public:
100 // Typedefs to circumvent ambiguities between parent classes
101 typedef typename facade_type::reference reference;
102 typedef typename facade_type::value_type value_type;
103 typedef typename facade_type::difference_type difference_type;
104
105 array_iterator() {}
106
107 array_iterator(index idx, TPtr base, const size_type* extents,
108 const index* strides,
109 const index* index_base) :
110 idx_(idx), base_(base), extents_(extents),
111 strides_(strides), index_base_(index_base) { }
112
113 template <typename OPtr, typename ORef, typename Cat>
114 array_iterator(
115 const array_iterator<T,OPtr,NumDims,ORef,Cat>& rhs
116 , typename boost::enable_if_convertible<OPtr,TPtr>::type* = 0
117 )
118 : idx_(rhs.idx_), base_(rhs.base_), extents_(rhs.extents_),
119 strides_(rhs.strides_), index_base_(rhs.index_base_) { }
120
121
122 // RG - we make our own operator->
123 operator_arrow_proxy<reference>
124 operator->() const
125 {
126 return operator_arrow_proxy<reference>(this->dereference());
127 }
128
129
130 reference dereference() const
131 {
132 typedef typename value_accessor_generator<T,NumDims>::type accessor;
133 return accessor::access(boost::type<reference>(),
134 idx_,
135 base_,
136 extents_,
137 strides_,
138 index_base_);
139 }
140
141 void increment() { ++idx_; }
142 void decrement() { --idx_; }
143
144 template <class IteratorAdaptor>
145 bool equal(IteratorAdaptor& rhs) const {
146 const std::size_t N = NumDims::value;
147 return (idx_ == rhs.idx_) &&
148 (base_ == rhs.base_) &&
149 ( (extents_ == rhs.extents_) ||
150 std::equal(extents_,extents_+N,rhs.extents_) ) &&
151 ( (strides_ == rhs.strides_) ||
152 std::equal(strides_,strides_+N,rhs.strides_) ) &&
153 ( (index_base_ == rhs.index_base_) ||
154 std::equal(index_base_,index_base_+N,rhs.index_base_) );
155 }
156
157 template <class DifferenceType>
158 void advance(DifferenceType n) {
159 idx_ += n;
160 }
161
162 template <class IteratorAdaptor>
163 typename facade_type::difference_type
164 distance_to(IteratorAdaptor& rhs) const {
165 return rhs.idx_ - idx_;
166 }
167
168
169};
170
171} // namespace multi_array
172} // namespace detail
173} // namespace boost
174
175#endif // ITERATOR_RG071801_HPP