diff options
Autocommit at 28.04.2022 @ 19:05
2 files changed, 85 insertions, 0 deletions
| -rw-r--r-- | src/List/List.cpp | 58 | ||||
| -rw-r--r-- | src/List/List.h | 27 |
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 <class T> +void List<T>::resize() { + length <<= 1; + T* temp = new T[length]; + for (int i = 0; i < count; i++) + temp[i] = elements[i]; + delete[] elements; + elements = temp; +} + +template <class T> +void List<T>::free() { + delete[] elements; +} + +template <class T> +void List<T>::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 <class T> +void List<T>::add(const T& element) { + if (length == count) resize(); + + elements[count++] = element; +} + +template <class T> +bool List<T>::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 <class T> +T& List<T>::operator[](unsigned index) { + return elements[index]; +} + +// Rule of 4 + +template <class T> +List<T>::List() { + +} diff --git a/src/List/List.h b/src/List/List.h new file mode 100644 index 0000000..c6f074d --- /dev/null +++ b/src/List/List.h @@ -0,0 +1,27 @@ +#ifndef HEADER_LIST +#define HEADER_LIST + +template <class T> +class List { + T* elements; + unsigned length; + unsigned count; + + void resize(); + void free(); + void copyFrom(const List& other); +public: + void add(const T& element); + bool removeAt(unsigned index); + T& operator[](unsigned index); + + List(); + List& operator=(const List& other); + List(const List& other); + ~List(); + + List(List&& other); + List& operator=(List&& other); +}; + +#endif |
