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 __static_bounded_iter_storage() = default;
41
42 _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __static_bounded_iter_storage(_Iterator __current, _Iterator __begin)
43 : __current_(__current), __begin_(__begin) {}
44
45 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator& __current() _NOEXCEPT { return __current_; }
46 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator __current() const _NOEXCEPT { return __current_; }
47 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator __begin() const _NOEXCEPT { return __begin_; }
48 _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 __static_bounded_iter_storage() = default;
58
59 _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __static_bounded_iter_storage(_Iterator __current, _Iterator /* __begin */)
60 : __current_(__current) {}
61
62 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator& __current() _NOEXCEPT { return __current_; }
63 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator __current() const _NOEXCEPT { return __current_; }
64 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator __begin() const _NOEXCEPT { return __current_; }
65 _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 __static_bounded_iter() = default;
96
97 __static_bounded_iter(__static_bounded_iter const&) = default;
98 __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_CONSTEXPR __static_bounded_iter(__static_bounded_iter<_OtherIterator, _Size> const& __other) _NOEXCEPT
107 : __storage_(__other.__storage_.__current(), __other.__storage_.__begin()) {}
108
109 // Assign a bounded iterator to another one, rebinding the bounds of the iterator as well.
110 __static_bounded_iter& operator=(__static_bounded_iter const&) = default;
111 __static_bounded_iter& operator=(__static_bounded_iter&&) = default;
112
113private:
114 // Create an iterator wrapping the given iterator, and whose bounds are described
115 // by the provided [begin, begin + _Size] range.
116
117 _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __static_bounded_iter(_Iterator __current, _Iterator __begin)
118 : __storage_(__current, __begin) {
119 _LIBCPP_ASSERT_INTERNAL(
120 __begin <= __current, "__static_bounded_iter(current, begin): current and begin are inconsistent");
121 _LIBCPP_ASSERT_INTERNAL(
122 __current <= __end(), "__static_bounded_iter(current, begin): current and (begin + Size) are inconsistent");
123 }
124
125 template <size_t _Sz, class _It>
126 friend _LIBCPP_CONSTEXPR __static_bounded_iter<_It, _Sz> __make_static_bounded_iter(_It, _It);
127
128public:
129 // Dereference and indexing operations.
130 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT {
131 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
132 __current() != __end(), "__static_bounded_iter::operator*: Attempt to dereference an iterator at the end");
133 return *__current();
134 }
135
136 _LIBCPP_CONSTEXPR_SINCE_CXX14 pointer operator->() const _NOEXCEPT {
137 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
138 __current() != __end(), "__static_bounded_iter::operator->: Attempt to dereference an iterator at the end");
139 return std::__to_address(__current());
140 }
141
142 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator[](difference_type __n) const _NOEXCEPT {
143 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
144 __n >= __begin() - __current(),
145 "__static_bounded_iter::operator[]: Attempt to index an iterator past the start");
146 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
147 __n < __end() - __current(),
148 "__static_bounded_iter::operator[]: Attempt to index an iterator at or past the end");
149 return __current()[__n];
150 }
151
152 // Arithmetic operations.
153 //
154 // These operations check that the iterator remains within `[begin, end]`.
155 _LIBCPP_CONSTEXPR_SINCE_CXX14 __static_bounded_iter& operator++() _NOEXCEPT {
156 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
157 __current() != __end(), "__static_bounded_iter::operator++: Attempt to advance an iterator past the end");
158 ++__current();
159 return *this;
160 }
161 _LIBCPP_CONSTEXPR_SINCE_CXX14 __static_bounded_iter operator++(int) _NOEXCEPT {
162 __static_bounded_iter __tmp(*this);
163 ++*this;
164 return __tmp;
165 }
166
167 _LIBCPP_CONSTEXPR_SINCE_CXX14 __static_bounded_iter& operator--() _NOEXCEPT {
168 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
169 __current() != __begin(), "__static_bounded_iter::operator--: Attempt to rewind an iterator past the start");
170 --__current();
171 return *this;
172 }
173 _LIBCPP_CONSTEXPR_SINCE_CXX14 __static_bounded_iter operator--(int) _NOEXCEPT {
174 __static_bounded_iter __tmp(*this);
175 --*this;
176 return __tmp;
177 }
178
179 _LIBCPP_CONSTEXPR_SINCE_CXX14 __static_bounded_iter& operator+=(difference_type __n) _NOEXCEPT {
180 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
181 __n >= __begin() - __current(),
182 "__static_bounded_iter::operator+=: Attempt to rewind an iterator past the start");
183 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
184 __n <= __end() - __current(), "__static_bounded_iter::operator+=: Attempt to advance an iterator past the end");
185 __current() += __n;
186 return *this;
187 }
188 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __static_bounded_iter
189 operator+(__static_bounded_iter const& __self, difference_type __n) _NOEXCEPT {
190 __static_bounded_iter __tmp(__self);
191 __tmp += __n;
192 return __tmp;
193 }
194 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __static_bounded_iter
195 operator+(difference_type __n, __static_bounded_iter const& __self) _NOEXCEPT {
196 __static_bounded_iter __tmp(__self);
197 __tmp += __n;
198 return __tmp;
199 }
200
201 _LIBCPP_CONSTEXPR_SINCE_CXX14 __static_bounded_iter& operator-=(difference_type __n) _NOEXCEPT {
202 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
203 __n <= __current() - __begin(),
204 "__static_bounded_iter::operator-=: Attempt to rewind an iterator past the start");
205 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
206 __n >= __current() - __end(), "__static_bounded_iter::operator-=: Attempt to advance an iterator past the end");
207 __current() -= __n;
208 return *this;
209 }
210 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __static_bounded_iter
211 operator-(__static_bounded_iter const& __self, difference_type __n) _NOEXCEPT {
212 __static_bounded_iter __tmp(__self);
213 __tmp -= __n;
214 return __tmp;
215 }
216 [[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX14 friend difference_type
217 operator-(__static_bounded_iter const& __x, __static_bounded_iter const& __y) _NOEXCEPT {
218 return __x.__current() - __y.__current();
219 }
220
221 // Comparison operations.
222 //
223 // These operations do not check whether the iterators are within their bounds.
224 // The valid range for each iterator is also not considered as part of the comparison,
225 // i.e. two iterators pointing to the same location will be considered equal even
226 // if they have different validity ranges.
227 _LIBCPP_CONSTEXPR friend bool
228 operator==(__static_bounded_iter const& __x, __static_bounded_iter const& __y) _NOEXCEPT {
229 return __x.__current() == __y.__current();
230 }
231
232#if _LIBCPP_STD_VER <= 17
233 _LIBCPP_CONSTEXPR friend bool
234 operator!=(__static_bounded_iter const& __x, __static_bounded_iter const& __y) _NOEXCEPT {
235 return __x.__current() != __y.__current();
236 }
237
238 _LIBCPP_CONSTEXPR friend bool
239 operator<(__static_bounded_iter const& __x, __static_bounded_iter const& __y) _NOEXCEPT {
240 return __x.__current() < __y.__current();
241 }
242 _LIBCPP_CONSTEXPR friend bool
243 operator>(__static_bounded_iter const& __x, __static_bounded_iter const& __y) _NOEXCEPT {
244 return __x.__current() > __y.__current();
245 }
246 _LIBCPP_CONSTEXPR friend bool
247 operator<=(__static_bounded_iter const& __x, __static_bounded_iter const& __y) _NOEXCEPT {
248 return __x.__current() <= __y.__current();
249 }
250 _LIBCPP_CONSTEXPR friend bool
251 operator>=(__static_bounded_iter const& __x, __static_bounded_iter const& __y) _NOEXCEPT {
252 return __x.__current() >= __y.__current();
253 }
254
255#else
256 constexpr friend strong_ordering
257 operator<=>(__static_bounded_iter const& __x, __static_bounded_iter const& __y) noexcept {
258 if constexpr (three_way_comparable<_Iterator, strong_ordering>) {
259 return __x.__current() <=> __y.__current();
260 } else {
261 if (__x.__current() < __y.__current())
262 return strong_ordering::less;
263
264 if (__x.__current() == __y.__current())
265 return strong_ordering::equal;
266
267 return strong_ordering::greater;
268 }
269 }
270#endif // _LIBCPP_STD_VER >= 20
271
272private:
273 template <class>
274 friend struct pointer_traits;
275 template <class, size_t>
276 friend struct __static_bounded_iter;
277 __static_bounded_iter_storage<_Iterator, _Size> __storage_;
278
279 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator& __current() _NOEXCEPT { return __storage_.__current(); }
280 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator __current() const _NOEXCEPT { return __storage_.__current(); }
281 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator __begin() const _NOEXCEPT { return __storage_.__begin(); }
282 _LIBCPP_CONSTEXPR_SINCE_CXX14 _Iterator __end() const _NOEXCEPT { return __storage_.__end(); }
283};
284
285template <size_t _Size, class _It>
286_LIBCPP_CONSTEXPR __static_bounded_iter<_It, _Size> __make_static_bounded_iter(_It __it, _It __begin) {
287 return __static_bounded_iter<_It, _Size>(std::move(__it), std::move(__begin));
288}
289
290#if _LIBCPP_STD_VER <= 17
291template <class _Iterator, size_t _Size>
292struct __libcpp_is_contiguous_iterator<__static_bounded_iter<_Iterator, _Size> > : true_type {};
293#endif
294
295template <class _Iterator, size_t _Size>
296struct pointer_traits<__static_bounded_iter<_Iterator, _Size> > {
297 using pointer = __static_bounded_iter<_Iterator, _Size>;
298 using element_type = typename pointer_traits<_Iterator>::element_type;
299 using difference_type = typename pointer_traits<_Iterator>::difference_type;
300
301 _LIBCPP_CONSTEXPR static element_type* to_address(pointer __it) _NOEXCEPT {
302 return std::__to_address(__it.__current());
303 }
304};
305
306_LIBCPP_END_NAMESPACE_STD
307
308_LIBCPP_POP_MACROS
309
310#endif // _LIBCPP___ITERATOR_STATIC_BOUNDED_ITER_H
311