diff options
Moved templated stuff into hpp files and added a blank main.cpp file
5 files changed, 83 insertions, 84 deletions
| -rw-r--r-- | src/List/List.h | 42 | ||||
| -rw-r--r-- | src/List/List.hpp (renamed from src/List/List.cpp) | 52 | ||||
| -rw-r--r-- | src/List/OrderedList.cpp | 18 | ||||
| -rw-r--r-- | src/List/OrderedList.h | 19 | ||||
| -rw-r--r-- | src/List/OrderedList.hpp | 36 |
diff --git a/src/List/List.h b/src/List/List.h deleted file mode 100644 index 23e3ee5..0000000 --- a/src/List/List.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef HEADER_LIST -#define HEADER_LIST - -#include "istream" -#include "ostream" - -template <class T> -class List { - 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); - bool 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); -}; - -#endif diff --git a/src/List/List.cpp b/src/List/List.hpp index e5b360c..1e75c89 100644 --- a/src/List/List.cpp +++ b/src/List/List.hpp @@ -1,4 +1,44 @@ -#include "List.h" +#ifndef HEADER_LIST +#define HEADER_LIST + +#include <istream> +#include <ostream> + +template <class T> +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 */ @@ -43,8 +83,8 @@ void List<T>::add(const T& element) { } template <class T> -bool List<T>::insertAt(const T& element, unsigned index) { - if (index >= count) return add(element); +void List<T>::insertAt(const T& element, unsigned index) { + if (index >= count) add(element); if (length == count) resize(); for (unsigned i = count; i > index; i--) @@ -58,10 +98,10 @@ 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; + count--; + return true; } template <class T> @@ -167,3 +207,5 @@ List<T>& List<T>::operator=(List&& other) { } return *this; } + +#endif diff --git a/src/List/OrderedList.cpp b/src/List/OrderedList.cpp deleted file mode 100644 index 7476574..0000000 --- a/src/List/OrderedList.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "OrderedList.h" - -template <class T> -OrderedList<T>::OrderedList() : OrderedList(nullptr, 0) {} - -template <class T> -OrderedList<T>::OrderedList(const T* elements, unsigned elementsCount) : List<T>::List(elements, elementsCount) {} - -template <class T> -bool OrderedList<T>::insert(const T& element) { - if (List<T>::length == List<T>::count) List<T>::resize(); - - unsigned insertionInd = 0; - while (List<T>::elements[insertionInd].compare(element) < 0 && insertionInd < List<T>::count) - insertionInd++; - - return List<T>::insertAt(element, insertionInd); -} diff --git a/src/List/OrderedList.h b/src/List/OrderedList.h deleted file mode 100644 index e0b104f..0000000 --- a/src/List/OrderedList.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef HEADER_ORDEREDLIST -#define HEADER_ORDEREDLIST - -#include "List.h" - -template <class T> -class OrderedList : public List<T> { -public: - OrderedList(); - OrderedList(const T* elements, unsigned elementsCount); - - void add(const T& element) = delete; - bool insertAt(const T& element, unsigned index) = delete; - - bool insert(const T& element); - // unsigned findIndex(const T& element) const; TODO: binary search -}; - -#endif diff --git a/src/List/OrderedList.hpp b/src/List/OrderedList.hpp new file mode 100644 index 0000000..4396d33 --- /dev/null +++ b/src/List/OrderedList.hpp @@ -0,0 +1,36 @@ +#ifndef HEADER_ORDEREDLIST +#define HEADER_ORDEREDLIST + +#include "List.hpp" + +template <class T> +class OrderedList : public List<T> { +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 <class T> +OrderedList<T>::OrderedList() : OrderedList(nullptr, 0) {} + +template <class T> +OrderedList<T>::OrderedList(const T* elements, unsigned elementsCount) : List<T>::List(elements, elementsCount) {} + +template <class T> +void OrderedList<T>::insert(const T& element) { + if (List<T>::length == List<T>::count) List<T>::resize(); + + unsigned insertionInd = 0; + while (List<T>::elements[insertionInd].compare(element) < 0 && insertionInd < List<T>::count) + insertionInd++; + + List<T>::insertAt(element, insertionInd); +} + +#endif |
