xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/ranges_is_partitioned.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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_IS_PARTITIONED_H
10 #define _LIBCPP___ALGORITHM_RANGES_IS_PARTITIONED_H
11 
12 #include <__config>
13 #include <__functional/identity.h>
14 #include <__functional/invoke.h>
15 #include <__iterator/concepts.h>
16 #include <__iterator/indirectly_comparable.h>
17 #include <__iterator/projected.h>
18 #include <__ranges/access.h>
19 #include <__ranges/concepts.h>
20 #include <__utility/move.h>
21 
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 #  pragma GCC system_header
24 #endif
25 
26 _LIBCPP_PUSH_MACROS
27 #include <__undef_macros>
28 
29 #if _LIBCPP_STD_VER >= 20
30 
31 _LIBCPP_BEGIN_NAMESPACE_STD
32 
33 namespace ranges {
34 namespace __is_partitioned {
35 struct __fn {
36   template <class _Iter, class _Sent, class _Proj, class _Pred>
37   _LIBCPP_HIDE_FROM_ABI constexpr static bool
__is_partitioned_impl__fn38   __is_partitioned_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) {
39     for (; __first != __last; ++__first) {
40       if (!std::invoke(__pred, std::invoke(__proj, *__first)))
41         break;
42     }
43 
44     if (__first == __last)
45       return true;
46     ++__first;
47 
48     for (; __first != __last; ++__first) {
49       if (std::invoke(__pred, std::invoke(__proj, *__first)))
50         return false;
51     }
52 
53     return true;
54   }
55 
56   template <input_iterator _Iter,
57             sentinel_for<_Iter> _Sent,
58             class _Proj = identity,
59             indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
60   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool
operator__fn61   operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const {
62     return __is_partitioned_impl(std::move(__first), std::move(__last), __pred, __proj);
63   }
64 
65   template <input_range _Range,
66             class _Proj = identity,
67             indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
68   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool
operator__fn69   operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const {
70     return __is_partitioned_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj);
71   }
72 };
73 } // namespace __is_partitioned
74 
75 inline namespace __cpo {
76 inline constexpr auto is_partitioned = __is_partitioned::__fn{};
77 } // namespace __cpo
78 } // namespace ranges
79 
80 _LIBCPP_END_NAMESPACE_STD
81 
82 #endif // _LIBCPP_STD_VER >= 20
83 
84 _LIBCPP_POP_MACROS
85 
86 #endif // _LIBCPP___ALGORITHM_RANGES_IS_PARTITIONED_H
87