Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
is_sorted.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "False positive: defines std::is_sorted"
2
3// Copyright 2026, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
7#ifndef INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_IS_SORTED_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_IS_SORTED_HPP_
9
10// IWYU pragma: private, include <algorithm>
11// IWYU pragma: friend "stdlib_detail/.*"
12
13#include "arene/base/algorithm/detail/traits.hpp"
15#include "arene/base/functional/detail/operator_less.hpp"
25
26// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
27// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
28
29namespace std {
30
31// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
32/// @brief Check if a range is sorted. Returns @c false if there are any pair of sequential elements @c i and @c j such
33/// that @c comp(j,i) is @c true otherwise returns @c true
34/// @tparam ForwardIterator The iterator type used to identify the range
35/// @tparam Comparator The type of the comparator
36/// @param first The start of the range
37/// @param last The end of the range
38/// @param comp The comparator
39/// @return @c true if the range is sorted, @c false otherwise
40/// @pre @c [first,last) must be a valid range
41/// @pre @c comp(*first,*first) must be valid and return a boolean-testable result
42/// @pre @c comp must provide a strict weak order over the elements of the range
43template <
44 typename ForwardIterator,
45 typename Comparator,
49 Comparator&,
50 typename std::iterator_traits<ForwardIterator>::reference>>
51 > = nullptr>
52auto is_sorted(ForwardIterator first, ForwardIterator last, Comparator comp) noexcept(
53 noexcept(first != last) && //
54 noexcept(arene::base::advance(first, 1)) && //
55 noexcept(comp(*first, *first)) && //
58) -> bool {
59 if (first != last) {
60 ForwardIterator prev{first};
61 arene::base::advance(first, 1);
62 while (first != last) {
63 if (comp(*first, *prev)) {
64 return false;
65 }
66 prev = first;
67 arene::base::advance(first, 1);
68 }
69 }
70 return true;
71}
72// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2
73
74/// @brief Check if a range is sorted. Returns @c false if there are any pair of sequential elements @c i and @c j such
75/// that @c j<i is @c true otherwise returns @c true
76/// @tparam ForwardIterator The iterator type used to identify the range
77/// @param first The start of the range
78/// @param last The end of the range
79/// @return @c true if the range is sorted, @c false otherwise
80/// @pre @c [first,last) must be a valid range
81/// @pre @c *first<*first must be valid and return a boolean-testable result
82/// @pre Less-than comparison of elements must provide a strict weak order over the elements of the range
83template <
84 typename ForwardIterator,
88 decltype(arene::base::functional_detail::operator_less)&,
90auto is_sorted(ForwardIterator first, ForwardIterator last) noexcept(
91 noexcept(is_sorted(std::move(first), std::move(last), arene::base::functional_detail::operator_less))
92) -> bool {
93 return is_sorted(std::move(first), std::move(last), arene::base::functional_detail::operator_less);
94}
95
96} // namespace std
97
98#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_IS_SORTED_HPP_
Provides a backport of C++17's constexpr std::advance.
provides a uniform interface to the properties of an iterator
Definition iterator_traits.hpp:90
Public export header for the constraints subpackage.
constexpr bool is_compare_v
determines if a type satisfies the Compare 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
STL namespace.
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
constexpr bool is_nothrow_copy_assignable_v
Type trait to detect if an instance of T is copy assignable without throwing.
constexpr bool is_nothrow_copy_constructible_v
Type trait to detect if an instance of T is copy constructible without throwing.
auto is_sorted(ForwardIterator first, ForwardIterator last, Comparator comp) noexcept(noexcept(first !=last) &&//noexcept(arene::base::advance(first, 1)) &&//noexcept(comp(*first, *first)) &&//is_nothrow_copy_constructible_v< ForwardIterator > &&//is_nothrow_copy_assignable_v< ForwardIterator >) -> bool
Check if a range is sorted. Returns false if there are any pair of sequential elements i and j such t...
Definition is_sorted.hpp:52