From ff8ca811c786e23d0c35408f911dc13aab5f5cfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Mon, 4 Apr 2022 18:19:00 +0200 Subject: [PATCH] AK: Add find_first_index to NonnullPtrVector that strips smart pointer When we want to use the find_first_index that base Vector provides, we need to provide an element of the real contained type. That's impossible for OwnPtr, however, and even with RefPtr there might be instances where we have a raw reference to the object we want to find, but no smart pointer. Therefore, overloading this function (with an identical body, the magic is done by the find_index templatization) with `T const&` as a parameter allows there use cases. --- AK/NonnullPtrVector.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/AK/NonnullPtrVector.h b/AK/NonnullPtrVector.h index b71f85c002..c95e8c2a50 100644 --- a/AK/NonnullPtrVector.h +++ b/AK/NonnullPtrVector.h @@ -47,6 +47,15 @@ public: ALWAYS_INLINE constexpr auto in_reverse() { return ReverseWrapper::in_reverse(*this); } ALWAYS_INLINE constexpr auto in_reverse() const { return ReverseWrapper::in_reverse(*this); } + Optional find_first_index(T const& value) const + { + if (auto const index = AK::find_index(begin(), end(), value); + index < size()) { + return index; + } + return {}; + } + ALWAYS_INLINE PtrType& ptr_at(size_t index) { return Base::at(index); } ALWAYS_INLINE PtrType const& ptr_at(size_t index) const { return Base::at(index); }