From b844597fd23bc57aa97e231fc90eecfd6322cecc Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 7 May 2022 08:28:27 +0300 Subject: Reorganized whole architecture --- src/List/List.hpp | 213 ----------------------------------------------- src/List/OrderedList.hpp | 36 -------- 2 files changed, 249 deletions(-) delete mode 100644 src/List/List.hpp delete mode 100644 src/List/OrderedList.hpp (limited to 'src/List') diff --git a/src/List/List.hpp b/src/List/List.hpp deleted file mode 100644 index 7088f2e..0000000 --- a/src/List/List.hpp +++ /dev/null @@ -1,213 +0,0 @@ -#ifndef HEADER_LIST -#define HEADER_LIST - -#include -#include - -template -class List { -protected: - T* elements; - unsigned length; - unsigned count; - - void resize(); - void free(); - void copyFrom(const List& other); - -public: - List(const T* elements, unsigned elementsCount); - void add(const T& element); - void insertAt(const T& element, unsigned index); - bool removeAt(unsigned index); - unsigned findIndex(const T& element) const; - bool contain(const T& element) const; - T& operator[](unsigned index); - const T& operator[](unsigned index) const; - - std::istream& read(std::istream& istr); - std::ostream& write(std::ostream& ostr) const; - - unsigned get_length() const; - unsigned get_count() const; - - List(); - List& operator=(const List& other); - List(const List& other); - ~List(); - - List(List&& other); - List& operator=(List&& other); -}; - -/* Private */ - -template -void List::resize() { - length = (length == 0) ? 8 : 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 -List::List(const T* elements, unsigned elementsCount) { - length = 8; - count = elementsCount; - this->elements = new T[length]; - for (unsigned i = 0; i < count; i++) - this->elements[i] = elements[i]; -} - -template -void List::add(const T& element) { - if (length == count) resize(); - - elements[count++] = element; -} - -template -void List::insertAt(const T& element, unsigned index) { - if (index >= count) add(element); - if (length == count) resize(); - - for (unsigned i = count; i > index; i--) - elements[i] = elements[i - 1]; - - elements[index] = element; - count++; -} - -template -bool List::removeAt(unsigned index) { - if (index >= count) return false; - - for (int i = index; i < count; i++) - elements[i] = elements[i + 1]; - count--; - return true; -} - -template -unsigned List::findIndex(const T& element) const { - unsigned ind = 0; - while (ind < count && elements[ind].compare(element) != 0) - ind++; - return ind; -} - -template -bool List::contain(const T& element) const { - return findIndex(element) < count; -} - -template -T& List::operator[](unsigned index) { - return elements[index]; -} - -template -const T& List::operator[](unsigned index) const { - return elements[index]; -} - -template -std::istream& List::read(std::istream& istr) { - istr.read((char*)&length, sizeof(length)); - istr.read((char*)&count, sizeof(count)); - - delete[] elements; - elements = new T[length]; - - for (int i = 0; i < count; i++) - elements[i].read(istr); // disgusting - - return istr; -} - -template -std::ostream& List::write(std::ostream& ostr) const { - ostr.write((const char*)&length, sizeof(length)); - ostr.write((const char*)&count, sizeof(count)); - - for (int i = 0; i < count; i++) - elements[i].write(ostr); // disgusting - - return ostr; -} - -template -unsigned List::get_length() const { - return length; -} - -template -unsigned List::get_count() const { - return count; -} - -// Rule of 4 - -template -List::List() : List(nullptr, 0) {} - -template -List& List::operator=(const List& other) { - if (this != &other) { - free(); - copyFrom(other); - } - return *this; -} - -template -List::List(const List& other) { - copyFrom(other); -} - -template -List::~List() { - free(); -} - -// Move semantics - -template -List::List(List&& other) { - length = other.length; - count = other.count; - elements = other.elements; - other.elements = nullptr; -} - -template -List& List::operator=(List&& other) { - if (this != &other) { - free(); - length = other.length; - count = other.count; - elements = other.elements; - other.elements = nullptr; - } - return *this; -} - -#endif diff --git a/src/List/OrderedList.hpp b/src/List/OrderedList.hpp deleted file mode 100644 index 4396d33..0000000 --- a/src/List/OrderedList.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef HEADER_ORDEREDLIST -#define HEADER_ORDEREDLIST - -#include "List.hpp" - -template -class OrderedList : public List { -public: - OrderedList(); - OrderedList(const T* elements, unsigned elementsCount); - - void add(const T& element) = delete; - void insertAt(const T& element, unsigned index) = delete; - - void insert(const T& element); - // unsigned findIndex(const T& element) const; TODO: binary search -}; - -template -OrderedList::OrderedList() : OrderedList(nullptr, 0) {} - -template -OrderedList::OrderedList(const T* elements, unsigned elementsCount) : List::List(elements, elementsCount) {} - -template -void OrderedList::insert(const T& element) { - if (List::length == List::count) List::resize(); - - unsigned insertionInd = 0; - while (List::elements[insertionInd].compare(element) < 0 && insertionInd < List::count) - insertionInd++; - - List::insertAt(element, insertionInd); -} - -#endif -- cgit v1.3.1-23-gd82d