mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-10-05 17:49:16 +03:00
AK: Add to_array()
This is useful if you want an array with an explicit type but still want its size to be inferred.
This commit is contained in:
parent
10216e1743
commit
d84b69ace9
Notes:
sideshowbarker
2024-07-17 11:30:54 +09:00
Author: https://github.com/nico Commit: https://github.com/SerenityOS/serenity/commit/d84b69ace9 Pull-request: https://github.com/SerenityOS/serenity/pull/23129
15
AK/Array.h
15
AK/Array.h
@ -162,9 +162,24 @@ constexpr auto iota_array(T const offset = {})
|
|||||||
return Detail::integer_sequence_generate_array<T>(offset, MakeIntegerSequence<T, N>());
|
return Detail::integer_sequence_generate_array<T>(offset, MakeIntegerSequence<T, N>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace Detail {
|
||||||
|
template<typename T, size_t N, size_t... Is>
|
||||||
|
constexpr auto to_array_impl(T (&&a)[N], IndexSequence<Is...>) -> Array<T, sizeof...(Is)>
|
||||||
|
{
|
||||||
|
return { { a[Is]... } };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t N>
|
||||||
|
constexpr auto to_array(T (&&a)[N])
|
||||||
|
{
|
||||||
|
return Detail::to_array_impl(move(a), MakeIndexSequence<N>());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if USING_AK_GLOBALLY
|
#if USING_AK_GLOBALLY
|
||||||
using AK::Array;
|
using AK::Array;
|
||||||
using AK::iota_array;
|
using AK::iota_array;
|
||||||
|
using AK::to_array;
|
||||||
#endif
|
#endif
|
||||||
|
@ -46,3 +46,12 @@ TEST_CASE(first_index_of)
|
|||||||
EXPECT(array.first_index_of(7) == 7u);
|
EXPECT(array.first_index_of(7) == 7u);
|
||||||
EXPECT(!array.first_index_of(42).has_value());
|
EXPECT(!array.first_index_of(42).has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE(to_array)
|
||||||
|
{
|
||||||
|
constexpr auto array = to_array<u8>({ 0, 2, 1 });
|
||||||
|
static_assert(array.size() == 3);
|
||||||
|
static_assert(array[0] == 0);
|
||||||
|
static_assert(array[1] == 2);
|
||||||
|
static_assert(array[2] == 1);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user