mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 08:07:44 +00:00
AK: Add Vector::insert_before_matching(T&&, callback);
This allows you to do things like: vector.insert_before_matching(value, [](auto& entry) { return value < entry; }); Basically it scans until it finds an element that matches the condition callback and then inserts the new value before the matching element.
This commit is contained in:
parent
57da8792fd
commit
55a5c46253
3 changed files with 47 additions and 0 deletions
12
AK/Vector.h
12
AK/Vector.h
|
@ -243,6 +243,18 @@ public:
|
|||
insert(index, T(value));
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
void insert_before_matching(T&& value, C callback)
|
||||
{
|
||||
for (int i = 0; i < size(); ++i) {
|
||||
if (callback(at(i))) {
|
||||
insert(i, move(value));
|
||||
return;
|
||||
}
|
||||
}
|
||||
append(move(value));
|
||||
}
|
||||
|
||||
Vector& operator=(const Vector& other)
|
||||
{
|
||||
if (this != &other) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue