From 50700c107fcde9d82e04c3fb86ffac299121bff2 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 27 Jun 2019 14:50:22 +0200 Subject: [PATCH] AK: Get rid of ConstVectorIterator. We can achieve the same with just a VectorIterator. --- AK/NonnullRefPtrVector.h | 2 +- AK/Tests/.gitignore | 3 +++ AK/Tests/Makefile | 5 ++++- AK/Tests/TestVector.cpp | 45 ++++++++++++++++++++++++++++++++++++++++ AK/Vector.h | 41 +----------------------------------- 5 files changed, 54 insertions(+), 42 deletions(-) create mode 100644 AK/Tests/TestVector.cpp diff --git a/AK/NonnullRefPtrVector.h b/AK/NonnullRefPtrVector.h index 151863fe836be4..f5e438f3de9443 100644 --- a/AK/NonnullRefPtrVector.h +++ b/AK/NonnullRefPtrVector.h @@ -29,7 +29,7 @@ class NonnullRefPtrVector : public Vector, inline_capacity> { Iterator begin() { return Iterator(*this, 0); } Iterator end() { return Iterator(*this, size()); } - using ConstIterator = ConstVectorIterator; + using ConstIterator = VectorIterator; ConstIterator begin() const { return ConstIterator(*this, 0); } ConstIterator end() const { return ConstIterator(*this, size()); } diff --git a/AK/Tests/.gitignore b/AK/Tests/.gitignore index 2e42a5a41ccabc..3e8c653d02feb8 100644 --- a/AK/Tests/.gitignore +++ b/AK/Tests/.gitignore @@ -1,2 +1,5 @@ TestString TestQueue +TestVector +*.d +*.o diff --git a/AK/Tests/Makefile b/AK/Tests/Makefile index dd7fc4a8e87433..1a87af2faa1e16 100644 --- a/AK/Tests/Makefile +++ b/AK/Tests/Makefile @@ -1,4 +1,4 @@ -all: TestString TestQueue +all: TestString TestQueue TestVector CXXFLAGS = -std=c++17 -Wall -Wextra @@ -8,5 +8,8 @@ TestString: TestString.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp TestQueue: TestQueue.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp TestHelpers.h $(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestQueue.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp +TestVector: TestVector.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp TestHelpers.h + $(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestVector.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp + clean: rm -f TestString TestQueue diff --git a/AK/Tests/TestVector.cpp b/AK/Tests/TestVector.cpp new file mode 100644 index 00000000000000..182a3acbe46a24 --- /dev/null +++ b/AK/Tests/TestVector.cpp @@ -0,0 +1,45 @@ +#include "TestHelpers.h" +#include +#include + +int main() +{ + EXPECT(Vector().is_empty()); + EXPECT(Vector().size() == 0); + + Vector ints; + ints.append(1); + ints.append(2); + ints.append(3); + EXPECT_EQ(ints.size(), 3); + EXPECT_EQ(ints.take_last(), 3); + EXPECT_EQ(ints.size(), 2); + EXPECT_EQ(ints.take_last(), 2); + EXPECT_EQ(ints.size(), 1); + EXPECT_EQ(ints.take_last(), 1); + EXPECT_EQ(ints.size(), 0); + + ints.clear(); + EXPECT_EQ(ints.size(), 0); + + Vector strings; + strings.append("ABC"); + strings.append("DEF"); + + int loop_counter = 0; + for (const String& string : strings) { + EXPECT(!string.is_null()); + EXPECT(!string.is_empty()); + ++loop_counter; + } + + loop_counter = 0; + for (auto& string : (const_cast&>(strings))) { + EXPECT(!string.is_null()); + EXPECT(!string.is_empty()); + ++loop_counter; + } + EXPECT_EQ(loop_counter, 2); + + return 0; +} diff --git a/AK/Vector.h b/AK/Vector.h index 559eaa8500eaf8..e639ba6c1e0995 100644 --- a/AK/Vector.h +++ b/AK/Vector.h @@ -51,45 +51,6 @@ class VectorIterator { int m_index { 0 }; }; -template -class ConstVectorIterator { -public: - bool operator!=(const ConstVectorIterator& other) { return m_index != other.m_index; } - bool operator==(const ConstVectorIterator& other) { return m_index == other.m_index; } - bool operator<(const ConstVectorIterator& other) { return m_index < other.m_index; } - bool operator>(const ConstVectorIterator& other) { return m_index > other.m_index; } - bool operator>=(const ConstVectorIterator& other) { return m_index >= other.m_index; } - ConstVectorIterator& operator++() - { - ++m_index; - return *this; - } - ConstVectorIterator& operator--() - { - --m_index; - return *this; - } - ConstVectorIterator operator-(int value) { return { m_vector, m_index - value }; } - ConstVectorIterator operator+(int value) { return { m_vector, m_index + value }; } - ConstVectorIterator& operator=(const ConstVectorIterator& other) - { - m_index = other.m_index; - return *this; - } - const ElementType& operator*() const { return m_vector[m_index]; } - int operator-(const ConstVectorIterator& other) { return m_index - other.m_index; } - -private: - friend VectorType; - ConstVectorIterator(const VectorType& vector, const int index) - : m_vector(vector) - , m_index(index) - { - } - const VectorType& m_vector; - int m_index { 0 }; -}; - template class Vector { public: @@ -416,7 +377,7 @@ class Vector { Iterator begin() { return Iterator(*this, 0); } Iterator end() { return Iterator(*this, size()); } - using ConstIterator = ConstVectorIterator; + using ConstIterator = VectorIterator; ConstIterator begin() const { return ConstIterator(*this, 0); } ConstIterator end() const { return ConstIterator(*this, size()); }