xref: /freebsd/contrib/llvm-project/libcxx/include/__ranges/drop_view.h (revision 13ec1e3155c7e9bf037b12af186351b7fa9b9450)
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___RANGES_DROP_VIEW_H
10 #define _LIBCPP___RANGES_DROP_VIEW_H
11 
12 #include <__config>
13 #include <__iterator/concepts.h>
14 #include <__iterator/iterator_traits.h>
15 #include <__iterator/next.h>
16 #include <__ranges/access.h>
17 #include <__ranges/all.h>
18 #include <__ranges/concepts.h>
19 #include <__ranges/enable_borrowed_range.h>
20 #include <__ranges/non_propagating_cache.h>
21 #include <__ranges/size.h>
22 #include <__ranges/view_interface.h>
23 #include <__utility/move.h>
24 #include <concepts>
25 #include <type_traits>
26 
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 #pragma GCC system_header
29 #endif
30 
31 _LIBCPP_PUSH_MACROS
32 #include <__undef_macros>
33 
34 _LIBCPP_BEGIN_NAMESPACE_STD
35 
36 #if !defined(_LIBCPP_HAS_NO_RANGES)
37 
38 namespace ranges {
39   template<view _View>
40   class drop_view
41     : public view_interface<drop_view<_View>>
42   {
43     // We cache begin() whenever ranges::next is not guaranteed O(1) to provide an
44     // amortized O(1) begin() method. If this is an input_range, then we cannot cache
45     // begin because begin is not equality preserving.
46     // Note: drop_view<input-range>::begin() is still trivially amortized O(1) because
47     // one can't call begin() on it more than once.
48     static constexpr bool _UseCache = forward_range<_View> && !(random_access_range<_View> && sized_range<_View>);
49     using _Cache = _If<_UseCache, __non_propagating_cache<iterator_t<_View>>, __empty_cache>;
50     [[no_unique_address]] _Cache __cached_begin_ = _Cache();
51     range_difference_t<_View> __count_ = 0;
52     _View __base_ = _View();
53 
54 public:
55     drop_view() requires default_initializable<_View> = default;
56 
57     _LIBCPP_HIDE_FROM_ABI
58     constexpr drop_view(_View __base, range_difference_t<_View> __count)
59       : __count_(__count)
60       , __base_(_VSTD::move(__base))
61     {
62       _LIBCPP_ASSERT(__count_ >= 0, "count must be greater than or equal to zero.");
63     }
64 
65     _LIBCPP_HIDE_FROM_ABI constexpr _View base() const& requires copy_constructible<_View> { return __base_; }
66     _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return _VSTD::move(__base_); }
67 
68     _LIBCPP_HIDE_FROM_ABI
69     constexpr auto begin()
70       requires (!(__simple_view<_View> &&
71                   random_access_range<const _View> && sized_range<const _View>))
72     {
73       if constexpr (_UseCache)
74         if (__cached_begin_.__has_value())
75           return *__cached_begin_;
76 
77       auto __tmp = ranges::next(ranges::begin(__base_), __count_, ranges::end(__base_));
78       if constexpr (_UseCache)
79         __cached_begin_.__set(__tmp);
80       return __tmp;
81     }
82 
83     _LIBCPP_HIDE_FROM_ABI
84     constexpr auto begin() const
85       requires random_access_range<const _View> && sized_range<const _View>
86     {
87       return ranges::next(ranges::begin(__base_), __count_, ranges::end(__base_));
88     }
89 
90     _LIBCPP_HIDE_FROM_ABI
91     constexpr auto end()
92       requires (!__simple_view<_View>)
93     { return ranges::end(__base_); }
94 
95     _LIBCPP_HIDE_FROM_ABI
96     constexpr auto end() const
97       requires range<const _View>
98     { return ranges::end(__base_); }
99 
100     _LIBCPP_HIDE_FROM_ABI
101     static constexpr auto __size(auto& __self) {
102       const auto __s = ranges::size(__self.__base_);
103       const auto __c = static_cast<decltype(__s)>(__self.__count_);
104       return __s < __c ? 0 : __s - __c;
105     }
106 
107     _LIBCPP_HIDE_FROM_ABI
108     constexpr auto size()
109       requires sized_range<_View>
110     { return __size(*this); }
111 
112     _LIBCPP_HIDE_FROM_ABI
113     constexpr auto size() const
114       requires sized_range<const _View>
115     { return __size(*this); }
116   };
117 
118   template<class _Range>
119   drop_view(_Range&&, range_difference_t<_Range>) -> drop_view<views::all_t<_Range>>;
120 
121   template<class _Tp>
122   inline constexpr bool enable_borrowed_range<drop_view<_Tp>> = enable_borrowed_range<_Tp>;
123 } // namespace ranges
124 
125 #endif // !defined(_LIBCPP_HAS_NO_RANGES)
126 
127 _LIBCPP_END_NAMESPACE_STD
128 
129 _LIBCPP_POP_MACROS
130 
131 #endif // _LIBCPP___RANGES_DROP_VIEW_H
132