diff options
author | mikhnenko <mikhnenko@yandex-team.com> | 2024-03-27 10:35:27 +0300 |
---|---|---|
committer | mikhnenko <mikhnenko@yandex-team.com> | 2024-03-27 10:47:39 +0300 |
commit | 9b902baa4a858f2176c82aa0b20f88232f0da0d8 (patch) | |
tree | 7165a551c2244c4b3c28479ac3a3f6d62346ec89 /contrib/libs/cxxsupp/libcxx/include/__ranges | |
parent | a1c989e67e438005fa0c34ed0e910536c8941862 (diff) | |
download | ydb-9b902baa4a858f2176c82aa0b20f88232f0da0d8.tar.gz |
Update libcxx to 10 Oct 2023 dc129d6f715cf83a2072fc8de8b4e4c70bca6935
97ce40d276e44357a49b7a945af841896126dca8
Diffstat (limited to 'contrib/libs/cxxsupp/libcxx/include/__ranges')
6 files changed, 256 insertions, 35 deletions
diff --git a/contrib/libs/cxxsupp/libcxx/include/__ranges/chunk_by_view.h b/contrib/libs/cxxsupp/libcxx/include/__ranges/chunk_by_view.h new file mode 100644 index 0000000000..cfb149b443 --- /dev/null +++ b/contrib/libs/cxxsupp/libcxx/include/__ranges/chunk_by_view.h @@ -0,0 +1,230 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___RANGES_CHUNK_BY_VIEW_H +#define _LIBCPP___RANGES_CHUNK_BY_VIEW_H + +#include <__algorithm/ranges_adjacent_find.h> +#include <__assert> +#include <__concepts/constructible.h> +#include <__config> +#include <__functional/bind_back.h> +#include <__functional/invoke.h> +#include <__functional/not_fn.h> +#include <__functional/reference_wrapper.h> +#include <__iterator/concepts.h> +#include <__iterator/default_sentinel.h> +#include <__iterator/iterator_traits.h> +#include <__iterator/next.h> +#include <__iterator/prev.h> +#include <__memory/addressof.h> +#include <__ranges/access.h> +#include <__ranges/all.h> +#include <__ranges/concepts.h> +#include <__ranges/movable_box.h> +#include <__ranges/non_propagating_cache.h> +#include <__ranges/range_adaptor.h> +#include <__ranges/reverse_view.h> +#include <__ranges/subrange.h> +#include <__ranges/view_interface.h> +#include <__type_traits/conditional.h> +#include <__type_traits/decay.h> +#include <__type_traits/is_nothrow_constructible.h> +#include <__type_traits/is_object.h> +#include <__utility/forward.h> +#include <__utility/in_place.h> +#include <__utility/move.h> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +# pragma GCC system_header +#endif + +_LIBCPP_PUSH_MACROS +#include <__undef_macros> + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if _LIBCPP_STD_VER >= 23 + +namespace ranges { + +template <forward_range _View, indirect_binary_predicate<iterator_t<_View>, iterator_t<_View>> _Pred> + requires view<_View> && is_object_v<_Pred> +class chunk_by_view : public view_interface<chunk_by_view<_View, _Pred>> { + _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View(); + _LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Pred> __pred_; + + // We cache the result of begin() to allow providing an amortized O(1). + using _Cache = __non_propagating_cache<iterator_t<_View>>; + _Cache __cached_begin_; + + class __iterator; + + _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_View> __find_next(iterator_t<_View> __current) { + _LIBCPP_ASSERT_UNCATEGORIZED( + __pred_.__has_value(), "Trying to call __find_next() on a chunk_by_view that does not have a valid predicate."); + + return ranges::next(ranges::adjacent_find(__current, ranges::end(__base_), std::not_fn(std::ref(*__pred_))), + 1, + ranges::end(__base_)); + } + + _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_View> __find_prev(iterator_t<_View> __current) + requires bidirectional_range<_View> + { + _LIBCPP_ASSERT_UNCATEGORIZED( + __current != ranges::begin(__base_), "Trying to call __find_prev() on a begin iterator."); + _LIBCPP_ASSERT_UNCATEGORIZED( + __pred_.__has_value(), "Trying to call __find_prev() on a chunk_by_view that does not have a valid predicate."); + + auto __first = ranges::begin(__base_); + reverse_view __reversed{subrange{__first, __current}}; + auto __reversed_pred = [this]<class _Tp, class _Up>(_Tp&& __x, _Up&& __y) { + return !std::invoke(*__pred_, std::forward<_Up>(__y), std::forward<_Tp>(__x)); + }; + return ranges::prev(ranges::adjacent_find(__reversed, __reversed_pred).base(), 1, std::move(__first)); + } + +public: + _LIBCPP_HIDE_FROM_ABI chunk_by_view() + requires default_initializable<_View> && default_initializable<_Pred> + = default; + + _LIBCPP_HIDE_FROM_ABI constexpr explicit chunk_by_view(_View __base, _Pred __pred) + : __base_(std::move(__base)), __pred_(in_place, std::move(__pred)) {} + + _LIBCPP_HIDE_FROM_ABI constexpr _View base() const& + requires copy_constructible<_View> + { + return __base_; + } + + _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); } + + _LIBCPP_HIDE_FROM_ABI constexpr const _Pred& pred() const { return *__pred_; } + + _LIBCPP_HIDE_FROM_ABI constexpr __iterator begin() { + _LIBCPP_ASSERT_UNCATEGORIZED( + __pred_.__has_value(), "Trying to call begin() on a chunk_by_view that does not have a valid predicate."); + + auto __first = ranges::begin(__base_); + if (!__cached_begin_.__has_value()) { + __cached_begin_.__emplace(__find_next(__first)); + } + return {*this, std::move(__first), *__cached_begin_}; + } + + _LIBCPP_HIDE_FROM_ABI constexpr auto end() { + if constexpr (common_range<_View>) { + return __iterator{*this, ranges::end(__base_), ranges::end(__base_)}; + } else { + return default_sentinel; + } + } +}; + +template <class _Range, class _Pred> +chunk_by_view(_Range&&, _Pred) -> chunk_by_view<views::all_t<_Range>, _Pred>; + +template <forward_range _View, indirect_binary_predicate<iterator_t<_View>, iterator_t<_View>> _Pred> + requires view<_View> && is_object_v<_Pred> +class chunk_by_view<_View, _Pred>::__iterator { + friend chunk_by_view; + + chunk_by_view* __parent_ = nullptr; + _LIBCPP_NO_UNIQUE_ADDRESS iterator_t<_View> __current_ = iterator_t<_View>(); + _LIBCPP_NO_UNIQUE_ADDRESS iterator_t<_View> __next_ = iterator_t<_View>(); + + _LIBCPP_HIDE_FROM_ABI constexpr __iterator( + chunk_by_view& __parent, iterator_t<_View> __current, iterator_t<_View> __next) + : __parent_(std::addressof(__parent)), __current_(__current), __next_(__next) {} + +public: + using value_type = subrange<iterator_t<_View>>; + using difference_type = range_difference_t<_View>; + using iterator_category = input_iterator_tag; + using iterator_concept = conditional_t<bidirectional_range<_View>, bidirectional_iterator_tag, forward_iterator_tag>; + + _LIBCPP_HIDE_FROM_ABI __iterator() = default; + + _LIBCPP_HIDE_FROM_ABI constexpr value_type operator*() const { + _LIBCPP_ASSERT_UNCATEGORIZED(__current_ != __next_, "Trying to dereference past-the-end chunk_by_view iterator."); + return {__current_, __next_}; + } + + _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() { + _LIBCPP_ASSERT_UNCATEGORIZED(__current_ != __next_, "Trying to increment past end chunk_by_view iterator."); + __current_ = __next_; + __next_ = __parent_->__find_next(__current_); + return *this; + } + + _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator++(int) { + auto __tmp = *this; + ++*this; + return __tmp; + } + + _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator--() + requires bidirectional_range<_View> + { + __next_ = __current_; + __current_ = __parent_->__find_prev(__next_); + return *this; + } + + _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator--(int) + requires bidirectional_range<_View> + { + auto __tmp = *this; + --*this; + return __tmp; + } + + _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y) { + return __x.__current_ == __y.__current_; + } + + _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, default_sentinel_t) { + return __x.__current_ == __x.__next_; + } +}; + +namespace views { +namespace __chunk_by { +struct __fn { + template <class _Range, class _Pred> + _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Pred&& __pred) const + noexcept(noexcept(/**/ chunk_by_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred)))) + -> decltype(/*--*/ chunk_by_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))) { + return /*-------------*/ chunk_by_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred)); + } + + template <class _Pred> + requires constructible_from<decay_t<_Pred>, _Pred> + _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Pred&& __pred) const + noexcept(is_nothrow_constructible_v<decay_t<_Pred>, _Pred>) { + return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Pred>(__pred))); + } +}; +} // namespace __chunk_by + +inline namespace __cpo { +inline constexpr auto chunk_by = __chunk_by::__fn{}; +} // namespace __cpo +} // namespace views +} // namespace ranges + +#endif // _LIBCPP_STD_VER >= 23 + +_LIBCPP_END_NAMESPACE_STD + +_LIBCPP_POP_MACROS + +#endif // _LIBCPP___RANGES_CHUNK_BY_VIEW_H diff --git a/contrib/libs/cxxsupp/libcxx/include/__ranges/iota_view.h b/contrib/libs/cxxsupp/libcxx/include/__ranges/iota_view.h index f372688abf..ccf0c7a8e8 100644 --- a/contrib/libs/cxxsupp/libcxx/include/__ranges/iota_view.h +++ b/contrib/libs/cxxsupp/libcxx/include/__ranges/iota_view.h @@ -363,15 +363,14 @@ namespace ranges { (integral<_Start> && integral<_BoundSentinel>) || sized_sentinel_for<_BoundSentinel, _Start> { if constexpr (__integer_like<_Start> && __integer_like<_BoundSentinel>) { - if (__value_ < 0) { - if (__bound_sentinel_ < 0) { - return std::__to_unsigned_like(-__value_) - std::__to_unsigned_like(-__bound_sentinel_); - } - return std::__to_unsigned_like(__bound_sentinel_) + std::__to_unsigned_like(-__value_); - } - return std::__to_unsigned_like(__bound_sentinel_) - std::__to_unsigned_like(__value_); + return (__value_ < 0) + ? ((__bound_sentinel_ < 0) + ? std::__to_unsigned_like(-__value_) - std::__to_unsigned_like(-__bound_sentinel_) + : std::__to_unsigned_like(__bound_sentinel_) + std::__to_unsigned_like(-__value_)) + : std::__to_unsigned_like(__bound_sentinel_) - std::__to_unsigned_like(__value_); + } else { + return std::__to_unsigned_like(__bound_sentinel_ - __value_); } - return std::__to_unsigned_like(__bound_sentinel_ - __value_); } }; diff --git a/contrib/libs/cxxsupp/libcxx/include/__ranges/movable_box.h b/contrib/libs/cxxsupp/libcxx/include/__ranges/movable_box.h index 8b3716a06c..6615533d37 100644 --- a/contrib/libs/cxxsupp/libcxx/include/__ranges/movable_box.h +++ b/contrib/libs/cxxsupp/libcxx/include/__ranges/movable_box.h @@ -26,6 +26,9 @@ # pragma GCC system_header #endif +_LIBCPP_PUSH_MACROS +#include <__undef_macros> + _LIBCPP_BEGIN_NAMESPACE_STD #if _LIBCPP_STD_VER >= 20 @@ -203,4 +206,6 @@ public: _LIBCPP_END_NAMESPACE_STD +_LIBCPP_POP_MACROS + #endif // _LIBCPP___RANGES_MOVABLE_BOX_H diff --git a/contrib/libs/cxxsupp/libcxx/include/__ranges/repeat_view.h b/contrib/libs/cxxsupp/libcxx/include/__ranges/repeat_view.h index fddf4baac8..459a1e2296 100644 --- a/contrib/libs/cxxsupp/libcxx/include/__ranges/repeat_view.h +++ b/contrib/libs/cxxsupp/libcxx/include/__ranges/repeat_view.h @@ -82,13 +82,13 @@ public: requires copy_constructible<_Tp> : __value_(in_place, __value), __bound_(__bound_sentinel) { if constexpr (!same_as<_Bound, unreachable_sentinel_t>) - _LIBCPP_ASSERT(__bound_ >= 0, "The value of bound must be greater than or equal to 0"); + _LIBCPP_ASSERT_UNCATEGORIZED(__bound_ >= 0, "The value of bound must be greater than or equal to 0"); } _LIBCPP_HIDE_FROM_ABI constexpr explicit repeat_view(_Tp&& __value, _Bound __bound_sentinel = _Bound()) : __value_(in_place, std::move(__value)), __bound_(__bound_sentinel) { if constexpr (!same_as<_Bound, unreachable_sentinel_t>) - _LIBCPP_ASSERT(__bound_ >= 0, "The value of bound must be greater than or equal to 0"); + _LIBCPP_ASSERT_UNCATEGORIZED(__bound_ >= 0, "The value of bound must be greater than or equal to 0"); } template <class... _TpArgs, class... _BoundArgs> @@ -98,7 +98,7 @@ public: : __value_(in_place, std::make_from_tuple<_Tp>(std::move(__value_args))), __bound_(std::make_from_tuple<_Bound>(std::move(__bound_args))) { if constexpr (!same_as<_Bound, unreachable_sentinel_t>) - _LIBCPP_ASSERT( + _LIBCPP_ASSERT_UNCATEGORIZED( __bound_ >= 0, "The behavior is undefined if Bound is not unreachable_sentinel_t and bound is negative"); } @@ -161,7 +161,7 @@ public: _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator--() { if constexpr (!same_as<_Bound, unreachable_sentinel_t>) - _LIBCPP_ASSERT(__current_ > 0, "The value of bound must be greater than or equal to 0"); + _LIBCPP_ASSERT_UNCATEGORIZED(__current_ > 0, "The value of bound must be greater than or equal to 0"); --__current_; return *this; } @@ -174,14 +174,14 @@ public: _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator+=(difference_type __n) { if constexpr (!same_as<_Bound, unreachable_sentinel_t>) - _LIBCPP_ASSERT(__current_ + __n >= 0, "The value of bound must be greater than or equal to 0"); + _LIBCPP_ASSERT_UNCATEGORIZED(__current_ + __n >= 0, "The value of bound must be greater than or equal to 0"); __current_ += __n; return *this; } _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator-=(difference_type __n) { if constexpr (!same_as<_Bound, unreachable_sentinel_t>) - _LIBCPP_ASSERT(__current_ - __n >= 0, "The value of bound must be greater than or equal to 0"); + _LIBCPP_ASSERT_UNCATEGORIZED(__current_ - __n >= 0, "The value of bound must be greater than or equal to 0"); __current_ -= __n; return *this; } diff --git a/contrib/libs/cxxsupp/libcxx/include/__ranges/take_while_view.h b/contrib/libs/cxxsupp/libcxx/include/__ranges/take_while_view.h index b4bdd1865d..a6f7f80ca7 100644 --- a/contrib/libs/cxxsupp/libcxx/include/__ranges/take_while_view.h +++ b/contrib/libs/cxxsupp/libcxx/include/__ranges/take_while_view.h @@ -41,18 +41,6 @@ _LIBCPP_BEGIN_NAMESPACE_STD namespace ranges { -// The spec uses the unnamed requirement inside the `begin` and `end` member functions: -// constexpr auto begin() const -// requires range<const V> && indirect_unary_predicate<const Pred, iterator_t<const V>> -// However, due to a clang-14 and clang-15 bug, the above produces a hard error when `const V` is not a range. -// The workaround is to create a named concept and use the concept instead. -// As of take_while_view is implemented, the clang-trunk has already fixed the bug. -// It is OK to remove the workaround once our CI no longer uses clang-14, clang-15 based compilers, -// because we don't actually expect a lot of vendors to ship a new libc++ with an old clang. -template <class _View, class _Pred> -concept __take_while_const_is_range = - range<const _View> && indirect_unary_predicate<const _Pred, iterator_t<const _View>>; - template <view _View, class _Pred> requires input_range<_View> && is_object_v<_Pred> && indirect_unary_predicate<const _Pred, iterator_t<_View>> class take_while_view : public view_interface<take_while_view<_View, _Pred>> { @@ -87,7 +75,7 @@ public: } _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const - requires __take_while_const_is_range<_View, _Pred> + requires range<const _View> && indirect_unary_predicate<const _Pred, iterator_t<const _View>> { return ranges::begin(__base_); } @@ -99,7 +87,7 @@ public: } _LIBCPP_HIDE_FROM_ABI constexpr auto end() const - requires __take_while_const_is_range<_View, _Pred> + requires range<const _View> && indirect_unary_predicate<const _Pred, iterator_t<const _View>> { return __sentinel</*_Const=*/true>(ranges::end(__base_), std::addressof(*__pred_)); } diff --git a/contrib/libs/cxxsupp/libcxx/include/__ranges/to.h b/contrib/libs/cxxsupp/libcxx/include/__ranges/to.h index 95c300bfa6..a519662e75 100644 --- a/contrib/libs/cxxsupp/libcxx/include/__ranges/to.h +++ b/contrib/libs/cxxsupp/libcxx/include/__ranges/to.h @@ -44,14 +44,13 @@ _LIBCPP_BEGIN_NAMESPACE_STD namespace ranges { -// TODO(clang-15): in the Standard, it's a `constexpr bool` variable, not a concept, but constexpr variables don't -// short-circuit properly on Clang 15 (fixed in later versions), so use a concept as a workaround. template <class _Container> -concept __reservable_container = sized_range<_Container> && requires(_Container& __c, range_size_t<_Container> __n) { - __c.reserve(__n); - { __c.capacity() } -> same_as<decltype(__n)>; - { __c.max_size() } -> same_as<decltype(__n)>; -}; +constexpr bool __reservable_container = + sized_range<_Container> && requires(_Container& __c, range_size_t<_Container> __n) { + __c.reserve(__n); + { __c.capacity() } -> same_as<decltype(__n)>; + { __c.max_size() } -> same_as<decltype(__n)>; + }; template <class _Container, class _Ref> constexpr bool __container_insertable = requires(_Container& __c, _Ref&& __ref) { |