diff --git a/lib/glacier/container/array.h b/lib/glacier/container/array.h new file mode 100644 index 0000000..b093ee6 --- /dev/null +++ b/lib/glacier/container/array.h @@ -0,0 +1,59 @@ +#pragma once + +#include + +#include "glacier/container/array_view.h" + +namespace glcr { + +template +class Array { + public: + 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(Array&& other) : data_(other.data), size_(other.size_) { + other.data_ = nullptr; + other.size_ = 0; + } + + Array& operator=(Array&& other) { + if (data_) { + delete[] data_; + } + data_ = other.data_; + size_ = other.size_; + other.data_ = nullptr; + other.size_ = 0; + return *this; + } + + ~Array() { + if (data_) { + delete[] data_; + } + } + + 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]; } + + private: + T* data_; + uint64_t size_; +}; + +} // namespace glcr diff --git a/lib/glacier/container/array_view.h b/lib/glacier/container/array_view.h new file mode 100644 index 0000000..a1666e1 --- /dev/null +++ b/lib/glacier/container/array_view.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +namespace glcr { + +template +class ArrayView { + public: + ArrayView() : data_(nullptr), size_(0) {} + ArrayView(const ArrayView&) = default; + ArrayView(T* data, uint64_t size) : data_(data), size_(size) {} + + 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]; } + + private: + T* data_; + uint64_t size_; +}; + +} // namespace glcr