Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
integer_sequences: Facilities For Manipulating std::integer_sequence

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

//:integer_sequences

Introduction

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 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.

Concatenating Sequences

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.

using sequence2 = std::integer_sequence<std::int32_t, 99, -123, -456>;
Implements a compile-time sequence of integers.
Definition integer_sequence.hpp:32
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:73
::int32_t int32_t
A 32-bit signed integer type.
Definition cstdint.hpp:66

Here, the resulting sequence is std::integer_sequence<std::int32_t, 10, 20, 30, 40, 99, -123, -456, 0, 3, 42>

Getting A Specific Element

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.

using values = std::integer_sequence<std::int32_t, 10, 20, 30, 40, 99, -123, -456, 0, 3, 42>;
static_assert(arene::base::integer_sequence_element<5, values>::value == -123, "got the correct element");
static_assert(arene::base::integer_sequence_element_v<6, values> == -456, "got the correct element");
Trait providing the Index th element of a std::integer_sequence. The value member holds the value....
Definition integer_sequence_ops.hpp:81
constexpr auto integer_sequence_element_v
The value of the Index th element of a std::integer_sequence.

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.

Checking If An Element Is Present In A 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.

using values = std::integer_sequence<std::int32_t, 10, 20, 30, 40, 99, -123, -456, 0, 3, 42>;
static_assert(arene::base::integer_sequence_contains_v<values, 3>,"Value is present");
static_assert(!arene::base::integer_sequence_contains_v<values, 500>,"Value is not present");
constexpr bool integer_sequence_contains_v
Checks if the specified integer sequence contains the specified value.

Finding The Index Of An Element In A Sequence

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.

using values = std::integer_sequence<std::int32_t, 10, 20, 30, 40, 99, -123, -456, 0, 3, 42>;
static_assert(arene::base::integer_sequence_index_of<values, 3>::value == 8,"Value is at index 8");
static_assert(arene::base::integer_sequence_index_of_v<values, 40> == 3,"Value is at index 3");
// const auto val = arene::base::integer_sequence_index_of_v<values, 500>; // compilation error
constexpr std::size_t integer_sequence_index_of_v
The index of the first occurrence of a value in a std::integer_sequence.
Trait for the index of the first occurrence of a value in a std::integer_sequence.
Definition integer_sequence_ops.hpp:158

Counting Copies Of An Element In A 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.

static_assert(arene::base::integer_sequence_count_of<values, 1>::value == 5,"1 appears 5 times");
static_assert(arene::base::integer_sequence_count_of_v<values, 40> == 0,"40 appears 0 times");
static_assert(arene::base::integer_sequence_count_of_v<values, 5> == 1,"5 appears once");
constexpr std::size_t integer_sequence_count_of_v
Get the number of occurrences of a value in the provided sequence.

Removing Duplicates

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.

using unique_values = arene::base::integer_sequence_unique_values<values>;
static_assert(arene::base::integer_sequence_count_of_v<unique_values, 40> == 0,"40 appears 0 times");
Trait to detect if the two template parameters are the same or not. The value member is true if the t...
Definition is_same.hpp:35

Repeating A Value

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.

static_assert(std::is_same<repeated, std::index_sequence<3, 3, 3, 3>>::value, "4 copies of 3");
repeat_type_t< Count, make_index_sequence_repeat_n_detail::to_index_sequence, std::integral_constant< std::size_t, Value > > make_index_sequence_repeat_n
An std::index_sequence whose elements are Count copies of Value.
Definition make_index_sequence_repeat_n.hpp:31

When Count is 0, the resulting sequence is empty:

static_assert(std::is_same<empty, std::index_sequence<>>::value, "no copies yields empty sequence");

Sequential Values

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:

std::vector<std::uint8_t> vec(arene::base::sequential_values<std::uint8_t,25>);
constexpr auto sequential_values
An array holding a sequence of integer values starting at 0, incrementing by 1 each time.

This will initialize the vector with the values 0 to 24.