mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 05:25:32 +03:00
AK: Get rid of ConstVectorIterator.
We can achieve the same with just a VectorIterator<const Vector, const T>.
This commit is contained in:
parent
ebe108efa6
commit
50700c107f
Notes:
sideshowbarker
2024-07-19 13:28:13 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/50700c107fc
@ -29,7 +29,7 @@ public:
|
||||
Iterator begin() { return Iterator(*this, 0); }
|
||||
Iterator end() { return Iterator(*this, size()); }
|
||||
|
||||
using ConstIterator = ConstVectorIterator<NonnullRefPtrVector, T>;
|
||||
using ConstIterator = VectorIterator<const NonnullRefPtrVector, const T>;
|
||||
ConstIterator begin() const { return ConstIterator(*this, 0); }
|
||||
ConstIterator end() const { return ConstIterator(*this, size()); }
|
||||
|
||||
|
3
AK/Tests/.gitignore
vendored
3
AK/Tests/.gitignore
vendored
@ -1,2 +1,5 @@
|
||||
TestString
|
||||
TestQueue
|
||||
TestVector
|
||||
*.d
|
||||
*.o
|
||||
|
@ -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
|
||||
|
45
AK/Tests/TestVector.cpp
Normal file
45
AK/Tests/TestVector.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
#include "TestHelpers.h"
|
||||
#include <AK/AKString.h>
|
||||
#include <AK/Vector.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
EXPECT(Vector<int>().is_empty());
|
||||
EXPECT(Vector<int>().size() == 0);
|
||||
|
||||
Vector<int> 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<String> 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<const Vector<String>&>(strings))) {
|
||||
EXPECT(!string.is_null());
|
||||
EXPECT(!string.is_empty());
|
||||
++loop_counter;
|
||||
}
|
||||
EXPECT_EQ(loop_counter, 2);
|
||||
|
||||
return 0;
|
||||
}
|
41
AK/Vector.h
41
AK/Vector.h
@ -51,45 +51,6 @@ private:
|
||||
int m_index { 0 };
|
||||
};
|
||||
|
||||
template<typename VectorType, typename ElementType>
|
||||
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<typename T, int inline_capacity = 0>
|
||||
class Vector {
|
||||
public:
|
||||
@ -416,7 +377,7 @@ public:
|
||||
Iterator begin() { return Iterator(*this, 0); }
|
||||
Iterator end() { return Iterator(*this, size()); }
|
||||
|
||||
using ConstIterator = ConstVectorIterator<Vector, T>;
|
||||
using ConstIterator = VectorIterator<const Vector, const T>;
|
||||
ConstIterator begin() const { return ConstIterator(*this, 0); }
|
||||
ConstIterator end() const { return ConstIterator(*this, size()); }
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user