diff options
Diffstat (limited to 'week11/Exercise09')
| -rw-r--r-- | week11/Exercise09/DirectConcat.hpp | 32 | ||||
| -rw-r--r-- | week11/Exercise09/DirectConcat.hpp.gch | bin | 0 -> 2106544 bytes | |||
| -rw-r--r-- | week11/Exercise09/MixedArray.hpp | 107 | ||||
| -rw-r--r-- | week11/Exercise09/MixedConcat.hpp | 34 |
4 files changed, 173 insertions, 0 deletions
diff --git a/week11/Exercise09/DirectConcat.hpp b/week11/Exercise09/DirectConcat.hpp new file mode 100644 index 0000000..53f420b --- /dev/null +++ b/week11/Exercise09/DirectConcat.hpp @@ -0,0 +1,32 @@ +#pragma once +#include "MixedArray.hpp" + +template <class T, class U> +class DirectConcat : public MixedArray<T, U> { +public: + virtual MixedArray<T, U>* operator+=(const MixedArray<T, U>* other) override; +}; + +template <class T, class U> +MixedArray<T, U>* DirectConcat<T, U>::operator+=(const MixedArray<T, U>* other) { + T* biggerFirst = new T[this->allocated + other->allocated]; + U* biggerSecond = new U[this->allocated + other->allocated]; + + for (int i = 0; i < this->size; i++) { + biggerFirst[i] = this->first[i]; + biggerSecond[i] = this->second[i]; + } + for (int i = 0; i < other->size; i++) { + biggerFirst[this->size + i] = other->first[i]; + biggerSecond[this->size + i] = other->second[i]; + } + + delete[] this->first; + this->first = biggerFirst; + delete[] this->second; + this->second = biggerSecond; + this->allocated += other->allocated; + this->size += other->size; + + return this; +} diff --git a/week11/Exercise09/DirectConcat.hpp.gch b/week11/Exercise09/DirectConcat.hpp.gch Binary files differnew file mode 100644 index 0000000..d7e6e3f --- /dev/null +++ b/week11/Exercise09/DirectConcat.hpp.gch diff --git a/week11/Exercise09/MixedArray.hpp b/week11/Exercise09/MixedArray.hpp new file mode 100644 index 0000000..096a227 --- /dev/null +++ b/week11/Exercise09/MixedArray.hpp @@ -0,0 +1,107 @@ +#pragma once + +template <class T, class U> +class MixedArray { + void free(); + void copyFrom(const MixedArray& other); + +protected: + T* first; + U* second; + unsigned allocated; + unsigned size; + +public: + MixedArray(); + virtual ~MixedArray(); + MixedArray(const MixedArray& other); + MixedArray& operator=(const MixedArray& other); + MixedArray(MixedArray&& other); + MixedArray& operator=(MixedArray&& other); + + void At(int index, T*& valueFirst, U*& valueSecond); + + virtual MixedArray* operator+=(MixedArray* other) = 0; +}; + +template <class T, class U> +void MixedArray<T, U>::free() { + delete[] first; + delete[] second; +} + +template <class T, class U> +void MixedArray<T, U>::copyFrom(const MixedArray& other) { + this->size = other.size; + this->allocated = other.allocated; + first = new T[allocated]; + second = new U[allocated]; + for (int i = 0; i < size; i++) { + first[i] = other.first[i]; + second[i] = other.second[i]; + } +} + +template <class T, class U> +MixedArray<T, U>::MixedArray() { + first = second = nullptr; + size = allocated = 0; +} + +template <class T, class U> +MixedArray<T, U>::~MixedArray() { + free(); +} + +template <class T, class U> +MixedArray<T, U>::MixedArray(const MixedArray& other) { + copyFrom(other); +} + +template <class T, class U> +MixedArray<T, U>& MixedArray<T, U>::operator=(const MixedArray& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +template <class T, class U> +MixedArray<T, U>::MixedArray(MixedArray&& other) { + this->first = other.first; + other.first = nullptr; + this->second = other.second; + other.second = nullptr; + this->allocated = other.allocated; + this->size = other.size; +} + +template <class T, class U> +MixedArray<T, U>& MixedArray<T, U>::operator=(MixedArray&& other) { + if (this != &other) { + free(); + + this->first = other.first; + other.first = nullptr; + this->second = other.second; + other.second = nullptr; + this->allocated = other.allocated; + this->size = other.size; + } + return *this; +} + +template <class T, class U> +void MixedArray<T, U>::At(int index, T*& valueFirst, U*& valueSecond) { + if (index < 0) throw "Index out of bounds!"; + + if (index % 2 == 0) { + valueFirst = *first[index / 2]; + valueSecond = nullptr; + } + else { + valueFirst = nullptr; + valueSecond = *second[(index - 1) / 2]; + } +} diff --git a/week11/Exercise09/MixedConcat.hpp b/week11/Exercise09/MixedConcat.hpp new file mode 100644 index 0000000..2b43f80 --- /dev/null +++ b/week11/Exercise09/MixedConcat.hpp @@ -0,0 +1,34 @@ +#pragma once +#include "MixedArray.hpp" + +template <class T, class U> +class MixedConcat : public MixedArray<T, U> { +public: + virtual MixedArray<T, U>* operator+=(const MixedArray<T, U>* other) override; +}; + +template <class T, class U> +MixedArray<T, U>* MixedConcat<T, U>::operator+=(const MixedArray<T, U>* other) { + T* biggerFirst = new T[this->allocated + other->allocated]; + U* biggerSecond = new U[this->allocated + other->allocated]; + + for (int i = 0; i < this->size + other->size; i++) { + if (i % 2 == 0) { + biggerFirst[i] = this->first[i / 2]; + biggerSecond[i] = this->second[i / 2]; + } + else { + biggerFirst[i] = other->first[(i - 1) / 2]; + biggerSecond[i] = other->second[(i - 1) / 2]; + } + } + + delete[] this->first; + this->first = biggerFirst; + delete[] this->second; + this->second = biggerSecond; + this->allocated += other->allocated; + this->size += other->size; + + return this; +} |
