mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:47:45 +00:00
AK: Add NonnullRefPtrVector<T>.
This is a slot-in convenience replacement for Vector<NonnullRefPtr<T>> that makes accessors return T& instead of NonnullRefPtr<T>&. Since NonnullRefPtr guarantees non-nullness, this allows you to access these vector elements using dot (.) rather than arrow (->). :^)
This commit is contained in:
parent
f83263a72b
commit
25a1bf0c90
2 changed files with 117 additions and 76 deletions
35
AK/NonnullRefPtrVector.h
Normal file
35
AK/NonnullRefPtrVector.h
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/NonnullRefPtr.h>
|
||||||
|
#include <AK/Vector.h>
|
||||||
|
|
||||||
|
namespace AK {
|
||||||
|
|
||||||
|
template<typename T, int inline_capacity = 0>
|
||||||
|
class NonnullRefPtrVector : public Vector<NonnullRefPtr<T>, inline_capacity> {
|
||||||
|
typedef Vector<NonnullRefPtr<T>> Base;
|
||||||
|
|
||||||
|
public:
|
||||||
|
NonnullRefPtrVector(Vector<NonnullRefPtr<T>>&& other)
|
||||||
|
: Base(static_cast<Base&&>(other))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
NonnullRefPtrVector(const Vector<NonnullRefPtr<T>>& other)
|
||||||
|
: Base(static_cast<const Base&>(other))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
using Base::size;
|
||||||
|
T& at(int index) { return *Base::at(index); }
|
||||||
|
const T& at(int index) const { return *Base::at(index); }
|
||||||
|
T& operator[](int index) { return at(index); }
|
||||||
|
const T& operator[](int index) const { return at(index); }
|
||||||
|
T& first() { return at(0); }
|
||||||
|
const T& first() const { return at(0); }
|
||||||
|
T& last() { return at(size() - 1); }
|
||||||
|
const T& last() const { return at(size() - 1); }
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
using AK::NonnullRefPtrVector;
|
158
AK/Vector.h
158
AK/Vector.h
|
@ -10,6 +10,86 @@
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
|
template<typename T, int inline_capacity> class Vector;
|
||||||
|
|
||||||
|
template<typename VectorType, typename ElementType>
|
||||||
|
class VectorIterator {
|
||||||
|
public:
|
||||||
|
bool operator!=(const VectorIterator& other) { return m_index != other.m_index; }
|
||||||
|
bool operator==(const VectorIterator& other) { return m_index == other.m_index; }
|
||||||
|
bool operator<(const VectorIterator& other) { return m_index < other.m_index; }
|
||||||
|
bool operator>(const VectorIterator& other) { return m_index > other.m_index; }
|
||||||
|
bool operator>=(const VectorIterator& other) { return m_index >= other.m_index; }
|
||||||
|
VectorIterator& operator++()
|
||||||
|
{
|
||||||
|
++m_index;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
VectorIterator& operator--()
|
||||||
|
{
|
||||||
|
--m_index;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
VectorIterator operator-(int value) { return { m_vector, m_index - value }; }
|
||||||
|
VectorIterator operator+(int value) { return { m_vector, m_index + value }; }
|
||||||
|
VectorIterator& operator=(const VectorIterator& other)
|
||||||
|
{
|
||||||
|
m_index = other.m_index;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
ElementType& operator*() { return m_vector[m_index]; }
|
||||||
|
int operator-(const VectorIterator& other) { return m_index - other.m_index; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
friend VectorType;
|
||||||
|
VectorIterator(VectorType& vector, int index)
|
||||||
|
: m_vector(vector)
|
||||||
|
, m_index(index)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
VectorType& m_vector;
|
||||||
|
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>
|
template<typename T, int inline_capacity = 0>
|
||||||
class Vector {
|
class Vector {
|
||||||
public:
|
public:
|
||||||
|
@ -332,85 +412,11 @@ public:
|
||||||
m_size = new_size;
|
m_size = new_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
class Iterator {
|
using Iterator = VectorIterator<Vector, T>;
|
||||||
public:
|
|
||||||
bool operator!=(const Iterator& other) { return m_index != other.m_index; }
|
|
||||||
bool operator==(const Iterator& other) { return m_index == other.m_index; }
|
|
||||||
bool operator<(const Iterator& other) { return m_index < other.m_index; }
|
|
||||||
bool operator>(const Iterator& other) { return m_index > other.m_index; }
|
|
||||||
bool operator>=(const Iterator& other) { return m_index >= other.m_index; }
|
|
||||||
Iterator& operator++()
|
|
||||||
{
|
|
||||||
++m_index;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
Iterator& operator--()
|
|
||||||
{
|
|
||||||
--m_index;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
Iterator operator-(int value) { return { m_vector, m_index - value }; }
|
|
||||||
Iterator operator+(int value) { return { m_vector, m_index + value }; }
|
|
||||||
Iterator& operator=(const Iterator& other)
|
|
||||||
{
|
|
||||||
m_index = other.m_index;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
T& operator*() { return m_vector[m_index]; }
|
|
||||||
int operator-(const Iterator& other) { return m_index - other.m_index; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
friend class Vector;
|
|
||||||
Iterator(Vector& vector, int index)
|
|
||||||
: m_vector(vector)
|
|
||||||
, m_index(index)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
Vector& m_vector;
|
|
||||||
int m_index { 0 };
|
|
||||||
};
|
|
||||||
|
|
||||||
Iterator begin() { return Iterator(*this, 0); }
|
Iterator begin() { return Iterator(*this, 0); }
|
||||||
Iterator end() { return Iterator(*this, size()); }
|
Iterator end() { return Iterator(*this, size()); }
|
||||||
|
|
||||||
class ConstIterator {
|
using ConstIterator = ConstVectorIterator<Vector, T>;
|
||||||
public:
|
|
||||||
bool operator!=(const ConstIterator& other) { return m_index != other.m_index; }
|
|
||||||
bool operator==(const ConstIterator& other) { return m_index == other.m_index; }
|
|
||||||
bool operator<(const ConstIterator& other) { return m_index < other.m_index; }
|
|
||||||
bool operator>(const ConstIterator& other) { return m_index > other.m_index; }
|
|
||||||
bool operator>=(const ConstIterator& other) { return m_index >= other.m_index; }
|
|
||||||
ConstIterator& operator++()
|
|
||||||
{
|
|
||||||
++m_index;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
ConstIterator& operator--()
|
|
||||||
{
|
|
||||||
--m_index;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
ConstIterator operator-(int value) { return { m_vector, m_index - value }; }
|
|
||||||
ConstIterator operator+(int value) { return { m_vector, m_index + value }; }
|
|
||||||
ConstIterator& operator=(const ConstIterator& other)
|
|
||||||
{
|
|
||||||
m_index = other.m_index;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
const T& operator*() const { return m_vector[m_index]; }
|
|
||||||
int operator-(const ConstIterator& other) { return m_index - other.m_index; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
friend class Vector;
|
|
||||||
ConstIterator(const Vector& vector, const int index)
|
|
||||||
: m_vector(vector)
|
|
||||||
, m_index(index)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
const Vector& m_vector;
|
|
||||||
int m_index { 0 };
|
|
||||||
};
|
|
||||||
|
|
||||||
ConstIterator begin() const { return ConstIterator(*this, 0); }
|
ConstIterator begin() const { return ConstIterator(*this, 0); }
|
||||||
ConstIterator end() const { return ConstIterator(*this, size()); }
|
ConstIterator end() const { return ConstIterator(*this, size()); }
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue