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