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_STATIC_BOUNDED_ITER_H
11#define _LIBCPP___ITERATOR_STATIC_BOUNDED_ITER_H
12
13#include <__assert>
14#include <__compare/ordering.h>
15#include <__compare/three_way_comparable.h>
16#include <__config>
17#include <__cstddef/size_t.h>
18#include <__iterator/iterator_traits.h>
19#include <__memory/pointer_traits.h>
20#include <__type_traits/conjunction.h>
21#include <__type_traits/disjunction.h>
22#include <__type_traits/enable_if.h>
23#include <__type_traits/integral_constant.h>
24#include <__type_traits/is_convertible.h>
25#include <__type_traits/is_same.h>
26#include <__type_traits/make_const_lvalue_ref.h>
27#include <__utility/move.h>
28
29#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
30# pragma GCC system_header
31#endif
32
33_LIBCPP_PUSH_MACROS
34#include <__undef_macros>
35
36_LIBCPP_BEGIN_NAMESPACE_STD
37
38template <class _Iterator, size_t _Size>
39struct __static_bounded_iter_storage {
40 _LIBCPP_HIDE_FROM_ABI __static_bounded_iter_storage() = default;
41 _LIBCPP_HIDE_FROM_ABI
42 _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __static_bounded_iter_storage(_Iterator __current, _Iterator __begin)
43 : __current_(__current), __begin_(__begin) {}
44
45 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator& __current() _NOEXCEPT { return __current_; }
46 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator __current() const _NOEXCEPT { return __current_; }
47 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator __begin() const _NOEXCEPT { return __begin_; }
48 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator __end() const _NOEXCEPT { return __begin_ + _Size; }
49
50private:
51 _Iterator __current_; // current iterator
52 _Iterator __begin_; // start of the valid range, which is [__begin_, __begin_ + _Size)
53};
54
55template <class _Iterator>
56struct __static_bounded_iter_storage<_Iterator, 0> {
57 _LIBCPP_HIDE_FROM_ABI __static_bounded_iter_storage() = default;
58 _LIBCPP_HIDE_FROM_ABI
59 _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __static_bounded_iter_storage(_Iterator __current, _Iterator /* __begin */)
60 : __current_(__current) {}
61
62 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator& __current() _NOEXCEPT { return __current_; }
63 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator __current() const _NOEXCEPT { return __current_; }
64 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator __begin() const _NOEXCEPT { return __current_; }
65 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator __end() const _NOEXCEPT { return __current_; }
66
67private:
68 _Iterator __current_; // current iterator
69};
70
71// This is an iterator wrapper for contiguous iterators that points within a range
72// whose size is known at compile-time. This is very similar to `__bounded_iter`,
73// except that we don't have to store the end of the range in physical memory since
74// it can be computed from the start of the range.
75//
76// The operations on which this iterator wrapper traps are the same as `__bounded_iter`.
77template <class _Iterator, size_t _Size>
78struct __static_bounded_iter {
79 static_assert(__libcpp_is_contiguous_iterator<_Iterator>::value,
80 "Only contiguous iterators can be adapted by __static_bounded_iter.");
81
82 using value_type = typename iterator_traits<_Iterator>::value_type;
83 using difference_type = typename iterator_traits<_Iterator>::difference_type;
84 using pointer = typename iterator_traits<_Iterator>::pointer;
85 using reference = typename iterator_traits<_Iterator>::reference;
86 using iterator_category = typename iterator_traits<_Iterator>::iterator_category;
87#if _LIBCPP_STD_VER >= 20
88 using iterator_concept = contiguous_iterator_tag;
89#endif
90
91 // Create a singular iterator.
92 //
93 // Such an iterator points past the end of an empty range, so it is not dereferenceable.
94 // Operations like comparison and assignment are valid.
95 _LIBCPP_HIDE_FROM_ABI __static_bounded_iter() = default;
96
97 _LIBCPP_HIDE_FROM_ABI __static_bounded_iter(__static_bounded_iter const&) = default;
98 _LIBCPP_HIDE_FROM_ABI __static_bounded_iter(__static_bounded_iter&&) = default;
99
100 template <class _OtherIterator,
101 __enable_if_t<
102 _And<is_convertible<const _OtherIterator&, _Iterator>,
103 _Or<is_same<reference, __iterator_reference<_OtherIterator> >,
104 is_same<reference, __make_const_lvalue_ref<__iterator_reference<_OtherIterator> > > > >::value,
105 int> = 0>
106 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
107 __static_bounded_iter(__static_bounded_iter<_OtherIterator, _Size> const& __other) _NOEXCEPT
108 : __storage_(__other.__storage_.__current(), __other.__storage_.__begin()) {}
109
110 // Assign a bounded iterator to another one, rebinding the bounds of the iterator as well.
111 _LIBCPP_HIDE_FROM_ABI __static_bounded_iter& operator=(__static_bounded_iter const&) = default;
112 _LIBCPP_HIDE_FROM_ABI __static_bounded_iter& operator=(__static_bounded_iter&&) = default;
113
114private:
115 // Create an iterator wrapping the given iterator, and whose bounds are described
116 // by the provided [begin, begin + _Size] range.
117 _LIBCPP_HIDE_FROM_ABI
118 _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __static_bounded_iter(_Iterator __current, _Iterator __begin)
119 : __storage_(__current, __begin) {
120 _LIBCPP_ASSERT_INTERNAL(
121 __begin <= __current, "__static_bounded_iter(current, begin): current and begin are inconsistent");
122 _LIBCPP_ASSERT_INTERNAL(
123 __current <= __end(), "__static_bounded_iter(current, begin): current and (begin + Size) are inconsistent");
124 }
125
126 template <size_t _Sz, class _It>
127 friend _LIBCPP_CONSTEXPR __static_bounded_iter<_It, _Sz> __make_static_bounded_iter(_It, _It);
128
129public:
130 // Dereference and indexing operations.
131 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT {
132 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
133 __current() != __end(), "__static_bounded_iter::operator*: Attempt to dereference an iterator at the end");
134 return *__current();
135 }
136
137 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pointer operator->() const _NOEXCEPT {
138 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
139 __current() != __end(), "__static_bounded_iter::operator->: Attempt to dereference an iterator at the end");
140 return std::__to_address(__current());
141 }
142
143 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference
144 operator[](difference_type __n) const _NOEXCEPT {
145 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
146 __n >= __begin() - __current(),
147 "__static_bounded_iter::operator[]: Attempt to index an iterator past the start");
148 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
149 __n < __end() - __current(),
150 "__static_bounded_iter::operator[]: Attempt to index an iterator at or past the end");
151 return __current()[__n];
152 }
153
154 // Arithmetic operations.
155 //
156 // These operations check that the iterator remains within `[begin, end]`.
157 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __static_bounded_iter& operator++() _NOEXCEPT {
158 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
159 __current() != __end(), "__static_bounded_iter::operator++: Attempt to advance an iterator past the end");
160 ++__current();
161 return *this;
162 }
163 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __static_bounded_iter operator++(int) _NOEXCEPT {
164 __static_bounded_iter __tmp(*this);
165 ++*this;
166 return __tmp;
167 }
168
169 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __static_bounded_iter& operator--() _NOEXCEPT {
170 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
171 __current() != __begin(), "__static_bounded_iter::operator--: Attempt to rewind an iterator past the start");
172 --__current();
173 return *this;
174 }
175 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __static_bounded_iter operator--(int) _NOEXCEPT {
176 __static_bounded_iter __tmp(*this);
177 --*this;
178 return __tmp;
179 }
180
181 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __static_bounded_iter& operator+=(difference_type __n) _NOEXCEPT {
182 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
183 __n >= __begin() - __current(),
184 "__static_bounded_iter::operator+=: Attempt to rewind an iterator past the start");
185 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
186 __n <= __end() - __current(), "__static_bounded_iter::operator+=: Attempt to advance an iterator past the end");
187 __current() += __n;
188 return *this;
189 }
190 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __static_bounded_iter
191 operator+(__static_bounded_iter const& __self, difference_type __n) _NOEXCEPT {
192 __static_bounded_iter __tmp(__self);
193 __tmp += __n;
194 return __tmp;
195 }
196 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __static_bounded_iter
197 operator+(difference_type __n, __static_bounded_iter const& __self) _NOEXCEPT {
198 __static_bounded_iter __tmp(__self);
199 __tmp += __n;
200 return __tmp;
201 }
202
203 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __static_bounded_iter& operator-=(difference_type __n) _NOEXCEPT {
204 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
205 __n <= __current() - __begin(),
206 "__static_bounded_iter::operator-=: Attempt to rewind an iterator past the start");
207 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
208 __n >= __current() - __end(), "__static_bounded_iter::operator-=: Attempt to advance an iterator past the end");
209 __current() -= __n;
210 return *this;
211 }
212 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __static_bounded_iter
213 operator-(__static_bounded_iter const& __self, difference_type __n) _NOEXCEPT {
214 __static_bounded_iter __tmp(__self);
215 __tmp -= __n;
216 return __tmp;
217 }
218 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend difference_type
219 operator-(__static_bounded_iter const& __x, __static_bounded_iter const& __y) _NOEXCEPT {
220 return __x.__current() - __y.__current();
221 }
222
223 // Comparison operations.
224 //
225 // These operations do not check whether the iterators are within their bounds.
226 // The valid range for each iterator is also not considered as part of the comparison,
227 // i.e. two iterators pointing to the same location will be considered equal even
228 // if they have different validity ranges.
229 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
230 operator==(__static_bounded_iter const& __x, __static_bounded_iter const& __y) _NOEXCEPT {
231 return __x.__current() == __y.__current();
232 }
233
234#if _LIBCPP_STD_VER <= 17
235 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
236 operator!=(__static_bounded_iter const& __x, __static_bounded_iter const& __y) _NOEXCEPT {
237 return __x.__current() != __y.__current();
238 }
239
240 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
241 operator<(__static_bounded_iter const& __x, __static_bounded_iter const& __y) _NOEXCEPT {
242 return __x.__current() < __y.__current();
243 }
244 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
245 operator>(__static_bounded_iter const& __x, __static_bounded_iter const& __y) _NOEXCEPT {
246 return __x.__current() > __y.__current();
247 }
248 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
249 operator<=(__static_bounded_iter const& __x, __static_bounded_iter const& __y) _NOEXCEPT {
250 return __x.__current() <= __y.__current();
251 }
252 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
253 operator>=(__static_bounded_iter const& __x, __static_bounded_iter const& __y) _NOEXCEPT {
254 return __x.__current() >= __y.__current();
255 }
256
257#else
258 _LIBCPP_HIDE_FROM_ABI constexpr friend strong_ordering
259 operator<=>(__static_bounded_iter const& __x, __static_bounded_iter const& __y) noexcept {
260 if constexpr (three_way_comparable<_Iterator, strong_ordering>) {
261 return __x.__current() <=> __y.__current();
262 } else {
263 if (__x.__current() < __y.__current())
264 return strong_ordering::less;
265
266 if (__x.__current() == __y.__current())
267 return strong_ordering::equal;
268
269 return strong_ordering::greater;
270 }
271 }
272#endif // _LIBCPP_STD_VER >= 20
273
274private:
275 template <class>
276 friend struct pointer_traits;
277 template <class, size_t>
278 friend struct __static_bounded_iter;
279 __static_bounded_iter_storage<_Iterator, _Size> __storage_;
280
281 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator& __current() _NOEXCEPT {
282 return __storage_.__current();
283 }
284 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator __current() const _NOEXCEPT {
285 return __storage_.__current();
286 }
287 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator __begin() const _NOEXCEPT {
288 return __storage_.__begin();
289 }
290 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator __end() const _NOEXCEPT { return __storage_.__end(); }
291};
292
293template <size_t _Size, class _It>
294_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __static_bounded_iter<_It, _Size>
295__make_static_bounded_iter(_It __it, _It __begin) {
296 return __static_bounded_iter<_It, _Size>(std::move(__it), std::move(__begin));
297}
298
299#if _LIBCPP_STD_VER <= 17
300template <class _Iterator, size_t _Size>
301struct __libcpp_is_contiguous_iterator<__static_bounded_iter<_Iterator, _Size> > : true_type {};
302#endif
303
304template <class _Iterator, size_t _Size>
305struct pointer_traits<__static_bounded_iter<_Iterator, _Size> > {
306 using pointer = __static_bounded_iter<_Iterator, _Size>;
307 using element_type = typename pointer_traits<_Iterator>::element_type;
308 using difference_type = typename pointer_traits<_Iterator>::difference_type;
309
310 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static element_type* to_address(pointer __it) _NOEXCEPT {
311 return std::__to_address(__it.__current());
312 }
313};
314
315_LIBCPP_END_NAMESPACE_STD
316
317_LIBCPP_POP_MACROS
318
319#endif // _LIBCPP___ITERATOR_STATIC_BOUNDED_ITER_H
320