Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
remove_duplicates.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_TYPE_LIST_REMOVE_DUPLICATES_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_LIST_REMOVE_DUPLICATES_HPP_
7
8// IWYU pragma: private, include "arene/base/type_list.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// parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
13// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
14
16#include "arene/base/detail/dynamic_extent.hpp"
18#include "arene/base/stdlib_choice/cstddef.hpp"
19#include "arene/base/stdlib_choice/ignore.hpp"
20#include "arene/base/stdlib_choice/integer_sequence.hpp"
21#include "arene/base/stdlib_choice/not_equal_to.hpp"
26
27namespace arene {
28namespace base {
29
30namespace type_lists {
31
32/// @cond INTERNAL
33
34namespace remove_duplicates_detail {
35
36/// @brief collapse L0 (aka TypeList0<T...>) to TypeList0<U...>, where U... does
37/// not contain any duplicates of the same type
38template <class L0>
39struct remove_duplicates_impl;
40
41// The overall approach of this algorithm is:
42// Starting from a list of types Ts...
43//
44// * create a masked index array where the value is either:
45// ** I if T is the *last* occurence of T in Ts...
46// ** dynamic_extent if T is *not* the last occurence of T in Ts...
47//
48// * create a unique index array by dropping all the dynamic_extent entries
49//
50// * create a new type list by mapping the indices back to the type in Ts...
51
52/// @brief return the number of elements in the range matching a value
53/// @tparam Range range type
54/// @param range range of elements to examine
55/// @param value value to count
56/// @return number of elements in @c range equal to @c value
57///
58/// @note arene::base::count is not defined
59///
60template <class Range>
61constexpr auto count(Range const& range, std::size_t value) //
62 -> std::size_t //
63{
64 std::size_t num{};
65 // parasoft-begin-suppress AUTOSAR-A7_1_5-a "False Positive: 'element' has return type of range element, which may
66 // be non-fundamental"
67 for (auto const& elem : range) {
68 if (elem == value) {
69 ++num;
70 }
71 }
72 // parasoft-end-suppress AUTOSAR-A7_1_5-a
73 return num;
74}
75
76// parasoft-begin-suppress AUTOSAR-A11_0_2-a "It is consistent with developer
77// expectations that a type should be defined as a struct if it has no
78// invariants to maintain"
79
80/// @brief an array of indices
81/// @tparam N array size
82///
83/// Simple wrapper around a C array.
84///
85/// @note This avoids using 'arene::base::array' to reduce dependencies.
86///
87template <std::size_t N>
88struct index_array {
89 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: this identifier does not hide anything"
90 /// @brief value_type
91 using value_type = std::size_t;
92 // parasoft-end-suppress AUTOSAR-A2_10_1-d
93
94 // parasoft-begin-suppress AUTOSAR-A18_1_1-a "This must work in contexts where std::array is not available."
95 /// @brief array values
96 // NOLINTNEXTLINE(hicpp-avoid-c-arrays,misc-non-private-member-variables-in-classes)
97 value_type values[N]{};
98 // parasoft-end-suppress AUTOSAR-A18_1_1-a
99
100 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: these standard range API names do not hide anything"
101 /// @brief iterator to the beginning of the range
102 /// @return iterator to the beginning of the range
103 constexpr auto begin() noexcept -> value_type* { return values; }
104 /// @brief iterator to the beginning of the range
105 /// @return iterator to the beginning of the range
106 constexpr auto begin() const noexcept -> value_type const* { return values; }
107
108 // parasoft-begin-suppress AUTOSAR-M5_0_15-a "The incremented pointer does point to an array"
109 /// @brief iterator past the end of the range
110 /// @return iterator past the end of the range
111 constexpr auto end() noexcept -> value_type* { return begin() + N; }
112 /// @brief iterator past the end of the range
113 /// @return iterator past the end of the range
114 constexpr auto end() const noexcept -> value_type const* { return begin() + N; }
115 // parasoft-end-suppress AUTOSAR-M5_0_15-a
116 // parasoft-end-suppress AUTOSAR-A2_10_1-d
117};
118
119// parasoft-end-suppress AUTOSAR-A11_0_2-a
120
121// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
122/// @brief returns indices of the last occurence of each type in a parameter pack
123/// @tparam Ts types
124/// @tparam Is indices
125/// @return a masked array of indices
126///
127/// Returns an array of indices. For each type @c T in @c Ts..., if @c T is the
128/// *last* occurence, the element in the returned array is equal to its index in
129/// @c Ts... . Otherwise the element is equal to @c dynamic_extent.
130///
131/// For example, the returned array for type sequence 'int, double, double', is
132/// '[0, dynamic_extent, 2]'.
133///
134template <class... Ts, std::size_t... Is>
135constexpr auto masked_indices_of_impl(std::index_sequence<Is...>) //
136 -> index_array<sizeof...(Ts)> //
137{ //
138 // parasoft-begin-suppress AUTOSAR-A5_16_1-a "Alternatives to ?: make the code less readable and harder to maintain"
139 // parasoft-begin-suppress AUTOSAR-M8_5_2-a "False positive: there is no subobject here that can be initialized"
140 return { //
141 ((Is == arene::base::last_index_of_v<Ts, Ts...>) //
142 ? Is
143 : dynamic_extent)...
144 };
145 // parasoft-end-suppress AUTOSAR-M8_5_2-a
146 // parasoft-end-suppress AUTOSAR-A5_16_1-a
147}
148// parasoft-end-suppress AUTOSAR-M3_3_2-a
149
150/// @brief masked array containing indices of the last occurence of each type in a type list
151/// @note The primary template is a dummy, 'remove_duplicates' will always use
152/// specialization below
153///
154template <class>
155extern constexpr auto masked_indices_of = nullptr;
156
157/// @brief masked array containing indices of the last occurence of each type in a type list
158/// @tparam List type list
159/// @tparam Ts types
160///
161template <template <class...> class List, class... Ts>
162extern constexpr auto masked_indices_of<List<Ts...>> = //
163 masked_indices_of_impl<Ts...>(std::index_sequence_for<Ts...>{});
164
165/// @brief obtains the unique number of types in a type list
166/// @tparam List type list
167///
168template <class List>
169extern constexpr auto unique_count_v = type_lists::size_v<List> - count(masked_indices_of<List>, dynamic_extent);
170
171// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: cannot apply static here"
172/// @brief returns indices of the last occurence of each type in a type list
173/// @tparam Ts types
174/// @tparam Is indices
175/// @return an array of indices
176///
177/// Returns an array of indices. Each index is the last occurence of a type @c T
178/// in @c Ts... .
179///
180/// For example, the returned array for type list '<int, double, double>', is
181/// '[0, 2]'.
182///
183template <class... Ts>
184constexpr auto unique_indices_impl() -> index_array<unique_count_v<Ts...>> //
185{
186 auto filtered = index_array<unique_count_v<Ts...>>{};
188 masked_indices_of<Ts...>.begin(),
189 masked_indices_of<Ts...>.end(),
190 filtered.begin(),
191 arene::base::bind_back(std::not_equal_to<>{}, dynamic_extent)
192 );
193 return filtered;
194}
195// parasoft-end-suppress AUTOSAR-M3_3_2-a
196
197/// @brief array containing indices of the last occurence of each type in a type list
198/// @tparam Ts types
199///
200template <class... Ts>
201extern constexpr auto unique_indices = unique_indices_impl<Ts...>();
202
203// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: cannot apply static here"
204/// @brief implementation function for @c remove_duplicates
205/// @tparam List type list
206/// @tparam Ts types
207/// @tparam Js unique indices
208/// @return type list with duplicates removed
209///
210template <template <class...> class List, class... Ts, std::size_t... Js>
211auto remove_duplicates_from_impl(std::index_sequence<Js...>) //
212 -> List< //
213 type_lists::at_t< //
214 List<Ts...>,
215 unique_indices<List<Ts...>>.begin()[Js]>...>;
216// parasoft-end-suppress AUTOSAR-M3_3_2-a
217
218// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: cannot apply static here"
219/// @brief implementation function for @c remove_duplicates
220/// @tparam List type list
221/// @tparam T0 first type
222/// @tparam Tn remaining types
223/// @return type list with duplicates removed
224///
225template <template <class...> class List, class T0, class... Tn>
226auto remove_duplicates_from(type_identity<List<T0, Tn...>>) //
227 -> decltype( //
228 remove_duplicates_from_impl<List, T0, Tn...>( //
229 std::make_index_sequence<unique_count_v<List<T0, Tn...>>>{} //
230 )
231 );
232// parasoft-end-suppress AUTOSAR-M3_3_2-a
233
234/// @brief implementation function for @c remove_duplicates
235/// @tparam List type list
236/// @return empty type list
237/// @note overload for an empty type list
238///
239template <template <class...> class List>
240auto remove_duplicates_from(type_identity<List<>>) -> List<>;
241
242/// @brief implementation type for @c remove_duplicates
243/// @tparam List type list
244/// @tparam Ts types
245///
246template <template <class...> class List, class... Ts>
247struct remove_duplicates_impl<List<Ts...>> {
248 static_assert(
249 sizeof...(Ts) != dynamic_extent, //
250 "cannot handle 'std::size_t(-1)' number of types"
251 );
252
253 /// @brief type list with duplicates removed
254 using type = decltype(remove_duplicates_from(type_identity<List<Ts...>>{}));
255};
256
257} // namespace remove_duplicates_detail
258
259/// @endcond
260
261/// @brief Filter a type-list to remove duplicates of elements.
262/// @details Only keeps the *last* instance of a type in the type list.
263///
264/// @tparam L0 An instantiation of a type-list that holds the types to filter
265/// @pre @c L0 is a type-list
266/// @return TypeList<T...>
267template <class L0>
268using remove_duplicates = arene::base::type_lists::remove_duplicates_detail::remove_duplicates_impl<L0>;
269
270/// @brief Filter a type-list to remove duplicates of elements.
271/// @details Only keeps the *last* instance of a type in the type list.
272///
273/// @tparam L0 An instantiation of a type-list that holds the types to filter
274/// @pre @c L0 is a type-list
275/// @return TypeList<T...>
276template <class L0>
278
279} // namespace type_lists
280
281} // namespace base
282} // namespace arene
283
284#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_LIST_REMOVE_DUPLICATES_HPP_
Definition apply_all.hpp:14
typename remove_duplicates< L0 >::type remove_duplicates_t
Filter a type-list to remove duplicates of elements.
Definition remove_duplicates.hpp:277
arene::base::type_lists::remove_duplicates_detail::remove_duplicates_impl< L0 > remove_duplicates
Filter a type-list to remove duplicates of elements.
Definition remove_duplicates.hpp:268
Definition accumulate.hpp:37
constexpr auto last_index_of_v
Template meta-function for querying the last 0-based index of 'T' within a list of 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
Definition accumulate.hpp:36
make_integer_sequence< size_t, Size > make_index_sequence
A helper alias template for make_integer_sequence<size_t, Size>
Definition integer_sequence.hpp:224
integer_sequence< size_t, Ints... > index_sequence
A helper alias template for integer_sequence<size_t, Ints>
Definition integer_sequence.hpp:218
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
constexpr auto end(Element(&array)[Size]) noexcept -> Element *
Obtain a pointer to the one-past-the-end element of the array.
Definition end.hpp:28
static constexpr auto const & ignore
An object to which anything can be assigned, which ignores the assigned value.
Definition ignore.hpp:72
constexpr auto begin(Element(&array)[Size]) noexcept -> Element *
Obtain a pointer to the first element of the array.
Definition begin.hpp:28
make_index_sequence< sizeof...(Types)> index_sequence_for
A helper alias template convert any type parameter pack into an index sequence of the same length.
Definition integer_sequence.hpp:230