Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "array_ptr.h"
- struct ReserveProxyObj {
- size_t capacity_to_reserve_;
- ReserveProxyObj(size_t capacity_to_reserve)
- : capacity_to_reserve_(capacity_to_reserve) {}
- };
- ReserveProxyObj Reserve(size_t capacity_to_reserve) {
- return ReserveProxyObj(capacity_to_reserve);
- }
- template <typename Type>
- class SimpleVector {
- private:
- ArrayPtr<Type> raw_vector_;
- size_t size_ = 0u;
- size_t capacity_ = 0u ;
- public:
- using Iterator = Type*;
- using ConstIterator = const Type*;
- SimpleVector() noexcept = default;
- explicit SimpleVector(size_t size) {
- ArrayPtr<Type> array_copy(size);
- array_copy.swap(raw_vector_);
- size_ = size;
- capacity_ = size;
- }
- SimpleVector(ReserveProxyObj obj) {
- Reserve(obj.capacity_to_reserve_);
- }
- SimpleVector(size_t size, Type value) {
- ArrayPtr<Type> array_copy(size);
- std::fill(array_copy.Get(), array_copy.Get() + size, value);
- array_copy.swap(raw_vector_);
- size_ = size;
- capacity_ = size;
- }
- SimpleVector(std::initializer_list<Type> init) {
- ArrayPtr<Type> array_copy(init.size());
- std::copy(std::make_move_iterator(init.begin()), std::make_move_iterator(init.end()), array_copy.Get());
- array_copy.swap(raw_vector_);
- size_ = init.size();
- capacity_ = init.size();
- }
- void swap(SimpleVector<Type> &other) noexcept {
- raw_vector_.swap(other.raw_vector_);
- std::swap(size_, other.size_);
- std::swap(capacity_, other.capacity_);
- }
- SimpleVector(const SimpleVector &other) {
- SimpleVector<Type> temp(other.size_);
- std::copy(other.begin(), other.end(), temp.begin());
- temp.size_ = other.size_;
- temp.capacity_ = other.capacity_;
- swap(temp);
- }
- SimpleVector(SimpleVector &&other) {
- raw_vector_ = std::move(other.raw_vector_);
- size_ = std::move(other.size_);
- capacity_ = size_;
- other.size_ = 0u;
- other.capacity_ = 0u;
- }
- SimpleVector& operator=(SimpleVector rhs) {
- if (raw_vector_.Get() != rhs.raw_vector_.Get()) {
- ArrayPtr<Type> array_copy(rhs.size_);
- std::copy(std::make_move_iterator(rhs.raw_vector_.Get()), std::make_move_iterator(rhs.raw_vector_.Get() + rhs.size_), array_copy.Get());
- raw_vector_.swap(array_copy);
- size_ = std::move(rhs.size_);
- capacity_ = std::move(rhs.size_);
- }
- return *this;
- }
- size_t GetSize() const noexcept {
- return size_;
- }
- size_t GetCapacity() const noexcept {
- return capacity_;
- }
- bool IsEmpty() const noexcept {
- return size_ == 0;
- }
- Type& operator[](size_t index) noexcept {
- assert(index < size_);
- return raw_vector_[index];
- }
- const Type& operator[](size_t index) const noexcept {
- assert(index < size_);
- return raw_vector_[index];
- }
- Type& At(size_t index) {
- if (index >= size_) {
- throw std::out_of_range("Index is out of range");
- }
- return raw_vector_[index];
- }
- const Type& At(size_t index) const {
- if (index >= size_) {
- throw std::out_of_range("Index is out of range");
- }
- return raw_vector_[index];
- }
- void Clear() noexcept {
- size_ = 0;
- }
- void Resize(size_t new_size) {
- if (new_size <= capacity_) {
- if (new_size <= size_) {
- size_ = new_size;
- } else {
- std::fill(raw_vector_.Get() + size_, raw_vector_.Get() + new_size, Type{});
- size_ = new_size;
- }
- } else {
- SimpleVector<Type> temp(std::max(capacity_ * 2, new_size));
- std::copy(begin(), end(), temp.begin());
- swap(temp);
- }
- }
- void Reserve(size_t new_capacity) {
- if (new_capacity > capacity_) {
- SimpleVector<Type> temp(new_capacity);
- std::copy(begin(), end(), temp.begin());
- temp.size_ = size_;
- swap(temp);
- }
- }
- Iterator begin() noexcept {
- return raw_vector_.Get();
- }
- Iterator end() noexcept {
- return raw_vector_.Get() + size_;
- }
- ConstIterator begin() const noexcept {
- return raw_vector_.Get();
- }
- ConstIterator end() const noexcept {
- return raw_vector_.Get() + size_;
- }
- ConstIterator cbegin() const noexcept {
- return raw_vector_.Get();
- }
- ConstIterator cend() const noexcept {
- return raw_vector_.Get() + size_;
- }
- void PushBack(Type item) {
- if (size_ < capacity_) {
- raw_vector_[size_] = std::move(item);
- ++size_;
- } else {
- SimpleVector<Type> temp(std::max(size_ * 2, static_cast<size_t>(1)));
- temp.size_ = size_ + 1;
- std::copy(std::make_move_iterator(begin()), std::make_move_iterator(end()), temp.begin());
- temp[size_] = std::move(item);
- swap(temp);
- }
- }
- void PopBack() noexcept {
- assert(!IsEmpty());
- --size_;
- }
- Iterator Insert(ConstIterator pos, Type value) {
- assert((pos >= begin()) && (pos <= end()));
- size_t index = static_cast<size_t>(pos - begin());
- if (size_ < capacity_) {
- std::copy_backward(std::make_move_iterator(begin() + index), std::make_move_iterator(end()), end() + 1);
- raw_vector_[index] = std::move(value);
- ++size_;
- } else {
- SimpleVector<Type> temp(std::max(size_ * 2, static_cast<size_t>(1)));
- temp.size_ = size_ + 1;
- std::copy(std::make_move_iterator(begin()), std::make_move_iterator(begin() + index), temp.begin());
- temp[index] = std::move(value);
- std::copy(std::make_move_iterator(begin() + index), std::make_move_iterator(end()), temp.begin() + index + 1);
- swap(temp);
- }
- return begin() + index;
- }
- Iterator Erase(ConstIterator pos) {
- assert((pos >= begin()) && (pos < end()));
- size_t index = static_cast<size_t>(pos - begin());
- std::copy(std::make_move_iterator(begin() + index + 1), std::make_move_iterator(end()), begin() + index);
- --size_;
- return begin() + index;
- }
- };
- template <typename Type>
- inline bool operator==(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
- return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
- }
- template <typename Type>
- inline bool operator!=(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
- return !(lhs == rhs);
- }
- template <typename Type>
- inline bool operator<(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
- return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
- }
- template <typename Type>
- inline bool operator<=(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
- return !(rhs < lhs);
- }
- template <typename Type>
- inline bool operator>(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
- return (rhs < lhs);
- }
- template <typename Type>
- inline bool operator>=(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
- return !(lhs < rhs);
- }
Advertisement
Add Comment
Please, Sign In to add comment