2023-06-26 15:46:03 -07:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <glacier/memory/move.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
namespace glcr {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class Vector {
|
|
|
|
public:
|
2023-11-15 18:43:35 -08:00
|
|
|
// Constructors.
|
2023-06-26 15:46:03 -07:00
|
|
|
Vector() : data_(nullptr), size_(0), capacity_(0) {}
|
|
|
|
|
|
|
|
Vector(const Vector&) = delete;
|
2023-11-15 18:43:35 -08:00
|
|
|
Vector& operator=(const Vector&) = delete;
|
2023-11-02 20:57:28 -07:00
|
|
|
|
2023-11-15 18:43:35 -08:00
|
|
|
Vector(Vector&& other);
|
|
|
|
Vector& operator=(Vector&& other);
|
2023-06-26 15:46:03 -07:00
|
|
|
|
2023-11-02 20:23:28 -07:00
|
|
|
~Vector() {
|
|
|
|
if (data_) {
|
|
|
|
delete[] data_;
|
|
|
|
}
|
|
|
|
}
|
2023-06-26 15:46:03 -07:00
|
|
|
|
|
|
|
// Accessors.
|
2023-11-15 18:43:35 -08:00
|
|
|
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]; }
|
2023-06-26 15:46:03 -07:00
|
|
|
|
|
|
|
uint64_t size() const { return size_; }
|
2023-11-15 18:43:35 -08:00
|
|
|
bool empty() const { return size_ == 0; }
|
2023-06-26 15:46:03 -07:00
|
|
|
uint64_t capacity() const { return capacity_; }
|
|
|
|
|
2023-11-03 02:48:21 -07:00
|
|
|
const T* RawPtr() const { return data_; }
|
|
|
|
|
2023-11-15 18:43:35 -08:00
|
|
|
// Setters.
|
|
|
|
// FIXME: Handle downsizing.
|
|
|
|
void Resize(uint64_t capacity);
|
|
|
|
|
|
|
|
void PushBack(const T& item);
|
|
|
|
void PushBack(T&& item);
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
void EmplaceBack(Args&&... args);
|
|
|
|
|
2023-06-26 15:46:03 -07:00
|
|
|
private:
|
|
|
|
T* data_;
|
|
|
|
uint64_t size_;
|
|
|
|
uint64_t capacity_;
|
|
|
|
|
|
|
|
void Expand();
|
2023-11-15 18:43:35 -08:00
|
|
|
}; // namespace glcr
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Vector<T>::Vector(Vector&& other)
|
|
|
|
: data_(other.data_), size_(other.size_), capacity_(other.capacity_) {
|
|
|
|
other.data_ = nullptr;
|
|
|
|
other.size_ = 0;
|
|
|
|
other.capacity_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
Vector<T>& Vector<T>::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;
|
|
|
|
}
|
2023-06-26 15:46:03 -07:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void Vector<T>::Resize(uint64_t capacity) {
|
2023-11-15 12:00:48 -08:00
|
|
|
T* new_data = new T[capacity];
|
|
|
|
if (data_) {
|
|
|
|
for (uint64_t i = 0; i < size_; i++) {
|
|
|
|
new_data[i] = glcr::Move(data_[i]);
|
|
|
|
}
|
|
|
|
delete[] data_;
|
2023-06-26 15:46:03 -07:00
|
|
|
}
|
|
|
|
data_ = new_data;
|
|
|
|
capacity_ = capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void Vector<T>::PushBack(const T& item) {
|
|
|
|
if (size_ >= capacity_) {
|
|
|
|
Expand();
|
|
|
|
}
|
|
|
|
|
|
|
|
data_[size_++] = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void Vector<T>::PushBack(T&& item) {
|
|
|
|
if (size_ >= capacity_) {
|
|
|
|
Expand();
|
|
|
|
}
|
|
|
|
|
|
|
|
data_[size_++] = glcr::Move(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
template <typename... Args>
|
2023-11-15 18:43:35 -08:00
|
|
|
void Vector<T>::EmplaceBack(Args&&... args) {
|
2023-06-26 15:46:03 -07:00
|
|
|
if (size_ >= capacity_) {
|
|
|
|
Expand();
|
|
|
|
}
|
|
|
|
|
|
|
|
data_[size_++] = T(args...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void Vector<T>::Expand() {
|
|
|
|
uint64_t new_capacity = capacity_ == 0 ? 1 : capacity_ * 2;
|
|
|
|
Resize(new_capacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace glcr
|