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_;
- explicit 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)
- :raw_vector_(size), size_(size), capacity_(size){
- ArrayPtr<Type> array_copy(size);
- array_copy.swap(raw_vector_);
- }
- SimpleVector(size_t size, const std::initializer_list<Type&> &value)
- //SimpleVector(size_t size, const Type &value)
- :raw_vector_(size), size_(size), capacity_(size){
- ArrayPtr<Type> array_copy(size);
- std::fill(array_copy.Get(), array_copy.Get() + size, value);
- array_copy.swap(raw_vector_);
- }
- SimpleVector(std::initializer_list<Type> init)
- :raw_vector_(init.size()), size_(init.size()), capacity_(init.size()){
- 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_);
- }
- SimpleVector(const SimpleVector &other)
- /*:raw_vector_(other.capacity_), size_(other.size_), capacity_(other.capacity_)*/{
- SimpleVector temp(other.capacity_);
- //SimpleVector<Type> temp(other.size_);
- temp.size_ = other.size_;
- //temp.capacity_ = other.capacity_;
- std::copy(other.begin(), other.end(), temp.begin());
- swap(temp);
- }
- SimpleVector(SimpleVector &&other){
- raw_vector_.swap(other.raw_vector_);
- size_ = std::exchange(other.size_, 0u);
- capacity_ = std::exchange(other.capacity_, 0u);
- }
- SimpleVector(ReserveProxyObj obj){
- Reserve(obj.capacity_to_reserve_);
- }
- SimpleVector &operator=(SimpleVector rhs){
- SimpleVector obj(rhs);
- swap(obj);
- //swap(rhs);
- return *this;
- }
- 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];
- }
- void swap(SimpleVector<Type> &other) noexcept{
- raw_vector_.swap(other.raw_vector_);
- std::swap(size_, other.size_);
- std::swap(capacity_, other.capacity_);
- }
- size_t GetSize() const noexcept{
- return size_;
- }
- size_t GetCapacity() const noexcept{
- return capacity_;
- }
- bool IsEmpty() const noexcept{
- return size_ == 0;
- }
- 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;
- }
- bool Reserve(size_t new_capacity){
- if(new_capacity <= capacity_){
- return false;
- }
- ArrayPtr<Type> new_data(new_capacity);
- std::move(raw_vector_.Get(), raw_vector_.Get() + size_, new_data.Get());
- raw_vector_.swap(new_data);
- capacity_ = new_capacity;
- return true;
- }
- void Resize(size_t new_size){
- if(new_size <= size_){
- size_ = new_size;
- } else{
- if(new_size <= capacity_){
- for(auto i = size_; i < new_size; i++){
- raw_vector_[i] = std::move(Type{});
- }
- size_ = new_size;
- } else{
- if(new_size > capacity_){
- ArrayPtr<Type> new_items(new_size);
- std::move(this->begin(), this->end(), new_items.Get());
- for(auto i = size_; i < new_size; i++){
- new_items[i] = std::move(Type{});
- }
- new_items.swap(raw_vector_);
- size_ = new_size;
- capacity_ = new_size;
- }
- }
- }
- }
- void PushBack(Type item){
- if(size_ < capacity_){
- raw_vector_[size_++] = std::move(item);
- } 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;
- }
- 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_;
- }
- };
- template <typename Type>
- inline bool operator==(const SimpleVector<Type> &lhs, const SimpleVector<Type> &rhs){
- if(lhs.GetSize() != rhs.GetSize()){
- return false;
- }
- 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