Path: blob/main/contrib/llvm-project/libcxx/include/__ranges/drop_view.h
35236 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___RANGES_DROP_VIEW_H10#define _LIBCPP___RANGES_DROP_VIEW_H1112#include <__algorithm/min.h>13#include <__assert>14#include <__concepts/constructible.h>15#include <__concepts/convertible_to.h>16#include <__config>17#include <__functional/bind_back.h>18#include <__fwd/span.h>19#include <__fwd/string_view.h>20#include <__iterator/concepts.h>21#include <__iterator/distance.h>22#include <__iterator/iterator_traits.h>23#include <__iterator/next.h>24#include <__ranges/access.h>25#include <__ranges/all.h>26#include <__ranges/concepts.h>27#include <__ranges/empty_view.h>28#include <__ranges/enable_borrowed_range.h>29#include <__ranges/iota_view.h>30#include <__ranges/non_propagating_cache.h>31#include <__ranges/range_adaptor.h>32#include <__ranges/repeat_view.h>33#include <__ranges/size.h>34#include <__ranges/subrange.h>35#include <__ranges/view_interface.h>36#include <__type_traits/conditional.h>37#include <__type_traits/decay.h>38#include <__type_traits/is_nothrow_constructible.h>39#include <__type_traits/make_unsigned.h>40#include <__type_traits/remove_cvref.h>41#include <__utility/auto_cast.h>42#include <__utility/forward.h>43#include <__utility/move.h>44#include <cstddef>4546#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)47# pragma GCC system_header48#endif4950_LIBCPP_PUSH_MACROS51#include <__undef_macros>5253_LIBCPP_BEGIN_NAMESPACE_STD5455#if _LIBCPP_STD_VER >= 205657namespace ranges {58template <view _View>59class drop_view : public view_interface<drop_view<_View>> {60// We cache begin() whenever ranges::next is not guaranteed O(1) to provide an61// amortized O(1) begin() method. If this is an input_range, then we cannot cache62// begin because begin is not equality preserving.63// Note: drop_view<input-range>::begin() is still trivially amortized O(1) because64// one can't call begin() on it more than once.65static constexpr bool _UseCache = forward_range<_View> && !(random_access_range<_View> && sized_range<_View>);66using _Cache = _If<_UseCache, __non_propagating_cache<iterator_t<_View>>, __empty_cache>;67_LIBCPP_NO_UNIQUE_ADDRESS _Cache __cached_begin_ = _Cache();68range_difference_t<_View> __count_ = 0;69_View __base_ = _View();7071public:72_LIBCPP_HIDE_FROM_ABI drop_view()73requires default_initializable<_View>74= default;7576_LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX2377drop_view(_View __base, range_difference_t<_View> __count)78: __count_(__count), __base_(std::move(__base)) {79_LIBCPP_ASSERT_UNCATEGORIZED(__count_ >= 0, "count must be greater than or equal to zero.");80}8182_LIBCPP_HIDE_FROM_ABI constexpr _View base() const&83requires copy_constructible<_View>84{85return __base_;86}87_LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }8889_LIBCPP_HIDE_FROM_ABI constexpr auto begin()90requires(!(__simple_view<_View> && random_access_range<const _View> && sized_range<const _View>))91{92if constexpr (random_access_range<_View> && sized_range<_View>) {93const auto __dist = std::min(ranges::distance(__base_), __count_);94return ranges::begin(__base_) + __dist;95}96if constexpr (_UseCache)97if (__cached_begin_.__has_value())98return *__cached_begin_;99100auto __tmp = ranges::next(ranges::begin(__base_), __count_, ranges::end(__base_));101if constexpr (_UseCache)102__cached_begin_.__emplace(__tmp);103return __tmp;104}105106_LIBCPP_HIDE_FROM_ABI constexpr auto begin() const107requires random_access_range<const _View> && sized_range<const _View>108{109const auto __dist = std::min(ranges::distance(__base_), __count_);110return ranges::begin(__base_) + __dist;111}112113_LIBCPP_HIDE_FROM_ABI constexpr auto end()114requires(!__simple_view<_View>)115{116return ranges::end(__base_);117}118119_LIBCPP_HIDE_FROM_ABI constexpr auto end() const120requires range<const _View>121{122return ranges::end(__base_);123}124125_LIBCPP_HIDE_FROM_ABI static constexpr auto __size(auto& __self) {126const auto __s = ranges::size(__self.__base_);127const auto __c = static_cast<decltype(__s)>(__self.__count_);128return __s < __c ? 0 : __s - __c;129}130131_LIBCPP_HIDE_FROM_ABI constexpr auto size()132requires sized_range<_View>133{134return __size(*this);135}136137_LIBCPP_HIDE_FROM_ABI constexpr auto size() const138requires sized_range<const _View>139{140return __size(*this);141}142};143144template <class _Range>145drop_view(_Range&&, range_difference_t<_Range>) -> drop_view<views::all_t<_Range>>;146147template <class _Tp>148inline constexpr bool enable_borrowed_range<drop_view<_Tp>> = enable_borrowed_range<_Tp>;149150namespace views {151namespace __drop {152153template <class _Tp>154inline constexpr bool __is_empty_view = false;155156template <class _Tp>157inline constexpr bool __is_empty_view<empty_view<_Tp>> = true;158159template <class _Tp>160inline constexpr bool __is_passthrough_specialization = false;161162template <class _Tp, size_t _Extent>163inline constexpr bool __is_passthrough_specialization<span<_Tp, _Extent>> = true;164165template <class _CharT, class _Traits>166inline constexpr bool __is_passthrough_specialization<basic_string_view<_CharT, _Traits>> = true;167168template <class _Np, class _Bound>169inline constexpr bool __is_passthrough_specialization<iota_view<_Np, _Bound>> = true;170171template <class _Iter, class _Sent, subrange_kind _Kind>172inline constexpr bool __is_passthrough_specialization<subrange<_Iter, _Sent, _Kind>> =173!subrange<_Iter, _Sent, _Kind>::_StoreSize;174175template <class _Tp>176inline constexpr bool __is_subrange_specialization_with_store_size = false;177178template <class _Iter, class _Sent, subrange_kind _Kind>179inline constexpr bool __is_subrange_specialization_with_store_size<subrange<_Iter, _Sent, _Kind>> =180subrange<_Iter, _Sent, _Kind>::_StoreSize;181182template <class _Tp>183struct __passthrough_type;184185template <class _Tp, size_t _Extent>186struct __passthrough_type<span<_Tp, _Extent>> {187using type = span<_Tp>;188};189190template <class _CharT, class _Traits>191struct __passthrough_type<basic_string_view<_CharT, _Traits>> {192using type = basic_string_view<_CharT, _Traits>;193};194195template <class _Np, class _Bound>196struct __passthrough_type<iota_view<_Np, _Bound>> {197using type = iota_view<_Np, _Bound>;198};199200template <class _Iter, class _Sent, subrange_kind _Kind>201struct __passthrough_type<subrange<_Iter, _Sent, _Kind>> {202using type = subrange<_Iter, _Sent, _Kind>;203};204205template <class _Tp>206using __passthrough_type_t = typename __passthrough_type<_Tp>::type;207208struct __fn {209// [range.drop.overview]: the `empty_view` case.210template <class _Range, convertible_to<range_difference_t<_Range>> _Np>211requires __is_empty_view<remove_cvref_t<_Range>>212[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Np&&) const213noexcept(noexcept(_LIBCPP_AUTO_CAST(std::forward<_Range>(__range))))214-> decltype(_LIBCPP_AUTO_CAST(std::forward<_Range>(__range))) {215return _LIBCPP_AUTO_CAST(std::forward<_Range>(__range));216}217218// [range.drop.overview]: the `span | basic_string_view | iota_view | subrange (StoreSize == false)` case.219template <class _Range,220convertible_to<range_difference_t<_Range>> _Np,221class _RawRange = remove_cvref_t<_Range>,222class _Dist = range_difference_t<_Range>>223requires(!__is_empty_view<_RawRange> && random_access_range<_RawRange> && sized_range<_RawRange> &&224__is_passthrough_specialization<_RawRange>)225[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __rng, _Np&& __n) const226noexcept(noexcept(__passthrough_type_t<_RawRange>(227ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)), ranges::end(__rng))))228-> decltype(__passthrough_type_t<_RawRange>(229// Note: deliberately not forwarding `__rng` to guard against double moves.230ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)),231ranges::end(__rng))) {232return __passthrough_type_t<_RawRange>(233ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)), ranges::end(__rng));234}235236// [range.drop.overview]: the `subrange (StoreSize == true)` case.237template <class _Range,238convertible_to<range_difference_t<_Range>> _Np,239class _RawRange = remove_cvref_t<_Range>,240class _Dist = range_difference_t<_Range>>241requires(!__is_empty_view<_RawRange> && random_access_range<_RawRange> && sized_range<_RawRange> &&242__is_subrange_specialization_with_store_size<_RawRange>)243[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __rng, _Np&& __n) const noexcept(noexcept(244_RawRange(ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)),245ranges::end(__rng),246std::__to_unsigned_like(ranges::distance(__rng) -247std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n))))))248-> decltype(_RawRange(249// Note: deliberately not forwarding `__rng` to guard against double moves.250ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)),251ranges::end(__rng),252std::__to_unsigned_like(ranges::distance(__rng) -253std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n))))) {254// Introducing local variables avoids calculating `min` and `distance` twice (at the cost of diverging from the255// expression used in the `noexcept` clause and the return statement).256auto __dist = ranges::distance(__rng);257auto __clamped = std::min<_Dist>(__dist, std::forward<_Np>(__n));258return _RawRange(ranges::begin(__rng) + __clamped, ranges::end(__rng), std::__to_unsigned_like(__dist - __clamped));259}260// clang-format off261#if _LIBCPP_STD_VER >= 23262// [range.drop.overview]: the `repeat_view` "_RawRange models sized_range" case.263template <class _Range,264convertible_to<range_difference_t<_Range>> _Np,265class _RawRange = remove_cvref_t<_Range>,266class _Dist = range_difference_t<_Range>>267requires (__is_repeat_specialization<_RawRange> && sized_range<_RawRange>)268[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Np&& __n) const269noexcept(noexcept(views::repeat(*__range.__value_, ranges::distance(__range) - std::min<_Dist>(ranges::distance(__range), std::forward<_Np>(__n)))))270-> decltype( views::repeat(*__range.__value_, ranges::distance(__range) - std::min<_Dist>(ranges::distance(__range), std::forward<_Np>(__n))))271{ return views::repeat(*__range.__value_, ranges::distance(__range) - std::min<_Dist>(ranges::distance(__range), std::forward<_Np>(__n))); }272273// [range.drop.overview]: the `repeat_view` "otherwise" case.274template <class _Range,275convertible_to<range_difference_t<_Range>> _Np,276class _RawRange = remove_cvref_t<_Range>,277class _Dist = range_difference_t<_Range>>278requires (__is_repeat_specialization<_RawRange> && !sized_range<_RawRange>)279[[nodiscard]] _LIBCPP_HIDE_FROM_ABI280constexpr auto operator()(_Range&& __range, _Np&&) const281noexcept(noexcept(_LIBCPP_AUTO_CAST(std::forward<_Range>(__range))))282-> decltype( _LIBCPP_AUTO_CAST(std::forward<_Range>(__range)))283{ return _LIBCPP_AUTO_CAST(std::forward<_Range>(__range)); }284#endif285// clang-format on286287// [range.drop.overview]: the "otherwise" case.288template <class _Range, convertible_to<range_difference_t<_Range>> _Np, class _RawRange = remove_cvref_t<_Range>>289// Note: without specifically excluding the other cases, GCC sees this overload as ambiguous with the other290// overloads.291requires(!(__is_empty_view<_RawRange> ||292# if _LIBCPP_STD_VER >= 23293__is_repeat_specialization<_RawRange> ||294# endif295(__is_subrange_specialization_with_store_size<_RawRange> && sized_range<_RawRange> &&296random_access_range<_RawRange>) ||297(__is_passthrough_specialization<_RawRange> && sized_range<_RawRange> &&298random_access_range<_RawRange>)))299[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Np&& __n) const300noexcept(noexcept(drop_view(std::forward<_Range>(__range), std::forward<_Np>(__n))))301-> decltype(drop_view(std::forward<_Range>(__range), std::forward<_Np>(__n))) {302return drop_view(std::forward<_Range>(__range), std::forward<_Np>(__n));303}304305template <class _Np>306requires constructible_from<decay_t<_Np>, _Np>307[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Np&& __n) const308noexcept(is_nothrow_constructible_v<decay_t<_Np>, _Np>) {309return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Np>(__n)));310}311};312313} // namespace __drop314315inline namespace __cpo {316inline constexpr auto drop = __drop::__fn{};317} // namespace __cpo318} // namespace views319320} // namespace ranges321322#endif // _LIBCPP_STD_VER >= 20323324_LIBCPP_END_NAMESPACE_STD325326_LIBCPP_POP_MACROS327328#endif // _LIBCPP___RANGES_DROP_VIEW_H329330331