From f3406754705e508f57768d3d0d8e457ff5b7620c Mon Sep 17 00:00:00 2001 From: Syndamia Date: Mon, 22 Apr 2024 15:43:49 +0300 Subject: [w8] Added exercise descriptions and solutions to ex 1-7 --- week08/Exercise5.hpp | 203 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 203 insertions(+) create mode 100644 week08/Exercise5.hpp (limited to 'week08/Exercise5.hpp') diff --git a/week08/Exercise5.hpp b/week08/Exercise5.hpp new file mode 100644 index 0000000..80c435e --- /dev/null +++ b/week08/Exercise5.hpp @@ -0,0 +1,203 @@ +#include + +template +class DynamicArray { + T *values; + int lastUnused; + int allocated; + + void free(); + void copyFrom(const DynamicArray& other); + + void resize(); + +public: + DynamicArray(); + ~DynamicArray(); + DynamicArray(const DynamicArray& other); + DynamicArray& operator=(const DynamicArray& other); + DynamicArray(DynamicArray&& other); + DynamicArray& operator=(DynamicArray&& other); + + void Add(const T& newValue); + + // Тези оператори променят аргумента си, затова е по-добре да са член-функции + DynamicArray& operator+=(const DynamicArray& other); + DynamicArray& operator+=(const T& newValue); + // По дефиниция не може operator=, operator(), operator[] и operator-> да бъдат имплементирани + // като приятелски функции + T& operator[](int index); + + // Тези оператори не променят аргументите си, затова е по-добре да са приятелски + template + friend DynamicArray operator+(const DynamicArray& left, const DynamicArray& right); + template + friend DynamicArray operator+(const DynamicArray& left, const U& right); + template + friend bool operator==(const DynamicArray& left, const DynamicArray& right); + template + friend bool operator!=(const DynamicArray& left, const DynamicArray& right); + template + friend std::ostream& operator<<(std::ostream& ostr, const DynamicArray& right); + template + friend std::istream& operator>>(std::istream& ostr, DynamicArray& right); +}; + +template +void DynamicArray::free() { + delete[] values; +} + +template +void DynamicArray::copyFrom(const DynamicArray& other) { + this->lastUnused = other.lastUnused; + this->allocated = other.allocated; + this->values = new T[allocated]; + for (int i = 0; i < this->lastUnused; i++) { + this->values[i] = other.values[i]; + } +} + +template +void DynamicArray::resize() { + allocated *= 2; + T* moreValues = new T[allocated]; + for (int i = 0; i < lastUnused; i++) { + moreValues[i] = values[i]; + } + delete[] values; + values = moreValues; +} + +template +DynamicArray::DynamicArray() { + values = nullptr; + lastUnused = allocated = 0; +} + +template +DynamicArray::~DynamicArray() { + free(); +} + +template +DynamicArray::DynamicArray(const DynamicArray& other) { + copyFrom(other); +} + +template +DynamicArray& DynamicArray::operator=(const DynamicArray& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +template +DynamicArray::DynamicArray(DynamicArray&& other) { + this->values = other.values; + other.values = nullptr; + + this->lastUnused = other.lastUnused; + this->allocated = other.allocated; +} + +template +DynamicArray& DynamicArray::operator=(DynamicArray&& other) { + if (this != &other) { + free(); + + this->values = other.values; + other.values = nullptr; + + this->lastUnused = other.lastUnused; + this->allocated = other.allocated; + } + return *this; +} + +template +void DynamicArray::Add(const T& number) { + if (allocated == lastUnused) { + resize(); + } + values[lastUnused++] = number; +} + +template +DynamicArray& DynamicArray::operator+=(const DynamicArray& other) { + for (int i = 0; i < other.lastUnused; i++) { + this->Add(other.values[i]); + } + return *this; +} + +template +DynamicArray& DynamicArray::operator+=(const T& number) { + for (int i = 0; i < lastUnused; i++) { + values[i] += number; + } + return *this; +} + +template +T& DynamicArray::operator[](int index) { + if (index < 0 || lastUnused <= index) { + throw "Invalid index!"; + } + return values[index]; +} + +template +DynamicArray operator+(const DynamicArray& left, const DynamicArray& right) { + DynamicArray output; + (output += left) += right; + return output; +} + +template +DynamicArray operator+(const DynamicArray& left, T right) { + DynamicArray output = left; // Копиращ конструктор + output += right; + return output; +} + +template +bool operator==(const DynamicArray& left, const DynamicArray& right) { + if (left.lastUnused != right.lastUnused) { + return false; + } + for (int i = 0; i < left.lastUnused; i++) { + if (left.values[i] != right.values[i]) { + return false; + } + } + return true; +} + +template +bool operator!=(const DynamicArray& left, const DynamicArray& right) { + return !(left == right); +} + +template +std::ostream& operator<<(std::ostream& ostr, const DynamicArray& right) { + for (int i = 0; i < right.lastUnused; i++) { + ostr << right.values[i] << " "; + } + return ostr; +} + +template +std::istream& operator>>(std::istream& istr, DynamicArray& right) { + int length; + istr >> length; + right.lastUnused = 0; + for (int i = 0; i < length; i++) { + T temp; + istr >> temp; + right.Add(temp); + } + return istr; +} -- cgit v1.2.3