iterator.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #ifndef LLVM_ADT_ITERATOR_H
  10. #define LLVM_ADT_ITERATOR_H
  11. #include <cstddef>
  12. #include <iterator>
  13. namespace llvm {
  14. /// \brief CRTP base class which implements the entire standard iterator facade
  15. /// in terms of a minimal subset of the interface.
  16. ///
  17. /// Use this when it is reasonable to implement most of the iterator
  18. /// functionality in terms of a core subset. If you need special behavior or
  19. /// there are performance implications for this, you may want to override the
  20. /// relevant members instead.
  21. ///
  22. /// Note, one abstraction that this does *not* provide is implementing
  23. /// subtraction in terms of addition by negating the difference. Negation isn't
  24. /// always information preserving, and I can see very reasonable iterator
  25. /// designs where this doesn't work well. It doesn't really force much added
  26. /// boilerplate anyways.
  27. ///
  28. /// Another abstraction that this doesn't provide is implementing increment in
  29. /// terms of addition of one. These aren't equivalent for all iterator
  30. /// categories, and respecting that adds a lot of complexity for little gain.
  31. template <typename DerivedT, typename IteratorCategoryT, typename T,
  32. typename DifferenceTypeT = std::ptrdiff_t, typename PointerT = T *,
  33. typename ReferenceT = T &>
  34. class iterator_facade_base
  35. : public std::iterator<IteratorCategoryT, T, DifferenceTypeT, PointerT,
  36. ReferenceT> {
  37. protected:
  38. enum {
  39. IsRandomAccess =
  40. std::is_base_of<std::random_access_iterator_tag, IteratorCategoryT>::value,
  41. IsBidirectional =
  42. std::is_base_of<std::bidirectional_iterator_tag, IteratorCategoryT>::value,
  43. };
  44. public:
  45. DerivedT operator+(DifferenceTypeT n) const {
  46. static_assert(
  47. IsRandomAccess,
  48. "The '+' operator is only defined for random access iterators.");
  49. DerivedT tmp = *static_cast<const DerivedT *>(this);
  50. tmp += n;
  51. return tmp;
  52. }
  53. friend DerivedT operator+(DifferenceTypeT n, const DerivedT &i) {
  54. static_assert(
  55. IsRandomAccess,
  56. "The '+' operator is only defined for random access iterators.");
  57. return i + n;
  58. }
  59. DerivedT operator-(DifferenceTypeT n) const {
  60. static_assert(
  61. IsRandomAccess,
  62. "The '-' operator is only defined for random access iterators.");
  63. DerivedT tmp = *static_cast<const DerivedT *>(this);
  64. tmp -= n;
  65. return tmp;
  66. }
  67. DerivedT &operator++() {
  68. return static_cast<DerivedT *>(this)->operator+=(1);
  69. }
  70. DerivedT operator++(int) {
  71. DerivedT tmp = *static_cast<DerivedT *>(this);
  72. ++*static_cast<DerivedT *>(this);
  73. return tmp;
  74. }
  75. DerivedT &operator--() {
  76. static_assert(
  77. IsBidirectional,
  78. "The decrement operator is only defined for bidirectional iterators.");
  79. return static_cast<DerivedT *>(this)->operator-=(1);
  80. }
  81. DerivedT operator--(int) {
  82. static_assert(
  83. IsBidirectional,
  84. "The decrement operator is only defined for bidirectional iterators.");
  85. DerivedT tmp = *static_cast<DerivedT *>(this);
  86. --*static_cast<DerivedT *>(this);
  87. return tmp;
  88. }
  89. bool operator!=(const DerivedT &RHS) const {
  90. return !static_cast<const DerivedT *>(this)->operator==(RHS);
  91. }
  92. bool operator>(const DerivedT &RHS) const {
  93. static_assert(
  94. IsRandomAccess,
  95. "Relational operators are only defined for random access iterators.");
  96. return !static_cast<const DerivedT *>(this)->operator<(RHS) &&
  97. !static_cast<const DerivedT *>(this)->operator==(RHS);
  98. }
  99. bool operator<=(const DerivedT &RHS) const {
  100. static_assert(
  101. IsRandomAccess,
  102. "Relational operators are only defined for random access iterators.");
  103. return !static_cast<const DerivedT *>(this)->operator>(RHS);
  104. }
  105. bool operator>=(const DerivedT &RHS) const {
  106. static_assert(
  107. IsRandomAccess,
  108. "Relational operators are only defined for random access iterators.");
  109. return !static_cast<const DerivedT *>(this)->operator<(RHS);
  110. }
  111. PointerT operator->() const {
  112. return &static_cast<const DerivedT *>(this)->operator*();
  113. }
  114. ReferenceT operator[](DifferenceTypeT n) const {
  115. static_assert(IsRandomAccess,
  116. "Subscripting is only defined for random access iterators.");
  117. return *static_cast<const DerivedT *>(this)->operator+(n);
  118. }
  119. };
  120. /// \brief CRTP base class for adapting an iterator to a different type.
  121. ///
  122. /// This class can be used through CRTP to adapt one iterator into another.
  123. /// Typically this is done through providing in the derived class a custom \c
  124. /// operator* implementation. Other methods can be overridden as well.
  125. template <
  126. typename DerivedT, typename WrappedIteratorT,
  127. typename IteratorCategoryT =
  128. typename std::iterator_traits<WrappedIteratorT>::iterator_category,
  129. typename T = typename std::iterator_traits<WrappedIteratorT>::value_type,
  130. typename DifferenceTypeT =
  131. typename std::iterator_traits<WrappedIteratorT>::difference_type,
  132. typename PointerT = T *, typename ReferenceT = T &,
  133. // Don't provide these, they are mostly to act as aliases below.
  134. typename WrappedTraitsT = std::iterator_traits<WrappedIteratorT>>
  135. class iterator_adaptor_base
  136. : public iterator_facade_base<DerivedT, IteratorCategoryT, T,
  137. DifferenceTypeT, PointerT, ReferenceT> {
  138. typedef typename iterator_adaptor_base::iterator_facade_base BaseT;
  139. protected:
  140. WrappedIteratorT I;
  141. iterator_adaptor_base() = default;
  142. template <typename U>
  143. explicit iterator_adaptor_base(
  144. U &&u,
  145. typename std::enable_if<
  146. !std::is_base_of<typename std::remove_cv<
  147. typename std::remove_reference<U>::type>::type,
  148. DerivedT>::value,
  149. int>::type = 0)
  150. : I(std::forward<U &&>(u)) {}
  151. const WrappedIteratorT &wrapped() const { return I; }
  152. public:
  153. typedef DifferenceTypeT difference_type;
  154. DerivedT &operator+=(difference_type n) {
  155. static_assert(
  156. BaseT::IsRandomAccess,
  157. "The '+=' operator is only defined for random access iterators.");
  158. I += n;
  159. return *static_cast<DerivedT *>(this);
  160. }
  161. DerivedT &operator-=(difference_type n) {
  162. static_assert(
  163. BaseT::IsRandomAccess,
  164. "The '-=' operator is only defined for random access iterators.");
  165. I -= n;
  166. return *static_cast<DerivedT *>(this);
  167. }
  168. using BaseT::operator-;
  169. difference_type operator-(const DerivedT &RHS) const {
  170. static_assert(
  171. BaseT::IsRandomAccess,
  172. "The '-' operator is only defined for random access iterators.");
  173. return I - RHS.I;
  174. }
  175. // We have to explicitly provide ++ and -- rather than letting the facade
  176. // forward to += because WrappedIteratorT might not support +=.
  177. using BaseT::operator++;
  178. DerivedT &operator++() {
  179. ++I;
  180. return *static_cast<DerivedT *>(this);
  181. }
  182. using BaseT::operator--;
  183. DerivedT &operator--() {
  184. static_assert(
  185. BaseT::IsBidirectional,
  186. "The decrement operator is only defined for bidirectional iterators.");
  187. --I;
  188. return *static_cast<DerivedT *>(this);
  189. }
  190. bool operator==(const DerivedT &RHS) const { return I == RHS.I; }
  191. bool operator<(const DerivedT &RHS) const {
  192. static_assert(
  193. BaseT::IsRandomAccess,
  194. "Relational operators are only defined for random access iterators.");
  195. return I < RHS.I;
  196. }
  197. ReferenceT operator*() const { return *I; }
  198. };
  199. /// \brief An iterator type that allows iterating over the pointees via some
  200. /// other iterator.
  201. ///
  202. /// The typical usage of this is to expose a type that iterates over Ts, but
  203. /// which is implemented with some iterator over T*s:
  204. ///
  205. /// \code
  206. /// typedef pointee_iterator<SmallVectorImpl<T *>::iterator> iterator;
  207. /// \endcode
  208. template <typename WrappedIteratorT,
  209. typename T = typename std::remove_reference<
  210. decltype(**std::declval<WrappedIteratorT>())>::type>
  211. struct pointee_iterator
  212. : iterator_adaptor_base<
  213. pointee_iterator<WrappedIteratorT>, WrappedIteratorT,
  214. typename std::iterator_traits<WrappedIteratorT>::iterator_category,
  215. T> {
  216. pointee_iterator() = default;
  217. template <typename U>
  218. pointee_iterator(U &&u)
  219. : pointee_iterator::iterator_adaptor_base(std::forward<U &&>(u)) {}
  220. T &operator*() const { return **this->I; }
  221. };
  222. }
  223. #endif