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
18 /// \brief CRTP base class which implements the entire standard iterator facade
19 /// in terms of a minimal subset of the interface.
21 /// Use this when it is reasonable to implement most of the iterator
22 /// functionality in terms of a core subset. If you need special behavior or
23 /// there are performance implications for this, you may want to override the
24 /// relevant members instead.
26 /// Note, one abstraction that this does *not* provide is implementing
27 /// subtraction in terms of addition by negating the difference. Negation isn't
28 /// always information preserving, and I can see very reasonable iterator
29 /// designs where this doesn't work well. It doesn't really force much added
30 /// boilerplate anyways.
32 /// Another abstraction that this doesn't provide is implementing increment in
33 /// terms of addition of one. These aren't equivalent for all iterator
34 /// categories, and respecting that adds a lot of complexity for little gain.
35 template <typename DerivedT, typename IteratorCategoryT, typename T,
36 typename DifferenceTypeT = std::ptrdiff_t, typename PointerT = T *,
37 typename ReferenceT = T &>
38 class iterator_facade_base
39 : public std::iterator<IteratorCategoryT, T, DifferenceTypeT, PointerT,
44 std::is_base_of<std::random_access_iterator_tag, IteratorCategoryT>::value,
46 std::is_base_of<std::bidirectional_iterator_tag, IteratorCategoryT>::value,
49 /// A proxy object for computing a reference via indirecting a copy of an
50 /// iterator. This is used in APIs which need to produce a reference via
51 /// indirection but for which the iterator object might be a temporary. The
52 /// proxy preserves the iterator internally and exposes the indirected
53 /// reference via a conversion operator.
54 class ReferenceProxy {
55 friend iterator_facade_base;
59 ReferenceProxy(DerivedT I) : I(std::move(I)) {}
62 operator ReferenceT() const { return *I; }
66 DerivedT operator+(DifferenceTypeT n) const {
69 "The '+' operator is only defined for random access iterators.");
70 DerivedT tmp = *static_cast<const DerivedT *>(this);
74 friend DerivedT operator+(DifferenceTypeT n, const DerivedT &i) {
77 "The '+' operator is only defined for random access iterators.");
80 DerivedT operator-(DifferenceTypeT n) const {
83 "The '-' operator is only defined for random access iterators.");
84 DerivedT tmp = *static_cast<const DerivedT *>(this);
89 DerivedT &operator++() {
90 return static_cast<DerivedT *>(this)->operator+=(1);
92 DerivedT operator++(int) {
93 DerivedT tmp = *static_cast<DerivedT *>(this);
94 ++*static_cast<DerivedT *>(this);
97 DerivedT &operator--() {
100 "The decrement operator is only defined for bidirectional iterators.");
101 return static_cast<DerivedT *>(this)->operator-=(1);
103 DerivedT operator--(int) {
106 "The decrement operator is only defined for bidirectional iterators.");
107 DerivedT tmp = *static_cast<DerivedT *>(this);
108 --*static_cast<DerivedT *>(this);
112 bool operator!=(const DerivedT &RHS) const {
113 return !static_cast<const DerivedT *>(this)->operator==(RHS);
116 bool operator>(const DerivedT &RHS) const {
119 "Relational operators are only defined for random access iterators.");
120 return !static_cast<const DerivedT *>(this)->operator<(RHS) &&
121 !static_cast<const DerivedT *>(this)->operator==(RHS);
123 bool operator<=(const DerivedT &RHS) const {
126 "Relational operators are only defined for random access iterators.");
127 return !static_cast<const DerivedT *>(this)->operator>(RHS);
129 bool operator>=(const DerivedT &RHS) const {
132 "Relational operators are only defined for random access iterators.");
133 return !static_cast<const DerivedT *>(this)->operator<(RHS);
136 PointerT operator->() const {
137 return &static_cast<const DerivedT *>(this)->operator*();
139 ReferenceProxy operator[](DifferenceTypeT n) const {
140 static_assert(IsRandomAccess,
141 "Subscripting is only defined for random access iterators.");
142 return ReferenceProxy(static_cast<const DerivedT *>(this)->operator+(n));
146 /// \brief CRTP base class for adapting an iterator to a different type.
148 /// This class can be used through CRTP to adapt one iterator into another.
149 /// Typically this is done through providing in the derived class a custom \c
150 /// operator* implementation. Other methods can be overridden as well.
152 typename DerivedT, typename WrappedIteratorT,
153 typename IteratorCategoryT =
154 typename std::iterator_traits<WrappedIteratorT>::iterator_category,
155 typename T = typename std::iterator_traits<WrappedIteratorT>::value_type,
156 typename DifferenceTypeT =
157 typename std::iterator_traits<WrappedIteratorT>::difference_type,
158 typename PointerT = typename std::conditional<
159 std::is_same<T, typename std::iterator_traits<
160 WrappedIteratorT>::value_type>::value,
161 typename std::iterator_traits<WrappedIteratorT>::pointer, T *>::type,
162 typename ReferenceT = typename std::conditional<
163 std::is_same<T, typename std::iterator_traits<
164 WrappedIteratorT>::value_type>::value,
165 typename std::iterator_traits<WrappedIteratorT>::reference, T &>::type,
166 // Don't provide these, they are mostly to act as aliases below.
167 typename WrappedTraitsT = std::iterator_traits<WrappedIteratorT>>
168 class iterator_adaptor_base
169 : public iterator_facade_base<DerivedT, IteratorCategoryT, T,
170 DifferenceTypeT, PointerT, ReferenceT> {
171 typedef typename iterator_adaptor_base::iterator_facade_base BaseT;
176 iterator_adaptor_base() = default;
178 explicit iterator_adaptor_base(WrappedIteratorT u) : I(std::move(u)) {}
180 const WrappedIteratorT &wrapped() const { return I; }
183 typedef DifferenceTypeT difference_type;
185 DerivedT &operator+=(difference_type n) {
187 BaseT::IsRandomAccess,
188 "The '+=' operator is only defined for random access iterators.");
190 return *static_cast<DerivedT *>(this);
192 DerivedT &operator-=(difference_type n) {
194 BaseT::IsRandomAccess,
195 "The '-=' operator is only defined for random access iterators.");
197 return *static_cast<DerivedT *>(this);
199 using BaseT::operator-;
200 difference_type operator-(const DerivedT &RHS) const {
202 BaseT::IsRandomAccess,
203 "The '-' operator is only defined for random access iterators.");
207 // We have to explicitly provide ++ and -- rather than letting the facade
208 // forward to += because WrappedIteratorT might not support +=.
209 using BaseT::operator++;
210 DerivedT &operator++() {
212 return *static_cast<DerivedT *>(this);
214 using BaseT::operator--;
215 DerivedT &operator--() {
217 BaseT::IsBidirectional,
218 "The decrement operator is only defined for bidirectional iterators.");
220 return *static_cast<DerivedT *>(this);
223 bool operator==(const DerivedT &RHS) const { return I == RHS.I; }
224 bool operator<(const DerivedT &RHS) const {
226 BaseT::IsRandomAccess,
227 "Relational operators are only defined for random access iterators.");
231 ReferenceT operator*() const { return *I; }
234 /// \brief An iterator type that allows iterating over the pointees via some
237 /// The typical usage of this is to expose a type that iterates over Ts, but
238 /// which is implemented with some iterator over T*s:
241 /// typedef pointee_iterator<SmallVectorImpl<T *>::iterator> iterator;
243 template <typename WrappedIteratorT,
244 typename T = typename std::remove_reference<
245 decltype(**std::declval<WrappedIteratorT>())>::type>
246 struct pointee_iterator
247 : iterator_adaptor_base<
248 pointee_iterator<WrappedIteratorT>, WrappedIteratorT,
249 typename std::iterator_traits<WrappedIteratorT>::iterator_category,
251 pointee_iterator() = default;
252 template <typename U>
253 pointee_iterator(U &&u)
254 : pointee_iterator::iterator_adaptor_base(std::forward<U &&>(u)) {}
256 T &operator*() const { return **this->I; }