1 //===- iterator.h - Utilities for using and defining iterators --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_ADT_ITERATOR_H
11 #define LLVM_ADT_ITERATOR_H
15 #include <type_traits>
19 /// \brief CRTP base class which implements the entire standard iterator facade
20 /// in terms of a minimal subset of the interface.
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.
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.
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.
37 /// Classes wishing to use `iterator_facade_base` should implement the following
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;
46 /// - DerivedT &operator++();
48 /// Bidirectional Iterators:
49 /// (All methods of forward iterators, plus the following)
50 /// - DerivedT &operator--();
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);
62 template <typename DerivedT, typename IteratorCategoryT, typename T,
63 typename DifferenceTypeT = std::ptrdiff_t, typename PointerT = T *,
64 typename ReferenceT = T &>
65 class iterator_facade_base
66 : public std::iterator<IteratorCategoryT, T, DifferenceTypeT, PointerT,
71 std::is_base_of<std::random_access_iterator_tag, IteratorCategoryT>::value,
73 std::is_base_of<std::bidirectional_iterator_tag, IteratorCategoryT>::value,
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.
81 class ReferenceProxy {
82 friend iterator_facade_base;
86 ReferenceProxy(DerivedT I) : I(std::move(I)) {}
89 operator ReferenceT() const { return *I; }
93 DerivedT operator+(DifferenceTypeT n) const {
96 "The '+' operator is only defined for random access iterators.");
97 DerivedT tmp = *static_cast<const DerivedT *>(this);
101 friend DerivedT operator+(DifferenceTypeT n, const DerivedT &i) {
104 "The '+' operator is only defined for random access iterators.");
107 DerivedT operator-(DifferenceTypeT n) const {
110 "The '-' operator is only defined for random access iterators.");
111 DerivedT tmp = *static_cast<const DerivedT *>(this);
116 DerivedT &operator++() {
117 return static_cast<DerivedT *>(this)->operator+=(1);
119 DerivedT operator++(int) {
120 DerivedT tmp = *static_cast<DerivedT *>(this);
121 ++*static_cast<DerivedT *>(this);
124 DerivedT &operator--() {
127 "The decrement operator is only defined for bidirectional iterators.");
128 return static_cast<DerivedT *>(this)->operator-=(1);
130 DerivedT operator--(int) {
133 "The decrement operator is only defined for bidirectional iterators.");
134 DerivedT tmp = *static_cast<DerivedT *>(this);
135 --*static_cast<DerivedT *>(this);
139 bool operator!=(const DerivedT &RHS) const {
140 return !static_cast<const DerivedT *>(this)->operator==(RHS);
143 bool operator>(const DerivedT &RHS) const {
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);
150 bool operator<=(const DerivedT &RHS) const {
153 "Relational operators are only defined for random access iterators.");
154 return !static_cast<const DerivedT *>(this)->operator>(RHS);
156 bool operator>=(const DerivedT &RHS) const {
159 "Relational operators are only defined for random access iterators.");
160 return !static_cast<const DerivedT *>(this)->operator<(RHS);
163 PointerT operator->() const {
164 return &static_cast<const DerivedT *>(this)->operator*();
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));
173 /// \brief CRTP base class for adapting an iterator to a different type.
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.
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>>
195 class iterator_adaptor_base
196 : public iterator_facade_base<DerivedT, IteratorCategoryT, T,
197 DifferenceTypeT, PointerT, ReferenceT> {
198 typedef typename iterator_adaptor_base::iterator_facade_base BaseT;
203 iterator_adaptor_base() = default;
205 explicit iterator_adaptor_base(WrappedIteratorT u) : I(std::move(u)) {}
207 const WrappedIteratorT &wrapped() const { return I; }
210 typedef DifferenceTypeT difference_type;
212 DerivedT &operator+=(difference_type n) {
214 BaseT::IsRandomAccess,
215 "The '+=' operator is only defined for random access iterators.");
217 return *static_cast<DerivedT *>(this);
219 DerivedT &operator-=(difference_type n) {
221 BaseT::IsRandomAccess,
222 "The '-=' operator is only defined for random access iterators.");
224 return *static_cast<DerivedT *>(this);
226 using BaseT::operator-;
227 difference_type operator-(const DerivedT &RHS) const {
229 BaseT::IsRandomAccess,
230 "The '-' operator is only defined for random access iterators.");
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++() {
239 return *static_cast<DerivedT *>(this);
241 using BaseT::operator--;
242 DerivedT &operator--() {
244 BaseT::IsBidirectional,
245 "The decrement operator is only defined for bidirectional iterators.");
247 return *static_cast<DerivedT *>(this);
250 bool operator==(const DerivedT &RHS) const { return I == RHS.I; }
251 bool operator<(const DerivedT &RHS) const {
253 BaseT::IsRandomAccess,
254 "Relational operators are only defined for random access iterators.");
258 ReferenceT operator*() const { return *I; }
261 /// \brief An iterator type that allows iterating over the pointees via some
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:
268 /// typedef pointee_iterator<SmallVectorImpl<T *>::iterator> iterator;
270 template <typename WrappedIteratorT,
271 typename T = typename std::remove_reference<
272 decltype(**std::declval<WrappedIteratorT>())>::type>
273 struct pointee_iterator
274 : iterator_adaptor_base<
275 pointee_iterator<WrappedIteratorT>, WrappedIteratorT,
276 typename std::iterator_traits<WrappedIteratorT>::iterator_category,
278 pointee_iterator() = default;
279 template <typename U>
280 pointee_iterator(U &&u)
281 : pointee_iterator::iterator_adaptor_base(std::forward<U &&>(u)) {}
283 T &operator*() const { return **this->I; }
286 template <typename WrappedIteratorT,
287 typename T = decltype(&*std::declval<WrappedIteratorT>())>
288 class pointer_iterator
289 : public iterator_adaptor_base<pointer_iterator<WrappedIteratorT>,
290 WrappedIteratorT, T> {
294 pointer_iterator() = default;
296 explicit pointer_iterator(WrappedIteratorT u)
297 : pointer_iterator::iterator_adaptor_base(std::move(u)) {}
299 T &operator*() { return Ptr = &*this->I; }
300 const T &operator*() const { return Ptr = &*this->I; }
303 } // end namespace llvm
305 #endif // LLVM_ADT_ITERATOR_H