diff options
| Commit | 5e9cb4ddc811465731596d8ee684f3d8cd5a6aea (patch) | |
|---|---|---|
| Author | 2026-05-02 16:05:06 +0300 | |
| Committer | 2026-05-02 16:05:06 +0300 | |
| Files | 33a9268d1136974d2ef138364dde508ba43b4a52 /Week11/Exercise05 | |
| oop-2025-solutions-main.tar oop-2025-solutions-main.tar.gz oop-2025-solutions-main.zip | ||
| Parent | 7546f0d0da487e16f0a8671c09a46e1cee2bc03a (diff) | |
2 files changed, 244 insertions, 0 deletions
| -rw-r--r-- | Week11/Exercise05/DoubleVector.hpp | 201 | ||||
| -rw-r--r-- | Week11/Exercise05/Pair.hpp | 43 |
diff --git a/Week11/Exercise05/DoubleVector.hpp b/Week11/Exercise05/DoubleVector.hpp new file mode 100644 index 0000000..7ffe26c --- /dev/null +++ b/Week11/Exercise05/DoubleVector.hpp @@ -0,0 +1,201 @@ +#ifndef _DOUBLEVECTOR +#define _DOUBLEVECTOR + +#include "Pair.hpp" + +template<class T, class U> +class DoubleVector { + T* first; + unsigned firstCount; + unsigned firstAlloc; + + T* second; + unsigned secondCount; + unsigned secondAlloc; + + void free(); + void copyFrom(const DoubleVector& other); + + void firstResize(); + void secondResize(); + +public: + DoubleVector(); + ~DoubleVector(); + DoubleVector(const DoubleVector& other); + DoubleVector& operator=(const DoubleVector& other); + + DoubleVector(DoubleVector&& other); + DoubleVector& operator=(DoubleVector&& other); + + Pair<unsigned, unsigned> Count() const; + + void FirstPushBack(const T& object); + void SecondPushBack(const U& object); + + Pair<T&, U&> operator[](unsigned index); + const Pair<T&, U&> operator[](unsigned index) const; + + DoubleVector& operator+=(const DoubleVector& rhs); +}; + +template<class T, class U> +DoubleVector<T, U> operator+(const DoubleVector<T, U>& lhs, const DoubleVector<T, U>& rhs); + +template<class T, class U> +void DoubleVector<T, U>::free() { + delete[] first; + delete[] second; +} + +template<class T, class U> +void DoubleVector<T, U>::copyFrom(const DoubleVector& other) { + firstCount = other.firstCount; + firstAlloc = other.firstAlloc; + + first = new T[firstAlloc]; + for (unsigned i = 0; i < firstCount; ++i) + first[i] = other.first[i]; + + secondCount = other.secondCount; + secondAlloc = other.secondAlloc; + + second = new T[secondAlloc]; + for (unsigned i = 0; i < secondCount; ++i) + second[i] = other.second[i]; +} + +template<class T, class U> +void DoubleVector<T, U>::firstResize() { + firstAlloc = firstAlloc == 0 ? 8 : firstAlloc * 2; + T* firstLarger = new T[firstAlloc]; + + for (unsigned i = 0; i < firstCount; ++i) + firstLarger[i] = first[i]; + + delete[] first; + first = firstLarger; +} + +template<class T, class U> +void DoubleVector<T, U>::secondResize() { + secondAlloc = secondAlloc == 0 ? 8 : secondAlloc * 2; + U* secondLarger = new U[secondAlloc]; + + for (unsigned i = 0; i < secondCount; ++i) + secondLarger[i] = second[i]; + + delete[] second; + second = secondLarger; +} + +template<class T, class U> +DoubleVector<T, U>::DoubleVector() + : first(), firstAlloc(), firstCount(), + second(), secondAlloc(), secondCount() +{ } + +template<class T, class U> +DoubleVector<T, U>::~DoubleVector() { + free(); +} + +template<class T, class U> +DoubleVector<T, U>::DoubleVector(const DoubleVector& other) { + copyFrom(other); +} + +template<class T, class U> +DoubleVector<T, U>& DoubleVector<T, U>::operator=(const DoubleVector& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +template<class T, class U> +DoubleVector<T, U>::DoubleVector(DoubleVector&& other) { + first = other.first; + firstCount = other.firstCount; + firstAlloc = other.firstAlloc; + + second = other.second; + secondCount = other.secondCount; + secondAlloc = other.secondAlloc; + + other.first = nullptr; + other.firstCount = other.firstAlloc = 0; + + other.second = nullptr; + other.secondCount = other.secondAlloc = 0; +} + +template<class T, class U> +DoubleVector<T, U>& DoubleVector<T, U>::operator=(DoubleVector&& other) { + if (this != &other) { + free(); + + first = other.first; + firstCount = other.firstCount; + firstAlloc = other.firstAlloc; + + second = other.second; + secondCount = other.secondCount; + secondAlloc = other.secondAlloc; + + other.first = nullptr; + other.firstCount = other.firstAlloc = 0; + + other.second = nullptr; + other.secondCount = other.secondAlloc = 0; + } + return *this; +} + +template<class T, class U> +Pair<unsigned, unsigned> DoubleVector<T, U>::Count() const { + return Pair(firstCount, secondCount); +} + +template<class T, class U> +void DoubleVector<T, U>::FirstPushBack(const T& object) { + if (firstCount >= firstAlloc) + firstResize(); + first[firstCount++] = object; +} + +template<class T, class U> +void DoubleVector<T, U>::SecondPushBack(const U& object) { + if (secondCount >= secondAlloc) + secondResize(); + second[secondCount++] = object; +} + +template<class T, class U> +Pair<T&, U&> DoubleVector<T, U>::operator[](unsigned index) { + return Pair(first[index], second[index]); +} + +template<class T, class U> +const Pair<T&, U&> DoubleVector<T, U>::operator[](unsigned index) const { + return Pair(first[index], second[index]); +} + +template<class T, class U> +DoubleVector<T, U>& DoubleVector<T, U>::operator+=(const DoubleVector& rhs) { + for (unsigned i = 0; i < rhs.firstCount; ++i) + FirstPushBack(rhs.first[i]); + for (unsigned i = 0; i < rhs.secondCount; ++i) + SecondPushBack(rhs.second[i]); + return *this; +} + +template<class T, class U> +DoubleVector<T, U> operator+(const DoubleVector<T, U>& lhs, const DoubleVector<T, U>& rhs) { + DoubleVector val(lhs); + val += rhs; + return val; +} + +#endif /* _DOUBLEVECTOR */ diff --git a/Week11/Exercise05/Pair.hpp b/Week11/Exercise05/Pair.hpp new file mode 100644 index 0000000..4fd47fd --- /dev/null +++ b/Week11/Exercise05/Pair.hpp @@ -0,0 +1,43 @@ +#ifndef _PAIR +#define _PAIR + +template<class T, class U> +class Pair { + T left; + U right; + +public: + Pair(const T& left, const U& right); + + T& Left(); + const T& Left() const; + + U& Right(); + const U& Right() const; +}; + +template<class T, class U> +Pair<T, U>::Pair(const T& left, const U& right) : left(left), right(right) +{ } + +template<class T, class U> +T& Pair<T, U>::Left() { + return left; +} + +template<class T, class U> +const T& Pair<T, U>::Left() const { + return left; +} + +template<class T, class U> +U& Pair<T, U>::Right() { + return right; +} + +template<class T, class U> +const U& Pair<T, U>::Right() const { + return right; +} + +#endif /* _PAIR */ |
