Website Summary Files Commits Branches @commit @diff @stats
path: root / Week11 / Exercise04
diff options
context:
space:
mode:
2 files changed, 170 insertions, 0 deletions
-rw-r--r--Week11/Exercise04/Vector.hpp147
-rw-r--r--Week11/Exercise04/main.cpp23
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 */
diff --git a/Week11/Exercise04/main.cpp b/Week11/Exercise04/main.cpp
new file mode 100644
index 0000000..cdf3555
--- /dev/null
+++ b/Week11/Exercise04/main.cpp
@@ -0,0 +1,23 @@
+#include "Vector.hpp"
+#include <iostream>
+
+void print(const Vector<int> numbers) {
+ for (unsigned i = 0; i < numbers.Count(); ++i)
+ std::cout << numbers[i] << ' ';
+ std::cout << std::endl;
+}
+
+int main() {
+ Vector<int> nums;
+ nums.PushBack(1);
+ nums.PushBack(2);
+ nums.PushBack(3);
+
+ Vector<int> numsMore(nums);
+ numsMore.PushBack(4);
+ numsMore.PushBack(5);
+
+ print(nums + numsMore);
+
+ return 0;
+}