-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_list.h
More file actions
288 lines (237 loc) · 6.16 KB
/
Copy patharray_list.h
File metadata and controls
288 lines (237 loc) · 6.16 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// copyright André William Régis 2017
#ifndef STRUCTURES_ARRAY_LIST_H
#define STRUCTURES_ARRAY_LIST_H
#include <cstdint>
#include <stdexcept>
namespace structures {
template<typename T>
/*!
* Lista genérica que pode ter um elemento adicionado e retirado em qualquer
* ponto do array
*/
class ArrayList {
public:
/*!
* Inicializa a lista sem elementos de tamanho "DEFAULT_MAX"
*/
ArrayList();
/*!
* Inicializa a lista sem elementos de tamanho "max_size"
*/
explicit ArrayList(std::size_t max_size);
/*!
* Deleta o array dos elementos também
*/
~ArrayList();
/*!
* limpa a lista atribuindo 0 ao "size_"
*/
void clear();
/*!
* Adiciona "data" ao final da lista
*/
void push_back(const T& data);
/*!
* Move todos os elementos um slot acima
* Insere "data" ao começo da lista
*/
void push_front(const T& data);
/*!
* Move todos os elementos a partir do "index" um slot acima
* Insere "data" no slot de "index"
*/
void insert(const T& data, std::size_t index);
/*!
* Move todos elementos a partir slot destinado ao "data" um slot acima
* Insere "data" no slot seguinte ao maior elemento menor que "data"
*/
void insert_sorted(const T& data);
/*!
* retira o elemento na posição "index"
*/
T pop(std::size_t index);
/*!
* retira o último elemento da lista
*/
T pop_back();
/*!
* retira o primeiro elemento da lista
*/
T pop_front();
/*!
* exclui o elemento equivalente ao "data"
*/
void remove(const T& data);
/*!
* retorna se a lista está cheia
*/
bool full() const;
/*!
* retorna se a lista está vazia
*/
bool empty() const;
/*!
* retorna se a lista contém "data"
*/
bool contains(const T& data) const;
/*!
* retorna a posição de "data" na lista
*/
std::size_t find(const T& data) const;
/*!
* retorna "size_"
*/
std::size_t size() const;
/*!
* retorna "max_size_"
*/
std::size_t max_size() const;
/*!
* verifica se index está fora dos limites
* retorna elemento na posição "index"
*/
T& at(std::size_t index);
/*!
* retorna elemento na posição "index"
*/
T& operator[](std::size_t index);
/*!
* verifica se index está fora dos limites
* retorna elemento na posição "index" como constante
*/
const T& at(std::size_t index) const;
/*!
* retorna elemento na posição "index" como constante
*/
const T& operator[](std::size_t index) const;
private:
T* contents;
std::size_t size_ = 0;
std::size_t max_size_;
static const auto DEFAULT_MAX = 10u;
}; // ArrayList
template<typename T>
ArrayList<T>::ArrayList() : ArrayList(DEFAULT_MAX) {}
template<typename T>
ArrayList<T>::ArrayList(std::size_t max_size):
contents {new T[max_size]},
max_size_ {max_size} {}
template<typename T>
ArrayList<T>::~ArrayList() {
delete[] contents;
}
template<typename T>
void ArrayList<T>::clear() {
size_ = 0;
}
template<typename T>
void ArrayList<T>::push_back(const T& data) {
if (full())
throw std::out_of_range("List is full!");
contents[size_++] = data;
}
template<typename T>
void ArrayList<T>::push_front(const T& data) {
if (full())
throw std::out_of_range("List is full!");
size_++;
for (int position = size_-1; position > 0; position--)
contents[position] = contents[position-1];
contents[0] = data;
}
template<typename T>
void ArrayList<T>::insert(const T& data, std::size_t index) {
if (full())
throw std::out_of_range("List is full!");
if ((index < 0 || index >= size_) && (empty() && index != 0))
throw std::out_of_range("Index invalid!");
for (int position = size_; position > index; position--)
contents[position] = contents[position-1];
contents[index] = data;
size_++;
}
template<typename T>
void ArrayList<T>::insert_sorted(const T& data) {
if (full())
throw std::out_of_range("List is full!");
int position = 0;
while (position < size_ && data > contents[position])
position++;
insert(data, position);
}
template<typename T>
T ArrayList<T>::pop(std::size_t index) {
if (empty())
throw std::out_of_range("List is empty!");
if (index < 0 || index >= size_)
throw std::out_of_range("List is empty!");
auto data = contents[index];
size_--;
for (int position = index; position < size_; position++)
contents[position] = contents[position+1];
return data;
}
template<typename T>
T ArrayList<T>::pop_back() {
if (empty())
throw std::out_of_range("List is empty!");
return contents[--size_];
}
template<typename T>
T ArrayList<T>::pop_front() {
return pop(0);
}
template<typename T>
void ArrayList<T>::remove(const T& data) {
pop(find(data));
}
template<typename T>
bool ArrayList<T>::full() const {
return size_ == max_size_;
}
template<typename T>
bool ArrayList<T>::empty() const {
return size_ == 0;
}
template<typename T>
bool ArrayList<T>::contains(const T& data) const {
for (int position = 0; position < size_; position++)
if (contents[position] == data)
return true;
return false;
}
template<typename T>
std::size_t ArrayList<T>::find(const T& data) const {
for (int position = 0; position < size_; position++)
if (contents[position] == data)
return position;
return size_;
}
template<typename T>
std::size_t ArrayList<T>::size() const {
return size_;
}
template<typename T>
std::size_t ArrayList<T>::max_size() const {
return max_size_;
}
template<typename T>
T& ArrayList<T>::at(std::size_t index) {
if (index < 0 || index >= size_)
throw std::out_of_range("Index invalid!");
return contents[index];
}
template<typename T>
T& ArrayList<T>::operator[](std::size_t index) {
return contents[index];
}
template<typename T>
const T& ArrayList<T>::at(std::size_t index) const {
return at(index);
}
template<typename T>
const T& ArrayList<T>::operator[](std::size_t index) const {
return operator[](index);
}
} // namespace structures
#endif