diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2021-02-07 15:24:36 +0330 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-02-08 18:08:55 +0100 |
commit | f0d85acc945ac50912c6044050e375cea18d3883 (patch) | |
tree | 0e0853517bd0be2673a86b1c303abc7758d7478f | |
parent | de947acaf9eda8d1ed69db56da2c2805e512cf5d (diff) | |
download | serenity-f0d85acc945ac50912c6044050e375cea18d3883.zip |
AK: Add an iota_array() function that can generate an array
...of increasing values with an optional offset.
-rw-r--r-- | AK/Array.h | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/AK/Array.h b/AK/Array.h index 6ed6db42a7..ef82eb34ff 100644 --- a/AK/Array.h +++ b/AK/Array.h @@ -84,6 +84,22 @@ struct Array { template<typename T, typename... Types> Array(T, Types...) -> Array<T, sizeof...(Types) + 1>; +namespace Detail { +template<typename T, unsigned long... Is> +constexpr auto integer_sequence_generate_array([[maybe_unused]] const T offset, IntegerSequence<T, Is...>) -> Array<T, sizeof...(Is)> +{ + return { { (offset + Is)... } }; +} +} + +template<typename T, T N> +constexpr static auto iota_array(const T offset = {}) +{ + static_assert(N >= T {}, "Negative sizes not allowed in iota_array()"); + return Detail::integer_sequence_generate_array<T>(offset, MakeIntegerSequence<T, N>()); +} + } using AK::Array; +using AK::iota_array; |