From c1c753cbbd384a46c6a81a62e667347299757267 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 28 Apr 2022 19:05:37 +0300 Subject: Autocommit at 28.04.2022 @ 19:05 --- src/List/List.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/List/List.cpp (limited to 'src/List/List.cpp') diff --git a/src/List/List.cpp b/src/List/List.cpp new file mode 100644 index 0000000..a1892dd --- /dev/null +++ b/src/List/List.cpp @@ -0,0 +1,58 @@ +#include "List.h" + +/* Private */ + +template +void List::resize() { + length <<= 1; + T* temp = new T[length]; + for (int i = 0; i < count; i++) + temp[i] = elements[i]; + delete[] elements; + elements = temp; +} + +template +void List::free() { + delete[] elements; +} + +template +void List::copyFrom(const List& other) { + elements = new T[other.length]; + for (int i = 0; i < other.count; i++) + elements[i] = other.elements[i]; + length = other.length; + count = other.count; +} + +/* Public */ + +template +void List::add(const T& element) { + if (length == count) resize(); + + elements[count++] = element; +} + +template +bool List::removeAt(unsigned index) { + if (index >= count) return false; + + delete elements[index]; + for (int i = index; i < count; i++) + elements[i] = elements[i + 1]; + elements[count--] = nullptr; +} + +template +T& List::operator[](unsigned index) { + return elements[index]; +} + +// Rule of 4 + +template +List::List() { + +} -- cgit v1.3.1-26-gb76a