Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
copy_if.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_COPY_IF_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_COPY_IF_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
14#include "arene/base/stdlib_choice/declval.hpp"
15#include "arene/base/stdlib_choice/enable_if.hpp"
16#include "arene/base/stdlib_choice/is_assignable.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// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
27/// @brief Copies the elements in the range, defined by <tt>[first, last)</tt>,
28/// to another range beginning at @c output. Only copies the elements for which
29/// the predicate @c pred returns true. The relative order of the elements that
30/// are copied is preserved.
31/// @tparam InputIterator The type of the Source iterator
32/// @tparam OutputIterator The type of the Destination iterator
33/// @tparam Predicate Function to test whether a value might be copied
34/// @param first Start of the range of elements to copy from
35/// @param last End (non-inclusive) of the range of elements to copy from
36/// @param output Start of the range of elements to copy into
37/// @param pred Unary predicate which returns @c true for the required elements
38/// @return OutputIterator for the last inserted item
39template <class InputIterator, class OutputIterator, class Predicate,
43 bool, Predicate &, typename std::iterator_traits<InputIterator>::value_type>>> = nullptr>
44// clang-format off
45constexpr auto copy_if( // CODEQLFP(DCL51-CPP) CODEQLFP(A7-1-1) CODEQLFP(A15-4-4)
46 InputIterator first, InputIterator last, OutputIterator output,
47 Predicate pred) noexcept(noexcept(*output++) && // CODEQLFP(EXP52-CPP)
48 noexcept(++first) && // CODEQLFP(EXP52-CPP) CODEQLFP(A5-2-6) CODEQLFP(M5-14-1)
49 noexcept(*first) && // CODEQLFP(EXP52-CPP) CODEQLFP(A5-2-6) CODEQLFP(M5-14-1)
50 noexcept(first != last) && // CODEQLFP(A5-2-6)
53 value && // CODEQLFP(A5-1-1) CODEQLFP(M5-3-1)
55 Predicate&,
56 typename std::iterator_traits<InputIterator>::value_type>) -> OutputIterator // CODEQLFP(M5-3-1) CODEQLFP(A5-2-6)
57// clang-format on
58{
59 // parasoft-begin-suppress AUTOSAR-A7_1_5-a "False positive: 'element' has return type of range element, which may
60 // be non-fundamental"
61 for (auto& element : arene::base::make_subrange(first, last)) { // CODEQLFP(A7-1-1) CODEQLFP(A7-1-2)
62 if (pred(element)) { // CODEQLFP(M0-1-2)
63 // parasoft-begin-suppress AUTOSAR-M5_0_15-a "These are iterator types, so incrementing is ok"
64 // parasoft-begin-suppress AUTOSAR-M5_2_10-a "idiomatic iterator operations permitted by M5-2-10 Permit #1"
65 *output++ = element; // CODEQLFP(CTR55-CPP) CODEQLFP(A4-7-1) CODEQLFP(M5-2-10)
66 // parasoft-end-suppress AUTOSAR-M5_0_15-a
67 // parasoft-end-suppress AUTOSAR-M5_2_10-a
68 }
69 } // CODEQLFP(A5-0-2)
70 // parasoft-end-suppress AUTOSAR-A7_1_5-a
71
72 return output;
73}
74// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
75
76} // namespace base
77} // namespace arene
78
79#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_COPY_IF_HPP_
Type trait to detect if an instance of To is assignable to from arguments of From without throwing.
Definition is_assignable.hpp:78
provides a uniform interface to the properties of an iterator
Definition iterator_traits.hpp:90
Definition accumulate.hpp:37
constexpr bool is_invocable_r_v
Determine if a functor type is invocable with the specified return and argument types.
constexpr auto make_subrange(Iterator iterator, Sentinel sentinel) noexcept(std::is_nothrow_move_constructible< Iterator >::value &&//std::is_nothrow_move_constructible< Sentinel >::value) -> make_subrange_detail::iterator_range< Iterator, Sentinel >
Combine an iterator and sentinel into a range type that can be used with range-based for loops.
Definition make_subrange.hpp:98
constexpr bool is_nothrow_invocable_v
Determine if a functor type is no-throw invocable with the specified argument types.
constexpr auto copy_if(InputIterator first, InputIterator last, OutputIterator output, Predicate pred) noexcept(noexcept(*output++) &&//CODEQLFP(EXP52-CPP) noexcept(++first) &&//CODEQLFP(EXP52-CPP) CODEQLFP(A5-2-6) CODEQLFP(M5-14-1) noexcept(*first) &&//CODEQLFP(EXP52-CPP) CODEQLFP(A5-2-6) CODEQLFP(M5-14-1) noexcept(first !=last) &&//CODEQLFP(A5-2-6) std::is_nothrow_assignable<, typename std::iterator_traits< InputIterator >::reference >::value &&//CODEQLFP(A5-1-1) CODEQLFP(M5-3-1) base::is_nothrow_invocable_v<//Predicate &, typename std::iterator_traits< InputIterator >::value_type >) -> OutputIterator
Copies the elements in the range, defined by [first, last), to another range beginning at output....
Definition copy_if.hpp:45
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
bool
Check if element Idx and all subsequent elements of the two tuples are equal.
Definition tuple.hpp:649
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