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