-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptrlist.h
523 lines (471 loc) · 11.4 KB
/
ptrlist.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
PtrList - A vector-like container template which stores elements as array of pointers
to each element to allow taking pointers to each element of container
Copyright (c) 2017-2025 Vitaliy Novichkov <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#ifndef PTRLIST_H
#define PTRLIST_H
#include <assert.h>
#include <inttypes.h>
#include <stddef.h>
#include <stdlib.h>
#include <memory.h>
#ifdef _MSC_VER
#ifdef _WIN64
typedef long long ssize_t;
#else
typedef int ssize_t;
#endif
#endif
template<class T>
class PtrList
{
T** m_data;
size_t m_size;
size_t m_sizeReserved;
public:
class iterator
{
friend class PtrList;
T** m_data;
size_t m_pos;
iterator(T** d, size_t pos) :
m_data(d),
m_pos(pos)
{}
public:
iterator():
m_data(nullptr),
m_pos(0)
{}
iterator(const iterator& it) :
m_data(it.m_data),
m_pos(it.m_pos)
{}
bool operator == (const iterator& i)
{
return (m_pos == i.m_pos);
}
bool operator != (const iterator& i)
{
return (m_pos != i.m_pos);
}
friend iterator operator+(const iterator &it, int inc)
{
iterator i = it;
i.m_pos += inc;
return i;
}
friend iterator operator-(const iterator &it, int inc)
{
iterator i = it;
i.m_pos -= inc;
return i;
}
iterator& operator++()
{
m_pos++;
return *this;
}
iterator& operator++(int)
{
m_pos++;
return *this;
}
iterator& operator+=(int inc)
{
m_pos += inc;
return *this;
}
iterator& operator-=(int dec)
{
m_pos -= dec;
return *this;
}
T& operator*()
{
return *(m_data[m_pos]);
}
};
iterator begin() { return iterator(m_data, 0u); }
iterator end() { return iterator(m_data, m_size); }
PtrList() :
m_data(nullptr),
m_size(0),
m_sizeReserved(0)
{}
PtrList(size_t size) :
m_data(nullptr),
m_size(0),
m_sizeReserved(0)
{
resize(size);
}
PtrList(const PtrList& o):
m_data(nullptr),
m_size(0),
m_sizeReserved(0)
{
reserve(m_size);
for(size_t i=0; i < o.m_size; i++)
{
m_data[i] = new T(*o.m_data[i]);
}
m_size = o.m_size;
}
PtrList& operator=(const PtrList& o)
{
if (this != &o)
{
clear();
m_size = o.m_size;
reserve(o.m_size);
for(size_t i = 0; i < o.m_size; i++)
m_data[i] = new T(*o.m_data[i]);
}
return *this;
}
~PtrList()
{
clear();
}
void clear()
{
if(m_data)
{
for(size_t i = 0; i < m_size; i++)
{
T* p = m_data[i];
delete p;
}
free(m_data);
}
m_data = nullptr;
m_size = 0;
m_sizeReserved = 0;
}
size_t size() const
{
return m_size;
}
size_t count() const
{
return m_size;
}
size_t capacity() const
{
return m_sizeReserved;
}
void resize(size_t sz)
{
if(sz == m_size)
return;
if(sz < m_size)
{
while(sz < m_size)
{
T* p = m_data[(m_size--) - 1];
delete p;
}
}
else
{
if(sz > m_sizeReserved)
reserve(sz - m_sizeReserved);
while( m_size < sz )
m_data[m_size++] = new T;
}
}
void reserve(size_t sz)
{
sz += m_sizeReserved;
size_t newSize = sizeof(T*) * sz;
if(m_data == 0)
m_data = (T**)malloc(newSize);
else
m_data = (T**)realloc(m_data, newSize);
assert(m_data);
m_sizeReserved = sz;
}
bool contains(const T& item)
{
for(size_t i = 0; i < m_size; i++)
{
if(*m_data[i] == item)
return true;
}
return false;
}
ssize_t indexOf(const T& item)
{
for(size_t i = 0; i < m_size; i++)
{
if(*m_data[i] == item)
return ssize_t(i);
}
return -1;
}
iterator find(const T& item)
{
iterator i = begin();
for(; i != end(); i++)
{
if(*i == item)
break;
}
return i;
}
void removeOne(const T& item)
{
size_t i = 0, moveTo = m_size;
for(; i < m_size; i++)
{
if(*m_data[i] == item)
{
delete m_data[i];
moveTo = i;
i++;
break;
}
}
for(; i < m_size; i++)
{
m_data[moveTo++] = m_data[i];
}
m_size--;
m_data[m_size] = nullptr;
}
void removeAll(const T& item)
{
size_t i = 0, moveTo = m_size;
for(; i < m_size; i++)
{
if(*m_data[i] == item)
{
delete m_data[i];
if(moveTo == m_size)
moveTo = i;
continue;
}
if(moveTo != m_size)
m_data[moveTo++] = m_data[i];
}
m_size -= (i - moveTo);
m_data[m_size] = nullptr;
}
iterator erase(iterator pos)
{
assert(pos.m_pos <= m_size);
removeAt(pos.m_pos, 1);
return iterator(m_data, pos.m_pos);
}
iterator erase(iterator from, iterator to)
{
assert(from.m_pos <= m_size);
assert(to.m_pos <= m_size);
assert(to.m_pos >= from.m_pos);
if(from == to)
return from;
removeAt(from.m_pos, to.m_pos - from.m_pos);
return iterator(m_data, from.m_pos);
}
void removeAt(size_t at)
{
assert(m_size > 0);
size_t i = at + 1, moveTo = at;
delete m_data[at];
for(; i < m_size; i++)
{
if(moveTo == m_size)
break;
m_data[moveTo++] = m_data[i];
}
m_size--;
m_data[m_size] = nullptr;
}
void removeAt(size_t at, size_t num)
{
assert(m_size > 0);
assert(m_size >= at + num);
size_t i = at, del_to = (at + num), moveTo = at;
while(i < del_to)
delete m_data[i++];
for(; i < m_size; i++)
{
if(moveTo == m_size)
break;
m_data[moveTo++] = m_data[i];
}
m_size -= num;
memset(m_data + m_size, 0, sizeof(T*)*num);
}
void pop_back()
{
removeAt(m_size - 1);
}
void pop_front()
{
removeAt(0);
}
void swap(size_t from, size_t to)
{
assert(m_size > from);
assert(m_size > to);
if(from == to)
return;
T* tmp = m_data[from];
m_data[from] = m_data[to];
m_data[to] = tmp;
}
void move(size_t from, size_t to)
{
assert(m_size > from);
assert(m_size > to);
if(from == to)
return;
if(from < to)
{
T* it = m_data[from];
while(from < to)
{
m_data[from] = m_data[from+1];
from++;
}
m_data[to] = it;
} else {
T* it = m_data[from];
while(from > to)
{
m_data[from] = m_data[from-1];
from--;
}
m_data[to] = it;
}
}
void push_back(const T& item)
{
if((m_size + 1) >= m_sizeReserved)
reserve(4);
m_data[m_size] = new T(item);
m_size++;
m_data[m_size] = nullptr;
}
void push_front(const T& item)
{
insert(0, item);
}
void append(const T& item)
{
if( (m_size + 1) >= m_sizeReserved)
reserve(4);
m_data[m_size] = new T(item);
m_size++;
m_data[m_size] = nullptr;
}
void append(const PtrList<T> &array)
{
reserve(array.size());
for(const T& t : array)
this->push_back(t);
}
void insert(size_t at, const T& item)
{
if(at >= m_size)
at = m_size;
if((m_size+1) >= m_sizeReserved)
reserve(4);
if(at < m_size)
{
unsigned int insertAt = m_size;
do{
insertAt--;
m_data[insertAt+1] = m_data[insertAt];
}while(insertAt > at);
}
m_data[at] = new T(item);
m_size++;
m_data[m_size] = nullptr;
}
T& last()
{
assert(m_size > 0);
return *(m_data[m_size-1]);
}
T& back()
{
assert(m_size > 0);
return *(m_data[m_size-1]);
}
T& first()
{
assert(m_size > 0);
return *(m_data[0]);
}
T& front()
{
assert(m_size>0);
return *(m_data[0]);
}
T &operator[](unsigned int index)
{
assert(index < (unsigned int)(m_size));
return *(m_data[index]);
}
T &operator[](int index)
{
assert(index >= 0);
assert(index < int(m_size));
return *(m_data[index]);
}
T &operator[](unsigned long index)
{
assert(index < (unsigned long)(m_size));
return *(m_data[index]);
}
T &operator[](long index)
{
assert(index >= 0);
assert(index < long(m_size));
return *(m_data[index]);
}
T &operator[](unsigned long long index)
{
assert(index < (unsigned long long)(m_size));
return *(m_data[index]);
}
T &operator[](long long index)
{
assert(index >= 0);
assert(index < (long long)(m_size));
return *(m_data[index]);
}
bool operator==(const PtrList<T>& o2)
{
if(m_size != o2.m_size)
return false;
for(int i=0; i<m_size; i++)
if( (*m_data[i]) != (*o2.m_data[i]))
return false;
return true;
}
bool operator!=(const PtrList<T>& o2)
{
return !operator==(o2);
}
};
#endif // PTRLIST_H