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) {}
- SimpleVector(size_t size, const Type &value)
- : raw_vector_(size), size_(size), capacity_(size) {
- std::fill(raw_vector_.Get(), raw_vector_.Get() + size, value);
- }
- SimpleVector(std::initializer_list<Type> init)
- : raw_vector_(init.size()), size_(init.size()), capacity_(init.size()) {
- std::copy(std::make_move_iterator(init.begin()), std::make_move_iterator(init.end()), raw_vector_.Get());
- }
- SimpleVector(const SimpleVector& other)
- : raw_vector_(other.capacity_), size_(other.size_), capacity_(other.capacity_) {
- std::copy(other.begin(), other.end(), begin());
- }
- SimpleVector(SimpleVector &&other)noexcept
- : raw_vector_(std::move(other.raw_vector_)), size_(std::exchange(other.size_, 0u)), capacity_(std::exchange(other.capacity_, 0u)) {}
- SimpleVector(ReserveProxyObj obj)
- : raw_vector_(obj.capacity_to_reserve_), capacity_(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& 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;
- }
- void Reserve(size_t new_capacity) {
- if (new_capacity <= capacity_) {
- return;
- }
- ArrayPtr<Type> temp(new_capacity);
- std::move(begin(), end(), temp.Get());
- raw_vector_.swap(temp);
- capacity_ = new_capacity;
- }
- 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(const Type& value) {
- if (size_ >= capacity_) {
- Reserve(capacity_ == 0 ? 1 : capacity_ * 2);
- }
- raw_vector_[size_++] = value;
- }
- void PushBack(Type&& value) {
- if (size_ >= capacity_) {
- Reserve(capacity_ == 0 ? 1 : capacity_ * 2);
- }
- raw_vector_[size_++] = std::move(value);
- }
- 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) {
- Iterator it = begin() + (pos - begin());
- std::move(it + 1, end(), it);
- --size_;
- return it;
- }
- 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