Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
find.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_FIND_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_FIND_HPP_
7
8// IWYU pragma: private, include "arene/base/algorithm.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
12
15#include "arene/base/stdlib_choice/enable_if.hpp"
16#include "arene/base/stdlib_choice/is_copy_constructible.hpp"
17#include "arene/base/stdlib_choice/iterator_traits.hpp"
21// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
22
23namespace arene {
24namespace base {
25
26/// @brief Constexpr-compatible implementation of @c std::find.
27/// @tparam Iterator The type of the iterators
28/// @tparam Value The type of the value being searched for
29/// @param first The iterator for the start of the range
30/// @param last The iterator for the end of the range
31/// @param value_to_find The value to search for
32/// @return Iterator The first iterator for which @c *it==value, or @c last if there is
33/// no such iterator
34template <
35 typename Iterator,
36 typename Value,
40 base::is_equality_comparable_v<typename std::iterator_traits<Iterator>::reference, Value const&>>> =
41 nullptr>
42constexpr auto find(
43 Iterator first,
44 Iterator last, // CODEQLFP(DCL51-CPP) CODEQLFP(A7-1-1) CODEQLFP(A15-4-4)
45 Value const& value_to_find
46) //
47 noexcept(
48 denotes_nothrow_iterable_range_v<Iterator, Iterator>&& noexcept(*first == value_to_find) &&
50 ) -> Iterator { // CODEQLFP(EXP52-CPP)
51 // CODEQLFP(A5-2-6)
52 // parasoft-begin-suppress AUTOSAR-A6_5_2-a-2 "False positive: The loop counter is 'pos'"
53 for (Iterator& pos{first}; pos != last; arene::base::advance(pos, 1)) { // CODEQLFP(CTR55-CPP) CODEQLFP(M6-5-5)
54 if (*pos == value_to_find) {
55 return pos;
56 }
57 } // CODEQLFP(A5-0-2)
58 // parasoft-end-suppress AUTOSAR-A6_5_2-a-2
59 return last;
60}
61
62} // namespace base
63} // namespace arene
64
65#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_FIND_HPP_
Provides a backport of C++17's constexpr std::advance.
static constexpr value_type value
Definition integral_constant.hpp:34
Definition accumulate.hpp:37
constexpr bool denotes_nothrow_iterable_range_v
Check if an iterator/sentinel pair form an iterable range that can be iterated without throwing.
constexpr auto find(Iterator first, Iterator last, Value const &value_to_find) noexcept(denotes_nothrow_iterable_range_v< Iterator, Iterator > &&noexcept(*first==value_to_find) &&std::is_nothrow_copy_constructible< Iterator >::value) -> Iterator
Constexpr-compatible implementation of std::find.
Definition find.hpp:42
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 accumulate.hpp:36
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