Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------------------------------------------------------
- main.cpp
- -----------------------------------------------------------------------
- #include "simple_vector.h"
- #include "tests.h"
- int main() {
- Test1();
- return 0;
- }
- -----------------------------------------------------------------------
- tests.h
- -----------------------------------------------------------------------
- #pragma once
- #include <cassert>
- #include <stdexcept>
- inline void Test1() {
- {
- SimpleVector<int> v;
- assert(v.GetSize() == 0u);
- assert(v.IsEmpty());
- assert(v.GetCapacity() == 0u);
- }
- {
- SimpleVector<int> v(5);
- assert(v.GetSize() == 5u);
- assert(v.GetCapacity() == 5u);
- assert(!v.IsEmpty());
- for (size_t i = 0; i < v.GetSize(); ++i) {
- assert(v[i] == 0);
- }
- }
- {
- SimpleVector<int> v(3, 42);
- assert(v.GetSize() == 3);
- assert(v.GetCapacity() == 3);
- for (size_t i = 0; i < v.GetSize(); ++i) {
- assert(v[i] == 42);
- }
- }
- {
- SimpleVector<int> v{1, 2, 3};
- assert(v.GetSize() == 3);
- assert(v.GetCapacity() == 3);
- assert(v[2] == 3);
- }
- {
- SimpleVector<int> v(3);
- assert(&v.At(2) == &v[2]);
- try {
- v.At(3);
- assert(false);
- } catch (const std::out_of_range&) {
- } catch (...) {
- assert(false);
- }
- }
- {
- SimpleVector<int> v(10);
- const size_t old_capacity = v.GetCapacity();
- v.Clear();
- assert(v.GetSize() == 0);
- assert(v.GetCapacity() == old_capacity);
- }
- {
- SimpleVector<int> v(3);
- v[2] = 17;
- v.Resize(7);
- assert(v.GetSize() == 7);
- assert(v.GetCapacity() >= v.GetSize());
- assert(v[2] == 17);
- assert(v[3] == 0);
- }
- {
- SimpleVector<int> v(3);
- v[0] = 42;
- v[1] = 55;
- const size_t old_capacity = v.GetCapacity();
- v.Resize(2);
- assert(v.GetSize() == 2);
- assert(v.GetCapacity() == old_capacity);
- assert(v[0] == 42);
- assert(v[1] == 55);
- }
- {
- const size_t old_size = 3;
- SimpleVector<int> v(3);
- v.Resize(old_size + 5);
- v[3] = 42;
- v.Resize(old_size);
- v.Resize(old_size + 2);
- assert(v[3] == 0);
- }
- {
- {
- SimpleVector<int> v;
- assert(v.begin() == nullptr);
- assert(v.end() == nullptr);
- }
- {
- SimpleVector<int> v(10, 42);
- assert(v.begin());
- assert(*v.begin() == 42);
- assert(v.end() == v.begin() + v.GetSize());
- }
- }
- }
- inline void Test2() {
- {
- SimpleVector<int> v(1);
- v.PushBack(42);
- assert(v.GetSize() == 2);
- assert(v.GetCapacity() >= v.GetSize());
- assert(v[0] == 0);
- assert(v[1] == 42);
- }
- {
- SimpleVector<int> v(2);
- v.Resize(1);
- const size_t old_capacity = v.GetCapacity();
- v.PushBack(123);
- assert(v.GetSize() == 2);
- assert(v.GetCapacity() == old_capacity);
- }
- {
- SimpleVector<int> v{0, 1, 2, 3};
- const size_t old_capacity = v.GetCapacity();
- const auto old_begin = v.begin();
- v.PopBack();
- assert(v.GetCapacity() == old_capacity);
- assert(v.begin() == old_begin);
- assert((v == SimpleVector<int>{0, 1, 2}));
- }
- {
- SimpleVector<int> numbers{1, 2};
- auto numbers_copy(numbers);
- assert(&numbers_copy[0] != &numbers[0]);
- assert(numbers_copy.GetSize() == numbers.GetSize());
- for (size_t i = 0; i < numbers.GetSize(); ++i) {
- assert(numbers_copy[i] == numbers[i]);
- assert(&numbers_copy[i] != &numbers[i]);
- }
- }
- {
- assert((SimpleVector{1, 2, 3} == SimpleVector{1, 2, 3}));
- assert((SimpleVector{1, 2, 3} != SimpleVector{1, 2, 2}));
- assert((SimpleVector{1, 2, 3} < SimpleVector{1, 2, 3, 1}));
- assert((SimpleVector{1, 2, 3} > SimpleVector{1, 2, 2, 1}));
- assert((SimpleVector{1, 2, 3} >= SimpleVector{1, 2, 3}));
- assert((SimpleVector{1, 2, 4} >= SimpleVector{1, 2, 3}));
- assert((SimpleVector{1, 2, 3} <= SimpleVector{1, 2, 3}));
- assert((SimpleVector{1, 2, 3} <= SimpleVector{1, 2, 4}));
- }
- {
- SimpleVector<int> v1{42, 666};
- SimpleVector<int> v2;
- v2.PushBack(0);
- v2.PushBack(1);
- v2.PushBack(2);
- const int* const begin1 = &v1[0];
- const int* const begin2 = &v2[0];
- const size_t capacity1 = v1.GetCapacity();
- const size_t capacity2 = v2.GetCapacity();
- const size_t size1 = v1.GetSize();
- const size_t size2 = v2.GetSize();
- static_assert(noexcept(v1.swap(v2)));
- v1.swap(v2);
- assert(&v2[0] == begin1);
- assert(&v1[0] == begin2);
- assert(v1.GetSize() == size2);
- assert(v2.GetSize() == size1);
- assert(v1.GetCapacity() == capacity2);
- assert(v2.GetCapacity() == capacity1);
- }
- {
- SimpleVector<int> src_vector{1, 2, 3, 4};
- SimpleVector<int> dst_vector{1, 2, 3, 4, 5, 6};
- dst_vector = src_vector;
- assert(dst_vector == src_vector);
- }
- {
- SimpleVector<int> v{1, 2, 3, 4};
- v.Insert(v.begin() + 2, 42);
- assert((v == SimpleVector<int>{1, 2, 42, 3, 4}));
- }
- {
- SimpleVector<int> v{1, 2, 3, 4};
- v.Erase(v.cbegin() + 2);
- assert((v == SimpleVector<int>{1, 2, 4}));
- }
- }
- -----------------------------------------------------------------------
- simple_vector.h
- -----------------------------------------------------------------------
- #pragma once
- #include <initializer_list>
- #include <algorithm>
- #include <stdexcept>
- #include "array_ptr.h"
- 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());
- }
- 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_);
- }
- 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);
- }
- -----------------------------------------------------------------------
- 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;
- };
Advertisement
Add Comment
Please, Sign In to add comment