WPILibC++  2020.3.2-60-g3011ebe
iterator.h
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 WPIUTIL_WPI_ITERATOR_H
11 #define WPIUTIL_WPI_ITERATOR_H
12 
13 #include "wpi/iterator_range.h"
14 #include <algorithm>
15 #include <cstddef>
16 #include <iterator>
17 #include <type_traits>
18 #include <utility>
19 
20 namespace wpi {
21 
65 template <typename DerivedT, typename IteratorCategoryT, typename T,
66  typename DifferenceTypeT = std::ptrdiff_t, typename PointerT = T *,
67  typename ReferenceT = T &>
69  : public std::iterator<IteratorCategoryT, T, DifferenceTypeT, PointerT,
70  ReferenceT> {
71 protected:
72  enum {
73  IsRandomAccess = std::is_base_of<std::random_access_iterator_tag,
74  IteratorCategoryT>::value,
75  IsBidirectional = std::is_base_of<std::bidirectional_iterator_tag,
76  IteratorCategoryT>::value,
77  };
78 
85  friend iterator_facade_base;
86 
87  DerivedT I;
88 
89  ReferenceProxy(DerivedT I) : I(std::move(I)) {}
90 
91  public:
92  operator ReferenceT() const { return *I; }
93  };
94 
95 public:
96  DerivedT operator+(DifferenceTypeT n) const {
97  static_assert(std::is_base_of<iterator_facade_base, DerivedT>::value,
98  "Must pass the derived type to this template!");
99  static_assert(
100  IsRandomAccess,
101  "The '+' operator is only defined for random access iterators.");
102  DerivedT tmp = *static_cast<const DerivedT *>(this);
103  tmp += n;
104  return tmp;
105  }
106  friend DerivedT operator+(DifferenceTypeT n, const DerivedT &i) {
107  static_assert(
108  IsRandomAccess,
109  "The '+' operator is only defined for random access iterators.");
110  return i + n;
111  }
112  DerivedT operator-(DifferenceTypeT n) const {
113  static_assert(
114  IsRandomAccess,
115  "The '-' operator is only defined for random access iterators.");
116  DerivedT tmp = *static_cast<const DerivedT *>(this);
117  tmp -= n;
118  return tmp;
119  }
120 
121  DerivedT &operator++() {
122  static_assert(std::is_base_of<iterator_facade_base, DerivedT>::value,
123  "Must pass the derived type to this template!");
124  return static_cast<DerivedT *>(this)->operator+=(1);
125  }
126  DerivedT operator++(int) {
127  DerivedT tmp = *static_cast<DerivedT *>(this);
128  ++*static_cast<DerivedT *>(this);
129  return tmp;
130  }
131  DerivedT &operator--() {
132  static_assert(
133  IsBidirectional,
134  "The decrement operator is only defined for bidirectional iterators.");
135  return static_cast<DerivedT *>(this)->operator-=(1);
136  }
137  DerivedT operator--(int) {
138  static_assert(
139  IsBidirectional,
140  "The decrement operator is only defined for bidirectional iterators.");
141  DerivedT tmp = *static_cast<DerivedT *>(this);
142  --*static_cast<DerivedT *>(this);
143  return tmp;
144  }
145 
146  bool operator!=(const DerivedT &RHS) const {
147  return !static_cast<const DerivedT *>(this)->operator==(RHS);
148  }
149 
150  bool operator>(const DerivedT &RHS) const {
151  static_assert(
152  IsRandomAccess,
153  "Relational operators are only defined for random access iterators.");
154  return !static_cast<const DerivedT *>(this)->operator<(RHS) &&
155  !static_cast<const DerivedT *>(this)->operator==(RHS);
156  }
157  bool operator<=(const DerivedT &RHS) const {
158  static_assert(
159  IsRandomAccess,
160  "Relational operators are only defined for random access iterators.");
161  return !static_cast<const DerivedT *>(this)->operator>(RHS);
162  }
163  bool operator>=(const DerivedT &RHS) const {
164  static_assert(
165  IsRandomAccess,
166  "Relational operators are only defined for random access iterators.");
167  return !static_cast<const DerivedT *>(this)->operator<(RHS);
168  }
169 
170  PointerT operator->() { return &static_cast<DerivedT *>(this)->operator*(); }
171  PointerT operator->() const {
172  return &static_cast<const DerivedT *>(this)->operator*();
173  }
174  ReferenceProxy operator[](DifferenceTypeT n) {
175  static_assert(IsRandomAccess,
176  "Subscripting is only defined for random access iterators.");
177  return ReferenceProxy(static_cast<DerivedT *>(this)->operator+(n));
178  }
179  ReferenceProxy operator[](DifferenceTypeT n) const {
180  static_assert(IsRandomAccess,
181  "Subscripting is only defined for random access iterators.");
182  return ReferenceProxy(static_cast<const DerivedT *>(this)->operator+(n));
183  }
184 };
185 
191 template <
192  typename DerivedT, typename WrappedIteratorT,
193  typename IteratorCategoryT =
194  typename std::iterator_traits<WrappedIteratorT>::iterator_category,
195  typename T = typename std::iterator_traits<WrappedIteratorT>::value_type,
196  typename DifferenceTypeT =
197  typename std::iterator_traits<WrappedIteratorT>::difference_type,
198  typename PointerT = typename std::conditional<
199  std::is_same<T, typename std::iterator_traits<
200  WrappedIteratorT>::value_type>::value,
201  typename std::iterator_traits<WrappedIteratorT>::pointer, T *>::type,
202  typename ReferenceT = typename std::conditional<
203  std::is_same<T, typename std::iterator_traits<
204  WrappedIteratorT>::value_type>::value,
205  typename std::iterator_traits<WrappedIteratorT>::reference, T &>::type>
207  : public iterator_facade_base<DerivedT, IteratorCategoryT, T,
208  DifferenceTypeT, PointerT, ReferenceT> {
209  using BaseT = typename iterator_adaptor_base::iterator_facade_base;
210 
211 protected:
212  WrappedIteratorT I;
213 
214  iterator_adaptor_base() = default;
215 
216  explicit iterator_adaptor_base(WrappedIteratorT u) : I(std::move(u)) {
217  static_assert(std::is_base_of<iterator_adaptor_base, DerivedT>::value,
218  "Must pass the derived type to this template!");
219  }
220 
221  const WrappedIteratorT &wrapped() const { return I; }
222 
223 public:
224  using difference_type = DifferenceTypeT;
225 
226  DerivedT &operator+=(difference_type n) {
227  static_assert(
228  BaseT::IsRandomAccess,
229  "The '+=' operator is only defined for random access iterators.");
230  I += n;
231  return *static_cast<DerivedT *>(this);
232  }
233  DerivedT &operator-=(difference_type n) {
234  static_assert(
235  BaseT::IsRandomAccess,
236  "The '-=' operator is only defined for random access iterators.");
237  I -= n;
238  return *static_cast<DerivedT *>(this);
239  }
240  using BaseT::operator-;
241  difference_type operator-(const DerivedT &RHS) const {
242  static_assert(
243  BaseT::IsRandomAccess,
244  "The '-' operator is only defined for random access iterators.");
245  return I - RHS.I;
246  }
247 
248  // We have to explicitly provide ++ and -- rather than letting the facade
249  // forward to += because WrappedIteratorT might not support +=.
250  using BaseT::operator++;
251  DerivedT &operator++() {
252  ++I;
253  return *static_cast<DerivedT *>(this);
254  }
255  using BaseT::operator--;
256  DerivedT &operator--() {
257  static_assert(
258  BaseT::IsBidirectional,
259  "The decrement operator is only defined for bidirectional iterators.");
260  --I;
261  return *static_cast<DerivedT *>(this);
262  }
263 
264  bool operator==(const DerivedT &RHS) const { return I == RHS.I; }
265  bool operator<(const DerivedT &RHS) const {
266  static_assert(
267  BaseT::IsRandomAccess,
268  "Relational operators are only defined for random access iterators.");
269  return I < RHS.I;
270  }
271 
272  ReferenceT operator*() const { return *I; }
273 };
274 
284 template <typename WrappedIteratorT,
285  typename T = typename std::remove_reference<
286  decltype(**std::declval<WrappedIteratorT>())>::type>
289  pointee_iterator<WrappedIteratorT, T>, WrappedIteratorT,
290  typename std::iterator_traits<WrappedIteratorT>::iterator_category,
291  T> {
292  pointee_iterator() = default;
293  template <typename U>
294  pointee_iterator(U &&u)
295  : pointee_iterator::iterator_adaptor_base(std::forward<U &&>(u)) {}
296 
297  T &operator*() const { return **this->I; }
298 };
299 
300 template <typename RangeT, typename WrappedIteratorT =
301  decltype(std::begin(std::declval<RangeT>()))>
303 make_pointee_range(RangeT &&Range) {
304  using PointeeIteratorT = pointee_iterator<WrappedIteratorT>;
305  return make_range(PointeeIteratorT(std::begin(std::forward<RangeT>(Range))),
306  PointeeIteratorT(std::end(std::forward<RangeT>(Range))));
307 }
308 
309 template <typename WrappedIteratorT,
310  typename T = decltype(&*std::declval<WrappedIteratorT>())>
312  : public iterator_adaptor_base<
313  pointer_iterator<WrappedIteratorT, T>, WrappedIteratorT,
314  typename std::iterator_traits<WrappedIteratorT>::iterator_category,
315  T> {
316  mutable T Ptr;
317 
318 public:
319  pointer_iterator() = default;
320 
321  explicit pointer_iterator(WrappedIteratorT u)
322  : pointer_iterator::iterator_adaptor_base(std::move(u)) {}
323 
324  T &operator*() { return Ptr = &*this->I; }
325  const T &operator*() const { return Ptr = &*this->I; }
326 };
327 
328 template <typename RangeT, typename WrappedIteratorT =
329  decltype(std::begin(std::declval<RangeT>()))>
331 make_pointer_range(RangeT &&Range) {
332  using PointerIteratorT = pointer_iterator<WrappedIteratorT>;
333  return make_range(PointerIteratorT(std::begin(std::forward<RangeT>(Range))),
334  PointerIteratorT(std::end(std::forward<RangeT>(Range))));
335 }
336 
337 } // end namespace wpi
338 
339 #endif // LLVM_ADT_ITERATOR_H
wpi::iterator_adaptor_base
CRTP base class for adapting an iterator to a different type.
Definition: iterator.h:206
wpi::pointer_iterator
Definition: iterator.h:311
wpi::iterator_range
A range adaptor for a pair of iterators.
Definition: iterator_range.h:32
wpi
WPILib C++ utilities (wpiutil) namespace.
Definition: Endian.h:31
wpi::make_range
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Definition: iterator_range.h:54
iterator_range.h
wpi::iterator_facade_base
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:68
wpi::pointee_iterator
An iterator type that allows iterating over the pointees via some other iterator.
Definition: iterator.h:287
wpi::iterator_facade_base::ReferenceProxy
A proxy object for computing a reference via indirecting a copy of an iterator.
Definition: iterator.h:84