Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
integer_sequence_ops.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_INTEGER_SEQUENCES_INTEGER_SEQUENCE_OPS_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INTEGER_SEQUENCES_INTEGER_SEQUENCE_OPS_HPP_
7
8// IWYU pragma: private, include "arene/base/integer_sequences.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/cstddef.hpp"
15#include "arene/base/stdlib_choice/initializer_list.hpp"
16#include "arene/base/stdlib_choice/integer_sequence.hpp"
17#include "arene/base/stdlib_choice/integral_constant.hpp"
19
20// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
21
22namespace arene {
23namespace base {
24
25namespace integer_sequence_ops_detail {
26
27/// @brief termination condition for @c arene::base::integer_sequence_cat
28template <typename... Sequences>
29struct integer_sequence_cat_impl {};
30
31/// @brief Concatenating a single @c integer_sequence is the identity function.
32/// @tparam T the type of the elements
33/// @tparam Elements the elements in the sequence
34template <typename T, T... Elements>
35struct integer_sequence_cat_impl<std::integer_sequence<T, Elements...>> {
36 /// @brief The input sequence
37 using type = std::integer_sequence<T, Elements...>;
38};
39
40/// @brief Concatenates two @c std::integer_sequences.
41///
42/// @tparam T the type of the elements
43/// @tparam SequenceElements1 the sequence of values in the first type sequence
44/// @tparam SequenceElements2 the sequence of values in the second type sequence
45template <typename T, T... SequenceElements1, T... SequenceElements2>
46struct integer_sequence_cat_impl<
47 std::integer_sequence<T, SequenceElements1...>,
48 std::integer_sequence<T, SequenceElements2...>> {
49 /// @brief An @c std::integer_sequence which contains all the elements in @c SequenceElements1 followed by all the
50 /// elements in
51 /// @c SequenceElements2
52 using type = std::integer_sequence<T, SequenceElements1..., SequenceElements2...>;
53};
54
55/// @brief Concatenates a @c std::integer_sequence with multiple sequences.
56/// @tparam Sequence1 the first sequence to concatenate
57/// @tparam OtherSequences the other sequences to concatenate
58template <typename Sequence1, typename... OtherSequences>
59struct integer_sequence_cat_impl<Sequence1, OtherSequences...> {
60 /// @brief An @c std::integer_sequence which contains all the elements in @c Sequence1, followed by the concatenation
61 /// of all the elements from all the sequences in @c OtherSequences
62 using type =
63 typename integer_sequence_cat_impl<Sequence1, typename integer_sequence_cat_impl<OtherSequences...>::type>::type;
64};
65
66} // namespace integer_sequence_ops_detail
67
68/// @brief Produces a single @c std::integer_sequence which is the concatenation of multiple @c std::integer_sequence
69/// @tparam Sequences the sequence of @c std::integer_sequence to concatenate
70/// @pre Each element in @c Sequences must be an instantiation of @c std::integer_sequence.
71/// @return std::integer_sequence A single sequence that contains the elements from all sequences in @c Sequences,
72/// maintaining order of appearance.
73template <typename... Sequences>
74using integer_sequence_cat = typename integer_sequence_ops_detail::integer_sequence_cat_impl<Sequences...>::type;
75
76/// @brief Trait providing the @c Index th element of a @c std::integer_sequence.
77/// The @c value member holds the value. If @c Index is out of range, there is no @c value member.
78/// @tparam Index the index of the element to retrieve
79/// @tparam Sequence the sequence to retrieve the element from
80/// @pre <c>Sequence</c> is an instantiation of @c std::integer_sequence
81template <std::size_t Index, typename Sequence, bool = (Index < Sequence::size())>
82class integer_sequence_element {};
83
84namespace integer_sequence_ops_detail {
85
86/// @brief Get the specified element of an integer sequence.
87/// @tparam Index the index of the element to retrieve
88/// @tparam T the type of the elements
89/// @tparam Elements the elements in the sequence
90/// @return the specified element
91/// @pre @c Index must be less than the number of elements in the sequence
92template <std::size_t Index, typename T, T... Elements>
93constexpr auto get_integer_sequence_element(std::integer_sequence<T, Elements...>) noexcept -> T {
94 static_assert(Index < sizeof...(Elements), "Index must be in range of the sequence");
95 return *(arene::base::next(std::initializer_list<T>{Elements...}.begin(), static_cast<std::ptrdiff_t>(Index)));
96}
97
98} // namespace integer_sequence_ops_detail
99
100// @cond INTERNAL
101// parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: Documented by reference"
102/// @copydoc arene::base::integer_sequence_element<Index, Sequence, bool>
103template <std::size_t Index, typename T, T... Elements>
104class integer_sequence_element<Index, std::integer_sequence<T, Elements...>, true>
105 : public std::integral_constant<
106 T,
107 integer_sequence_ops_detail::get_integer_sequence_element<Index>(std::integer_sequence<T, Elements...>{})> {};
108// parasoft-end-suppress AUTOSAR-A2_7_3-a-2
109// @endcond
110
111/// @brief The value of the @c Index th element of a @c std::integer_sequence.
112/// @see arene::base::integer_sequence_element
113/// @pre @c Sequence must be an instantiation of @c std::integer_sequence.
114template <std::size_t Index, typename Sequence>
115extern constexpr auto integer_sequence_element_v = integer_sequence_element<Index, Sequence>::value;
116
117namespace integer_sequence_ops_detail {
118/// @brief Check if an integer sequence contains a value.
119/// @tparam T the type of the value to search for
120/// @tparam Value the value to search for
121/// @tparam Elements the elements in the sequence
122/// @param value The value to search for
123/// @return true if @c Value is in @c Elements
124/// @return false otherwise.
125template <typename T, T... Elements>
126constexpr auto integer_sequence_contains(T value, std::integer_sequence<T, Elements...>) noexcept -> bool {
127 for (T const element : {Elements...}) {
128 if (element == value) {
129 return true;
130 }
131 }
132 return false;
133}
134/// @brief Check if an empty integer sequence contains a value.
135/// @tparam T the type of the value to search for
136/// @return false
137template <typename T>
138constexpr auto integer_sequence_contains(T, std::integer_sequence<T>) noexcept -> bool {
139 return false;
140}
141
142} // namespace integer_sequence_ops_detail
143
144/// @brief Checks if the specified integer sequence contains the specified value.
145/// @tparam Sequence the sequence to search
146/// @tparam value the value to search for
147/// @pre @c Sequence must be an instantiation of @c std::integer_sequence.
148template <typename Sequence, typename Sequence::value_type Value>
149extern constexpr bool integer_sequence_contains_v =
150 integer_sequence_ops_detail::integer_sequence_contains(Value, Sequence{});
151
152/// @brief Trait for the index of the first occurrence of a value in a @c std::integer_sequence.
153///
154/// The result is stored in the @c value member if it is found, otherwise the member @c value is not provided.
155/// @tparam Sequence the sequence to search
156/// @tparam value the value to search for
157/// @pre @c Sequence must be an instantiation of @c std::integer_sequence.
158template <typename Sequence, typename Sequence::value_type Value, bool = integer_sequence_contains_v<Sequence, Value>>
160
161namespace integer_sequence_ops_detail {
162
163/// @brief Implementation helper for @c get_integer_sequence_index_of to detemplitize it and allow 100% branch coverage.
164/// @tparam T the type of the value to search for
165/// @param value the value to search for
166/// @param elements the elements in the sequence
167/// @return The index of the first instance of <c>value</c> in <c>elements</c>, else @c elements.size() if not found.
168template <typename T>
169constexpr auto get_integer_sequence_index_of_impl(T value, std::initializer_list<T> elements) noexcept -> std::size_t {
170 std::size_t index{0U};
171 for (T const element : elements) {
172 if (element == value) {
173 return index;
174 }
175 ++index;
176 }
177 return index;
178}
179
180/// @brief Return the index of a value in an integer sequence
181/// @tparam T the type of the value to search for
182/// @tparam Value the value to search for
183/// @tparam Elements the elements in the sequence
184/// @return The index of <c>Value</c> in <c>Elements</c>
185/// @pre Value must be present in @c Elements else compilation will fail.
186template <typename T, T Value, T... Elements>
187constexpr auto get_integer_sequence_index_of(std::integer_sequence<T, Elements...>) noexcept -> std::size_t {
188 constexpr std::size_t index{get_integer_sequence_index_of_impl(Value, {Elements...})};
189 static_assert(index < sizeof...(Elements), "Value must be in input sequence!");
190 return index;
191}
192} // namespace integer_sequence_ops_detail
193
194// @cond INTERNAL
195// parasoft-begin-suppress AUTOSAR-A2_7_3-a-2 "False positive: Documented by reference"
196/// @copydoc arene::base::integer_sequence_index_of<Sequence, Value, bool>
197template <typename Sequence, typename Sequence::value_type Value>
198class integer_sequence_index_of<Sequence, Value, true>
199 : public std::integral_constant<
200 std::size_t,
201 integer_sequence_ops_detail::get_integer_sequence_index_of<typename Sequence::value_type, Value>(Sequence{}
202 )> {};
203// parasoft-end-suppress AUTOSAR-A2_7_3-a-2
204// @endcond
205
206/// @brief The index of the first occurrence of a value in a @c std::integer_sequence.
207/// @see arene::base::integer_sequence_index_of_v
208/// @pre @c Sequence must be an instantiation of @c std::integer_sequence.
209/// @pre @c value must occur in @c Sequence else compilation will fail.
210template <typename Sequence, typename Sequence::value_type Value>
211extern constexpr std::size_t integer_sequence_index_of_v = integer_sequence_index_of<Sequence, Value>::value;
212
213namespace integer_sequence_ops_detail {
214/// @brief Return the number of occurrences of a value in an integer sequence
215/// @tparam T the type of the value to search for
216/// @tparam Value the value to search for
217/// @tparam Elements the elements in the sequence
218/// @param value The element to search for
219/// @return The number of occurrences of @c value in @c Elements
220template <typename T, T... Elements>
221constexpr auto get_integer_sequence_count_of(T value, std::integer_sequence<T, Elements...>) noexcept -> std::size_t {
222 std::size_t count{0U};
223 for (T const element : {Elements...}) {
224 if (element == value) {
225 ++count;
226 }
227 }
228 return count;
229}
230/// @brief Return the number of occurrences of a value in an empty integer sequence
231/// @tparam T the type of the value to search for
232/// @return Zero
233template <typename T>
234constexpr auto get_integer_sequence_count_of(T, std::integer_sequence<T>) noexcept -> std::size_t {
235 return 0U;
236}
237
238} // namespace integer_sequence_ops_detail
239
240/// @brief Get the number of occurrences of a value in the provided sequence.
241/// The result is stored in the <c>value</c> member.
242/// @tparam Sequence the sequence to search
243/// @tparam value the value to search for
244/// @pre @c Sequence must be an instantiation of @c std::integer_sequence.
245template <typename Sequence, typename Sequence::value_type Value>
247 : public std::integral_constant<
248 std::size_t,
249 integer_sequence_ops_detail::get_integer_sequence_count_of(Value, Sequence{})> {};
250
251/// @brief Get the number of occurrences of a value in the provided sequence.
252/// @tparam Sequence the sequence to search
253/// @tparam value the value to search for
254/// @pre @c Sequence must be an instantiation of @c std::integer_sequence.
255template <typename Sequence, typename Sequence::value_type Value>
257
258namespace integer_sequence_ops_detail {
259/// @brief Create a new integer sequence with the unique values from the provided
260/// integer sequence. The values in the resultant sequence are in the order of
261/// their first occurrence in the provided sequence.
262/// The resultant sequence is returned in the <c>type</c> member type.
263/// @tparam Sequence the provided integer sequence
264/// @pre <c>Sequence</c> must be an instantiation of
265/// <c>std::integer_sequence</c>
266template <typename Sequence>
267class integer_sequence_unique_elements_impl;
268
269/// @brief Create a new integer sequence with the unique values from the provided
270/// integer sequence. The values in the resultant sequence are in the order of
271/// their first occurrence in the provided sequence.
272/// The resultant sequence is returned in the <c>type</c> member type.
273/// @tparam T the type of the integer sequence elements
274template <typename T>
275class integer_sequence_unique_elements_impl<std::integer_sequence<T>> {
276 public:
277 /// @brief The resulting sequence
278 using type = std::integer_sequence<T>;
279};
280
281/// @brief Merge two integer sequences together to produce a result with no duplicates.
282/// The resultant sequence is returned in the <c>type</c> member type.
283/// @tparam UniqueSequence The first sequence
284/// @tparam Sequence2 The second sequence
285/// @pre <c>UniqueSequence</c> and <c>Sequence2</c> must both be instantiations
286/// of <c>std::integer_sequence</c> with the same element type.
287/// <c>UniqueSequence</c> must not have any duplicate values.
288template <typename UniqueSequence, typename Sequence2>
289class integer_sequence_merge_unique_elements_impl;
290
291/// @brief Merge two integer sequences together to produce a result with no duplicates.
292/// The resultant sequence is returned in the <c>type</c> member type.
293/// @tparam UniqueSequence The first sequence
294/// @tparam T The type of elements in the second sequence
295/// @pre <c>UniqueSequence</c> must both be an instantiation of
296/// <c>std::integer_sequence</c> with an element type of <c>T</c>.
297/// <c>UniqueSequence</c> must not have any duplicate values
298template <typename UniqueSequence, typename T>
299class integer_sequence_merge_unique_elements_impl<UniqueSequence, std::integer_sequence<T>> {
300 public:
301 /// @brief The resulting sequence
302 using type = UniqueSequence;
303};
304
305/// @brief Merge two integer sequences together to produce a result with no duplicates.
306/// The resultant sequence is returned in the <c>type</c> member type.
307/// @tparam T The type of elements in the sequences
308/// @tparam UniqueElements The elements in the first sequence
309/// @tparam FirstElement The first element in the second sequence
310/// @tparam OtherElements The other elements in the second sequence
311/// @pre <c>UniqueElements</c> must not have any duplicate values
312template <typename T, T... UniqueElements, T FirstElement, T... OtherElements>
313class integer_sequence_merge_unique_elements_impl<
314 std::integer_sequence<T, UniqueElements...>,
315 std::integer_sequence<T, FirstElement, OtherElements...>> {
316 private:
317 /// @brief The current sequence of unique elements
318 using unique_sequence = std::integer_sequence<T, UniqueElements...>;
319 /// @brief A sequence of the remaining unprocessed elements from the second sequence
320 using remainder_sequence = std::integer_sequence<T, OtherElements...>;
321
322 public:
323 /// @brief The resulting sequence
324 using type = typename conditional_t<
325 integer_sequence_contains_v<unique_sequence, FirstElement>,
326 integer_sequence_merge_unique_elements_impl<unique_sequence, remainder_sequence>,
327 integer_sequence_merge_unique_elements_impl<
328 std::integer_sequence<T, UniqueElements..., FirstElement>,
329 remainder_sequence>>::type;
330};
331
332} // namespace integer_sequence_ops_detail
333
334/// @brief Create a new integer sequence with the unique values from the provided
335/// integer sequence. The values in the resultant sequence are in the order of
336/// their first occurrence in the provided sequence.
337/// @tparam Sequence the provided integer sequence
338/// @pre <c>Sequence</c> must be an instantiation of
339/// <c>std::integer_sequence</c>
340template <typename Sequence>
341using integer_sequence_unique_elements = typename integer_sequence_ops_detail::
342 integer_sequence_merge_unique_elements_impl<std::integer_sequence<typename Sequence::value_type>, Sequence>::type;
343
344} // namespace base
345} // namespace arene
346
347#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INTEGER_SEQUENCES_INTEGER_SEQUENCE_OPS_HPP_
Get the number of occurrences of a value in the provided sequence. The result is stored in the value ...
Definition integer_sequence_ops.hpp:249
Implementation of std::initializer_list for brace initialization.
Definition initializer_list.hpp:21
constexpr auto begin() const noexcept -> iterator
Get a pointer to the first element.
Definition initializer_list.hpp:46
Implements a compile-time sequence of integers.
Definition integer_sequence.hpp:32
A helper class to represent a compile-time constant with the specified type and value....
Definition integral_constant.hpp:23
Definition accumulate.hpp:37
ARENE_NODISCARD constexpr auto next(Iterator itr, typename std::iterator_traits< Iterator >::difference_type steps=1) noexcept(noexcept(::arene::base::advance(itr, steps))) -> Iterator
Produces the Nth successor (or predecessor if steps is negative) of an iterator.
Definition next.hpp:38
constexpr bool integer_sequence_contains_v
Checks if the specified integer sequence contains the specified value.
typename integer_sequence_ops_detail:: integer_sequence_merge_unique_elements_impl< std::integer_sequence< typename Sequence::value_type >, Sequence >::type integer_sequence_unique_elements
Create a new integer sequence with the unique values from the provided integer sequence....
Definition integer_sequence_ops.hpp:341
constexpr std::size_t integer_sequence_index_of_v
The index of the first occurrence of a value in a std::integer_sequence.
constexpr std::size_t integer_sequence_count_of_v
Get the number of occurrences of a value in the provided sequence.
typename integer_sequence_ops_detail::integer_sequence_cat_impl< Sequences... >::type integer_sequence_cat
Produces a single std::integer_sequence which is the concatenation of multiple std::integer_sequence.
Definition integer_sequence_ops.hpp:74
Definition accumulate.hpp:36
STL namespace.
::ptrdiff_t ptrdiff_t
A type suitable for holding the difference between two pointers.
Definition cstddef.hpp:31
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
Trait for the index of the first occurrence of a value in a std::integer_sequence.
Definition integer_sequence_ops.hpp:159