bounded_iter.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 that [begin, end) range when it is
  30. // dereferenced.
  31. //
  32. // Arithmetic operations are allowed and the bounds of the resulting iterator
  33. // are not checked. Hence, it is possible to create an iterator pointing outside
  34. // its range, but it is not possible to dereference it.
  35. template <class _Iterator, class = __enable_if_t< __libcpp_is_contiguous_iterator<_Iterator>::value > >
  36. struct ___bounded_iter {
  37. using value_type = typename iterator_traits<_Iterator>::value_type;
  38. using difference_type = typename iterator_traits<_Iterator>::difference_type;
  39. using pointer = typename iterator_traits<_Iterator>::pointer;
  40. using reference = typename iterator_traits<_Iterator>::reference;
  41. using iterator_category = typename iterator_traits<_Iterator>::iterator_category;
  42. #if _LIBCPP_STD_VER >= 20
  43. using iterator_concept = contiguous_iterator_tag;
  44. #endif
  45. // Create a singular iterator.
  46. //
  47. // Such an iterator does not point to any object and is conceptually out of bounds, so it is
  48. // not dereferenceable. Observing operations like comparison and assignment are valid.
  49. _LIBCPP_HIDE_FROM_ABI ___bounded_iter() = default;
  50. _LIBCPP_HIDE_FROM_ABI ___bounded_iter(___bounded_iter const&) = default;
  51. _LIBCPP_HIDE_FROM_ABI ___bounded_iter(___bounded_iter&&) = default;
  52. template <class _OtherIterator, class = __enable_if_t< is_convertible<_OtherIterator, _Iterator>::value > >
  53. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR ___bounded_iter(___bounded_iter<_OtherIterator> const& __other) _NOEXCEPT
  54. : __current_(__other.__current_),
  55. __begin_(__other.__begin_),
  56. __end_(__other.__end_) {}
  57. // Assign a bounded iterator to another one, rebinding the bounds of the iterator as well.
  58. _LIBCPP_HIDE_FROM_ABI ___bounded_iter& operator=(___bounded_iter const&) = default;
  59. _LIBCPP_HIDE_FROM_ABI ___bounded_iter& operator=(___bounded_iter&&) = default;
  60. private:
  61. // Create an iterator wrapping the given iterator, and whose bounds are described
  62. // by the provided [begin, end) range.
  63. //
  64. // This constructor does not check whether the resulting iterator is within its bounds.
  65. // However, it does check that the provided [begin, end) range is a valid range (that
  66. // is, begin <= end).
  67. //
  68. // Since it is non-standard for iterators to have this constructor, ___bounded_iter must
  69. // be created via `std::__make_bounded_iter`.
  70. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit ___bounded_iter(
  71. _Iterator __current, _Iterator __begin, _Iterator __end)
  72. : __current_(__current), __begin_(__begin), __end_(__end) {
  73. _LIBCPP_ASSERT_INTERNAL(__begin <= __end, "___bounded_iter(current, begin, end): [begin, end) is not a valid range");
  74. }
  75. template <class _It>
  76. friend _LIBCPP_CONSTEXPR ___bounded_iter<_It> __make_bounded_iter(_It, _It, _It);
  77. public:
  78. // Dereference and indexing operations.
  79. //
  80. // These operations check that the iterator is dereferenceable, that is within [begin, end).
  81. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT {
  82. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  83. __in_bounds(__current_), "___bounded_iter::operator*: Attempt to dereference an out-of-range iterator");
  84. return *__current_;
  85. }
  86. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pointer operator->() const _NOEXCEPT {
  87. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  88. __in_bounds(__current_), "___bounded_iter::operator->: Attempt to dereference an out-of-range iterator");
  89. return std::__to_address(__current_);
  90. }
  91. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator[](difference_type __n) const _NOEXCEPT {
  92. _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
  93. __in_bounds(__current_ + __n), "___bounded_iter::operator[]: Attempt to index an iterator out-of-range");
  94. return __current_[__n];
  95. }
  96. // Arithmetic operations.
  97. //
  98. // These operations do not check that the resulting iterator is within the bounds, since that
  99. // would make it impossible to create a past-the-end iterator.
  100. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 ___bounded_iter& operator++() _NOEXCEPT {
  101. ++__current_;
  102. return *this;
  103. }
  104. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 ___bounded_iter operator++(int) _NOEXCEPT {
  105. ___bounded_iter __tmp(*this);
  106. ++*this;
  107. return __tmp;
  108. }
  109. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 ___bounded_iter& operator--() _NOEXCEPT {
  110. --__current_;
  111. return *this;
  112. }
  113. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 ___bounded_iter operator--(int) _NOEXCEPT {
  114. ___bounded_iter __tmp(*this);
  115. --*this;
  116. return __tmp;
  117. }
  118. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 ___bounded_iter& operator+=(difference_type __n) _NOEXCEPT {
  119. __current_ += __n;
  120. return *this;
  121. }
  122. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend ___bounded_iter
  123. operator+(___bounded_iter const& __self, difference_type __n) _NOEXCEPT {
  124. ___bounded_iter __tmp(__self);
  125. __tmp += __n;
  126. return __tmp;
  127. }
  128. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend ___bounded_iter
  129. operator+(difference_type __n, ___bounded_iter const& __self) _NOEXCEPT {
  130. ___bounded_iter __tmp(__self);
  131. __tmp += __n;
  132. return __tmp;
  133. }
  134. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 ___bounded_iter& operator-=(difference_type __n) _NOEXCEPT {
  135. __current_ -= __n;
  136. return *this;
  137. }
  138. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend ___bounded_iter
  139. operator-(___bounded_iter const& __self, difference_type __n) _NOEXCEPT {
  140. ___bounded_iter __tmp(__self);
  141. __tmp -= __n;
  142. return __tmp;
  143. }
  144. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend difference_type
  145. operator-(___bounded_iter const& __x, ___bounded_iter const& __y) _NOEXCEPT {
  146. return __x.__current_ - __y.__current_;
  147. }
  148. // Comparison operations.
  149. //
  150. // These operations do not check whether the iterators are within their bounds.
  151. // The valid range for each iterator is also not considered as part of the comparison,
  152. // i.e. two iterators pointing to the same location will be considered equal even
  153. // if they have different validity ranges.
  154. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
  155. operator==(___bounded_iter const& __x, ___bounded_iter const& __y) _NOEXCEPT {
  156. return __x.__current_ == __y.__current_;
  157. }
  158. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
  159. operator!=(___bounded_iter const& __x, ___bounded_iter const& __y) _NOEXCEPT {
  160. return __x.__current_ != __y.__current_;
  161. }
  162. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
  163. operator<(___bounded_iter const& __x, ___bounded_iter const& __y) _NOEXCEPT {
  164. return __x.__current_ < __y.__current_;
  165. }
  166. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
  167. operator>(___bounded_iter const& __x, ___bounded_iter const& __y) _NOEXCEPT {
  168. return __x.__current_ > __y.__current_;
  169. }
  170. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
  171. operator<=(___bounded_iter const& __x, ___bounded_iter const& __y) _NOEXCEPT {
  172. return __x.__current_ <= __y.__current_;
  173. }
  174. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
  175. operator>=(___bounded_iter const& __x, ___bounded_iter const& __y) _NOEXCEPT {
  176. return __x.__current_ >= __y.__current_;
  177. }
  178. private:
  179. // Return whether the given iterator is in the bounds of this ___bounded_iter.
  180. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool __in_bounds(_Iterator const& __iter) const {
  181. return __iter >= __begin_ && __iter < __end_;
  182. }
  183. template <class>
  184. friend struct pointer_traits;
  185. _Iterator __current_; // current iterator
  186. _Iterator __begin_, __end_; // valid range represented as [begin, end)
  187. };
  188. template <class _It>
  189. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR ___bounded_iter<_It> __make_bounded_iter(_It __it, _It __begin, _It __end) {
  190. return ___bounded_iter<_It>(std::move(__it), std::move(__begin), std::move(__end));
  191. }
  192. #if _LIBCPP_STD_VER <= 17
  193. template <class _Iterator>
  194. struct __libcpp_is_contiguous_iterator<___bounded_iter<_Iterator> > : true_type {};
  195. #endif
  196. template <class _Iterator>
  197. struct pointer_traits<___bounded_iter<_Iterator> > {
  198. using pointer = ___bounded_iter<_Iterator>;
  199. using element_type = typename pointer_traits<_Iterator>::element_type;
  200. using difference_type = typename pointer_traits<_Iterator>::difference_type;
  201. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static element_type* to_address(pointer __it) _NOEXCEPT {
  202. return std::__to_address(__it.__current_);
  203. }
  204. };
  205. _LIBCPP_END_NAMESPACE_STD
  206. _LIBCPP_POP_MACROS
  207. #endif // _LIBCPP___ITERATOR_BOUNDED_ITER_H