diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2023-04-21 11:21:47 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-04-21 20:44:47 +0100 |
commit | 892470a91228e01d973230cd76327bbf5187319e (patch) | |
tree | db13c3a3de7f8ae60f03e72685ad1b23f7f2443d /AK/Array.h | |
parent | 955528055cf9415a94588e17d1e6a8cf95b05710 (diff) | |
download | serenity-892470a91228e01d973230cd76327bbf5187319e.zip |
AK: Add Array::contains_slow() and ::first_index_of(), with tests :^)
Diffstat (limited to 'AK/Array.h')
-rw-r--r-- | AK/Array.h | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/AK/Array.h b/AK/Array.h index 3bb11998a3..52ffa5560e 100644 --- a/AK/Array.h +++ b/AK/Array.h @@ -7,6 +7,7 @@ #pragma once #include <AK/Iterator.h> +#include <AK/Optional.h> #include <AK/Span.h> #include <AK/StdLibExtras.h> #include <AK/TypedTransfer.h> @@ -119,6 +120,20 @@ struct Array { return value; } + bool contains_slow(T const& value) const + { + return first_index_of(value).has_value(); + } + + Optional<size_t> first_index_of(T const& value) const + { + for (size_t i = 0; i < Size; ++i) { + if (__data[i] == value) + return i; + } + return {}; + } + Conditional<Size == 0, Detail::EmptyArrayStorage<T>, T[Size]> __data; }; |