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 struct iterator_facade_base
39 : std::iterator<IteratorCategoryT, T, DifferenceTypeT, PointerT,
41 DerivedT operator+(DifferenceTypeT n) const {
42 DerivedT tmp = *static_cast<const DerivedT *>(this);
46 friend DerivedT operator+(DifferenceTypeT n, const DerivedT &i) {
49 DerivedT operator-(DifferenceTypeT n) const {
50 DerivedT tmp = *static_cast<const DerivedT *>(this);
55 DerivedT &operator++() {
56 return static_cast<DerivedT *>(this)->operator+=(1);
58 DerivedT operator++(int) {
59 DerivedT tmp = *static_cast<DerivedT *>(this);
60 ++*static_cast<DerivedT *>(this);
63 DerivedT &operator--() {
64 return static_cast<DerivedT *>(this)->operator-=(1);
66 DerivedT operator--(int) {
67 DerivedT tmp = *static_cast<DerivedT *>(this);
68 --*static_cast<DerivedT *>(this);
72 bool operator!=(const DerivedT &RHS) const {
73 return !static_cast<const DerivedT *>(this)->operator==(RHS);
76 bool operator>(const DerivedT &RHS) const {
77 return !static_cast<const DerivedT *>(this)->operator<(RHS) &&
78 !static_cast<const DerivedT *>(this)->operator==(RHS);
80 bool operator<=(const DerivedT &RHS) const {
81 return !static_cast<const DerivedT *>(this)->operator>(RHS);
83 bool operator>=(const DerivedT &RHS) const {
84 return !static_cast<const DerivedT *>(this)->operator<(RHS);
87 PointerT operator->() const {
88 return &static_cast<const DerivedT *>(this)->operator*();
90 ReferenceT operator[](DifferenceTypeT n) const {
91 return *static_cast<const DerivedT *>(this)->operator+(n);
95 /// \brief CRTP base class for adapting an iterator to a different type.
97 /// This class can be used through CRTP to adapt one iterator into another.
98 /// Typically this is done through providing in the derived class a custom \c
99 /// operator* implementation. Other methods can be overridden as well.
100 template <typename DerivedT, typename WrappedIteratorT, typename T,
101 typename PointerT = T *, typename ReferenceT = T &,
102 // Don't provide these, they are mostly to act as aliases below.
103 typename WrappedTraitsT = std::iterator_traits<WrappedIteratorT>>
104 class iterator_adaptor_base
105 : public iterator_facade_base<
106 DerivedT, typename WrappedTraitsT::iterator_category, T,
107 typename WrappedTraitsT::difference_type, PointerT, ReferenceT> {
108 typedef typename iterator_adaptor_base::iterator_facade_base BaseT;
113 iterator_adaptor_base() {}
115 template <typename U>
116 explicit iterator_adaptor_base(
118 typename std::enable_if<
119 !std::is_base_of<typename std::remove_cv<
120 typename std::remove_reference<U>::type>::type,
123 : I(std::forward<U &&>(u)) {}
126 typedef typename WrappedTraitsT::difference_type difference_type;
128 DerivedT &operator+=(difference_type n) {
130 return *static_cast<DerivedT *>(this);
132 DerivedT &operator-=(difference_type n) {
134 return *static_cast<DerivedT *>(this);
136 using BaseT::operator-;
137 difference_type operator-(const DerivedT &RHS) const { return I - RHS.I; }
139 // We have to explicitly provide ++ and -- rather than letting the facade
140 // forward to += because WrappedIteratorT might not support +=.
141 using BaseT::operator++;
142 DerivedT &operator++() {
144 return *static_cast<DerivedT *>(this);
146 using BaseT::operator--;
147 DerivedT &operator--() {
149 return *static_cast<DerivedT *>(this);
152 bool operator==(const DerivedT &RHS) const { return I == RHS.I; }
153 bool operator<(const DerivedT &RHS) const { return I < RHS.I; }
155 ReferenceT operator*() const { return *I; }
158 /// \brief An iterator type that allows iterating over the pointees via some
161 /// The typical usage of this is to expose a type that iterates over Ts, but
162 /// which is implemented with some iterator over T*s:
165 /// typedef pointee_iterator<SmallVectorImpl<T *>::iterator> iterator;
167 template <typename WrappedIteratorT,
168 typename T = typename std::remove_reference<
169 decltype(**std::declval<WrappedIteratorT>())>::type>
170 struct pointee_iterator
171 : iterator_adaptor_base<pointee_iterator<WrappedIteratorT>,
172 WrappedIteratorT, T> {
173 pointee_iterator() {}
174 template <typename U>
175 pointee_iterator(U &&u)
176 : pointee_iterator::iterator_adaptor_base(std::forward<U &&>(u)) {}
178 T &operator*() const { return **this->I; }