![]() |
Arene Base
Fundamental Utilities For Safety Critical C++
|
The integer_sequences sub-package provides a set of facilities for creating and manipulating sequences of integers. Mostly, these are represented using std::integer_sequence, for manipulation during compile time.
The public header is
The Bazel target is
std::integer_sequence is great for holding a list of integers, but it can be difficult to manipulate. The integer_sequences package provides a set of facilities to make such manipulation easier:
arene::base::integer_sequence_catarene::base::integer_sequence_element and arene::base::integer_sequence_element_varene::base::integer_sequence_contains_varene::base::integer_sequence_index_of andarene::base::integer_sequence_index_of_varene::base::integer_sequence_count_of and arene::base::integer_sequence_count_of_varene::base::integer_sequence_unique_elementsstd::index_sequence by repeating a single value a given number of times, with arene::base::make_index_sequence_repeat_nArene Base also provides arene::base::sequential_values, which is a variable template for an arene::base::array holding the specified number of objects, starting from 0.
arene::base::integer_sequence_cat is a template alias for a sequence that is the concatenation of the supplied sequences. All sequences must have the same element type.
Here, the resulting sequence is std::integer_sequence<std::int32_t, 10, 20, 30, 40, 99, -123, -456, 0, 3, 42>
arene::base::integer_sequence_element<Index, Sequence> derives from std::integral_constant<T, Value>, where T is the element type of the sequence, and Value is the Index-th element. Thus the value can be retrieved using the value member, or the function call operator.
On the other hand, arene::base::integer_sequence_element_v<Index, Sequence> is a variable template that holds the value of the Index-th element of Sequence directly.
Both these templates are undefined (and will thus fail to compile) if the index is greater than or equal to the number of elements in the sequence.
This falls to arene::base::integer_sequence_contains_v, which is a boolean variable template that is true if the element is in the sequence, and false otherwise.
Knowing that an element is present in a sequence is not always enough: sometimes you need to know the exact index where it occurs. arene::base::integer_sequence_index_of<Sequence, Value> is a struct that derives from std::integral_constant<std::size_t, Index>, where Index is the index of Value in Sequence; arene::base::integer_sequence_index_of_v<Sequence, Value> is a variable template that holds the index value directly.
Both these templates are undefined, and will cause a compilation error, if the element is not in the sequence.
arene::base::integer_sequence_count_of<Sequence, Value> and arene::base::integer_sequence_count_of_v<Sequence, Value> answer the perennial question how many times does Value appear in Sequence? arene::base::integer_sequence_count_of<Sequence, Value> is a struct that derives from std::integral_constant<std::size_t, Count>, where Count is the number of times Value appears in Sequence; arene::base::integer_sequence_count_of_v<Sequence, Value> is a variable template that holds the count value directly.
If Value does not appear in Sequence, then the value is just zero.
If you want to ensure that arene::base::integer_sequence_count_of_v<Sequence, Value> is either 0 or 1 for some Sequence, whatever Value is passed, then arene::base::integer_sequence_unique_elements<Sequence> is what you need. It is a type alias for a new std::integer_sequence that removes any duplicated elements from the original Sequence, so there is one copy of each value. In each case, it is the first element of each value that is preserved.
arene::base::make_index_sequence_repeat_n<Value, Count> is a template alias for an std::index_sequence whose elements are Count copies of Value.
When Count is 0, the resulting sequence is empty:
arene::base::sequential_values<Type, Count> is a variable template, which is an arene::base::array<Type, Count> holding the values 0 to Count - 1. Thus arene::base::sequential_values<std::int16_t, 10> is an array of 10 std::int16_t objects with the values 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
This can be used to initialize other containers:
This will initialize the vector with the values 0 to 24.