aboutsummaryrefslogtreecommitdiff
path: root/week11/Exercise06/TwoArray.hpp
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2024-05-10 10:10:21 +0300
committerSyndamia <kamen@syndamia.com>2024-05-10 10:10:21 +0300
commit7b19cabee8b08478f31f6e4594ed28e1d04e153c (patch)
tree3574b2c3fd75ab66701def640fe7476651236184 /week11/Exercise06/TwoArray.hpp
parent437e306dc9b79905105fb2e8af6dd1eae1b908ae (diff)
downloadoop-2023-solutions-7b19cabee8b08478f31f6e4594ed28e1d04e153c.tar
oop-2023-solutions-7b19cabee8b08478f31f6e4594ed28e1d04e153c.tar.gz
oop-2023-solutions-7b19cabee8b08478f31f6e4594ed28e1d04e153c.zip
[w11] Solved exercises
Diffstat (limited to 'week11/Exercise06/TwoArray.hpp')
-rw-r--r--week11/Exercise06/TwoArray.hpp117
1 files changed, 117 insertions, 0 deletions
diff --git a/week11/Exercise06/TwoArray.hpp b/week11/Exercise06/TwoArray.hpp
new file mode 100644
index 0000000..62ff2c0
--- /dev/null
+++ b/week11/Exercise06/TwoArray.hpp
@@ -0,0 +1,117 @@
+#pragma once
+
+template <class T, class U>
+class TwoArray {
+ T* first;
+ U* second;
+ unsigned allocated;
+ unsigned size;
+
+ void free();
+ void copyFrom(const TwoArray& other);
+
+public:
+ TwoArray();
+ ~TwoArray();
+ TwoArray(const TwoArray& other);
+ TwoArray& operator=(const TwoArray& other);
+ TwoArray(TwoArray&& other);
+ TwoArray& operator=(TwoArray&& other);
+
+ T& operator[](int index);
+ const T& operator[](int index) const;
+ U& operator()(int index);
+ const U& operator()(int index) const;
+};
+
+template <class T, class U>
+void TwoArray<T, U>::free() {
+ delete[] first;
+ delete[] second;
+}
+
+template <class T, class U>
+void TwoArray<T, U>::copyFrom(const TwoArray& 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>
+TwoArray<T, U>::TwoArray() {
+ first = second = nullptr;
+ size = allocated = 0;
+}
+
+template <class T, class U>
+TwoArray<T, U>::~TwoArray() {
+ free();
+}
+
+template <class T, class U>
+TwoArray<T, U>::TwoArray(const TwoArray& other) {
+ copyFrom(other);
+}
+
+template <class T, class U>
+TwoArray<T, U>& TwoArray<T, U>::operator=(const TwoArray& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+template <class T, class U>
+TwoArray<T, U>::TwoArray(TwoArray&& 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>
+TwoArray<T, U>& TwoArray<T, U>::operator=(TwoArray&& 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>
+T& TwoArray<T, U>::operator[](int index) {
+ if (index < 0 || index >= size) throw "Invalid index!";
+ return first[index];
+}
+
+template <class T, class U>
+const T& TwoArray<T, U>::operator[](int index) const {
+ if (index < 0 || index >= size) throw "Invalid index!";
+ return first[index];
+}
+
+template <class T, class U>
+U& TwoArray<T, U>::operator()(int index) {
+ if (index < 0 || index >= size) throw "Invalid index!";
+ return second[index];
+}
+
+template <class T, class U>
+const U& TwoArray<T, U>::operator()(int index) const {
+ if (index < 0 || index >= size) throw "Invalid index!";
+ return second[index];
+}