LLVM  4.0.0
iterator.h
Go to the documentation of this file.
1 //===- iterator.h - Utilities for using and defining iterators --*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_ADT_ITERATOR_H
11 #define LLVM_ADT_ITERATOR_H
12 
13 #include <cstddef>
14 #include <iterator>
15 #include <type_traits>
16 
17 namespace llvm {
18 
19 /// \brief CRTP base class which implements the entire standard iterator facade
20 /// in terms of a minimal subset of the interface.
21 ///
22 /// Use this when it is reasonable to implement most of the iterator
23 /// functionality in terms of a core subset. If you need special behavior or
24 /// there are performance implications for this, you may want to override the
25 /// relevant members instead.
26 ///
27 /// Note, one abstraction that this does *not* provide is implementing
28 /// subtraction in terms of addition by negating the difference. Negation isn't
29 /// always information preserving, and I can see very reasonable iterator
30 /// designs where this doesn't work well. It doesn't really force much added
31 /// boilerplate anyways.
32 ///
33 /// Another abstraction that this doesn't provide is implementing increment in
34 /// terms of addition of one. These aren't equivalent for all iterator
35 /// categories, and respecting that adds a lot of complexity for little gain.
36 ///
37 /// Classes wishing to use `iterator_facade_base` should implement the following
38 /// methods:
39 ///
40 /// Forward Iterators:
41 /// (All of the following methods)
42 /// - DerivedT &operator=(const DerivedT &R);
43 /// - bool operator==(const DerivedT &R) const;
44 /// - const T &operator*() const;
45 /// - T &operator*();
46 /// - DerivedT &operator++();
47 ///
48 /// Bidirectional Iterators:
49 /// (All methods of forward iterators, plus the following)
50 /// - DerivedT &operator--();
51 ///
52 /// Random-access Iterators:
53 /// (All methods of bidirectional iterators excluding the following)
54 /// - DerivedT &operator++();
55 /// - DerivedT &operator--();
56 /// (and plus the following)
57 /// - bool operator<(const DerivedT &RHS) const;
58 /// - DifferenceTypeT operator-(const DerivedT &R) const;
59 /// - DerivedT &operator+=(DifferenceTypeT N);
60 /// - DerivedT &operator-=(DifferenceTypeT N);
61 ///
62 template <typename DerivedT, typename IteratorCategoryT, typename T,
63  typename DifferenceTypeT = std::ptrdiff_t, typename PointerT = T *,
64  typename ReferenceT = T &>
66  : public std::iterator<IteratorCategoryT, T, DifferenceTypeT, PointerT,
67  ReferenceT> {
68 protected:
69  enum {
71  std::is_base_of<std::random_access_iterator_tag, IteratorCategoryT>::value,
73  std::is_base_of<std::bidirectional_iterator_tag, IteratorCategoryT>::value,
74  };
75 
76  /// A proxy object for computing a reference via indirecting a copy of an
77  /// iterator. This is used in APIs which need to produce a reference via
78  /// indirection but for which the iterator object might be a temporary. The
79  /// proxy preserves the iterator internally and exposes the indirected
80  /// reference via a conversion operator.
82  friend iterator_facade_base;
83 
84  DerivedT I;
85 
86  ReferenceProxy(DerivedT I) : I(std::move(I)) {}
87 
88  public:
89  operator ReferenceT() const { return *I; }
90  };
91 
92 public:
93  DerivedT operator+(DifferenceTypeT n) const {
94  static_assert(
96  "The '+' operator is only defined for random access iterators.");
97  DerivedT tmp = *static_cast<const DerivedT *>(this);
98  tmp += n;
99  return tmp;
100  }
101  friend DerivedT operator+(DifferenceTypeT n, const DerivedT &i) {
102  static_assert(
104  "The '+' operator is only defined for random access iterators.");
105  return i + n;
106  }
107  DerivedT operator-(DifferenceTypeT n) const {
108  static_assert(
110  "The '-' operator is only defined for random access iterators.");
111  DerivedT tmp = *static_cast<const DerivedT *>(this);
112  tmp -= n;
113  return tmp;
114  }
115 
116  DerivedT &operator++() {
117  return static_cast<DerivedT *>(this)->operator+=(1);
118  }
119  DerivedT operator++(int) {
120  DerivedT tmp = *static_cast<DerivedT *>(this);
121  ++*static_cast<DerivedT *>(this);
122  return tmp;
123  }
124  DerivedT &operator--() {
125  static_assert(
127  "The decrement operator is only defined for bidirectional iterators.");
128  return static_cast<DerivedT *>(this)->operator-=(1);
129  }
130  DerivedT operator--(int) {
131  static_assert(
133  "The decrement operator is only defined for bidirectional iterators.");
134  DerivedT tmp = *static_cast<DerivedT *>(this);
135  --*static_cast<DerivedT *>(this);
136  return tmp;
137  }
138 
139  bool operator!=(const DerivedT &RHS) const {
140  return !static_cast<const DerivedT *>(this)->operator==(RHS);
141  }
142 
143  bool operator>(const DerivedT &RHS) const {
144  static_assert(
146  "Relational operators are only defined for random access iterators.");
147  return !static_cast<const DerivedT *>(this)->operator<(RHS) &&
148  !static_cast<const DerivedT *>(this)->operator==(RHS);
149  }
150  bool operator<=(const DerivedT &RHS) const {
151  static_assert(
153  "Relational operators are only defined for random access iterators.");
154  return !static_cast<const DerivedT *>(this)->operator>(RHS);
155  }
156  bool operator>=(const DerivedT &RHS) const {
157  static_assert(
159  "Relational operators are only defined for random access iterators.");
160  return !static_cast<const DerivedT *>(this)->operator<(RHS);
161  }
162 
163  PointerT operator->() const {
164  return &static_cast<const DerivedT *>(this)->operator*();
165  }
166  ReferenceProxy operator[](DifferenceTypeT n) const {
167  static_assert(IsRandomAccess,
168  "Subscripting is only defined for random access iterators.");
169  return ReferenceProxy(static_cast<const DerivedT *>(this)->operator+(n));
170  }
171 };
172 
173 /// \brief CRTP base class for adapting an iterator to a different type.
174 ///
175 /// This class can be used through CRTP to adapt one iterator into another.
176 /// Typically this is done through providing in the derived class a custom \c
177 /// operator* implementation. Other methods can be overridden as well.
178 template <
179  typename DerivedT, typename WrappedIteratorT,
180  typename IteratorCategoryT =
181  typename std::iterator_traits<WrappedIteratorT>::iterator_category,
182  typename T = typename std::iterator_traits<WrappedIteratorT>::value_type,
183  typename DifferenceTypeT =
184  typename std::iterator_traits<WrappedIteratorT>::difference_type,
185  typename PointerT = typename std::conditional<
186  std::is_same<T, typename std::iterator_traits<
187  WrappedIteratorT>::value_type>::value,
188  typename std::iterator_traits<WrappedIteratorT>::pointer, T *>::type,
189  typename ReferenceT = typename std::conditional<
190  std::is_same<T, typename std::iterator_traits<
191  WrappedIteratorT>::value_type>::value,
192  typename std::iterator_traits<WrappedIteratorT>::reference, T &>::type,
193  // Don't provide these, they are mostly to act as aliases below.
194  typename WrappedTraitsT = std::iterator_traits<WrappedIteratorT>>
196  : public iterator_facade_base<DerivedT, IteratorCategoryT, T,
197  DifferenceTypeT, PointerT, ReferenceT> {
198  typedef typename iterator_adaptor_base::iterator_facade_base BaseT;
199 
200 protected:
201  WrappedIteratorT I;
202 
203  iterator_adaptor_base() = default;
204 
205  explicit iterator_adaptor_base(WrappedIteratorT u) : I(std::move(u)) {}
206 
207  const WrappedIteratorT &wrapped() const { return I; }
208 
209 public:
210  typedef DifferenceTypeT difference_type;
211 
213  static_assert(
214  BaseT::IsRandomAccess,
215  "The '+=' operator is only defined for random access iterators.");
216  I += n;
217  return *static_cast<DerivedT *>(this);
218  }
220  static_assert(
221  BaseT::IsRandomAccess,
222  "The '-=' operator is only defined for random access iterators.");
223  I -= n;
224  return *static_cast<DerivedT *>(this);
225  }
226  using BaseT::operator-;
227  difference_type operator-(const DerivedT &RHS) const {
228  static_assert(
229  BaseT::IsRandomAccess,
230  "The '-' operator is only defined for random access iterators.");
231  return I - RHS.I;
232  }
233 
234  // We have to explicitly provide ++ and -- rather than letting the facade
235  // forward to += because WrappedIteratorT might not support +=.
236  using BaseT::operator++;
237  DerivedT &operator++() {
238  ++I;
239  return *static_cast<DerivedT *>(this);
240  }
241  using BaseT::operator--;
242  DerivedT &operator--() {
243  static_assert(
244  BaseT::IsBidirectional,
245  "The decrement operator is only defined for bidirectional iterators.");
246  --I;
247  return *static_cast<DerivedT *>(this);
248  }
249 
250  bool operator==(const DerivedT &RHS) const { return I == RHS.I; }
251  bool operator<(const DerivedT &RHS) const {
252  static_assert(
253  BaseT::IsRandomAccess,
254  "Relational operators are only defined for random access iterators.");
255  return I < RHS.I;
256  }
257 
258  ReferenceT operator*() const { return *I; }
259 };
260 
261 /// \brief An iterator type that allows iterating over the pointees via some
262 /// other iterator.
263 ///
264 /// The typical usage of this is to expose a type that iterates over Ts, but
265 /// which is implemented with some iterator over T*s:
266 ///
267 /// \code
268 /// typedef pointee_iterator<SmallVectorImpl<T *>::iterator> iterator;
269 /// \endcode
270 template <typename WrappedIteratorT,
271  typename T = typename std::remove_reference<
272  decltype(**std::declval<WrappedIteratorT>())>::type>
275  pointee_iterator<WrappedIteratorT>, WrappedIteratorT,
276  typename std::iterator_traits<WrappedIteratorT>::iterator_category,
277  T> {
278  pointee_iterator() = default;
279  template <typename U>
281  : pointee_iterator::iterator_adaptor_base(std::forward<U &&>(u)) {}
282 
283  T &operator*() const { return **this->I; }
284 };
285 
286 template <typename WrappedIteratorT,
287  typename T = decltype(&*std::declval<WrappedIteratorT>())>
289  : public iterator_adaptor_base<pointer_iterator<WrappedIteratorT>,
290  WrappedIteratorT, T> {
291  mutable T Ptr;
292 
293 public:
294  pointer_iterator() = default;
295 
296  explicit pointer_iterator(WrappedIteratorT u)
297  : pointer_iterator::iterator_adaptor_base(std::move(u)) {}
298 
299  T &operator*() { return Ptr = &*this->I; }
300  const T &operator*() const { return Ptr = &*this->I; }
301 };
302 
303 } // end namespace llvm
304 
305 #endif // LLVM_ADT_ITERATOR_H
WrappedIteratorT I
Definition: iterator.h:201
DifferenceTypeT difference_type
Definition: iterator.h:210
iterator_adaptor_base(WrappedIteratorT u)
Definition: iterator.h:205
difference_type operator-(const DerivedT &RHS) const
Definition: iterator.h:227
size_t i
bool operator>(const DerivedT &RHS) const
Definition: iterator.h:143
bool operator>=(const DerivedT &RHS) const
Definition: iterator.h:156
const T & operator*() const
Definition: iterator.h:300
A proxy object for computing a reference via indirecting a copy of an iterator.
Definition: iterator.h:81
DerivedT operator-(DifferenceTypeT n) const
Definition: iterator.h:107
DerivedT operator+(DifferenceTypeT n) const
Definition: iterator.h:93
DerivedT & operator--()
Definition: iterator.h:124
bool operator==(const DerivedT &RHS) const
Definition: iterator.h:250
bool operator<=(const DerivedT &RHS) const
Definition: iterator.h:150
DerivedT & operator+=(difference_type n)
Definition: iterator.h:212
#define T
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:65
bool operator!=(const DerivedT &RHS) const
Definition: iterator.h:139
ReferenceT operator*() const
Definition: iterator.h:258
T & operator*() const
Definition: iterator.h:283
CRTP base class for adapting an iterator to a different type.
Definition: iterator.h:195
friend DerivedT operator+(DifferenceTypeT n, const DerivedT &i)
Definition: iterator.h:101
DerivedT & operator++()
Definition: iterator.h:116
An iterator type that allows iterating over the pointees via some other iterator. ...
Definition: iterator.h:273
pointer_iterator(WrappedIteratorT u)
Definition: iterator.h:296
const WrappedIteratorT & wrapped() const
Definition: iterator.h:207
DerivedT operator--(int)
Definition: iterator.h:130
DerivedT operator++(int)
Definition: iterator.h:119
DerivedT & operator-=(difference_type n)
Definition: iterator.h:219
PointerT operator->() const
Definition: iterator.h:163
ReferenceProxy operator[](DifferenceTypeT n) const
Definition: iterator.h:166
bool operator<(const DerivedT &RHS) const
Definition: iterator.h:251