Data Structures in C++
OrderedList.hpp
Go to the documentation of this file.
1 //
2 // Created by dodo on 15/06/17.
3 //
4 
5 #ifndef AULA1_ORDEREDLIST_HPP
6 #define AULA1_ORDEREDLIST_HPP
7 
8 #include "LinkedList.hpp"
9 #include <type_traits>
10 #include <functional>
11 
12 //! Doubly-linked ordered list implementation with dynamic memory allocation.
13 //! This class searches for the appropriate place to insert an element
14 //! keeping the array sorted ata ll times, much like insertion sort.
15 //! \tparam T The type of object the data structure will contain
16 template<class T>
17 class OrderedList : public LinkedList<T> {
18  private:
19  function<int(T, T)> compare;
20 
21  public:
22  string getName() { return "Ordered List"; }
23 
24  /*!
25  \brief Creates the structure
26  \param compareFunc a C++ 11 compliant function parameter that allows comparation between template objects
27  */
29  }
30 
31  //! create the structure and populate it with the data from the array
32  //! \param data an array with data with which the structure will be initialized
33  //! \param compareFunc a C++ 11 compliant function parameter that allows comparation between template objects
34  explicit OrderedList(const T data[], std::function<int(T, T)> compareFunc) : compare(compareFunc) {
35  for (int i = 0; i <= (sizeof(data) / sizeof(data[0])); i ++) {
36  // use this class implementation of insert() to ensure order
37  insert(data[i]);
38  }
39  }
40 
41  //! Insert an element at its position on the list
42  //! \param val the value to be inserted
43  void insert(T val) {
44  if (LinkedList<T>::isEmpty()) {
45  LinkedList<T>::insert(val);
46  }
47  else {
48  Node<T> *tmp = LinkedList<T>::getFirst();
49  int index = 0;
50  while (tmp != NULL && compare(val, tmp->getValue()) < 0) {
51  index ++;
52  tmp = tmp->getNext();
53  }
54  LinkedList<T>::insert(val, index);
55  }
56  }
57 
58  void insert(T val, int index) {
59  insert(val);
60  }
61 
62  //! Finds an element in the ordered list and returns its index.
63  //! \param val the element to be found
64  //! \return The index of the first element where compareFunc(val, element) == 0
65  int indexOf(T val) {
66  Node<T> *tmp = LinkedList<T>::getFirst();
67  int index = 0;
68  while (tmp != NULL && compare(val, tmp->getValue()) >= 0) {
69  if (compare(val, tmp->getValue()) == 0)
70  return index;
71  index ++;
72  tmp = tmp->getNext();
73  }
74 
75  return - 1;
76  }
77 
78  //! Answers whether the ordered list contains an enemy.
79  //! \param val the element to be found
80  //! \return true if there is at least one element where compareFunc(val, element) == 0, otherwise false
81  bool contains(T val) {
82  return indexOf(val) != - 1;
83  }
84 
85  T remove(int index) {
86  return LinkedList<T>::remove(index);
87  }
88 
89  T get(int index) {
90  return LinkedList<T>::get(index);
91  }
92 };
93 
94 #endif
Node used inside other data structures.
Definition: Node.hpp:15
function< int(T, T)> compare
Definition: OrderedList.hpp:19
Doubly-linked list implementation with dynamic memory allocation.
Definition: LinkedList.hpp:17
int indexOf(T val)
Finds an element in the ordered list and returns its index.
Definition: OrderedList.hpp:65
T get(int index)
Get the element at the specified position in the list, without removing it.
Definition: OrderedList.hpp:89
OrderedList(std::function< int(T, T)> compareFunc)
Creates the structure.
Definition: OrderedList.hpp:28
void insert(T val)
Insert an element at its position on the list.
Definition: OrderedList.hpp:43
string getName()
Provides the name of the data structure as a string representation.
Definition: OrderedList.hpp:22
bool contains(T val)
Answers whether the ordered list contains an enemy.
Definition: OrderedList.hpp:81
OrderedList(const T data[], std::function< int(T, T)> compareFunc)
create the structure and populate it with the data from the array
Definition: OrderedList.hpp:34
T remove(int index)
Remove an element from the list.
Definition: OrderedList.hpp:85
void insert(T val, int index)
Insert an element at the specified position in the list.
Definition: OrderedList.hpp:58