#ifndef HEADER_ORDEREDLIST #define HEADER_ORDEREDLIST /*! \file OrderedList.hpp * \brief Stores declaration and definition of templated class OrderedList */ #include "List.hpp" /*! \brief Inherits List, but contains it's elements in a sorted manner * * \warning Outside those defined in List, insert() requires the type to have defined a "compare" of functions. * \sa insert() */ template class OrderedList : public List { public: OrderedList(); OrderedList(const T* elements, unsigned elementsCount); /*! \brief \warning Function is deleted, use insert() instead * \sa insert() */ void add(T element) = delete; /*! \brief \warning Function is deleted, use insert() instead * \sa insert() */ void insertAt(T element, unsigned index) = delete; //! Inserts an element at a sorted position void insert(T element); }; template OrderedList::OrderedList() : OrderedList(nullptr, 0) {} template OrderedList::OrderedList(const T* elements, unsigned elementsCount) : List::List(elements, elementsCount) {} /*! * \warning The function depends on the type having a function "compare" defined, which takes two elements and returns a number <0 if elem1 < elem2, >0 if elem1 > elem2, 0 if elem1 == elem2 * \note Insertion is done linearly */ template void OrderedList::insert(T element) { if (List::length == List::count) List::resize(); unsigned insertionInd = 0; while (insertionInd < List::count && List::elements[insertionInd].compare(element) < 0) insertionInd++; List::insertAt(element, insertionInd); } #endif