Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------------------------------------------------------
- main.cpp
- -----------------------------------------------------------------------
- #include "simple_vector.h"
- #include <cassert>
- #include <iostream>
- using namespace std;
- void TestReserveConstructor() {
- cout << "TestReserveConstructor"s << endl;
- SimpleVector<int> v(Reserve(5));
- assert(v.GetCapacity() == 5);
- assert(v.IsEmpty());
- cout << "Done!"s << endl;
- }
- void TestReserveMethod() {
- cout << "TestReserveMethod"s << endl;
- SimpleVector<int> v;
- v.Reserve(5);
- assert(v.GetCapacity() == 5);
- assert(v.IsEmpty());
- v.Reserve(1);
- assert(v.GetCapacity() == 5);
- for (int i = 0; i < 10; ++i) {
- v.PushBack(i);
- }
- assert(v.GetSize() == 10);
- v.Reserve(100);
- assert(v.GetSize() == 10);
- assert(v.GetCapacity() == 100);
- for (int i = 0; i < 10; ++i) {
- assert(v[i] == i);
- }
- cout << "Done!"s << endl;
- }
- int main() {
- TestReserveConstructor();
- TestReserveMethod();
- }
- -----------------------------------------------------------------------
- array_ptr.h
- -----------------------------------------------------------------------
- #pragma once
- #include <stdexcept>
- #include <algorithm>
- #include <cassert>
- #include <cstdlib>
- template <typename Type>
- class ArrayPtr {
- public:
- ArrayPtr() = default;
- explicit ArrayPtr(size_t size) {
- if (size) {
- simple_vector_ = new Type[size]();
- } else{
- simple_vector_= nullptr;
- }
- }
- ArrayPtr(const ArrayPtr&) = delete;
- ~ArrayPtr() {
- delete[] simple_vector_;
- }
- ArrayPtr(const ArrayPtr& other, size_t other_size, size_t new_size) {
- simple_vector_ = new Type[new_size];
- std::copy(other.simple_vector_, other.simple_vector_ + other_size, simple_vector_);
- }
- Type& operator[](size_t index) noexcept {
- return simple_vector_[index];
- }
- const Type& operator[](size_t index) const noexcept {
- return simple_vector_[index];
- }
- explicit operator bool() const {
- return simple_vector_;
- }
- void swap(ArrayPtr& other) noexcept {
- std::swap(simple_vector_, other.simple_vector_);
- }
- private:
- Type* simple_vector_ = nullptr;
- };
- -----------------------------------------------------------------------
- simple_vector.h
- -----------------------------------------------------------------------
- #pragma once
- #include <initializer_list>
- #include <algorithm>
- #include <stdexcept>
- #include "array_ptr.h"
- class ReserveProxyObj {
- public:
- explicit ReserveProxyObj (size_t capacity_to_reserve) : capacity_(capacity_to_reserve){}
- size_t Reserve_capacity() {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) :simple_vector_(size), size_(size), capacity_(size){}
- SimpleVector(size_t size, const Type& value) :simple_vector_(size), size_(size), capacity_(size){
- std::fill(begin(), end(), value);
- }
- SimpleVector(std::initializer_list<Type> init) :simple_vector_(init.size()), size_(init.size()), capacity_(init.size()){
- std::copy(init.begin(), init.end(), begin());
- }
- SimpleVector(ReserveProxyObj capacity_to_reserve) {
- Reserve(capacity_to_reserve.Reserve_capacity());
- }
- size_t GetSize() const noexcept {
- return size_;
- }
- size_t GetCapacity() const noexcept {
- return capacity_;
- }
- bool IsEmpty() const noexcept {
- return !size_;
- }
- Type& operator[](size_t index) noexcept {
- return simple_vector_[index];
- }
- const Type& operator[](size_t index) const noexcept {
- return simple_vector_[index];
- }
- Type& At(size_t index) {
- if (index < size_) {
- return simple_vector_[index];
- }else{
- throw std::out_of_range("Non-existent vector element.");
- }
- }
- const Type& At(size_t index) const {
- if (index < size_) {
- return simple_vector_[index];
- }else{
- throw std::out_of_range("Non-existent vector element.");
- }
- }
- void Clear() noexcept {
- size_ = 0;
- }
- void Resize(size_t new_size) {
- if (size_ >= new_size){
- size_ = new_size;
- return;
- }else if(size_ < new_size && capacity_ > new_size){
- std::fill(end(), begin() + new_size, Type());//
- size_ = new_size;
- return;
- }else{
- ArrayPtr<Type> helper(new_size);
- std::copy(begin(), end(), &helper[0]);
- simple_vector_.swap(helper);
- size_ = new_size;
- capacity_ = new_size*2;
- }
- }
- Iterator begin() noexcept {
- return Iterator(&simple_vector_[0]);
- }
- Iterator end() noexcept {
- return Iterator(&simple_vector_[size_]);
- }
- ConstIterator begin() const noexcept {
- return cbegin();
- }
- ConstIterator end() const noexcept {
- return cend();
- }
- ConstIterator cbegin() const noexcept {
- return ConstIterator(&simple_vector_[0]);
- }
- ConstIterator cend() const noexcept {
- return ConstIterator(&simple_vector_[size_]);
- }
- SimpleVector(const SimpleVector& other) {
- ArrayPtr<Type> helper(other.size_);
- size_ = other.size_;
- capacity_= other.capacity_;
- std::copy(other.begin(), other.end(), &helper[0]);
- simple_vector_.swap(helper);
- }
- SimpleVector& operator=(const SimpleVector& rhs) {
- if (this !=&rhs){
- SimpleVector helper(rhs);
- swap(helper);
- }
- return *this;
- }
- void PushBack(const Type& item) {
- this->Resize(size_+1);
- simple_vector_[size_-1] = item;
- }
- Iterator Insert(ConstIterator pos, const Type& value) {
- if(begin()<= pos && end()>= pos){
- auto index = std::distance(cbegin(), pos);
- if (size_== capacity_){
- if(size_){
- ArrayPtr<Type> helper(size_);
- std::copy(begin(), end(), &helper[0]);
- simple_vector_.swap(helper);
- size_ = size_;
- capacity_ = size_*2;
- }else{
- ArrayPtr<Type> helper(1);
- std::copy(begin(), end(), &helper[0]);
- simple_vector_.swap(helper);
- capacity_ =1;
- }
- }
- for (size_t i = size_; i >(size_t)index; --i) {
- simple_vector_[i] = simple_vector_[i-1];
- }
- ++size_;
- simple_vector_[index] = value;
- return const_cast<Iterator>(index +begin());
- }else{
- throw std::out_of_range("Non-existent vector pos.");
- }
- }
- void PopBack() noexcept {
- if(size_) --size_;
- }
- Iterator Erase(ConstIterator pos) {
- auto index = std::distance(cbegin(), pos);
- std::copy(++pos, cend(), &simple_vector_[index]);
- --size_;
- return Iterator(&simple_vector_[index]);
- }
- void swap(SimpleVector& other) noexcept {
- simple_vector_.swap(other.simple_vector_);
- std::swap(size_, other.size_);
- std::swap(capacity_, other.capacity_);
- }
- void Reserve(size_t new_capacity){
- if (new_capacity> capacity_) {
- ArrayPtr<Type> helper(new_capacity);
- for (size_t i=0; i< size_; ++i){
- helper[i] = simple_vector_[i];
- }
- simple_vector_.swap(helper);
- capacity_ = new_capacity;
- }else{
- return;
- }
- }
- private:
- ArrayPtr<Type> simple_vector_;
- size_t size_ = 0;
- size_t capacity_ = 0;
- };
- 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 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment