-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListLib.h
364 lines (299 loc) · 6.68 KB
/
ListLib.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
354
355
356
357
358
359
360
361
362
363
364
/***************************************************
Copyright (c) 2017 Luis Llamas
(www.luisllamas.es)
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License
****************************************************/
#ifndef _ListLib_h
#define _ListLib_h
#if defined(ARDUINO) && ARDUINO >= 100
#include <Arduino.h>
#else
#include "WProgram.h"
#endif
template <typename T>
class List
{
public:
List();
List(size_t capacity);
~List();
size_t Capacity() const;
size_t Count() const;
T& operator[](const size_t index);
bool Contains(T item);
size_t IndexOf(T item);
T& First();
T& Last();
void Add(T item);
void AddRange(T* items, size_t numItems);
void Insert(T item);
void Insert(size_t index, T item);
void InsertRange(T* items, size_t numItems);
void InsertRange(size_t index, T* items, size_t numItems);
void RemoveFirst();
void Remove(size_t index);
void RemoveLast();
void RemoveRange(size_t index, size_t numItems);
void Replace(size_t index, T item);
void ReplaceRange(size_t index, T* items, size_t numItems);
void Reverse();
void Clear();
bool IsEmpty();
bool IsFull();
void Trim();
void Trim(size_t reserve);
T* ToArray();
T* ToArray(size_t index, size_t numItems);
void CopyTo(T* items);
void CopyTo(T* items, size_t index, size_t numItems);
void FromArray(T* items, size_t numItems);
private:
T* _items;
size_t _count = 0;
size_t _capacity = 4;
void shift(size_t index, size_t numItems);
void unshift(size_t index, size_t numItems);
void reserve(size_t size);
void resize(size_t size);
};
template <typename T>
List<T>::List()
{
_items = new T[_capacity];
}
template <typename T>
List<T>::List(size_t capacity)
{
_capacity = capacity;
_items = new T[_capacity];
}
template <typename T>
List<T>::~List()
{
delete[] _items;
}
template <typename T>
T& List<T>::operator[](const size_t index)
{
return _items[index];
}
template <typename T>
size_t List<T>::Capacity() const
{
return _capacity;
}
template <typename T>
size_t List<T>::Count() const
{
return _count;
}
template <typename T>
T& List<T>::First()
{
return _items[0];
}
template <typename T>
T& List<T>::Last()
{
return _items[_count - 1];
}
template <typename T>
void List<T>::Add(T item)
{
++_count;
reserve(_count);
_items[_count - 1] = item;
}
template <typename T>
void List<T>::AddRange(T* items, size_t numItems)
{
reserve(_count + numItems);
memmove(_items + _count , items, numItems * sizeof(T));
_count += numItems;
}
template <typename T>
void List<T>::Insert(T item)
{
++_count;
reserve(_count);
shift(0, 1);
_items[0] = item;
}
template <typename T>
void List<T>::InsertRange(T* items, size_t numItems)
{
_count += numItems;
reserve(_count);
shift(0, numItems);
memmove(_items, items, numItems * sizeof(T));
}
template <typename T>
void List<T>::Insert(size_t index, T item)
{
if (index > _count - 1) return;
++_count;
reserve(_count);
shift(index, 1);
_items[index] = item;
}
template <typename T>
void List<T>::InsertRange(size_t index, T* items, size_t numItems)
{
if (index > _count - 1) return;
_count += numItems;
reserve(_count);
shift(index, numItems);
memmove(_items + index, items, numItems * sizeof(T));
}
template <typename T>
void List<T>::RemoveFirst()
{
if (_count == 0) return;
unshift(0, 1);
--_count;
}
template <typename T>
void List<T>::Remove(size_t index)
{
if (index > _count - 1) return;
unshift(index, 1);
--_count;
}
template <typename T>
void List<T>::RemoveLast()
{
if (_count == 0) return;
--_count;
}
template <typename T>
void List<T>::RemoveRange(size_t index, size_t numItems)
{
unshift(index, numItems);
_count -= numItems;
}
template <typename T>
void List<T>::Replace(size_t index, T item)
{
if (index > _count - 1) return;
_items[index] = item;
}
template <typename T>
void List<T>::ReplaceRange(size_t index, T* items, size_t numItems)
{
memmove(_items + index, items, numItems * sizeof(T));
}
template <typename T>
void List<T>::Reverse()
{
T item;
for (int index = 0; index < _count / 2; index++)
{
item = _items[index];
_items[index] = _items[_count - 1 - index];
_items[_count -1 - index] = item;
}
}
template <typename T>
void List<T>::Clear()
{
_count = 0;
}
template <typename T>
bool List<T>::IsEmpty()
{
return (_count == 0);
}
template <typename T>
bool List<T>::IsFull()
{
return (_count >= _capacity);
}
template <typename T>
void List<T>::Trim()
{
resize(_count);
}
template <typename T>
void List<T>::Trim(size_t reserve)
{
resize(_count + reserve);
}
template <typename T>
T* List<T>::ToArray()
{
T* items = new T[_count];
memmove(items, _items, _count * sizeof(T));
return items;
}
template <typename T>
T* List<T>::ToArray(size_t index, size_t numItems)
{
T* items = new T[numItems];
memmove(items, _items + index, numItems * sizeof(T));
return items;
}
template <typename T>
void List<T>::CopyTo(T* items)
{
memmove(items, _items, _count * sizeof(T));
}
template <typename T>
void List<T>::CopyTo(T* items, size_t index, size_t numItems)
{
memmove(items, _items + index , numItems * sizeof(T));
}
template <typename T>
void List<T>::FromArray(T* items, size_t numItems)
{
reserve(numItems);
_count = numItems;
memmove(_items, items, numItems * sizeof(T));
}
template <typename T>
bool List<T>::Contains(T item)
{
for (int index = 0; index < _count; index++)
if (_items[index] == item)
return true;
return false;
}
template <typename T>
size_t List<T>::IndexOf(T item)
{
for (int index = 0; index < _count; index++)
if (_items[index] == item)
return index;
return -1;
}
template <typename T>
void List<T>::shift(size_t index, size_t numItems)
{
memmove(_items + index + numItems, _items + index, (_count - index - 1) * sizeof(T));
}
template <typename T>
void List<T>::unshift(size_t index, size_t numItems)
{
memmove(_items + index , _items + index + numItems, (_count - index - numItems + 1) * sizeof(T));
}
template <typename T>
void List<T>::reserve(size_t size)
{
if (_count > _capacity)
{
size_t newSize = _capacity * 2 > size ? _capacity * 2 : size;
resize(newSize);
}
}
template <typename T>
void List<T>::resize(size_t size)
{
if (_count > size) return;
if (_capacity == size) return;
T* newItems = new T[size];
memmove(newItems, _items, _count * sizeof(T));
delete[] _items;
_capacity = size;
_items = newItems;
}
#endif