mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-22 09:21:57 -05: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. (cherry picked from commit 44798f44ef1c112aa9dbee7abee7ff6a5568c4f4)
This commit is contained in:
parent
5fb6942726
commit
df5640785b
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…
Reference in a new issue