From 5e9cb4ddc811465731596d8ee684f3d8cd5a6aea Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 2 May 2026 16:05:06 +0300 Subject: feat(Week11): Add solutions --- Week11/Exercise04/Vector.hpp | 147 +++++++++++++++++++++++++++++++++++++++++++ Week11/Exercise04/main.cpp | 23 +++++++ 2 files changed, 170 insertions(+) create mode 100644 Week11/Exercise04/Vector.hpp create mode 100644 Week11/Exercise04/main.cpp (limited to 'Week11/Exercise04') diff --git a/Week11/Exercise04/Vector.hpp b/Week11/Exercise04/Vector.hpp new file mode 100644 index 0000000..78dba08 --- /dev/null +++ b/Week11/Exercise04/Vector.hpp @@ -0,0 +1,147 @@ +#ifndef _VECTOR +#define _VECTOR + +template +class Vector { + T* objects; + unsigned objectsCount; + unsigned objectsAlloc; + + void free(); + void copyFrom(const Vector& other); + + void resize(); + +public: + Vector(); + ~Vector(); + Vector(const Vector& other); + Vector& operator=(const Vector& other); + + Vector(Vector&& other); + Vector& operator=(Vector&& other); + + unsigned Count() const; + void PushBack(const T& object); + + T& operator[](unsigned index); + const T& operator[](unsigned index) const; + + Vector& operator+=(const Vector& rhs); +}; + +template +Vector operator+(const Vector& lhs, const Vector& rhs); + +template +void Vector::free() { + delete[] objects; +} + +template +void Vector::copyFrom(const Vector& other) { + objectsCount = other.objectsCount; + objectsAlloc = other.objectsAlloc; + + objects = new T[objectsAlloc]; + for (unsigned i = 0; i < objectsCount; ++i) + objects[i] = other.objects[i]; +} + +template +void Vector::resize() { + objectsAlloc = objectsAlloc == 0 ? 8 : objectsAlloc * 2; + T* objectsLarger = new T[objectsAlloc]; + + for (unsigned i = 0; i < objectsCount; ++i) + objectsLarger[i] = objects[i]; + + delete[] objects; + objects = objectsLarger; +} + +template +Vector::Vector() : objects(), objectsAlloc(), objectsCount() +{ } + +template +Vector::~Vector() { + free(); +} + +template +Vector::Vector(const Vector& other) { + copyFrom(other); +} + +template +Vector& Vector::operator=(const Vector& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +template +Vector::Vector(Vector&& other) { + objects = other.objects; + objectsCount = other.objectsCount; + objectsAlloc = other.objectsAlloc; + + other.objects = nullptr; + other.objectsCount = other.objectsAlloc = 0; +} + +template +Vector& Vector::operator=(Vector&& other) { + if (this != &other) { + free(); + + objects = other.objects; + objectsCount = other.objectsCount; + objectsAlloc = other.objectsAlloc; + + other.objects = nullptr; + other.objectsCount = other.objectsAlloc = 0; + } + return *this; +} + +template +unsigned Vector::Count() const { + return objectsCount; +} + +template +void Vector::PushBack(const T& object) { + if (objectsCount >= objectsAlloc) + resize(); + objects[objectsCount++] = object; +} + +template +T& Vector::operator[](unsigned index) { + return objects[index]; +} + +template +const T& Vector::operator[](unsigned index) const { + return objects[index]; +} + +template +Vector& Vector::operator+=(const Vector& rhs) { + for (unsigned i = 0; i < rhs.objectsCount; ++i) + PushBack(rhs.objects[i]); + return *this; +} + +template +Vector operator+(const Vector& lhs, const Vector& rhs) { + Vector val(lhs); + val += rhs; + return val; +} + +#endif /* _VECTOR */ diff --git a/Week11/Exercise04/main.cpp b/Week11/Exercise04/main.cpp new file mode 100644 index 0000000..cdf3555 --- /dev/null +++ b/Week11/Exercise04/main.cpp @@ -0,0 +1,23 @@ +#include "Vector.hpp" +#include + +void print(const Vector numbers) { + for (unsigned i = 0; i < numbers.Count(); ++i) + std::cout << numbers[i] << ' '; + std::cout << std::endl; +} + +int main() { + Vector nums; + nums.PushBack(1); + nums.PushBack(2); + nums.PushBack(3); + + Vector numsMore(nums); + numsMore.PushBack(4); + numsMore.PushBack(5); + + print(nums + numsMore); + + return 0; +} -- cgit v1.3.1-23-gd82d