From 569945f06d45e01e37952f46dd422b06e9e0d943 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Wed, 15 Nov 2023 18:43:35 -0800 Subject: [PATCH] [Glacier] Cleanup formatting for Array and Vector. --- lib/glacier/container/array.h | 61 ++++++++++------- lib/glacier/container/array_view.h | 11 +++- lib/glacier/container/vector.h | 101 +++++++++++++---------------- 3 files changed, 90 insertions(+), 83 deletions(-) diff --git a/lib/glacier/container/array.h b/lib/glacier/container/array.h index 2280a06..171550b 100644 --- a/lib/glacier/container/array.h +++ b/lib/glacier/container/array.h @@ -9,33 +9,17 @@ namespace glcr { template class Array { public: + // Constructors. Array() : data_(nullptr), size_(0) {} - explicit Array(uint64_t size) : data_(new T[size]), size_(size) {} - - Array(const ArrayView view) : Array(view.size()) { - for (uint64_t i = 0; i < size_; i++) { - data_[i] = view[i]; - } - } - Array(const Array&) = delete; + Array& operator=(const Array&) = delete; - Array(Array&& other) : data_(other.data_), size_(other.size_) { - other.data_ = nullptr; - other.size_ = 0; - } + Array(Array&&); + Array& operator=(Array&&); - Array& operator=(Array&& other) { - if (data_) { - delete[] data_; - } - data_ = other.data_; - size_ = other.size_; - other.data_ = nullptr; - other.size_ = 0; - return *this; - } + explicit Array(uint64_t size) : data_(new T[size]), size_(size) {} + Array(const ArrayView& view); ~Array() { if (data_) { @@ -43,17 +27,44 @@ class Array { } } + // Accessors. + T& operator[](uint64_t index) { return data_[index]; } + const T& operator[](uint64_t index) const { return data_[index]; } + T* RawPtr() { return data_; } const T* RawPtr() const { return data_; } uint64_t size() const { return size_; } - - T& operator[](uint64_t index) { return data_[index]; } - const T& operator[](uint64_t index) const { return data_[index]; } + bool empty() const { return size_ == 0; } private: T* data_; uint64_t size_; }; +template +Array::Array(Array&& other) : data_(other.data_), size_(other.size_) { + other.data_ = nullptr; + other.size_ = 0; +} + +template +Array& Array::operator=(Array&& other) { + if (data_) { + delete[] data_; + } + data_ = other.data_; + size_ = other.size_; + other.data_ = nullptr; + other.size_ = 0; + return *this; +} + +template +Array::Array(const ArrayView& view) : Array(view.size()) { + for (uint64_t i = 0; i < size_; i++) { + data_[i] = view[i]; + } +} + } // namespace glcr diff --git a/lib/glacier/container/array_view.h b/lib/glacier/container/array_view.h index a1666e1..da16e9d 100644 --- a/lib/glacier/container/array_view.h +++ b/lib/glacier/container/array_view.h @@ -8,16 +8,21 @@ template class ArrayView { public: ArrayView() : data_(nullptr), size_(0) {} + ArrayView(const ArrayView&) = default; + ArrayView(ArrayView&&) = default; + ArrayView(T* data, uint64_t size) : data_(data), size_(size) {} + // Accessors. + T& operator[](uint64_t index) { return data_[index]; } + const T& operator[](uint64_t index) const { return data_[index]; } + T* RawPtr() { return data_; } const T* RawPtr() const { return data_; } uint64_t size() const { return size_; } - - T& operator[](uint64_t index) { return data_[index]; } - const T& operator[](uint64_t index) const { return data_[index]; } + bool empty() const { return size_; } private: T* data_; diff --git a/lib/glacier/container/vector.h b/lib/glacier/container/vector.h index 7d81cf6..08a5653 100644 --- a/lib/glacier/container/vector.h +++ b/lib/glacier/container/vector.h @@ -8,30 +8,14 @@ namespace glcr { template class Vector { public: + // Constructors. Vector() : data_(nullptr), size_(0), capacity_(0) {} Vector(const Vector&) = delete; - Vector(Vector&& other) - : data_(other.data_), size_(other.size_), capacity_(other.capacity_) { - other.data_ = nullptr; - other.size_ = 0; - other.capacity_ = 0; - } - Vector& operator=(Vector&& other) { - if (data_) { - delete[] data_; - } + Vector& operator=(const Vector&) = delete; - data_ = other.data_; - size_ = other.size_; - capacity_ = other.capacity_; - - other.data_ = nullptr; - other.size_ = 0; - other.capacity_ = 0; - - return *this; - } + Vector(Vector&& other); + Vector& operator=(Vector&& other); ~Vector() { if (data_) { @@ -39,33 +23,60 @@ class Vector { } } - // FIXME: Handle downsizing. - void Resize(uint64_t capacity); - - // Setters. - void PushBack(const T& item); - void PushBack(T&& item); - template - void EmplaceBack(Args... args); - // Accessors. - T& operator[](uint64_t index); - const T& operator[](uint64_t index) const; - T& at(uint64_t index); - const T& at(uint64_t index) const; + T& operator[](uint64_t index) { return data_[index]; } + const T& operator[](uint64_t index) const { return data_[index]; } + T& at(uint64_t index) { return data_[index]; } + const T& at(uint64_t index) const { return data_[index]; } uint64_t size() const { return size_; } + bool empty() const { return size_ == 0; } uint64_t capacity() const { return capacity_; } const T* RawPtr() const { return data_; } + // Setters. + // FIXME: Handle downsizing. + void Resize(uint64_t capacity); + + void PushBack(const T& item); + void PushBack(T&& item); + + template + void EmplaceBack(Args&&... args); + private: T* data_; uint64_t size_; uint64_t capacity_; void Expand(); -}; +}; // namespace glcr + +template +Vector::Vector(Vector&& other) + : data_(other.data_), size_(other.size_), capacity_(other.capacity_) { + other.data_ = nullptr; + other.size_ = 0; + other.capacity_ = 0; +} + +template +Vector& Vector::operator=(Vector&& other) { + if (data_) { + delete[] data_; + } + + data_ = other.data_; + size_ = other.size_; + capacity_ = other.capacity_; + + other.data_ = nullptr; + other.size_ = 0; + other.capacity_ = 0; + + return *this; +} template void Vector::Resize(uint64_t capacity) { @@ -100,7 +111,7 @@ void Vector::PushBack(T&& item) { template template -void Vector::EmplaceBack(Args... args) { +void Vector::EmplaceBack(Args&&... args) { if (size_ >= capacity_) { Expand(); } @@ -108,26 +119,6 @@ void Vector::EmplaceBack(Args... args) { data_[size_++] = T(args...); } -template -T& Vector::operator[](uint64_t index) { - return data_[index]; -} - -template -const T& Vector::operator[](uint64_t index) const { - return data_[index]; -} - -template -T& Vector::at(uint64_t index) { - return data_[index]; -} - -template -const T& Vector::at(uint64_t index) const { - return data_[index]; -} - template void Vector::Expand() { uint64_t new_capacity = capacity_ == 0 ? 1 : capacity_ * 2;