mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-09 17:44:56 +09:00
AK: Add Vector::get(index) convenience function
This is similar to HashMap::get(key), which returns an Optional, empty if the index is out of bounds for the vector.
This commit is contained in:
parent
f8092455e2
commit
44798f44ef
Notes:
github-actions[bot]
2024-12-13 09:01:34 +00:00
Author: https://github.com/alimpfard
Commit: 44798f44ef
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2410
Reviewed-by: https://github.com/LucasChollet
Reviewed-by: https://github.com/konradekk
1 changed files with 14 additions and 0 deletions
14
AK/Vector.h
14
AK/Vector.h
|
@ -155,6 +155,20 @@ public:
|
|||
ALWAYS_INLINE VisibleType const& operator[](size_t i) const { return at(i); }
|
||||
ALWAYS_INLINE VisibleType& operator[](size_t i) { return at(i); }
|
||||
|
||||
Optional<VisibleType&> get(size_t i)
|
||||
{
|
||||
if (i >= size())
|
||||
return {};
|
||||
return at(i);
|
||||
}
|
||||
|
||||
Optional<VisibleType const&> get(size_t i) const
|
||||
{
|
||||
if (i >= size())
|
||||
return {};
|
||||
return at(i);
|
||||
}
|
||||
|
||||
VisibleType const& first() const { return at(0); }
|
||||
VisibleType& first() { return at(0); }
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue