diff options
author | Federico Guerinoni <guerinoni.federico@gmail.com> | 2022-03-05 14:05:56 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-03-09 17:16:28 +0100 |
commit | 0aed2f0f868e2b5ec00b0ad77e037b77d40b9967 (patch) | |
tree | d7286ae4267b8016bf4dc13c9a0478295012c201 | |
parent | f34fff852bff6ff6ab703f0b9b9327f345a811e3 (diff) | |
download | serenity-0aed2f0f868e2b5ec00b0ad77e037b77d40b9967.zip |
AK: Add reverse iterator as member
-rw-r--r-- | AK/NonnullPtrVector.h | 5 | ||||
-rw-r--r-- | AK/Vector.h | 5 | ||||
-rw-r--r-- | Tests/AK/TestVector.cpp | 4 |
3 files changed, 14 insertions, 0 deletions
diff --git a/AK/NonnullPtrVector.h b/AK/NonnullPtrVector.h index 728d37314d..c628196c1a 100644 --- a/AK/NonnullPtrVector.h +++ b/AK/NonnullPtrVector.h @@ -41,6 +41,11 @@ public: ALWAYS_INLINE constexpr Iterator end() { return Iterator::end(*this); } ALWAYS_INLINE constexpr ReverseIterator rend() { return ReverseIterator::rend(*this); } + ALWAYS_INLINE constexpr auto in_reverse() + { + return ReverseWrapper::in_reverse(*this); + } + ALWAYS_INLINE PtrType& ptr_at(size_t index) { return Base::at(index); } ALWAYS_INLINE const PtrType& ptr_at(size_t index) const { return Base::at(index); } diff --git a/AK/Vector.h b/AK/Vector.h index bc4eb553dd..9c81689d80 100644 --- a/AK/Vector.h +++ b/AK/Vector.h @@ -704,6 +704,11 @@ public: Iterator end() { return Iterator::end(*this); } ReverseIterator rend() { return ReverseIterator::rend(*this); } + ALWAYS_INLINE constexpr auto in_reverse() + { + return ReverseWrapper::in_reverse(*this); + } + template<typename TUnaryPredicate> ConstIterator find_if(TUnaryPredicate&& finder) const { diff --git a/Tests/AK/TestVector.cpp b/Tests/AK/TestVector.cpp index fe10b017cf..74038f3788 100644 --- a/Tests/AK/TestVector.cpp +++ b/Tests/AK/TestVector.cpp @@ -567,4 +567,8 @@ TEST_CASE(reverse_range_for_loop) int index = 9; for (auto item : AK::ReverseWrapper::in_reverse(v)) EXPECT_EQ(item, index--); + + index = 9; + for (auto item : v.in_reverse()) + EXPECT_EQ(item, index--); } |