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 _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 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator[](difference_type __n) const _NOEXCEPT {
130 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
131 __n >= __begin_ - __current_, "__bounded_iter::operator[]: Attempt to index an iterator past the start");
132 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
133 __n < __end_ - __current_, "__bounded_iter::operator[]: Attempt to index an iterator at or past the end");
134 return __current_[__n];
135 }
136
137 // Arithmetic operations.
138 //
139 // These operations check that the iterator remains within `[begin, end]`.
140 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __bounded_iter& operator++() _NOEXCEPT {
141 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
142 __current_ != __end_, "__bounded_iter::operator++: Attempt to advance an iterator past the end");
143 ++__current_;
144 return *this;
145 }
146 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __bounded_iter operator++(int) _NOEXCEPT {
147 __bounded_iter __tmp(*this);
148 ++*this;
149 return __tmp;
150 }
151
152 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __bounded_iter& operator--() _NOEXCEPT {
153 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
154 __current_ != __begin_, "__bounded_iter::operator--: Attempt to rewind an iterator past the start");
155 --__current_;
156 return *this;
157 }
158 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __bounded_iter operator--(int) _NOEXCEPT {
159 __bounded_iter __tmp(*this);
160 --*this;
161 return __tmp;
162 }
163
164 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __bounded_iter& operator+=(difference_type __n) _NOEXCEPT {
165 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
166 __n >= __begin_ - __current_, "__bounded_iter::operator+=: Attempt to rewind an iterator past the start");
167 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
168 __n <= __end_ - __current_, "__bounded_iter::operator+=: Attempt to advance an iterator past the end");
169 __current_ += __n;
170 return *this;
171 }
172 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __bounded_iter
173 operator+(__bounded_iter const& __self, difference_type __n) _NOEXCEPT {
174 __bounded_iter __tmp(__self);
175 __tmp += __n;
176 return __tmp;
177 }
178 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __bounded_iter
179 operator+(difference_type __n, __bounded_iter const& __self) _NOEXCEPT {
180 __bounded_iter __tmp(__self);
181 __tmp += __n;
182 return __tmp;
183 }
184
185 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __bounded_iter& operator-=(difference_type __n) _NOEXCEPT {
186 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
187 __n <= __current_ - __begin_, "__bounded_iter::operator-=: Attempt to rewind an iterator past the start");
188 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
189 __n >= __current_ - __end_, "__bounded_iter::operator-=: Attempt to advance an iterator past the end");
190 __current_ -= __n;
191 return *this;
192 }
193 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __bounded_iter
194 operator-(__bounded_iter const& __self, difference_type __n) _NOEXCEPT {
195 __bounded_iter __tmp(__self);
196 __tmp -= __n;
197 return __tmp;
198 }
199 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend difference_type
200 operator-(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT {
201 return __x.__current_ - __y.__current_;
202 }
203
204 // Comparison operations.
205 //
206 // These operations do not check whether the iterators are within their bounds.
207 // The valid range for each iterator is also not considered as part of the comparison,
208 // i.e. two iterators pointing to the same location will be considered equal even
209 // if they have different validity ranges.
210 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
211 operator==(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT {
212 return __x.__current_ == __y.__current_;
213 }
214
215#if _LIBCPP_STD_VER <= 17
216 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
217 operator!=(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT {
218 return __x.__current_ != __y.__current_;
219 }
220
221 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
222 operator<(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT {
223 return __x.__current_ < __y.__current_;
224 }
225 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
226 operator>(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT {
227 return __x.__current_ > __y.__current_;
228 }
229 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
230 operator<=(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT {
231 return __x.__current_ <= __y.__current_;
232 }
233 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
234 operator>=(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT {
235 return __x.__current_ >= __y.__current_;
236 }
237
238#else
239 _LIBCPP_HIDE_FROM_ABI constexpr friend strong_ordering
240 operator<=>(__bounded_iter const& __x, __bounded_iter const& __y) noexcept {
241 if constexpr (three_way_comparable<_Iterator, strong_ordering>) {
242 return __x.__current_ <=> __y.__current_;
243 } else {
244 if (__x.__current_ < __y.__current_)
245 return strong_ordering::less;
246
247 if (__x.__current_ == __y.__current_)
248 return strong_ordering::equal;
249
250 return strong_ordering::greater;
251 }
252 }
253#endif // _LIBCPP_STD_VER >= 20
254
255private:
256 template <class>
257 friend struct pointer_traits;
258 template <class>
259 friend struct __bounded_iter;
260 _Iterator __current_; // current iterator
261 _Iterator __begin_, __end_; // valid range represented as [begin, end]
262};
263
264template <class _It>
265_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __bounded_iter<_It> __make_bounded_iter(_It __it, _It __begin, _It __end) {
266 return __bounded_iter<_It>(std::move(__it), std::move(__begin), std::move(__end));
267}
268
269#if _LIBCPP_STD_VER <= 17
270template <class _Iterator>
271struct __libcpp_is_contiguous_iterator<__bounded_iter<_Iterator> > : true_type {};
272#endif
273
274template <class _Iterator>
275struct pointer_traits<__bounded_iter<_Iterator> > {
276 using pointer = __bounded_iter<_Iterator>;
277 using element_type = typename pointer_traits<_Iterator>::element_type;
278 using difference_type = typename pointer_traits<_Iterator>::difference_type;
279
280 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static element_type* to_address(pointer __it) _NOEXCEPT {
281 return std::__to_address(__it.__current_);
282 }
283};
284
285_LIBCPP_END_NAMESPACE_STD
286
287_LIBCPP_POP_MACROS
288
289#endif // _LIBCPP___ITERATOR_BOUNDED_ITER_H
290