Path: blob/main/contrib/llvm-project/libcxx/include/__ranges/take_while_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_WHILE_VIEW_H10#define _LIBCPP___RANGES_TAKE_WHILE_VIEW_H1112#include <__concepts/constructible.h>13#include <__concepts/convertible_to.h>14#include <__config>15#include <__functional/bind_back.h>16#include <__functional/invoke.h>17#include <__iterator/concepts.h>18#include <__memory/addressof.h>19#include <__ranges/access.h>20#include <__ranges/all.h>21#include <__ranges/concepts.h>22#include <__ranges/movable_box.h>23#include <__ranges/range_adaptor.h>24#include <__ranges/view_interface.h>25#include <__type_traits/decay.h>26#include <__type_traits/is_nothrow_constructible.h>27#include <__type_traits/is_object.h>28#include <__type_traits/maybe_const.h>29#include <__utility/forward.h>30#include <__utility/in_place.h>31#include <__utility/move.h>3233#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)34# pragma GCC system_header35#endif3637_LIBCPP_PUSH_MACROS38#include <__undef_macros>3940_LIBCPP_BEGIN_NAMESPACE_STD4142#if _LIBCPP_STD_VER >= 204344namespace ranges {4546template <view _View, class _Pred>47requires input_range<_View> && is_object_v<_Pred> && indirect_unary_predicate<const _Pred, iterator_t<_View>>48class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS take_while_view : public view_interface<take_while_view<_View, _Pred>> {49template <bool>50class __sentinel;5152_LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();53_LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Pred> __pred_;5455public:56_LIBCPP_HIDE_FROM_ABI take_while_view()57requires default_initializable<_View> && default_initializable<_Pred>58= default;5960_LIBCPP_HIDE_FROM_ABI constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 take_while_view(_View __base, _Pred __pred)61: __base_(std::move(__base)), __pred_(std::in_place, std::move(__pred)) {}6263_LIBCPP_HIDE_FROM_ABI constexpr _View base() const&64requires copy_constructible<_View>65{66return __base_;67}6869_LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }7071_LIBCPP_HIDE_FROM_ABI constexpr const _Pred& pred() const { return *__pred_; }7273_LIBCPP_HIDE_FROM_ABI constexpr auto begin()74requires(!__simple_view<_View>)75{76return ranges::begin(__base_);77}7879_LIBCPP_HIDE_FROM_ABI constexpr auto begin() const80requires range<const _View> && indirect_unary_predicate<const _Pred, iterator_t<const _View>>81{82return ranges::begin(__base_);83}8485_LIBCPP_HIDE_FROM_ABI constexpr auto end()86requires(!__simple_view<_View>)87{88return __sentinel</*_Const=*/false>(ranges::end(__base_), std::addressof(*__pred_));89}9091_LIBCPP_HIDE_FROM_ABI constexpr auto end() const92requires range<const _View> && indirect_unary_predicate<const _Pred, iterator_t<const _View>>93{94return __sentinel</*_Const=*/true>(ranges::end(__base_), std::addressof(*__pred_));95}96};9798template <class _Range, class _Pred>99take_while_view(_Range&&, _Pred) -> take_while_view<views::all_t<_Range>, _Pred>;100101template <view _View, class _Pred>102requires input_range<_View> && is_object_v<_Pred> && indirect_unary_predicate<const _Pred, iterator_t<_View>>103template <bool _Const>104class take_while_view<_View, _Pred>::__sentinel {105using _Base = __maybe_const<_Const, _View>;106107sentinel_t<_Base> __end_ = sentinel_t<_Base>();108const _Pred* __pred_ = nullptr;109110friend class __sentinel<!_Const>;111112public:113_LIBCPP_HIDE_FROM_ABI __sentinel() = default;114115_LIBCPP_HIDE_FROM_ABI constexpr explicit __sentinel(sentinel_t<_Base> __end, const _Pred* __pred)116: __end_(std::move(__end)), __pred_(__pred) {}117118_LIBCPP_HIDE_FROM_ABI constexpr __sentinel(__sentinel<!_Const> __s)119requires _Const && convertible_to<sentinel_t<_View>, sentinel_t<_Base>>120: __end_(std::move(__s.__end_)), __pred_(__s.__pred_) {}121122_LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_Base> base() const { return __end_; }123124_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const iterator_t<_Base>& __x, const __sentinel& __y) {125return __x == __y.__end_ || !std::invoke(*__y.__pred_, *__x);126}127128template <bool _OtherConst = !_Const>129requires sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>130_LIBCPP_HIDE_FROM_ABI friend constexpr bool131operator==(const iterator_t<__maybe_const<_OtherConst, _View>>& __x, const __sentinel& __y) {132return __x == __y.__end_ || !std::invoke(*__y.__pred_, *__x);133}134};135136namespace views {137namespace __take_while {138139struct __fn {140template <class _Range, class _Pred>141[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range, _Pred&& __pred) const142noexcept(noexcept(/**/ take_while_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))))143-> decltype(/*--*/ take_while_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred))) {144return /*-------------*/ take_while_view(std::forward<_Range>(__range), std::forward<_Pred>(__pred));145}146147template <class _Pred>148requires constructible_from<decay_t<_Pred>, _Pred>149[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Pred&& __pred) const150noexcept(is_nothrow_constructible_v<decay_t<_Pred>, _Pred>) {151return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Pred>(__pred)));152}153};154155} // namespace __take_while156157inline namespace __cpo {158inline constexpr auto take_while = __take_while::__fn{};159} // namespace __cpo160} // namespace views161} // namespace ranges162163#endif // _LIBCPP_STD_VER >= 20164165_LIBCPP_END_NAMESPACE_STD166167_LIBCPP_POP_MACROS168169#endif // _LIBCPP___RANGES_TAKE_WHILE_VIEW_H170171172