Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / 08-Practice-12.04.2022 / Vector.cpp
diff options
context:
space:
mode:
Commit6d4fa30cfd908e20a2ca2c70864a07007347ce98 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-04-19 15:11:17 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-04-19 15:11:20 +0300
Files45a318ee4fd017723b40bafa505fba0e368cde65 /Year 1/Object-oriented Programming/08-Practice-12.04.2022/Vector.cpp
uni-projects-6d4fa30cfd908e20a2ca2c70864a07007347ce98.tar
uni-projects-6d4fa30cfd908e20a2ca2c70864a07007347ce98.tar.gz
uni-projects-6d4fa30cfd908e20a2ca2c70864a07007347ce98.zip
Parent33153fb1da042f5604f195c296e2b82e9a6b9da6 (diff)
Added work from previous oop practice
1 files changed, 148 insertions, 0 deletions
-rw-r--r--Year 1/Object-oriented Programming/08-Practice-12.04.2022/Vector.cpp148
diff --git a/Year 1/Object-oriented Programming/08-Practice-12.04.2022/Vector.cpp b/Year 1/Object-oriented Programming/08-Practice-12.04.2022/Vector.cpp
new file mode 100644
index 0000000..073d12d
--- /dev/null
+++ b/Year 1/Object-oriented Programming/08-Practice-12.04.2022/Vector.cpp
@@ -0,0 +1,148 @@
+#include "Vector.h"
+
+void Vector::resize()
+{
+ capacity *= 2;
+ int* newArr = new int[capacity];
+ for (size_t i = 0; i < size; i++)
+ {
+ newArr[i] = arr[i];
+ }
+ free();
+ arr = newArr;
+}
+
+void Vector::free()
+{
+ delete[] arr;
+}
+
+void Vector::copyFrom(const Vector& other)
+{
+ size = other.size;
+ capacity = other.capacity;
+ arr = new int[capacity];
+ for (int i = 0; i < size; i++)
+ {
+ arr[i] = other.arr[i];
+ }
+}
+
+void Vector::swap(const int ind1, const int ind2) {
+ int temp = arr[ind1];
+ arr[ind1] = arr[ind2];
+ arr[ind2] = temp;
+}
+
+Vector::Vector()
+{
+ arr = new int[2];
+ capacity = 2;
+ size = 0;
+}
+
+Vector::Vector(const Vector& other)
+{
+ copyFrom(other);
+}
+
+Vector& Vector::operator=(const Vector& other)
+{
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+Vector::~Vector()
+{
+ free();
+}
+
+void Vector::push_back(const int element)
+{
+ if (size < capacity) {
+ arr[size] = element;
+ size++;
+ }
+ else
+ {
+ resize();
+ push_back(element);
+ }
+}
+
+int Vector::getSize() const
+{
+ return size;
+}
+
+int& Vector::operator[](const int index)
+{
+ if (index >= 0 && index < size) {
+ return arr[index];
+ }
+ throw "Out of range exception";
+
+}
+
+const int& Vector::operator[](int index) const
+{
+ if (index >= 0 && index < size) {
+ return arr[index];
+ }
+ throw "Out of range exception";
+}
+
+void Vector::deleteAt(const int index) {
+ for (int i = index; i < size; i++) {
+ arr[i] = arr[i + 1];
+ }
+ pop_back();
+}
+
+void Vector::pop_back() {
+ arr[size--] = int();
+}
+
+int Vector::findItem(const int item) const {
+ for (int i = 0; i < size; i++) {
+ if (arr[i] == item)
+ return i;
+ }
+ return -1;
+}
+
+void Vector::clear() {
+ delete[] arr;
+ size = capacity = 0;
+}
+
+void Vector::sort() {
+ for (int i = 0; i < size; i++) {
+ for (int j = 0; j < size; j++) {
+ if (arr[i] > arr[j])
+ swap(i, j);
+ }
+ }
+}
+
+void Vector::reverse() {
+ for (int halfSize = size / 2, offset = 0; offset < halfSize; offset++)
+ swap(offset, size - offset - 1);
+}
+
+std::ostream& operator<<(std::ostream& ostr, const Vector& vec) {
+ ostr << "<";
+ for (int i = 0; i < vec.getSize() - 2; i++) {
+ ostr << vec[i] << ", ";
+ }
+ ostr << vec[vec.getSize() - 1] << ">";
+ return ostr;
+}
+
+std::istream& operator<<(std::istream& istr, const Vector& vec) {
+
+ return istr;
+}