diff options
| Commit | 5e9cb4ddc811465731596d8ee684f3d8cd5a6aea (patch) | |
|---|---|---|
| Author | 2026-05-02 16:05:06 +0300 | |
| Committer | 2026-05-02 16:05:06 +0300 | |
| Files | 33a9268d1136974d2ef138364dde508ba43b4a52 /Week11/Exercise04/Vector.hpp | |
| oop-2025-solutions-main.tar oop-2025-solutions-main.tar.gz oop-2025-solutions-main.zip | ||
| Parent | 7546f0d0da487e16f0a8671c09a46e1cee2bc03a (diff) | |
1 files changed, 147 insertions, 0 deletions
| -rw-r--r-- | Week11/Exercise04/Vector.hpp | 147 |
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 T> +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<class T> +Vector<T> operator+(const Vector<T>& lhs, const Vector<T>& rhs); + +template<class T> +void Vector<T>::free() { + delete[] objects; +} + +template<class T> +void Vector<T>::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<class T> +void Vector<T>::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<class T> +Vector<T>::Vector() : objects(), objectsAlloc(), objectsCount() +{ } + +template<class T> +Vector<T>::~Vector() { + free(); +} + +template<class T> +Vector<T>::Vector(const Vector& other) { + copyFrom(other); +} + +template<class T> +Vector<T>& Vector<T>::operator=(const Vector& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +template<class T> +Vector<T>::Vector(Vector&& other) { + objects = other.objects; + objectsCount = other.objectsCount; + objectsAlloc = other.objectsAlloc; + + other.objects = nullptr; + other.objectsCount = other.objectsAlloc = 0; +} + +template<class T> +Vector<T>& Vector<T>::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<class T> +unsigned Vector<T>::Count() const { + return objectsCount; +} + +template<class T> +void Vector<T>::PushBack(const T& object) { + if (objectsCount >= objectsAlloc) + resize(); + objects[objectsCount++] = object; +} + +template<class T> +T& Vector<T>::operator[](unsigned index) { + return objects[index]; +} + +template<class T> +const T& Vector<T>::operator[](unsigned index) const { + return objects[index]; +} + +template<class T> +Vector<T>& Vector<T>::operator+=(const Vector& rhs) { + for (unsigned i = 0; i < rhs.objectsCount; ++i) + PushBack(rhs.objects[i]); + return *this; +} + +template<class T> +Vector<T> operator+(const Vector<T>& lhs, const Vector<T>& rhs) { + Vector val(lhs); + val += rhs; + return val; +} + +#endif /* _VECTOR */ |
