bounded_iter.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___ITERATOR_BOUNDED_ITER_H
  10. #define _LIBCPP___ITERATOR_BOUNDED_ITER_H
  11. #include <__assert>
  12. #include <__config>
  13. #include <__iterator/iterator_traits.h>
  14. #include <__memory/pointer_traits.h>
  15. #include <__type_traits/enable_if.h>
  16. #include <__type_traits/integral_constant.h>
  17. #include <__type_traits/is_convertible.h>
  18. #include <__utility/move.h>
  19. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  20. # pragma GCC system_header
  21. #endif
  22. _LIBCPP_PUSH_MACROS
  23. #include <__undef_macros>
  24. _LIBCPP_BEGIN_NAMESPACE_STD
  25. // Iterator wrapper that carries the valid range it is allowed to access.
  26. //
  27. // This is a simple iterator wrapper for contiguous iterators that points
  28. // within a [begin, end] range and carries these bounds with it. The iterator
  29. // ensures that it is pointing within [begin, end) range when it is
  30. // dereferenced. It also ensures that it is never iterated outside of
  31. // [begin, end]. This is important for two reasons:
  32. //
  33. // 1. It allows `operator*` and `operator++` bounds checks to be `iter != end`.
  34. // This is both less for the optimizer to prove, and aligns with how callers
  35. // typically use iterators.
  36. //
  37. // 2. Advancing an iterator out of bounds is undefined behavior (see the table
  38. // in [input.iterators]). In particular, when the underlying iterator is a
  39. // pointer, it is undefined at the language level (see [expr.add]). If
  40. // bounded iterators exhibited this undefined behavior, we risk compiler
  41. // optimizations deleting non-redundant bounds checks.
  42. template <class _Iterator, class = __enable_if_t< __libcpp_is_contiguous_iterator<_Iterator>::value > >
  43. struct ___bounded_iter {
  44. using value_type = typename iterator_traits<_Iterator>::value_type;
  45. using difference_type = typename iterator_traits<_Iterator>::difference_type;
  46. using pointer = typename iterator_traits<_Iterator>::pointer;
  47. using reference = typename iterator_traits<_Iterator>::reference;
  48. using iterator_category = typename iterator_traits<_Iterator>::iterator_category;
  49. #if _LIBCPP_STD_VER >= 20
  50. using iterator_concept = contiguous_iterator_tag;
  51. #endif
  52. // Create a singular iterator.
  53. //
  54. // Such an iterator points past the end of an empty span, so it is not dereferenceable.
  55. // Observing operations like comparison and assignment are valid.
  56. _LIBCPP_HIDE_FROM_ABI ___bounded_iter() = default;
  57. _LIBCPP_HIDE_FROM_ABI ___bounded_iter(___bounded_iter const&) = default;
  58. _LIBCPP_HIDE_FROM_ABI ___bounded_iter(___bounded_iter&&) = default;
  59. template <class _OtherIterator, __enable_if_t< is_convertible<_OtherIterator, _Iterator>::value, int> = 0>
  60. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR ___bounded_iter(___bounded_iter<_OtherIterator> const& __other) _NOEXCEPT
  61. : __current_(__other.__current_),
  62. __begin_(__other.__begin_),
  63. __end_(__other.__end_) {}
  64. // Assign a bounded iterator to another one, rebinding the bounds of the iterator as well.
  65. _LIBCPP_HIDE_FROM_ABI ___bounded_iter& operator=(___bounded_iter const&) = default;
  66. _LIBCPP_HIDE_FROM_ABI ___bounded_iter& operator=(___bounded_iter&&) = default;
  67. private:
  68. // Create an iterator wrapping the given iterator, and whose bounds are described
  69. // by the provided [begin, end] range.
  70. //
  71. // The constructor does not check whether the resulting iterator is within its bounds. It is a
  72. // responsibility of the container to ensure that the given bounds are valid.
  73. //
  74. // Since it is non-standard for iterators to have this constructor, ___bounded_iter must
  75. // be created via `std::__make_bounded_iter`.
  76. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit ___bounded_iter(
  77. _Iterator __current, _Iterator __begin, _Iterator __end)
  78. : __current_(__current), __begin_(__begin), __end_(__end) {
  79. _LIBCPP_ASSERT_INTERNAL(
  80. __begin <= __current, "___bounded_iter(current, begin, end): current and begin are inconsistent");
  81. _LIBCPP_ASSERT_INTERNAL(
  82. __current <= __end, "___bounded_iter(current, begin, end): current and end are inconsistent");
  83. }
  84. template <class _It>
  85. friend _LIBCPP_CONSTEXPR ___bounded_iter<_It> __make_bounded_iter(_It, _It, _It);
  86. public:
  87. // Dereference and indexing operations.
  88. //
  89. // These operations check that the iterator is dereferenceable. Since the class invariant is
  90. // that the iterator is always within `[begin, end]`, we only need to check it's not pointing to
  91. // `end`. This is easier for the optimizer because it aligns with the `iter != container.end()`
  92. // checks that typical callers already use (see
  93. // https://github.com/llvm/llvm-project/issues/78829).
  94. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT {
  95. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  96. __current_ != __end_, "___bounded_iter::operator*: Attempt to dereference an iterator at the end");
  97. return *__current_;
  98. }
  99. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pointer operator->() const _NOEXCEPT {
  100. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  101. __current_ != __end_, "___bounded_iter::operator->: Attempt to dereference an iterator at the end");
  102. return std::__to_address(__current_);
  103. }
  104. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator[](difference_type __n) const _NOEXCEPT {
  105. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  106. __n >= __begin_ - __current_, "___bounded_iter::operator[]: Attempt to index an iterator past the start");
  107. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  108. __n < __end_ - __current_, "___bounded_iter::operator[]: Attempt to index an iterator at or past the end");
  109. return __current_[__n];
  110. }
  111. // Arithmetic operations.
  112. //
  113. // These operations check that the iterator remains within `[begin, end]`.
  114. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 ___bounded_iter& operator++() _NOEXCEPT {
  115. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  116. __current_ != __end_, "___bounded_iter::operator++: Attempt to advance an iterator past the end");
  117. ++__current_;
  118. return *this;
  119. }
  120. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 ___bounded_iter operator++(int) _NOEXCEPT {
  121. ___bounded_iter __tmp(*this);
  122. ++*this;
  123. return __tmp;
  124. }
  125. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 ___bounded_iter& operator--() _NOEXCEPT {
  126. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  127. __current_ != __begin_, "___bounded_iter::operator--: Attempt to rewind an iterator past the start");
  128. --__current_;
  129. return *this;
  130. }
  131. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 ___bounded_iter operator--(int) _NOEXCEPT {
  132. ___bounded_iter __tmp(*this);
  133. --*this;
  134. return __tmp;
  135. }
  136. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 ___bounded_iter& operator+=(difference_type __n) _NOEXCEPT {
  137. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  138. __n >= __begin_ - __current_, "___bounded_iter::operator+=: Attempt to rewind an iterator past the start");
  139. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  140. __n <= __end_ - __current_, "___bounded_iter::operator+=: Attempt to advance an iterator past the end");
  141. __current_ += __n;
  142. return *this;
  143. }
  144. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend ___bounded_iter
  145. operator+(___bounded_iter const& __self, difference_type __n) _NOEXCEPT {
  146. ___bounded_iter __tmp(__self);
  147. __tmp += __n;
  148. return __tmp;
  149. }
  150. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend ___bounded_iter
  151. operator+(difference_type __n, ___bounded_iter const& __self) _NOEXCEPT {
  152. ___bounded_iter __tmp(__self);
  153. __tmp += __n;
  154. return __tmp;
  155. }
  156. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 ___bounded_iter& operator-=(difference_type __n) _NOEXCEPT {
  157. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  158. __n <= __current_ - __begin_, "___bounded_iter::operator-=: Attempt to rewind an iterator past the start");
  159. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  160. __n >= __current_ - __end_, "___bounded_iter::operator-=: Attempt to advance an iterator past the end");
  161. __current_ -= __n;
  162. return *this;
  163. }
  164. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend ___bounded_iter
  165. operator-(___bounded_iter const& __self, difference_type __n) _NOEXCEPT {
  166. ___bounded_iter __tmp(__self);
  167. __tmp -= __n;
  168. return __tmp;
  169. }
  170. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend difference_type
  171. operator-(___bounded_iter const& __x, ___bounded_iter const& __y) _NOEXCEPT {
  172. return __x.__current_ - __y.__current_;
  173. }
  174. // Comparison operations.
  175. //
  176. // These operations do not check whether the iterators are within their bounds.
  177. // The valid range for each iterator is also not considered as part of the comparison,
  178. // i.e. two iterators pointing to the same location will be considered equal even
  179. // if they have different validity ranges.
  180. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
  181. operator==(___bounded_iter const& __x, ___bounded_iter const& __y) _NOEXCEPT {
  182. return __x.__current_ == __y.__current_;
  183. }
  184. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
  185. operator!=(___bounded_iter const& __x, ___bounded_iter const& __y) _NOEXCEPT {
  186. return __x.__current_ != __y.__current_;
  187. }
  188. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
  189. operator<(___bounded_iter const& __x, ___bounded_iter const& __y) _NOEXCEPT {
  190. return __x.__current_ < __y.__current_;
  191. }
  192. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
  193. operator>(___bounded_iter const& __x, ___bounded_iter const& __y) _NOEXCEPT {
  194. return __x.__current_ > __y.__current_;
  195. }
  196. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
  197. operator<=(___bounded_iter const& __x, ___bounded_iter const& __y) _NOEXCEPT {
  198. return __x.__current_ <= __y.__current_;
  199. }
  200. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
  201. operator>=(___bounded_iter const& __x, ___bounded_iter const& __y) _NOEXCEPT {
  202. return __x.__current_ >= __y.__current_;
  203. }
  204. private:
  205. template <class>
  206. friend struct pointer_traits;
  207. _Iterator __current_; // current iterator
  208. _Iterator __begin_, __end_; // valid range represented as [begin, end]
  209. };
  210. template <class _It>
  211. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR ___bounded_iter<_It> __make_bounded_iter(_It __it, _It __begin, _It __end) {
  212. return ___bounded_iter<_It>(std::move(__it), std::move(__begin), std::move(__end));
  213. }
  214. #if _LIBCPP_STD_VER <= 17
  215. template <class _Iterator>
  216. struct __libcpp_is_contiguous_iterator<___bounded_iter<_Iterator> > : true_type {};
  217. #endif
  218. template <class _Iterator>
  219. struct pointer_traits<___bounded_iter<_Iterator> > {
  220. using pointer = ___bounded_iter<_Iterator>;
  221. using element_type = typename pointer_traits<_Iterator>::element_type;
  222. using difference_type = typename pointer_traits<_Iterator>::difference_type;
  223. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static element_type* to_address(pointer __it) _NOEXCEPT {
  224. return std::__to_address(__it.__current_);
  225. }
  226. };
  227. _LIBCPP_END_NAMESPACE_STD
  228. _LIBCPP_POP_MACROS
  229. #endif // _LIBCPP___ITERATOR_BOUNDED_ITER_H