1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 10:07:44 +00:00

AK: Add Array::contains_slow() and ::first_index_of(), with tests :^)

This commit is contained in:
Sam Atkins 2023-04-21 11:21:47 +01:00 committed by Linus Groh
parent 955528055c
commit 892470a912
2 changed files with 33 additions and 0 deletions

View file

@ -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;
};