From 25a1bf0c907818f6ec7b0eec5e522726a3cd520e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 27 Jun 2019 12:04:27 +0200 Subject: [PATCH] AK: Add NonnullRefPtrVector. This is a slot-in convenience replacement for Vector> that makes accessors return T& instead of NonnullRefPtr&. Since NonnullRefPtr guarantees non-nullness, this allows you to access these vector elements using dot (.) rather than arrow (->). :^) --- AK/NonnullRefPtrVector.h | 35 +++++++++ AK/Vector.h | 158 ++++++++++++++++++++------------------- 2 files changed, 117 insertions(+), 76 deletions(-) create mode 100644 AK/NonnullRefPtrVector.h diff --git a/AK/NonnullRefPtrVector.h b/AK/NonnullRefPtrVector.h new file mode 100644 index 00000000000..a87a6086149 --- /dev/null +++ b/AK/NonnullRefPtrVector.h @@ -0,0 +1,35 @@ +#pragma once + +#include +#include + +namespace AK { + +template +class NonnullRefPtrVector : public Vector, inline_capacity> { + typedef Vector> Base; + +public: + NonnullRefPtrVector(Vector>&& other) + : Base(static_cast(other)) + { + } + NonnullRefPtrVector(const Vector>& other) + : Base(static_cast(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; diff --git a/AK/Vector.h b/AK/Vector.h index adc0e768238..559eaa8500e 100644 --- a/AK/Vector.h +++ b/AK/Vector.h @@ -10,6 +10,86 @@ namespace AK { +template class Vector; + +template +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 +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: @@ -332,85 +412,11 @@ public: m_size = new_size; } - class Iterator { - 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 }; - }; - + using Iterator = VectorIterator; Iterator begin() { return Iterator(*this, 0); } Iterator end() { return Iterator(*this, size()); } - class ConstIterator { - 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 }; - }; - + using ConstIterator = ConstVectorIterator; ConstIterator begin() const { return ConstIterator(*this, 0); } ConstIterator end() const { return ConstIterator(*this, size()); }