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 #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
27 
28 _LIBCPP_BEGIN_NAMESPACE_STD
29 
30 namespace ranges {
31 namespace __is_partitioned {
32 struct __fn {
33 
34   template <class _Iter, class _Sent, class _Proj, class _Pred>
35   _LIBCPP_HIDE_FROM_ABI constexpr static
36   bool __is_parititioned_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) {
37     for (; __first != __last; ++__first) {
38       if (!std::invoke(__pred, std::invoke(__proj, *__first)))
39         break;
40     }
41 
42     if (__first == __last)
43       return true;
44     ++__first;
45 
46     for (; __first != __last; ++__first) {
47       if (std::invoke(__pred, std::invoke(__proj, *__first)))
48         return false;
49     }
50 
51     return true;
52   }
53 
54   template <input_iterator _Iter, sentinel_for<_Iter> _Sent,
55             class _Proj = identity,
56             indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
57   _LIBCPP_HIDE_FROM_ABI constexpr
58   bool operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const {
59     return __is_parititioned_impl(std::move(__first), std::move(__last), __pred, __proj);
60   }
61 
62   template <input_range _Range,
63             class _Proj = identity,
64             indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
65   _LIBCPP_HIDE_FROM_ABI constexpr
66   bool operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const {
67     return __is_parititioned_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj);
68   }
69 };
70 } // namespace __is_partitioned
71 
72 inline namespace __cpo {
73   inline constexpr auto is_partitioned = __is_partitioned::__fn{};
74 } // namespace __cpo
75 } // namespace ranges
76 
77 _LIBCPP_END_NAMESPACE_STD
78 
79 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
80 
81 #endif // _LIBCPP___ALGORITHM_RANGES_IS_PARTITIONED_H
82