Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "array_ptr.h"
- #include <stdexcept>
- #include <algorithm>
- #include <initializer_list>
- class ReserveProxyObj {
- public:
- explicit ReserveProxyObj (size_t capacity_to_reserve)
- :capacity_(capacity_to_reserve)
- {
- }
- size_t ReserveCapasity(){
- return capacity_;
- }
- private:
- size_t capacity_;
- };
- ReserveProxyObj Reserve(size_t capacity_to_reserve) {
- return ReserveProxyObj(capacity_to_reserve);
- }
- template <typename Type>
- class SimpleVector {
- public:
- using Iterator = Type*;
- using ConstIterator = const Type*;
- SimpleVector() noexcept = default;
- explicit SimpleVector(size_t size)
- :items_(size)
- {
- size_ = size;
- capacity_ = size;
- std::fill(begin(), end(), Type());
- }
- SimpleVector(size_t size, const Type& value)
- :items_(size)
- {
- size_ = size;
- capacity_ = size;
- std::fill(begin(), end(), value);
- }
- SimpleVector(std::initializer_list<Type> init)
- :items_(init.size())
- {
- size_ = init.size();
- capacity_ = init.size();
- std::copy(init.begin(), init.end(), begin());
- }
- SimpleVector(const SimpleVector& other)
- :items_(other.GetSize())
- {
- size_ = other.GetSize();
- capacity_ = other.GetSize();
- std::copy(other.begin(), other.end(), begin());
- }
- SimpleVector( ReserveProxyObj capacity_to_reserve) {
- Reserve(capacity_to_reserve.ReserveCapasity() );
- }
- size_t GetSize() const noexcept {
- return size_;
- }
- size_t GetCapacity() const noexcept {
- return capacity_;
- }
- bool IsEmpty() const noexcept {
- return size_ == 0;
- }
- SimpleVector& operator=(const SimpleVector& rhs) {
- SimpleVector<Type> tmp(rhs);
- this->swap(tmp);
- return *this;
- }
- Type& operator[](size_t index) noexcept {
- return items_[index];
- }
- const Type& operator[](size_t index) const noexcept {
- return items_[index];
- }
- Type& At(size_t index) {
- if (index >= GetSize()) {
- throw std::out_of_range("Error: no index");
- }
- return items_[index];
- }
- const Type& At(size_t index) const {
- if (index >= GetSize()) {
- throw std::out_of_range("Error: no index");
- }
- return items_[index];
- }
- void Clear() noexcept {
- size_ = 0;
- }
- void Resize(size_t new_size) {
- if (new_size < size_ && new_size < capacity_) {
- size_ = new_size;
- }
- if (new_size > size_ || new_size > capacity_) {
- SimpleVector<Type> tmp(new_size);
- std::copy(begin(), end(), tmp.begin());
- items_.swap(tmp.items_);
- size_ = new_size;
- capacity_ = new_size;
- }
- if (new_size > size_ || new_size < capacity_) {
- SimpleVector<Type> tmp(new_size);
- std::fill(tmp.begin(), tmp.end(), Type());
- std::copy(begin(), end(), tmp.begin());
- items_.swap(tmp.items_);
- size_ = new_size;
- }
- }
- void PushBack(const Type& item) {
- this->Resize(size_ + 1);
- items_[size_ - 1] = item;
- }
- void PushBack(Type&& item) {
- this->Resize(size_ + 1);
- items_[size_ - 1] = std::move(item);
- }
- void PopBack() noexcept {
- if (IsEmpty()) {
- return;
- }
- auto it_pop = begin();
- std::advance(it_pop, size_ - 1);
- Erase(it_pop);
- }
- void swap(SimpleVector& other) noexcept {
- items_.swap(other.items_);
- std::swap(size_ , other.size_);
- std::swap(capacity_ , other.capacity_);
- }
- Iterator Erase(ConstIterator pos) {
- auto distance_ = std::distance(cbegin(), pos);
- std::copy(pos + 1, cend(), &items_[distance_]);
- --size_;
- return Iterator(&items_[distance_]);
- }
- Iterator Insert(ConstIterator pos, Type & value) {
- if (IsEmpty()){
- PushBack(value);
- return begin();
- }
- auto shift{ std::distance(cbegin(), pos) };
- if (size_ == capacity_) {
- Reserve(capacity_ * 2);
- }
- auto pos2{const_cast<Iterator>(std::next(cbegin(), shift))};
- std::copy_backward(pos2, end(), std::next(end()));
- *pos2 = std::move(value);
- ++size_;
- return pos2;
- }
- void Reserve(size_t new_capacity) {
- if (new_capacity <= capacity_) {
- return;
- }
- ArrayPtr<Type> tmp_items(new_capacity);
- for (std::size_t i{}; i < size_; ++i){
- tmp_items[i] = items_[i];
- }
- items_.swap(tmp_items);
- capacity_ = new_capacity;
- }
- Iterator begin() noexcept {
- return &items_[0];
- }
- Iterator end() noexcept {
- return &items_[size_];
- }
- ConstIterator begin() const noexcept {
- return &items_[0];
- }
- ConstIterator end() const noexcept {
- return &items_[size_];
- }
- ConstIterator cbegin() const noexcept {
- return &items_[0];
- }
- ConstIterator cend() const noexcept {
- return &items_[size_];
- }
- private:
- ArrayPtr<Type> items_;
- size_t size_ = 0;
- size_t capacity_ = 0;
- };
- template <typename Type>
- void swap(SimpleVector<Type>& lhs, SimpleVector<Type>& rhs) noexcept {
- lhs.swap(rhs);
- }
- template <typename Type>
- inline bool operator==(const SimpleVector<Type>& lhs, const SimpleVector<Type>& rhs) {
- return (lhs.GetSize() == rhs.GetSize()) && std::equal(lhs.begin(), lhs.end(), rhs.begin());
- }
- 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 (lhs == rhs) || (lhs < rhs);
- }
- 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) || (rhs < lhs);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement