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());
- }
- }
- }
- -----------------------------------------------------------------------
- simple_vector.h
- -----------------------------------------------------------------------
- #pragma once
- #include <cassert>
- #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_]);
- }
- private:
- ArrayPtr<Type> simple_vector_;
- size_t size_ = 0;
- size_t capacity_ = 0;
- };
- -----------------------------------------------------------------------
- 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& operator=(const ArrayPtr&) = delete;
- 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