From 88f15e35713c9632216931d26443dc588238732f Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 7 May 2024 22:17:12 +0300 Subject: [w10] Added rough solutions to ex 1-10 --- week10/Exercise02/Array.hpp | 143 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 week10/Exercise02/Array.hpp (limited to 'week10/Exercise02') diff --git a/week10/Exercise02/Array.hpp b/week10/Exercise02/Array.hpp new file mode 100644 index 0000000..114a1bc --- /dev/null +++ b/week10/Exercise02/Array.hpp @@ -0,0 +1,143 @@ +#pragma once + +template +class Array { + T* arr; + unsigned size; + unsigned allocated; + + void resize(); + + void free(); + void copyFrom(const Array& other); + +public: + Array(); + ~Array(); + Array(const Array& other); + Array& operator=(const Array& other); + Array(Array&& other); + Array& operator=(Array&& other); + + T& operator[](unsigned index); + T& operator[](unsigned index) const; + Array& operator+=(const Array& right); + friend bool operator==(const Array& left, const Array& right); + friend bool operator!=(const Array& left, const Array& right); + friend Array operator+(const Array& left, const Array& right); +}; + +template +void Array::resize() { + allocated *= 2; + T* biggerArr = new T[allocated]; + for (int i = 0; i < size; i++) { + biggerArr[i] = arr[i]; + } + delete[] arr; + arr = biggerArr; +} + +template +void Array::free() { + delete[] arr; +} + +template +void Array::copyFrom(const Array& other) { + this->size = other.size; + this->allocated = other.allocated; + this->arr = new T[allocated]; + for (int i = 0; i < size; i++) { + this->arr[i] = other.arr[i]; + } +} + +template +Array::Array() { + this->arr = nullptr; + allocated = size = 0; +} + +template +Array::~Array() { + free(); +} + +template +Array::Array(const Array& other) { + copyFrom(other); +} + +template +Array& Array::operator=(const Array& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +template +Array::Array(Array&& other) { + this->arr = other.arr; + other.arr = nullptr; +} + +template +Array& Array::operator=(Array&& other) { + if (this != &other) { + free(); + + this->arr = other.arr; + other.arr = nullptr; + } + return *this; +} + +template +T& Array::operator[](unsigned index) { + return arr[index]; +} + +template +T& Array::operator[](unsigned index) const { + return arr[index]; +} + +template +Array& Array::operator+=(const Array& right) { + for (int i = 0; i < right.size; i++) { + if (size == allocated) { + resize(); + } + arr[size++] = right[i]; + } +} + +template +bool operator==(const Array& left, const Array& right) { + if (left.size != right.size) { + return false; + } + + for (int i = 0; i < left.size; i++) { + if (left.arr[i] != right.arr[i]) { + return false; + } + } + return true; +} + +template +bool operator!=(const Array& left, const Array& right) { + return !(left == right); +} + +template +Array operator+(const Array& left, const Array& right) { + Array concat; + concat += left; + concat += right; + return concat; +} -- cgit v1.2.3