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:
Ali Mohammad Pur 2024-12-10 23:33:38 +01:00 committed by Nico Weber
parent 5fb6942726
commit df5640785b

View file

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