[Glacier] Cleanup formatting for Array and Vector.
This commit is contained in:
parent
1c7eacd977
commit
569945f06d
|
@ -9,33 +9,17 @@ namespace glcr {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class Array {
|
class Array {
|
||||||
public:
|
public:
|
||||||
|
// Constructors.
|
||||||
Array() : data_(nullptr), size_(0) {}
|
Array() : data_(nullptr), size_(0) {}
|
||||||
|
|
||||||
explicit Array(uint64_t size) : data_(new T[size]), size_(size) {}
|
|
||||||
|
|
||||||
Array(const ArrayView<T> view) : Array(view.size()) {
|
|
||||||
for (uint64_t i = 0; i < size_; i++) {
|
|
||||||
data_[i] = view[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Array(const Array&) = delete;
|
Array(const Array&) = delete;
|
||||||
|
Array& operator=(const Array&) = delete;
|
||||||
|
|
||||||
Array(Array&& other) : data_(other.data_), size_(other.size_) {
|
Array(Array&&);
|
||||||
other.data_ = nullptr;
|
Array& operator=(Array&&);
|
||||||
other.size_ = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
Array& operator=(Array&& other) {
|
explicit Array(uint64_t size) : data_(new T[size]), size_(size) {}
|
||||||
if (data_) {
|
Array(const ArrayView<T>& view);
|
||||||
delete[] data_;
|
|
||||||
}
|
|
||||||
data_ = other.data_;
|
|
||||||
size_ = other.size_;
|
|
||||||
other.data_ = nullptr;
|
|
||||||
other.size_ = 0;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
~Array() {
|
~Array() {
|
||||||
if (data_) {
|
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_; }
|
T* RawPtr() { return data_; }
|
||||||
const T* RawPtr() const { return data_; }
|
const T* RawPtr() const { return data_; }
|
||||||
|
|
||||||
uint64_t size() const { return size_; }
|
uint64_t size() const { return size_; }
|
||||||
|
bool empty() const { return size_ == 0; }
|
||||||
T& operator[](uint64_t index) { return data_[index]; }
|
|
||||||
const T& operator[](uint64_t index) const { return data_[index]; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T* data_;
|
T* data_;
|
||||||
uint64_t size_;
|
uint64_t size_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Array<T>::Array(Array&& other) : data_(other.data_), size_(other.size_) {
|
||||||
|
other.data_ = nullptr;
|
||||||
|
other.size_ = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Array<T>& Array<T>::operator=(Array&& other) {
|
||||||
|
if (data_) {
|
||||||
|
delete[] data_;
|
||||||
|
}
|
||||||
|
data_ = other.data_;
|
||||||
|
size_ = other.size_;
|
||||||
|
other.data_ = nullptr;
|
||||||
|
other.size_ = 0;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Array<T>::Array(const ArrayView<T>& view) : Array(view.size()) {
|
||||||
|
for (uint64_t i = 0; i < size_; i++) {
|
||||||
|
data_[i] = view[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace glcr
|
} // namespace glcr
|
||||||
|
|
|
@ -8,16 +8,21 @@ template <typename T>
|
||||||
class ArrayView {
|
class ArrayView {
|
||||||
public:
|
public:
|
||||||
ArrayView() : data_(nullptr), size_(0) {}
|
ArrayView() : data_(nullptr), size_(0) {}
|
||||||
|
|
||||||
ArrayView(const ArrayView&) = default;
|
ArrayView(const ArrayView&) = default;
|
||||||
|
ArrayView(ArrayView&&) = default;
|
||||||
|
|
||||||
ArrayView(T* data, uint64_t size) : data_(data), size_(size) {}
|
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_; }
|
T* RawPtr() { return data_; }
|
||||||
const T* RawPtr() const { return data_; }
|
const T* RawPtr() const { return data_; }
|
||||||
|
|
||||||
uint64_t size() const { return size_; }
|
uint64_t size() const { return size_; }
|
||||||
|
bool empty() const { return size_; }
|
||||||
T& operator[](uint64_t index) { return data_[index]; }
|
|
||||||
const T& operator[](uint64_t index) const { return data_[index]; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T* data_;
|
T* data_;
|
||||||
|
|
|
@ -8,30 +8,14 @@ namespace glcr {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class Vector {
|
class Vector {
|
||||||
public:
|
public:
|
||||||
|
// Constructors.
|
||||||
Vector() : data_(nullptr), size_(0), capacity_(0) {}
|
Vector() : data_(nullptr), size_(0), capacity_(0) {}
|
||||||
|
|
||||||
Vector(const Vector&) = delete;
|
Vector(const Vector&) = delete;
|
||||||
Vector(Vector&& other)
|
Vector& operator=(const Vector&) = delete;
|
||||||
: 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_;
|
|
||||||
}
|
|
||||||
|
|
||||||
data_ = other.data_;
|
Vector(Vector&& other);
|
||||||
size_ = other.size_;
|
Vector& operator=(Vector&& other);
|
||||||
capacity_ = other.capacity_;
|
|
||||||
|
|
||||||
other.data_ = nullptr;
|
|
||||||
other.size_ = 0;
|
|
||||||
other.capacity_ = 0;
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
~Vector() {
|
~Vector() {
|
||||||
if (data_) {
|
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 <typename... Args>
|
|
||||||
void EmplaceBack(Args... args);
|
|
||||||
|
|
||||||
// Accessors.
|
// Accessors.
|
||||||
T& operator[](uint64_t index);
|
T& operator[](uint64_t index) { return data_[index]; }
|
||||||
const T& operator[](uint64_t index) const;
|
const T& operator[](uint64_t index) const { return data_[index]; }
|
||||||
T& at(uint64_t index);
|
T& at(uint64_t index) { return data_[index]; }
|
||||||
const T& at(uint64_t index) const;
|
const T& at(uint64_t index) const { return data_[index]; }
|
||||||
|
|
||||||
uint64_t size() const { return size_; }
|
uint64_t size() const { return size_; }
|
||||||
|
bool empty() const { return size_ == 0; }
|
||||||
uint64_t capacity() const { return capacity_; }
|
uint64_t capacity() const { return capacity_; }
|
||||||
|
|
||||||
const T* RawPtr() const { return data_; }
|
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 <typename... Args>
|
||||||
|
void EmplaceBack(Args&&... args);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T* data_;
|
T* data_;
|
||||||
uint64_t size_;
|
uint64_t size_;
|
||||||
uint64_t capacity_;
|
uint64_t capacity_;
|
||||||
|
|
||||||
void Expand();
|
void Expand();
|
||||||
};
|
}; // 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;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void Vector<T>::Resize(uint64_t capacity) {
|
void Vector<T>::Resize(uint64_t capacity) {
|
||||||
|
@ -100,7 +111,7 @@ void Vector<T>::PushBack(T&& item) {
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
void Vector<T>::EmplaceBack(Args... args) {
|
void Vector<T>::EmplaceBack(Args&&... args) {
|
||||||
if (size_ >= capacity_) {
|
if (size_ >= capacity_) {
|
||||||
Expand();
|
Expand();
|
||||||
}
|
}
|
||||||
|
@ -108,26 +119,6 @@ void Vector<T>::EmplaceBack(Args... args) {
|
||||||
data_[size_++] = T(args...);
|
data_[size_++] = T(args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
T& Vector<T>::operator[](uint64_t index) {
|
|
||||||
return data_[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
const T& Vector<T>::operator[](uint64_t index) const {
|
|
||||||
return data_[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
T& Vector<T>::at(uint64_t index) {
|
|
||||||
return data_[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
const T& Vector<T>::at(uint64_t index) const {
|
|
||||||
return data_[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void Vector<T>::Expand() {
|
void Vector<T>::Expand() {
|
||||||
uint64_t new_capacity = capacity_ == 0 ? 1 : capacity_ * 2;
|
uint64_t new_capacity = capacity_ == 0 ? 1 : capacity_ * 2;
|
||||||
|
|
Loading…
Reference in New Issue