forked from jacyara/GenESyS-Reborn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.h
201 lines (169 loc) · 4.01 KB
/
List.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: TManager.h
* Author: cancian
*
* Created on 21 de Junho de 2018, 12:55
*/
#ifndef LISTMANAGER_H
#define LISTMANAGER_H
#include <string>
#include <list>
#include <map>
#include <iterator>
#include <functional>
#include <algorithm>
#include "Util.h"
class Simulator;
template <typename T>
class List {
public:
using CompFunct = std::function<bool(const T, const T)>;
public:
List();
List(const List& orig);
virtual ~List();
public: // direct access to list
unsigned int size();
bool empty();
void clear();
void pop_front();
template<class Compare>
void sort(Compare comp);
std::list<T>* getList() const;
public: // new methods
T create();
template<typename U>
T create(U arg);
std::string show();
typename std::list<T>::iterator find(T element);
public: // improved (easier) methods
void insert(T element);
void remove (T element);
T next();
T first();
T last();
T previous();
T actual(); // get current element on the list (the last used)
void setSortFunc(CompFunct _sortFunc);
private:
//std::map<Util::identitifcation, T>* _map;
std::list<T>* _list;
CompFunct _sortFunc{[](const T, const T) {return false;}}; //! Default function: insert at the end of the list.
typename std::list<T>::iterator _it;
};
template <typename T>
List<T>::List() {
//_map = new std::map<Util::identitifcation, T>();
_list = new std::list<T>();
_it = _list->begin();
}
template <typename T>
std::list<T>* List<T>::getList() const {
return _list;
}
template <typename T>
unsigned int List<T>::size() {
return _list->size();
}
template <typename T>
List<T>::List(const List& orig) {
}
template <typename T>
List<T>::~List() {
}
template <typename T>
std::string List<T>::show() {
int i = 0;
std::string text = "{";
for (typename std::list<T>::iterator it = _list->begin(); it != _list->end(); it++, i++) {
text += "[" + std::to_string(i) + "]=(" + (*it)->show() + "),";
}
text += "}";
return text;
}
template <typename T>
void List<T>::insert(T element) {
_list->insert(std::upper_bound(_list->begin(), _list->end(), element, _sortFunc), element);
}
template <typename T>
bool List<T>::empty() {
return _list->empty();
}
template <typename T>
void List<T>::pop_front() {
typename std::list<T>::iterator itTemp = _list->begin();
_list->pop_front();
if (_it == itTemp) { /* TODO +: check this */
_it = _list->begin(); // if it points to the removed element, then changes to begin
}
}
template <typename T>
void List<T>::remove(T element) {
_list->remove(element);
if ((*_it) == element) { /* TODO +: check this */
_it = _list->begin(); // if it points to the removed element, then changes to begin
}
}
template <typename T>
T List<T>::create() {
return new T();
}
template <typename T>
void List<T>::clear() {
_list->clear();
}
template <typename T>
T List<T>::next() {
_it++;
return (*_it);
}
template <typename T>
typename std::list<T>::iterator List<T>::find(T element) {
for (typename std::list<T>::iterator it = _list->begin(); it != _list->end(); it++) {
if ((*it) == element) {
return it;
}
}
return _list->end(); /* TODO+-: check nullptr or invalid iterator when not found */
//return nullptr;
}
template <typename T>
T List<T>::first() {
_it = _list->begin();
return (*_it);
}
template <typename T>
T List<T>::last() {
_it = _list->end();
return (*_it);
}
template <typename T>
T List<T>::previous() {
_it--;
return (*_it);
}
template <typename T>
T List<T>::actual() {
/* TODO: To implement. Must actualize _it on other methods when other elements are accessed */
return (*_it);
}
template <typename T>
void List<T>::setSortFunc(CompFunct _sortFunc) {
this->_sortFunc = _sortFunc;
}
template <typename T>
template<typename U>
T List<T>::create(U arg) {
return T(arg);
}
template <typename T>
template<class Compare>
void List<T>::sort(Compare comp) {
_list->sort(comp);
}
#endif /* LISTMANAGER_H */