aboutsummaryrefslogtreecommitdiff
path: root/week13/Exercise2
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2024-05-25 16:08:17 +0300
committerSyndamia <kamen@syndamia.com>2024-05-25 16:08:17 +0300
commit9c11fababb9b6b194e646fbeb62d141aacf74c43 (patch)
treef1a294fcd66b22463b9424c838ff171db8539a15 /week13/Exercise2
parentc6fae5f1a0cc8394b1de3b671f07de7b3d677c59 (diff)
downloadoop-2023-solutions-9c11fababb9b6b194e646fbeb62d141aacf74c43.tar
oop-2023-solutions-9c11fababb9b6b194e646fbeb62d141aacf74c43.tar.gz
oop-2023-solutions-9c11fababb9b6b194e646fbeb62d141aacf74c43.zip
[w13] Added solutions
Diffstat (limited to 'week13/Exercise2')
-rw-r--r--week13/Exercise2/Container.hpp75
-rw-r--r--week13/Exercise2/Indexable.hpp25
-rw-r--r--week13/Exercise2/Resizeable.hpp34
-rw-r--r--week13/Exercise2/Vector.hpp35
4 files changed, 169 insertions, 0 deletions
diff --git a/week13/Exercise2/Container.hpp b/week13/Exercise2/Container.hpp
new file mode 100644
index 0000000..ace942f
--- /dev/null
+++ b/week13/Exercise2/Container.hpp
@@ -0,0 +1,75 @@
+#pragma once
+
+template <class T>
+class Container {
+ void free();
+ void copyFrom(const Container& other);
+
+protected:
+ T* arr;
+ unsigned size;
+
+public:
+ Container();
+ ~Container();
+ Container(const Container& other);
+ Container& operator=(const Container& other);
+ Container(Container&& other);
+ Container& operator=(Container&& other);
+};
+
+template <class T>
+void Container<T>::free() {
+ delete[] arr;
+}
+
+template <class T>
+void Container<T>::copyFrom(const Container& other) {
+ this->size = other.size;
+ this->arr = new T[size];
+ for (int i = 0; i < size; i++) {
+ this->arr[i] = other.arr[i];
+ }
+}
+
+template <class T>
+Container<T>::Container() {
+ this->arr = nullptr;
+ size = 0;
+}
+
+template <class T>
+Container<T>::~Container() {
+ free();
+}
+
+template <class T>
+Container<T>::Container(const Container<T>& other) {
+ copyFrom(other);
+}
+
+template <class T>
+Container<T>& Container<T>::operator=(const Container<T>& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+template <class T>
+Container<T>::Container(Container<T>&& other) {
+ this->arr = other.arr;
+ other.arr = nullptr;
+}
+
+template <class T>
+Container<T>& Container<T>::operator=(Container<T>&& other) {
+ if (this != &other) {
+ free();
+
+ this->arr = other.arr;
+ other.arr = nullptr;
+ }
+ return *this;
+}
diff --git a/week13/Exercise2/Indexable.hpp b/week13/Exercise2/Indexable.hpp
new file mode 100644
index 0000000..dd5fb6b
--- /dev/null
+++ b/week13/Exercise2/Indexable.hpp
@@ -0,0 +1,25 @@
+#pragma once
+#include "Container.hpp"
+
+template <class T>
+class Indexable : virtual public Container<T> {
+public:
+ T& operator[](unsigned index);
+ const T& operator[](unsigned index) const;
+};
+
+template <class T>
+T& Indexable<T>::operator[](unsigned index) {
+ if (index > this->size) {
+ throw "Index out of range!";
+ }
+ return this->arr[index];
+}
+
+template <class T>
+const T& Indexable<T>::operator[](unsigned index) const {
+ if (index > this->size) {
+ throw "Index out of range!";
+ }
+ return this->arr[index];
+}
diff --git a/week13/Exercise2/Resizeable.hpp b/week13/Exercise2/Resizeable.hpp
new file mode 100644
index 0000000..9b4fe04
--- /dev/null
+++ b/week13/Exercise2/Resizeable.hpp
@@ -0,0 +1,34 @@
+#pragma once
+#include "Container.hpp"
+
+template <class T>
+class Resizeable : virtual public Container<T> {
+ void resize();
+
+public:
+ void InsertAt(const T& element, unsigned index);
+};
+
+template <class T>
+void Resizeable<T>::resize() {
+ this->size++;
+ T* biggerArr = new T[this->size];
+ for (int i = 0; i < this->size - 1; i++) {
+ biggerArr[i] = this->arr[i];
+ }
+ delete[] this->arr;
+ this->arr = biggerArr;
+}
+
+template <class T>
+void Resizeable<T>::InsertAt(const T& element, unsigned index) {
+ if (index >= this->size) {
+ throw "Index out of range!";
+ }
+
+ resize();
+ for (int i = this->size - 1; i > index; i++) {
+ this->arr[i] = this->arr[i-1];
+ }
+ this->arr[index] = element;
+}
diff --git a/week13/Exercise2/Vector.hpp b/week13/Exercise2/Vector.hpp
new file mode 100644
index 0000000..ba66049
--- /dev/null
+++ b/week13/Exercise2/Vector.hpp
@@ -0,0 +1,35 @@
+#pragma once
+#include "Indexable.hpp"
+#include "Resizeable.hpp"
+
+template <class T>
+class Vector : public Indexable<T>, public Resizeable<T> {
+public:
+ void pop_back();
+ void pop_front();
+ void push_back(const T& element);
+ void push_front(const T& element);
+};
+
+template <class T>
+void Vector<T>::pop_back() {
+ this->size--;
+}
+
+template <class T>
+void Vector<T>::pop_front() {
+ for (int i = 0; i < this->size - 1; i++) {
+ this->arr[i] = this->arr[i+1];
+ }
+ this->size--;
+}
+
+template <class T>
+void Vector<T>::push_back(const T& element) {
+ this->InsertAt(this->size - 1, element);
+}
+
+template <class T>
+void Vector<T>::push_front(const T& element) {
+ this->InsertAt(0, element);
+}