Path: blob/main/contrib/llvm-project/libcxx/include/__iterator/bounded_iter.h
35233 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___ITERATOR_BOUNDED_ITER_H10#define _LIBCPP___ITERATOR_BOUNDED_ITER_H1112#include <__assert>13#include <__compare/ordering.h>14#include <__compare/three_way_comparable.h>15#include <__config>16#include <__iterator/iterator_traits.h>17#include <__memory/pointer_traits.h>18#include <__type_traits/enable_if.h>19#include <__type_traits/integral_constant.h>20#include <__type_traits/is_convertible.h>21#include <__utility/move.h>2223#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)24# pragma GCC system_header25#endif2627_LIBCPP_PUSH_MACROS28#include <__undef_macros>2930_LIBCPP_BEGIN_NAMESPACE_STD3132// Iterator wrapper that carries the valid range it is allowed to access.33//34// This is a simple iterator wrapper for contiguous iterators that points35// within a [begin, end] range and carries these bounds with it. The iterator36// ensures that it is pointing within [begin, end) range when it is37// dereferenced. It also ensures that it is never iterated outside of38// [begin, end]. This is important for two reasons:39//40// 1. It allows `operator*` and `operator++` bounds checks to be `iter != end`.41// This is both less for the optimizer to prove, and aligns with how callers42// typically use iterators.43//44// 2. Advancing an iterator out of bounds is undefined behavior (see the table45// in [input.iterators]). In particular, when the underlying iterator is a46// pointer, it is undefined at the language level (see [expr.add]). If47// bounded iterators exhibited this undefined behavior, we risk compiler48// optimizations deleting non-redundant bounds checks.49template <class _Iterator, class = __enable_if_t< __libcpp_is_contiguous_iterator<_Iterator>::value > >50struct __bounded_iter {51using value_type = typename iterator_traits<_Iterator>::value_type;52using difference_type = typename iterator_traits<_Iterator>::difference_type;53using pointer = typename iterator_traits<_Iterator>::pointer;54using reference = typename iterator_traits<_Iterator>::reference;55using iterator_category = typename iterator_traits<_Iterator>::iterator_category;56#if _LIBCPP_STD_VER >= 2057using iterator_concept = contiguous_iterator_tag;58#endif5960// Create a singular iterator.61//62// Such an iterator points past the end of an empty span, so it is not dereferenceable.63// Observing operations like comparison and assignment are valid.64_LIBCPP_HIDE_FROM_ABI __bounded_iter() = default;6566_LIBCPP_HIDE_FROM_ABI __bounded_iter(__bounded_iter const&) = default;67_LIBCPP_HIDE_FROM_ABI __bounded_iter(__bounded_iter&&) = default;6869template <class _OtherIterator, __enable_if_t< is_convertible<_OtherIterator, _Iterator>::value, int> = 0>70_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __bounded_iter(__bounded_iter<_OtherIterator> const& __other) _NOEXCEPT71: __current_(__other.__current_),72__begin_(__other.__begin_),73__end_(__other.__end_) {}7475// Assign a bounded iterator to another one, rebinding the bounds of the iterator as well.76_LIBCPP_HIDE_FROM_ABI __bounded_iter& operator=(__bounded_iter const&) = default;77_LIBCPP_HIDE_FROM_ABI __bounded_iter& operator=(__bounded_iter&&) = default;7879private:80// Create an iterator wrapping the given iterator, and whose bounds are described81// by the provided [begin, end] range.82//83// The constructor does not check whether the resulting iterator is within its bounds. It is a84// responsibility of the container to ensure that the given bounds are valid.85//86// Since it is non-standard for iterators to have this constructor, __bounded_iter must87// be created via `std::__make_bounded_iter`.88_LIBCPP_HIDE_FROM_ABI89_LIBCPP_CONSTEXPR_SINCE_CXX14 explicit __bounded_iter(_Iterator __current, _Iterator __begin, _Iterator __end)90: __current_(__current), __begin_(__begin), __end_(__end) {91_LIBCPP_ASSERT_INTERNAL(92__begin <= __current, "__bounded_iter(current, begin, end): current and begin are inconsistent");93_LIBCPP_ASSERT_INTERNAL(94__current <= __end, "__bounded_iter(current, begin, end): current and end are inconsistent");95}9697template <class _It>98friend _LIBCPP_CONSTEXPR __bounded_iter<_It> __make_bounded_iter(_It, _It, _It);99100public:101// Dereference and indexing operations.102//103// These operations check that the iterator is dereferenceable. Since the class invariant is104// that the iterator is always within `[begin, end]`, we only need to check it's not pointing to105// `end`. This is easier for the optimizer because it aligns with the `iter != container.end()`106// checks that typical callers already use (see107// https://github.com/llvm/llvm-project/issues/78829).108_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator*() const _NOEXCEPT {109_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(110__current_ != __end_, "__bounded_iter::operator*: Attempt to dereference an iterator at the end");111return *__current_;112}113114_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pointer operator->() const _NOEXCEPT {115_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(116__current_ != __end_, "__bounded_iter::operator->: Attempt to dereference an iterator at the end");117return std::__to_address(__current_);118}119120_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 reference operator[](difference_type __n) const _NOEXCEPT {121_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(122__n >= __begin_ - __current_, "__bounded_iter::operator[]: Attempt to index an iterator past the start");123_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(124__n < __end_ - __current_, "__bounded_iter::operator[]: Attempt to index an iterator at or past the end");125return __current_[__n];126}127128// Arithmetic operations.129//130// These operations check that the iterator remains within `[begin, end]`.131_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __bounded_iter& operator++() _NOEXCEPT {132_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(133__current_ != __end_, "__bounded_iter::operator++: Attempt to advance an iterator past the end");134++__current_;135return *this;136}137_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __bounded_iter operator++(int) _NOEXCEPT {138__bounded_iter __tmp(*this);139++*this;140return __tmp;141}142143_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __bounded_iter& operator--() _NOEXCEPT {144_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(145__current_ != __begin_, "__bounded_iter::operator--: Attempt to rewind an iterator past the start");146--__current_;147return *this;148}149_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __bounded_iter operator--(int) _NOEXCEPT {150__bounded_iter __tmp(*this);151--*this;152return __tmp;153}154155_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __bounded_iter& operator+=(difference_type __n) _NOEXCEPT {156_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(157__n >= __begin_ - __current_, "__bounded_iter::operator+=: Attempt to rewind an iterator past the start");158_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(159__n <= __end_ - __current_, "__bounded_iter::operator+=: Attempt to advance an iterator past the end");160__current_ += __n;161return *this;162}163_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __bounded_iter164operator+(__bounded_iter const& __self, difference_type __n) _NOEXCEPT {165__bounded_iter __tmp(__self);166__tmp += __n;167return __tmp;168}169_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __bounded_iter170operator+(difference_type __n, __bounded_iter const& __self) _NOEXCEPT {171__bounded_iter __tmp(__self);172__tmp += __n;173return __tmp;174}175176_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 __bounded_iter& operator-=(difference_type __n) _NOEXCEPT {177_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(178__n <= __current_ - __begin_, "__bounded_iter::operator-=: Attempt to rewind an iterator past the start");179_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(180__n >= __current_ - __end_, "__bounded_iter::operator-=: Attempt to advance an iterator past the end");181__current_ -= __n;182return *this;183}184_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend __bounded_iter185operator-(__bounded_iter const& __self, difference_type __n) _NOEXCEPT {186__bounded_iter __tmp(__self);187__tmp -= __n;188return __tmp;189}190_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 friend difference_type191operator-(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT {192return __x.__current_ - __y.__current_;193}194195// Comparison operations.196//197// These operations do not check whether the iterators are within their bounds.198// The valid range for each iterator is also not considered as part of the comparison,199// i.e. two iterators pointing to the same location will be considered equal even200// if they have different validity ranges.201_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool202operator==(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT {203return __x.__current_ == __y.__current_;204}205206#if _LIBCPP_STD_VER <= 17207_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool208operator!=(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT {209return __x.__current_ != __y.__current_;210}211#endif212213// TODO(mordante) disable these overloads in the LLVM 20 release.214_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool215operator<(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT {216return __x.__current_ < __y.__current_;217}218_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool219operator>(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT {220return __x.__current_ > __y.__current_;221}222_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool223operator<=(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT {224return __x.__current_ <= __y.__current_;225}226_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool227operator>=(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT {228return __x.__current_ >= __y.__current_;229}230231#if _LIBCPP_STD_VER >= 20232_LIBCPP_HIDE_FROM_ABI constexpr friend strong_ordering233operator<=>(__bounded_iter const& __x, __bounded_iter const& __y) noexcept {234if constexpr (three_way_comparable<_Iterator, strong_ordering>) {235return __x.__current_ <=> __y.__current_;236} else {237if (__x.__current_ < __y.__current_)238return strong_ordering::less;239240if (__x.__current_ == __y.__current_)241return strong_ordering::equal;242243return strong_ordering::greater;244}245}246#endif // _LIBCPP_STD_VER >= 20247248private:249template <class>250friend struct pointer_traits;251template <class, class>252friend struct __bounded_iter;253_Iterator __current_; // current iterator254_Iterator __begin_, __end_; // valid range represented as [begin, end]255};256257template <class _It>258_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __bounded_iter<_It> __make_bounded_iter(_It __it, _It __begin, _It __end) {259return __bounded_iter<_It>(std::move(__it), std::move(__begin), std::move(__end));260}261262#if _LIBCPP_STD_VER <= 17263template <class _Iterator>264struct __libcpp_is_contiguous_iterator<__bounded_iter<_Iterator> > : true_type {};265#endif266267template <class _Iterator>268struct pointer_traits<__bounded_iter<_Iterator> > {269using pointer = __bounded_iter<_Iterator>;270using element_type = typename pointer_traits<_Iterator>::element_type;271using difference_type = typename pointer_traits<_Iterator>::difference_type;272273_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR static element_type* to_address(pointer __it) _NOEXCEPT {274return std::__to_address(__it.__current_);275}276};277278_LIBCPP_END_NAMESPACE_STD279280_LIBCPP_POP_MACROS281282#endif // _LIBCPP___ITERATOR_BOUNDED_ITER_H283284285