Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
upper_bound.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_UPPER_BOUND_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_UPPER_BOUND_HPP_
7
8#include "arene/base/algorithm/detail/traits.hpp"
11#include "arene/base/functional/detail/operator_less.hpp"
19
20// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
21// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
22
23// IWYU pragma: private, include <algorithm>
24// IWYU pragma: friend "stdlib_detail/.*"
25
26namespace std {
27
28// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
29
30/// @brief returns an iterator to the first element greater than the given value
31/// @tparam ForwardIterator iterator type
32/// @tparam T value type to compare against
33/// @tparam Compare binary predicate type
34/// @param first beginning of the partitioned range of elements
35/// @param last end of the partitioned range of elements
36/// @param value value to compare elements to
37/// @param comp binary predicate which returns @c true if the first argument is ordered before the second
38///
39/// @pre @c ForwardIterator must satisfy the forward iterator requirements.
40/// @pre <tt>[begin, end)</tt> must be a valid range.
41/// @pre The expression <tt>comp(value, e)</tt> must be convertible to @c bool
42/// for every argument @c e of type @c RT, where @c RT is the reference type
43/// of @c ForwardIterator, regardless of value category, and must not modify
44/// @c e or @c value.
45/// @pre The elements @c e of <tt>[begin, end)</tt> must be partitioned with
46/// respect to the expression <tt>comp(value, e)</tt>.
47///
48/// @return iterator to the first element of the range <tt>[first, last)</tt>
49/// ordered after @c value, or @c last if no such element is found.
50///
51/// @note Complexity
52/// Given N as <tt>std::distance(first, last)</tt>, at most <tt>log2(N) +
53/// O(1)</tt> applications of the comparator @c comp.
54///
55template <
56 class ForwardIterator,
57 class T,
58 class Compare,
62 Compare&,
63 T const&,
64 arene::base::algorithm_detail::iter_reference_t<ForwardIterator>>>> = nullptr>
65constexpr auto upper_bound(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) noexcept(
67 std::move(first),
68 std::move(last),
69 arene::base::bind_front(std::declval<decltype(arene::base::not_fn(comp))&>(), value)
70 ))
71) -> ForwardIterator {
72 auto not_comp = arene::base::not_fn(comp);
73 return std::internal::partition_point(std::move(first), std::move(last), arene::base::bind_front(not_comp, value));
74}
75
76/// @brief returns an iterator to the first element greater than the given value
77/// @tparam ForwardIterator iterator type
78/// @tparam T value type to compare against
79/// @tparam Compare binary predicate type
80/// @param first beginning of the partitioned range of elements
81/// @param last end of the partitioned range of elements
82/// @param value value to compare elements to
83///
84/// @pre @c ForwardIterator must satisfy the forward iterator requirements.
85/// @pre <tt>[begin, end)</tt> must be a valid range.
86/// @pre The expression <tt>value < e</tt> must be convertible to @c bool
87/// for every argument @c e of type @c RT, where @c RT is the reference type
88/// of @c ForwardIterator, regardless of value category, and must not modify
89/// @c e or @c value.
90/// @pre The elements @c e of <tt>[begin, end)</tt> must be partitioned with
91/// respect to the expression <tt>value < e</tt>.
92///
93/// @return iterator to the first element of the range <tt>[first, last)</tt>
94/// ordered after @c value, or @c last if no such element is found.
95///
96/// @note Complexity
97/// Given N as <tt>std::distance(first, last)</tt>, at most <tt>log2(N) +
98/// O(1)</tt> comparisons with @c value using @c operator<.
99///
100template <
101 class ForwardIterator,
102 class T,
106 decltype(arene::base::functional_detail::operator_less)&,
107 T const&,
108 arene::base::algorithm_detail::iter_reference_t<ForwardIterator>>>> = nullptr>
109constexpr auto upper_bound(ForwardIterator first, ForwardIterator last, T const& value) noexcept(
110 noexcept(std::upper_bound(std::move(first), std::move(last), value, arene::base::functional_detail::operator_less))
111) -> ForwardIterator {
112 return std::upper_bound(std::move(first), std::move(last), value, arene::base::functional_detail::operator_less);
113}
114
115// parasoft-end-suppress AUTOSAR-M3_3_2-a
116
117} // namespace std
118
119#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_UPPER_BOUND_HPP_
Public export header for the constraints subpackage.
constexpr bool is_binary_predicate_v
determines if a type satisfies the BinaryPredicate named requirement
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
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.
constexpr Type integral_constant< Type, Value >::value
Out-of-line definition for the value constant.
Definition integral_constant.hpp:64
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
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
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...
constexpr auto upper_bound(ForwardIterator first, ForwardIterator last, T const &value, Compare comp) noexcept(noexcept(std::internal::partition_point(std::move(first), std::move(last), arene::base::bind_front(std::declval< & >(), value)))) -> ForwardIterator
returns an iterator to the first element greater than the given value
Definition upper_bound.hpp:65