xref: /llvm-project/libcxx/include/__algorithm/ranges_contains_subrange.h (revision a6b846ae1e58e11160185e427e20a995f6656859)
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___ALGORITHM_RANGES_CONTAINS_SUBRANGE_H
10 #define _LIBCPP___ALGORITHM_RANGES_CONTAINS_SUBRANGE_H
11 
12 #include <__algorithm/ranges_search.h>
13 #include <__config>
14 #include <__functional/identity.h>
15 #include <__functional/ranges_operations.h>
16 #include <__functional/reference_wrapper.h>
17 #include <__iterator/concepts.h>
18 #include <__iterator/distance.h>
19 #include <__iterator/indirectly_comparable.h>
20 #include <__iterator/projected.h>
21 #include <__ranges/access.h>
22 #include <__ranges/concepts.h>
23 #include <__ranges/subrange.h>
24 #include <__utility/move.h>
25 
26 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27 #  pragma GCC system_header
28 #endif
29 
30 _LIBCPP_PUSH_MACROS
31 #include <__undef_macros>
32 
33 #if _LIBCPP_STD_VER >= 23
34 
35 _LIBCPP_BEGIN_NAMESPACE_STD
36 
37 namespace ranges {
38 namespace __contains_subrange {
39 struct __fn {
40   template <forward_iterator _Iter1,
41             sentinel_for<_Iter1> _Sent1,
42             forward_iterator _Iter2,
43             sentinel_for<_Iter2> _Sent2,
44             class _Pred  = ranges::equal_to,
45             class _Proj1 = identity,
46             class _Proj2 = identity>
47     requires indirectly_comparable<_Iter1, _Iter2, _Pred, _Proj1, _Proj2>
48   _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool static operator()(
49       _Iter1 __first1,
50       _Sent1 __last1,
51       _Iter2 __first2,
52       _Sent2 __last2,
53       _Pred __pred   = {},
54       _Proj1 __proj1 = {},
55       _Proj2 __proj2 = {}) {
56     auto __n2 = ranges::distance(__first2, __last2);
57     if (__n2 == 0)
58       return true;
59 
60     auto __ret = ranges::search(
61         std::move(__first1), __last1, std::move(__first2), __last2, __pred, std::ref(__proj1), std::ref(__proj2));
62     return __ret.empty() == false;
63   }
64 
65   template <forward_range _Range1,
66             forward_range _Range2,
67             class _Pred  = ranges::equal_to,
68             class _Proj1 = identity,
69             class _Proj2 = identity>
70     requires indirectly_comparable<iterator_t<_Range1>, iterator_t<_Range2>, _Pred, _Proj1, _Proj2>
71   _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr bool static
72   operator()(_Range1&& __range1, _Range2&& __range2, _Pred __pred = {}, _Proj1 __proj1 = {}, _Proj2 __proj2 = {}) {
73     auto __n2 = 0;
74     if constexpr (sized_range<_Range2>) {
75       __n2 = ranges::size(__range2);
76     } else {
77       __n2 = std::distance(cbegin(__range2), cend(__range2));
78     }
79     if (__n2 == 0)
80       return true;
81 
82     auto __ret = ranges::search(__range1, __range2, __pred, std::ref(__proj1), std::ref(__proj2));
83     return __ret.empty() == false;
84   }
85 };
86 } // namespace __contains_subrange
87 
88 inline namespace __cpo {
89 inline constexpr auto contains_subrange = __contains_subrange::__fn{};
90 } // namespace __cpo
91 } // namespace ranges
92 
93 _LIBCPP_END_NAMESPACE_STD
94 
95 #endif // _LIBCPP_STD_VER >= 23
96 
97 _LIBCPP_POP_MACROS
98 
99 #endif // _LIBCPP___ALGORITHM_RANGES_CONTAINS_SUBRANGE_H
100