-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayList.h
353 lines (294 loc) · 7.96 KB
/
ArrayList.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#ifndef ARRAY_LIST_H
#define ARRAY_LIST_H
#include <functional>
#include "Arduino.h"
template <typename T>
class ArrayList {
public:
class Iterator {
public:
Iterator(T* ptr)
: m_Ptr(ptr) {}
Iterator operator++() {
++m_Ptr;
return *this;
}
Iterator operator++(int) {
Iterator tmp = *this;
++m_Ptr;
return tmp;
}
Iterator operator--() {
--m_Ptr;
return *this;
}
Iterator operator--(int) {
Iterator tmp = *this;
--m_Ptr;
return tmp;
}
Iterator operator+(const int& n) {
return Iterator(m_Ptr + n);
}
Iterator operator-(const int& n) {
return Iterator(m_Ptr - n);
}
Iterator& operator+=(const int& n) {
m_Ptr += n;
return *this;
}
Iterator& operator-=(const int& n) {
m_Ptr -= n;
return *this;
}
bool operator==(const Iterator& other) {
return m_Ptr == other.m_Ptr;
}
bool operator!=(const Iterator& other) const {
return m_Ptr != other.m_Ptr;
}
T& operator[](const int& n) {
return *(m_Ptr + n);
}
T& operator*() const {
return *m_Ptr;
}
T* operator->() const {
return m_Ptr;
}
private:
T* m_Ptr;
};
ArrayList()
: m_Data(NULL),
m_Size(0),
m_Capacity(0) {
_reallocate(2);
}
ArrayList(const ArrayList& other)
: ArrayList() {
if (_reallocate(other.m_Capacity)) {
m_Size = other.m_Size;
const size_t size = m_Size * sizeof(T);
memcpy(m_Data, other.m_Data, size);
}
}
ArrayList(ArrayList&& other) noexcept
: ArrayList() {
_swap(other);
}
ArrayList(T arr[], size_t len)
: m_Data(NULL),
m_Size(0),
m_Capacity(0) {
if (_reallocate(len + len / 2)) {
for (size_t i = 0; i < len; i++) {
add(arr[i]);
}
}
}
template <typename... Args>
ArrayList(Args... args)
: ArrayList() {
if (_reallocate(sizeof...(args) + sizeof...(args) / 2)) {
int dummy[] = {(add(args), 0)...};
(void)dummy;
}
}
~ArrayList() {
_deallocate();
}
bool add(const T& element) {
if (m_Size >= m_Capacity) {
if (!_reallocate(m_Capacity + m_Capacity / 2)) return false;
}
m_Data[m_Size] = element;
m_Size++;
return true;
}
bool add(T&& element) {
if (m_Size >= m_Capacity) {
if (!_reallocate(m_Capacity + m_Capacity / 2)) return false;
}
m_Data[m_Size] = element;
m_Size++;
return true;
}
bool addAll(const ArrayList& other) {
if (m_Size + other.m_Size >= m_Capacity) {
if (!_reallocate(m_Capacity + other.m_Capacity)) return false;
}
for (size_t i = 0; i < other.m_Size; i++) {
m_Data[m_Size + i] = other.m_Data[i];
}
m_Size += other.m_Size;
return true;
}
void resize(const size_t& newSize) {
_reallocate(newSize);
}
void remove(const T& element) {
removeAt(indexOf(element));
}
void removeAt(size_t index) {
if (index < 0) return;
m_Size--;
for (size_t i = index; i < m_Size; i++) {
m_Data[i] = m_Data[i + 1];
}
}
void removeIf(std::function<bool(T)> predicate) {
for (size_t i = 0; i < m_Size; i++) {
if (predicate(m_Data[i])) {
removeAt(i);
i--;
}
}
}
bool contains(const T& element) {
for (size_t i = 0; i < m_Size; i++) {
if (m_Data[i] == element) return true;
}
return false;
}
bool contains(std::function<bool(T)> predicate) {
for (size_t i = 0; i < m_Size; i++) {
if (predicate(m_Data[i])) return true;
}
return false;
}
int indexOf(const T& element) {
for (size_t i = 0; i < m_Size; i++) {
if (m_Data[i] == element) return i;
}
return -1;
}
int indexOf(std::function<bool(T)> predicate) {
for (size_t i = 0; i < m_Size; i++) {
if (predicate(m_Data[i])) return i;
}
return -1;
}
void clear() {
_deallocate();
_reallocate(2);
}
const T get(const size_t& index, const T& defaultValue) const {
if (index >= m_Size) {
return defaultValue;
}
return m_Data[index];
}
T get(const size_t& index, const T& defaultValue) {
if (index >= m_Size) return const_cast<T&>(defaultValue);
return m_Data[index];
}
const T& operator[](const size_t& index) const {
return m_Data[index];
}
T& operator[](const size_t& index) {
return m_Data[index];
}
ArrayList& operator=(const ArrayList& other) {
m_Size = 0;
if (_reallocate(other.m_Capacity)) {
m_Size = other.m_Size;
const size_t size = m_Size * sizeof(T);
memcpy(m_Data, other.m_Data, size);
}
return *this;
}
ArrayList& operator=(ArrayList&& other) noexcept {
_swap(other);
return *this;
}
void forEach(std::function<bool(T&, size_t)> predicate) {
for (size_t i = 0; i < m_Size; i++) {
if (!predicate(m_Data[i], i)) break;
}
}
template <typename E = T>
ArrayList<E> map(std::function<E(T&, size_t)> predicate) {
ArrayList<E> buffer;
for (size_t i = 0; i < m_Size; i++) {
buffer.add(predicate(m_Data[i], i));
}
return buffer;
}
ArrayList filter(std::function<bool(T&)> predicate) {
ArrayList buffer;
for (size_t i = 0; i < m_Size; i++) {
if (predicate(m_Data[i])) buffer.add(m_Data[i]);
}
return buffer;
}
void sort(std::function<bool(T&, T&)> predicate) {
for (size_t i = 1; i < m_Size; i++) {
for (size_t j = i; j > 0 && predicate(m_Data[j - 1], m_Data[j]); j--) {
T tmp = m_Data[j - 1];
m_Data[j - 1] = m_Data[j];
m_Data[j] = tmp;
}
}
}
void sort() {
sort([](T a, T b) -> bool { return a > b; });
}
void reverse() {
for (size_t i = 0; i < m_Size / 2; i++) {
T buffer = m_Data[i];
m_Data[i] = m_Data[m_Size - 1 - i];
m_Data[m_Size - 1 - i] = buffer;
}
}
Iterator begin() {
return Iterator(m_Data);
}
Iterator end() {
return Iterator(m_Data + m_Size);
}
size_t size() const {
return m_Size;
}
bool isEmpty() const {
return m_Size == 0;
}
private:
T* m_Data;
size_t m_Size;
size_t m_Capacity;
bool _reallocate(size_t newCapacity) {
T* newBlock = new T[newCapacity];
if (newBlock) {
if (newCapacity < m_Size) {
m_Size = newCapacity;
}
for (size_t i = 0; i < m_Size; i++) {
newBlock[i] = m_Data[i];
}
delete[] m_Data;
m_Data = newBlock;
m_Capacity = newCapacity;
return true;
}
return false;
}
void _deallocate() {
delete[] m_Data;
m_Data = NULL;
m_Size = 0;
m_Capacity = 0;
}
void _swap(ArrayList& other) {
T* _data = m_Data;
size_t _size = m_Size;
size_t _capacity = m_Capacity;
m_Data = other.m_Data;
m_Size = other.m_Size;
m_Capacity = other.m_Capacity;
other.m_Data = _data;
other.m_Size = _size;
other.m_Capacity = _capacity;
}
};
#endif