Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
internal_partition_point.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_INTERNAL_PARTITION_POINT_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_INTERNAL_PARTITION_POINT_HPP_
7
8#include "arene/base/algorithm/detail/traits.hpp"
17
18// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
19// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
20
21// IWYU pragma: friend "stdlib_detail/.*"
22
23namespace std {
24namespace internal {
25
26// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
27
28/// @brief locates the partition point of a range partitioned with respect to an unary predicate
29/// @tparam ForwardIterator iterator type
30/// @tparam UnaryPred unary predicate type
31/// @param first beginning of the partitioned range of elements
32/// @param last end of the partitioned range of elements
33/// @param pred predicate specifying the partition
34///
35/// @return iterator past the end of the first partition within <tt>[first,
36/// last)</tt> or @c last if all elements satisfy @c pred.
37///
38template <
39 class ForwardIterator,
40 class UnaryPred,
44 UnaryPred&,
45 arene::base::algorithm_detail::iter_reference_t<ForwardIterator>>>> = nullptr>
46constexpr auto partition_point(ForwardIterator first, ForwardIterator last, UnaryPred pred) noexcept(
49 noexcept(arene::base::distance(first, last)) and //
50 noexcept(arene::base::next(first)) and //
51 noexcept(pred(*first))
52) -> ForwardIterator {
53 using difference_type = arene::base::algorithm_detail::iter_difference_t<ForwardIterator>;
54 constexpr difference_type two{2};
55
58
59 auto length = distance(first, last);
60 while (length > difference_type{}) {
61 auto const half = length / two;
62 auto const middle = next(first, half);
63
64 if (pred(*middle)) {
65 first = next(middle);
66 length -= (half + difference_type{1});
67 } else {
68 length = half;
69 }
70 }
71
72 return first;
73}
74
75// parasoft-end-suppress AUTOSAR-M3_3_2-a
76
77} // namespace internal
78} // namespace std
79
80#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_INTERNAL_PARTITION_POINT_HPP_
Public export header for the constraints subpackage.
constexpr bool is_predicate_v
backport for the C++20 predicate concept
ARENE_NODISCARD constexpr auto next(Iterator itr, typename std::iterator_traits< Iterator >::difference_type steps=1) noexcept(noexcept(::arene::base::advance(itr, steps))) -> Iterator
Produces the Nth successor (or predecessor if steps is negative) of an iterator.
Definition next.hpp:38
constexpr auto distance(Iterator first, Iterator last) noexcept(noexcept(distance_impl::do_distance(first, last, typename std::iterator_traits< Iterator >::iterator_category{}))) -> typename std::iterator_traits< Iterator >::difference_type
Find the number of elements between first and last.
Definition distance.hpp:79
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition internal_named_requirements.hpp:19
constexpr auto partition_point(ForwardIterator first, ForwardIterator last, UnaryPred pred) noexcept(std::is_nothrow_move_constructible_v< ForwardIterator > and//std::is_nothrow_move_assignable_v< ForwardIterator >and//noexcept(arene::base::distance(first, last)) and//noexcept(arene::base::next(first)) and//noexcept(pred(*first))) -> ForwardIterator
locates the partition point of a range partitioned with respect to an unary predicate
Definition internal_partition_point.hpp:46
STL namespace.
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
constexpr bool is_nothrow_move_constructible_v
Type trait to detect if an instance of T is move constructible without throwing.
constexpr bool is_nothrow_move_assignable_v
Type trait to detect if an instance of T is move assignable without throwing.