Path: blob/main/contrib/llvm-project/libcxx/include/__ranges/take_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_TAKE_VIEW_H10#define _LIBCPP___RANGES_TAKE_VIEW_H1112#include <__algorithm/min.h>13#include <__algorithm/ranges_min.h>14#include <__assert>15#include <__concepts/constructible.h>16#include <__concepts/convertible_to.h>17#include <__config>18#include <__functional/bind_back.h>19#include <__fwd/span.h>20#include <__fwd/string_view.h>21#include <__iterator/concepts.h>22#include <__iterator/counted_iterator.h>23#include <__iterator/default_sentinel.h>24#include <__iterator/distance.h>25#include <__iterator/iterator_traits.h>26#include <__ranges/access.h>27#include <__ranges/all.h>28#include <__ranges/concepts.h>29#include <__ranges/empty_view.h>30#include <__ranges/enable_borrowed_range.h>31#include <__ranges/iota_view.h>32#include <__ranges/range_adaptor.h>33#include <__ranges/repeat_view.h>34#include <__ranges/size.h>35#include <__ranges/subrange.h>36#include <__ranges/view_interface.h>37#include <__type_traits/decay.h>38#include <__type_traits/is_nothrow_constructible.h>39#include <__type_traits/maybe_const.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 {5859template <view _View>60class take_view : public view_interface<take_view<_View>> {61_LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();62range_difference_t<_View> __count_ = 0;6364template <bool>65class __sentinel;6667public:68_LIBCPP_HIDE_FROM_ABI take_view()69requires default_initializable<_View>70= default;7172_LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX2373take_view(_View __base, range_difference_t<_View> __count)74: __base_(std::move(__base)), __count_(__count) {75_LIBCPP_ASSERT_UNCATEGORIZED(__count >= 0, "count has to be greater than or equal to zero");76}7778_LIBCPP_HIDE_FROM_ABI constexpr _View base() const&79requires copy_constructible<_View>80{81return __base_;82}8384_LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }8586_LIBCPP_HIDE_FROM_ABI constexpr auto begin()87requires(!__simple_view<_View>)88{89if constexpr (sized_range<_View>) {90if constexpr (random_access_range<_View>) {91return ranges::begin(__base_);92} else {93using _DifferenceT = range_difference_t<_View>;94auto __size = size();95return counted_iterator(ranges::begin(__base_), static_cast<_DifferenceT>(__size));96}97} else {98return counted_iterator(ranges::begin(__base_), __count_);99}100}101102_LIBCPP_HIDE_FROM_ABI constexpr auto begin() const103requires range<const _View>104{105if constexpr (sized_range<const _View>) {106if constexpr (random_access_range<const _View>) {107return ranges::begin(__base_);108} else {109using _DifferenceT = range_difference_t<const _View>;110auto __size = size();111return counted_iterator(ranges::begin(__base_), static_cast<_DifferenceT>(__size));112}113} else {114return counted_iterator(ranges::begin(__base_), __count_);115}116}117118_LIBCPP_HIDE_FROM_ABI constexpr auto end()119requires(!__simple_view<_View>)120{121if constexpr (sized_range<_View>) {122if constexpr (random_access_range<_View>) {123return ranges::begin(__base_) + size();124} else {125return default_sentinel;126}127} else {128return __sentinel<false>{ranges::end(__base_)};129}130}131132_LIBCPP_HIDE_FROM_ABI constexpr auto end() const133requires range<const _View>134{135if constexpr (sized_range<const _View>) {136if constexpr (random_access_range<const _View>) {137return ranges::begin(__base_) + size();138} else {139return default_sentinel;140}141} else {142return __sentinel<true>{ranges::end(__base_)};143}144}145146_LIBCPP_HIDE_FROM_ABI constexpr auto size()147requires sized_range<_View>148{149auto __n = ranges::size(__base_);150return ranges::min(__n, static_cast<decltype(__n)>(__count_));151}152153_LIBCPP_HIDE_FROM_ABI constexpr auto size() const154requires sized_range<const _View>155{156auto __n = ranges::size(__base_);157return ranges::min(__n, static_cast<decltype(__n)>(__count_));158}159};160161template <view _View>162template <bool _Const>163class take_view<_View>::__sentinel {164using _Base = __maybe_const<_Const, _View>;165template <bool _OtherConst>166using _Iter = counted_iterator<iterator_t<__maybe_const<_OtherConst, _View>>>;167_LIBCPP_NO_UNIQUE_ADDRESS sentinel_t<_Base> __end_ = sentinel_t<_Base>();168169template <bool>170friend class take_view<_View>::__sentinel;171172public:173_LIBCPP_HIDE_FROM_ABI __sentinel() = default;174175_LIBCPP_HIDE_FROM_ABI constexpr explicit __sentinel(sentinel_t<_Base> __end) : __end_(std::move(__end)) {}176177_LIBCPP_HIDE_FROM_ABI constexpr __sentinel(__sentinel<!_Const> __s)178requires _Const && convertible_to<sentinel_t<_View>, sentinel_t<_Base>>179: __end_(std::move(__s.__end_)) {}180181_LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_Base> base() const { return __end_; }182183_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const _Iter<_Const>& __lhs, const __sentinel& __rhs) {184return __lhs.count() == 0 || __lhs.base() == __rhs.__end_;185}186187template <bool _OtherConst = !_Const>188requires sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>189_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const _Iter<_OtherConst>& __lhs, const __sentinel& __rhs) {190return __lhs.count() == 0 || __lhs.base() == __rhs.__end_;191}192};193194template <class _Range>195take_view(_Range&&, range_difference_t<_Range>) -> take_view<views::all_t<_Range>>;196197template <class _Tp>198inline constexpr bool enable_borrowed_range<take_view<_Tp>> = enable_borrowed_range<_Tp>;199200namespace views {201namespace __take {202203template <class _Tp>204inline constexpr bool __is_empty_view = false;205206template <class _Tp>207inline constexpr bool __is_empty_view<empty_view<_Tp>> = true;208209template <class _Tp>210inline constexpr bool __is_passthrough_specialization = false;211212template <class _Tp, size_t _Extent>213inline constexpr bool __is_passthrough_specialization<span<_Tp, _Extent>> = true;214215template <class _CharT, class _Traits>216inline constexpr bool __is_passthrough_specialization<basic_string_view<_CharT, _Traits>> = true;217218template <class _Iter, class _Sent, subrange_kind _Kind>219inline constexpr bool __is_passthrough_specialization<subrange<_Iter, _Sent, _Kind>> = true;220221template <class _Tp>222inline constexpr bool __is_iota_specialization = false;223224template <class _Np, class _Bound>225inline constexpr bool __is_iota_specialization<iota_view<_Np, _Bound>> = true;226227template <class _Tp>228struct __passthrough_type;229230template <class _Tp, size_t _Extent>231struct __passthrough_type<span<_Tp, _Extent>> {232using type = span<_Tp>;233};234235template <class _CharT, class _Traits>236struct __passthrough_type<basic_string_view<_CharT, _Traits>> {237using type = basic_string_view<_CharT, _Traits>;238};239240template <class _Iter, class _Sent, subrange_kind _Kind>241requires requires { typename subrange<_Iter>; }242struct __passthrough_type<subrange<_Iter, _Sent, _Kind>> {243using type = subrange<_Iter>;244};245246template <class _Tp>247using __passthrough_type_t = typename __passthrough_type<_Tp>::type;248249struct __fn {250// [range.take.overview]: the `empty_view` case.251template <class _Range, convertible_to<range_difference_t<_Range>> _Np>252requires __is_empty_view<remove_cvref_t<_Range>>253[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Np&&) const254noexcept(noexcept(_LIBCPP_AUTO_CAST(std::forward<_Range>(__range))))255-> decltype(_LIBCPP_AUTO_CAST(std::forward<_Range>(__range))) {256return _LIBCPP_AUTO_CAST(std::forward<_Range>(__range));257}258259// [range.take.overview]: the `span | basic_string_view | subrange` case.260template <class _Range,261convertible_to<range_difference_t<_Range>> _Np,262class _RawRange = remove_cvref_t<_Range>,263class _Dist = range_difference_t<_Range>>264requires(!__is_empty_view<_RawRange> && random_access_range<_RawRange> && sized_range<_RawRange> &&265__is_passthrough_specialization<_RawRange>)266[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto267operator()(_Range&& __rng, _Np&& __n) const noexcept(noexcept(__passthrough_type_t<_RawRange>(268ranges::begin(__rng), ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)))))269-> decltype(__passthrough_type_t<_RawRange>(270// Note: deliberately not forwarding `__rng` to guard against double moves.271ranges::begin(__rng),272ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)))) {273return __passthrough_type_t<_RawRange>(274ranges::begin(__rng), ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)));275}276277// [range.take.overview]: the `iota_view` case.278// clang-format off279template <class _Range,280convertible_to<range_difference_t<_Range>> _Np,281class _RawRange = remove_cvref_t<_Range>,282class _Dist = range_difference_t<_Range>>283requires (!__is_empty_view<_RawRange> &&284random_access_range<_RawRange> &&285sized_range<_RawRange> &&286__is_iota_specialization<_RawRange>)287[[nodiscard]] _LIBCPP_HIDE_FROM_ABI288constexpr auto operator()(_Range&& __rng, _Np&& __n) const289noexcept(noexcept(ranges::iota_view(290*ranges::begin(__rng),291*(ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)))292)))293-> decltype( ranges::iota_view(294// Note: deliberately not forwarding `__rng` to guard against double moves.295*ranges::begin(__rng),296*(ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)))297))298{ return ranges::iota_view(299*ranges::begin(__rng),300*(ranges::begin(__rng) + std::min<_Dist>(ranges::distance(__rng), std::forward<_Np>(__n)))301); }302303#if _LIBCPP_STD_VER >= 23304// [range.take.overview]: the `repeat_view` "_RawRange models sized_range" case.305template <class _Range,306convertible_to<range_difference_t<_Range>> _Np,307class _RawRange = remove_cvref_t<_Range>,308class _Dist = range_difference_t<_Range>>309requires(__is_repeat_specialization<_RawRange> && sized_range<_RawRange>)310[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Np&& __n) const311noexcept(noexcept(views::repeat(*__range.__value_, std::min<_Dist>(ranges::distance(__range), std::forward<_Np>(__n)))))312-> decltype( views::repeat(*__range.__value_, std::min<_Dist>(ranges::distance(__range), std::forward<_Np>(__n))))313{ return views::repeat(*__range.__value_, std::min<_Dist>(ranges::distance(__range), std::forward<_Np>(__n))); }314315// [range.take.overview]: the `repeat_view` "otherwise" case.316template <class _Range,317convertible_to<range_difference_t<_Range>> _Np,318class _RawRange = remove_cvref_t<_Range>,319class _Dist = range_difference_t<_Range>>320requires(__is_repeat_specialization<_RawRange> && !sized_range<_RawRange>)321[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Np&& __n) const322noexcept(noexcept(views::repeat(*__range.__value_, static_cast<_Dist>(__n))))323-> decltype( views::repeat(*__range.__value_, static_cast<_Dist>(__n)))324{ return views::repeat(*__range.__value_, static_cast<_Dist>(__n)); }325#endif326// clang-format on327328// [range.take.overview]: the "otherwise" case.329template <class _Range, convertible_to<range_difference_t<_Range>> _Np, class _RawRange = remove_cvref_t<_Range>>330// Note: without specifically excluding the other cases, GCC sees this overload as ambiguous with the other331// overloads.332requires(!(__is_empty_view<_RawRange> ||333# if _LIBCPP_STD_VER >= 23334__is_repeat_specialization<_RawRange> ||335# endif336(__is_iota_specialization<_RawRange> && sized_range<_RawRange> && random_access_range<_RawRange>) ||337(__is_passthrough_specialization<_RawRange> && sized_range<_RawRange> &&338random_access_range<_RawRange>)))339[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Np&& __n) const340noexcept(noexcept(take_view(std::forward<_Range>(__range), std::forward<_Np>(__n))))341-> decltype(take_view(std::forward<_Range>(__range), std::forward<_Np>(__n))) {342return take_view(std::forward<_Range>(__range), std::forward<_Np>(__n));343}344345template <class _Np>346requires constructible_from<decay_t<_Np>, _Np>347[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Np&& __n) const348noexcept(is_nothrow_constructible_v<decay_t<_Np>, _Np>) {349return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Np>(__n)));350}351};352353} // namespace __take354355inline namespace __cpo {356inline constexpr auto take = __take::__fn{};357} // namespace __cpo358} // namespace views359360} // namespace ranges361362#endif // _LIBCPP_STD_VER >= 20363364_LIBCPP_END_NAMESPACE_STD365366_LIBCPP_POP_MACROS367368#endif // _LIBCPP___RANGES_TAKE_VIEW_H369370371