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