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,
50 DerivedT operator+(DifferenceTypeT n) const {
53 "The '+' operator is only defined for random access iterators.");
54 DerivedT tmp = *static_cast<const DerivedT *>(this);
58 friend DerivedT operator+(DifferenceTypeT n, const DerivedT &i) {
61 "The '+' operator is only defined for random access iterators.");
64 DerivedT operator-(DifferenceTypeT n) const {
67 "The '-' operator is only defined for random access iterators.");
68 DerivedT tmp = *static_cast<const DerivedT *>(this);
73 DerivedT &operator++() {
74 return static_cast<DerivedT *>(this)->operator+=(1);
76 DerivedT operator++(int) {
77 DerivedT tmp = *static_cast<DerivedT *>(this);
78 ++*static_cast<DerivedT *>(this);
81 DerivedT &operator--() {
84 "The decrement operator is only defined for bidirectional iterators.");
85 return static_cast<DerivedT *>(this)->operator-=(1);
87 DerivedT operator--(int) {
90 "The decrement operator is only defined for bidirectional iterators.");
91 DerivedT tmp = *static_cast<DerivedT *>(this);
92 --*static_cast<DerivedT *>(this);
96 bool operator!=(const DerivedT &RHS) const {
97 return !static_cast<const DerivedT *>(this)->operator==(RHS);
100 bool operator>(const DerivedT &RHS) const {
103 "Relational operators are only defined for random access iterators.");
104 return !static_cast<const DerivedT *>(this)->operator<(RHS) &&
105 !static_cast<const DerivedT *>(this)->operator==(RHS);
107 bool operator<=(const DerivedT &RHS) const {
110 "Relational operators are only defined for random access iterators.");
111 return !static_cast<const DerivedT *>(this)->operator>(RHS);
113 bool operator>=(const DerivedT &RHS) const {
116 "Relational operators are only defined for random access iterators.");
117 return !static_cast<const DerivedT *>(this)->operator<(RHS);
120 PointerT operator->() const {
121 return &static_cast<const DerivedT *>(this)->operator*();
123 ReferenceT operator[](DifferenceTypeT n) const {
124 static_assert(IsRandomAccess,
125 "Subscripting is only defined for random access iterators.");
126 return *static_cast<const DerivedT *>(this)->operator+(n);
130 /// \brief CRTP base class for adapting an iterator to a different type.
132 /// This class can be used through CRTP to adapt one iterator into another.
133 /// Typically this is done through providing in the derived class a custom \c
134 /// operator* implementation. Other methods can be overridden as well.
136 typename DerivedT, typename WrappedIteratorT,
137 typename IteratorCategoryT =
138 typename std::iterator_traits<WrappedIteratorT>::iterator_category,
139 typename T = typename std::iterator_traits<WrappedIteratorT>::value_type,
140 typename DifferenceTypeT =
141 typename std::iterator_traits<WrappedIteratorT>::difference_type,
142 typename PointerT = T *, typename ReferenceT = T &,
143 // Don't provide these, they are mostly to act as aliases below.
144 typename WrappedTraitsT = std::iterator_traits<WrappedIteratorT>>
145 class iterator_adaptor_base
146 : public iterator_facade_base<DerivedT, IteratorCategoryT, T,
147 DifferenceTypeT, PointerT, ReferenceT> {
148 typedef typename iterator_adaptor_base::iterator_facade_base BaseT;
153 iterator_adaptor_base() = default;
155 template <typename U>
156 explicit iterator_adaptor_base(
158 typename std::enable_if<
159 !std::is_base_of<typename std::remove_cv<
160 typename std::remove_reference<U>::type>::type,
163 : I(std::forward<U &&>(u)) {}
165 const WrappedIteratorT &wrapped() const { return I; }
168 typedef DifferenceTypeT difference_type;
170 DerivedT &operator+=(difference_type n) {
172 BaseT::IsRandomAccess,
173 "The '+=' operator is only defined for random access iterators.");
175 return *static_cast<DerivedT *>(this);
177 DerivedT &operator-=(difference_type n) {
179 BaseT::IsRandomAccess,
180 "The '-=' operator is only defined for random access iterators.");
182 return *static_cast<DerivedT *>(this);
184 using BaseT::operator-;
185 difference_type operator-(const DerivedT &RHS) const {
187 BaseT::IsRandomAccess,
188 "The '-' operator is only defined for random access iterators.");
192 // We have to explicitly provide ++ and -- rather than letting the facade
193 // forward to += because WrappedIteratorT might not support +=.
194 using BaseT::operator++;
195 DerivedT &operator++() {
197 return *static_cast<DerivedT *>(this);
199 using BaseT::operator--;
200 DerivedT &operator--() {
202 BaseT::IsBidirectional,
203 "The decrement operator is only defined for bidirectional iterators.");
205 return *static_cast<DerivedT *>(this);
208 bool operator==(const DerivedT &RHS) const { return I == RHS.I; }
209 bool operator<(const DerivedT &RHS) const {
211 BaseT::IsRandomAccess,
212 "Relational operators are only defined for random access iterators.");
216 ReferenceT operator*() const { return *I; }
219 /// \brief An iterator type that allows iterating over the pointees via some
222 /// The typical usage of this is to expose a type that iterates over Ts, but
223 /// which is implemented with some iterator over T*s:
226 /// typedef pointee_iterator<SmallVectorImpl<T *>::iterator> iterator;
228 template <typename WrappedIteratorT,
229 typename T = typename std::remove_reference<
230 decltype(**std::declval<WrappedIteratorT>())>::type>
231 struct pointee_iterator
232 : iterator_adaptor_base<
233 pointee_iterator<WrappedIteratorT>, WrappedIteratorT,
234 typename std::iterator_traits<WrappedIteratorT>::iterator_category,
236 pointee_iterator() = default;
237 template <typename U>
238 pointee_iterator(U &&u)
239 : pointee_iterator::iterator_adaptor_base(std::forward<U &&>(u)) {}
241 T &operator*() const { return **this->I; }