Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/templates/list.h
9973 views
1
/**************************************************************************/
2
/* list.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/error/error_macros.h"
34
#include "core/os/memory.h"
35
#include "core/templates/sort_list.h"
36
37
#include <initializer_list>
38
39
/**
40
* Generic Templatized Linked List Implementation.
41
* The implementation differs from the STL one because
42
* a compatible preallocated linked list can be written
43
* using the same API, or features such as erasing an element
44
* from the iterator.
45
*/
46
47
template <typename T, typename A = DefaultAllocator>
48
class List {
49
struct _Data;
50
51
public:
52
class Element {
53
private:
54
friend class List<T, A>;
55
56
T value;
57
Element *next_ptr = nullptr;
58
Element *prev_ptr = nullptr;
59
_Data *data = nullptr;
60
61
public:
62
/**
63
* Get NEXT Element iterator, for constant lists.
64
*/
65
_FORCE_INLINE_ const Element *next() const {
66
return next_ptr;
67
}
68
/**
69
* Get NEXT Element iterator,
70
*/
71
_FORCE_INLINE_ Element *next() {
72
return next_ptr;
73
}
74
75
/**
76
* Get PREV Element iterator, for constant lists.
77
*/
78
_FORCE_INLINE_ const Element *prev() const {
79
return prev_ptr;
80
}
81
/**
82
* Get PREV Element iterator,
83
*/
84
_FORCE_INLINE_ Element *prev() {
85
return prev_ptr;
86
}
87
88
/**
89
* * operator, for using as *iterator, when iterators are defined on stack.
90
*/
91
_FORCE_INLINE_ const T &operator*() const {
92
return value;
93
}
94
/**
95
* operator->, for using as iterator->, when iterators are defined on stack, for constant lists.
96
*/
97
_FORCE_INLINE_ const T *operator->() const {
98
return &value;
99
}
100
/**
101
* * operator, for using as *iterator, when iterators are defined on stack,
102
*/
103
_FORCE_INLINE_ T &operator*() {
104
return value;
105
}
106
/**
107
* operator->, for using as iterator->, when iterators are defined on stack, for constant lists.
108
*/
109
_FORCE_INLINE_ T *operator->() {
110
return &value;
111
}
112
113
/**
114
* get the value stored in this element.
115
*/
116
_FORCE_INLINE_ T &get() {
117
return value;
118
}
119
/**
120
* get the value stored in this element, for constant lists
121
*/
122
_FORCE_INLINE_ const T &get() const {
123
return value;
124
}
125
/**
126
* set the value stored in this element.
127
*/
128
_FORCE_INLINE_ void set(const T &p_value) {
129
value = (T &)p_value;
130
}
131
132
void erase() {
133
data->erase(this);
134
}
135
136
void transfer_to_back(List<T, A> *p_dst_list);
137
138
_FORCE_INLINE_ Element() {}
139
};
140
141
typedef T ValueType;
142
143
struct ConstIterator {
144
_FORCE_INLINE_ const T &operator*() const {
145
return E->get();
146
}
147
_FORCE_INLINE_ const T *operator->() const { return &E->get(); }
148
_FORCE_INLINE_ ConstIterator &operator++() {
149
E = E->next();
150
return *this;
151
}
152
_FORCE_INLINE_ ConstIterator &operator--() {
153
E = E->prev();
154
return *this;
155
}
156
157
_FORCE_INLINE_ bool operator==(const ConstIterator &b) const { return E == b.E; }
158
_FORCE_INLINE_ bool operator!=(const ConstIterator &b) const { return E != b.E; }
159
160
_FORCE_INLINE_ ConstIterator(const Element *p_E) { E = p_E; }
161
_FORCE_INLINE_ ConstIterator() {}
162
_FORCE_INLINE_ ConstIterator(const ConstIterator &p_it) { E = p_it.E; }
163
164
private:
165
const Element *E = nullptr;
166
};
167
168
struct Iterator {
169
_FORCE_INLINE_ T &operator*() const {
170
return E->get();
171
}
172
_FORCE_INLINE_ T *operator->() const { return &E->get(); }
173
_FORCE_INLINE_ Iterator &operator++() {
174
E = E->next();
175
return *this;
176
}
177
_FORCE_INLINE_ Iterator &operator--() {
178
E = E->prev();
179
return *this;
180
}
181
182
_FORCE_INLINE_ bool operator==(const Iterator &b) const { return E == b.E; }
183
_FORCE_INLINE_ bool operator!=(const Iterator &b) const { return E != b.E; }
184
185
Iterator(Element *p_E) { E = p_E; }
186
Iterator() {}
187
Iterator(const Iterator &p_it) { E = p_it.E; }
188
189
operator ConstIterator() const {
190
return ConstIterator(E);
191
}
192
193
private:
194
Element *E = nullptr;
195
};
196
197
_FORCE_INLINE_ Iterator begin() {
198
return Iterator(front());
199
}
200
_FORCE_INLINE_ Iterator end() {
201
return Iterator(nullptr);
202
}
203
204
#if 0
205
//to use when replacing find()
206
_FORCE_INLINE_ Iterator find(const K &p_key) {
207
return Iterator(find(p_key));
208
}
209
#endif
210
_FORCE_INLINE_ ConstIterator begin() const {
211
return ConstIterator(front());
212
}
213
_FORCE_INLINE_ ConstIterator end() const {
214
return ConstIterator(nullptr);
215
}
216
#if 0
217
//to use when replacing find()
218
_FORCE_INLINE_ ConstIterator find(const K &p_key) const {
219
return ConstIterator(find(p_key));
220
}
221
#endif
222
private:
223
struct _Data {
224
Element *first = nullptr;
225
Element *last = nullptr;
226
int size_cache = 0;
227
228
bool erase(Element *p_I) {
229
ERR_FAIL_NULL_V(p_I, false);
230
ERR_FAIL_COND_V(p_I->data != this, false);
231
232
if (first == p_I) {
233
first = p_I->next_ptr;
234
}
235
236
if (last == p_I) {
237
last = p_I->prev_ptr;
238
}
239
240
if (p_I->prev_ptr) {
241
p_I->prev_ptr->next_ptr = p_I->next_ptr;
242
}
243
244
if (p_I->next_ptr) {
245
p_I->next_ptr->prev_ptr = p_I->prev_ptr;
246
}
247
248
memdelete_allocator<Element, A>(p_I);
249
size_cache--;
250
251
return true;
252
}
253
};
254
255
_Data *_data = nullptr;
256
257
public:
258
/**
259
* return a const iterator to the beginning of the list.
260
*/
261
_FORCE_INLINE_ const Element *front() const {
262
return _data ? _data->first : nullptr;
263
}
264
265
/**
266
* return an iterator to the beginning of the list.
267
*/
268
_FORCE_INLINE_ Element *front() {
269
return _data ? _data->first : nullptr;
270
}
271
272
/**
273
* return a const iterator to the last member of the list.
274
*/
275
_FORCE_INLINE_ const Element *back() const {
276
return _data ? _data->last : nullptr;
277
}
278
279
/**
280
* return an iterator to the last member of the list.
281
*/
282
_FORCE_INLINE_ Element *back() {
283
return _data ? _data->last : nullptr;
284
}
285
286
/**
287
* store a new element at the end of the list
288
*/
289
Element *push_back(const T &value) {
290
if (!_data) {
291
_data = memnew_allocator(_Data, A);
292
_data->first = nullptr;
293
_data->last = nullptr;
294
_data->size_cache = 0;
295
}
296
297
Element *n = memnew_allocator(Element, A);
298
n->value = (T &)value;
299
300
n->prev_ptr = _data->last;
301
n->next_ptr = nullptr;
302
n->data = _data;
303
304
if (_data->last) {
305
_data->last->next_ptr = n;
306
}
307
308
_data->last = n;
309
310
if (!_data->first) {
311
_data->first = n;
312
}
313
314
_data->size_cache++;
315
316
return n;
317
}
318
319
void pop_back() {
320
if (_data && _data->last) {
321
erase(_data->last);
322
}
323
}
324
325
/**
326
* store a new element at the beginning of the list
327
*/
328
Element *push_front(const T &value) {
329
if (!_data) {
330
_data = memnew_allocator(_Data, A);
331
_data->first = nullptr;
332
_data->last = nullptr;
333
_data->size_cache = 0;
334
}
335
336
Element *n = memnew_allocator(Element, A);
337
n->value = (T &)value;
338
n->prev_ptr = nullptr;
339
n->next_ptr = _data->first;
340
n->data = _data;
341
342
if (_data->first) {
343
_data->first->prev_ptr = n;
344
}
345
346
_data->first = n;
347
348
if (!_data->last) {
349
_data->last = n;
350
}
351
352
_data->size_cache++;
353
354
return n;
355
}
356
357
void pop_front() {
358
if (_data && _data->first) {
359
erase(_data->first);
360
}
361
}
362
363
Element *insert_after(Element *p_element, const T &p_value) {
364
CRASH_COND(p_element && (!_data || p_element->data != _data));
365
366
if (!p_element) {
367
return push_back(p_value);
368
}
369
370
Element *n = memnew_allocator(Element, A);
371
n->value = (T &)p_value;
372
n->prev_ptr = p_element;
373
n->next_ptr = p_element->next_ptr;
374
n->data = _data;
375
376
if (!p_element->next_ptr) {
377
_data->last = n;
378
} else {
379
p_element->next_ptr->prev_ptr = n;
380
}
381
382
p_element->next_ptr = n;
383
384
_data->size_cache++;
385
386
return n;
387
}
388
389
Element *insert_before(Element *p_element, const T &p_value) {
390
CRASH_COND(p_element && (!_data || p_element->data != _data));
391
392
if (!p_element) {
393
return push_back(p_value);
394
}
395
396
Element *n = memnew_allocator(Element, A);
397
n->value = (T &)p_value;
398
n->prev_ptr = p_element->prev_ptr;
399
n->next_ptr = p_element;
400
n->data = _data;
401
402
if (!p_element->prev_ptr) {
403
_data->first = n;
404
} else {
405
p_element->prev_ptr->next_ptr = n;
406
}
407
408
p_element->prev_ptr = n;
409
410
_data->size_cache++;
411
412
return n;
413
}
414
415
/**
416
* find an element in the list,
417
*/
418
template <typename T_v>
419
Element *find(const T_v &p_val) {
420
Element *it = front();
421
while (it) {
422
if (it->value == p_val) {
423
return it;
424
}
425
it = it->next();
426
}
427
428
return nullptr;
429
}
430
431
/**
432
* erase an element in the list, by iterator pointing to it. Return true if it was found/erased.
433
*/
434
bool erase(Element *p_I) {
435
if (_data && p_I) {
436
bool ret = _data->erase(p_I);
437
438
if (_data->size_cache == 0) {
439
memdelete_allocator<_Data, A>(_data);
440
_data = nullptr;
441
}
442
443
return ret;
444
}
445
446
return false;
447
}
448
449
/**
450
* erase the first element in the list, that contains value
451
*/
452
bool erase(const T &value) {
453
Element *I = find(value);
454
return erase(I);
455
}
456
457
/**
458
* return whether the list is empty
459
*/
460
_FORCE_INLINE_ bool is_empty() const {
461
return (!_data || !_data->size_cache);
462
}
463
464
/**
465
* clear the list
466
*/
467
void clear() {
468
while (front()) {
469
erase(front());
470
}
471
}
472
473
_FORCE_INLINE_ int size() const {
474
return _data ? _data->size_cache : 0;
475
}
476
477
void swap(Element *p_A, Element *p_B) {
478
ERR_FAIL_COND(!p_A || !p_B);
479
ERR_FAIL_COND(p_A->data != _data);
480
ERR_FAIL_COND(p_B->data != _data);
481
482
if (p_A == p_B) {
483
return;
484
}
485
Element *A_prev = p_A->prev_ptr;
486
Element *A_next = p_A->next_ptr;
487
Element *B_prev = p_B->prev_ptr;
488
Element *B_next = p_B->next_ptr;
489
490
if (A_prev) {
491
A_prev->next_ptr = p_B;
492
} else {
493
_data->first = p_B;
494
}
495
if (B_prev) {
496
B_prev->next_ptr = p_A;
497
} else {
498
_data->first = p_A;
499
}
500
if (A_next) {
501
A_next->prev_ptr = p_B;
502
} else {
503
_data->last = p_B;
504
}
505
if (B_next) {
506
B_next->prev_ptr = p_A;
507
} else {
508
_data->last = p_A;
509
}
510
p_A->prev_ptr = A_next == p_B ? p_B : B_prev;
511
p_A->next_ptr = B_next == p_A ? p_B : B_next;
512
p_B->prev_ptr = B_next == p_A ? p_A : A_prev;
513
p_B->next_ptr = A_next == p_B ? p_A : A_next;
514
}
515
/**
516
* copy the list
517
*/
518
void operator=(const List &p_list) {
519
clear();
520
const Element *it = p_list.front();
521
while (it) {
522
push_back(it->get());
523
it = it->next();
524
}
525
}
526
void operator=(List &&p_list) {
527
if (unlikely(this == &p_list)) {
528
return;
529
}
530
531
clear();
532
_data = p_list._data;
533
p_list._data = nullptr;
534
}
535
536
// Random access to elements, use with care,
537
// do not use for iteration.
538
T &get(int p_index) {
539
CRASH_BAD_INDEX(p_index, size());
540
541
Element *I = front();
542
int c = 0;
543
while (c < p_index) {
544
I = I->next();
545
c++;
546
}
547
548
return I->get();
549
}
550
551
// Random access to elements, use with care,
552
// do not use for iteration.
553
const T &get(int p_index) const {
554
CRASH_BAD_INDEX(p_index, size());
555
556
const Element *I = front();
557
int c = 0;
558
while (c < p_index) {
559
I = I->next();
560
c++;
561
}
562
563
return I->get();
564
}
565
566
void move_to_back(Element *p_I) {
567
ERR_FAIL_COND(p_I->data != _data);
568
if (!p_I->next_ptr) {
569
return;
570
}
571
572
if (_data->first == p_I) {
573
_data->first = p_I->next_ptr;
574
}
575
576
if (_data->last == p_I) {
577
_data->last = p_I->prev_ptr;
578
}
579
580
if (p_I->prev_ptr) {
581
p_I->prev_ptr->next_ptr = p_I->next_ptr;
582
}
583
584
p_I->next_ptr->prev_ptr = p_I->prev_ptr;
585
586
_data->last->next_ptr = p_I;
587
p_I->prev_ptr = _data->last;
588
p_I->next_ptr = nullptr;
589
_data->last = p_I;
590
}
591
592
void reverse() {
593
int s = size() / 2;
594
Element *F = front();
595
Element *B = back();
596
for (int i = 0; i < s; i++) {
597
SWAP(F->value, B->value);
598
F = F->next();
599
B = B->prev();
600
}
601
}
602
603
void move_to_front(Element *p_I) {
604
ERR_FAIL_COND(p_I->data != _data);
605
if (!p_I->prev_ptr) {
606
return;
607
}
608
609
if (_data->first == p_I) {
610
_data->first = p_I->next_ptr;
611
}
612
613
if (_data->last == p_I) {
614
_data->last = p_I->prev_ptr;
615
}
616
617
p_I->prev_ptr->next_ptr = p_I->next_ptr;
618
619
if (p_I->next_ptr) {
620
p_I->next_ptr->prev_ptr = p_I->prev_ptr;
621
}
622
623
_data->first->prev_ptr = p_I;
624
p_I->next_ptr = _data->first;
625
p_I->prev_ptr = nullptr;
626
_data->first = p_I;
627
}
628
629
void move_before(Element *value, Element *where) {
630
if (value->prev_ptr) {
631
value->prev_ptr->next_ptr = value->next_ptr;
632
} else {
633
_data->first = value->next_ptr;
634
}
635
if (value->next_ptr) {
636
value->next_ptr->prev_ptr = value->prev_ptr;
637
} else {
638
_data->last = value->prev_ptr;
639
}
640
641
value->next_ptr = where;
642
if (!where) {
643
value->prev_ptr = _data->last;
644
_data->last = value;
645
return;
646
}
647
648
value->prev_ptr = where->prev_ptr;
649
650
if (where->prev_ptr) {
651
where->prev_ptr->next_ptr = value;
652
} else {
653
_data->first = value;
654
}
655
656
where->prev_ptr = value;
657
}
658
659
void sort() {
660
sort_custom<Comparator<T>>();
661
}
662
663
template <typename C>
664
void sort_custom() {
665
if (size() < 2) {
666
return;
667
}
668
669
SortList<Element, T, &Element::value, &Element::prev_ptr, &Element::next_ptr, C> sorter;
670
sorter.sort(_data->first, _data->last);
671
}
672
673
const void *id() const {
674
return (void *)_data;
675
}
676
677
/**
678
* copy constructor for the list
679
*/
680
List(const List &p_list) {
681
const Element *it = p_list.front();
682
while (it) {
683
push_back(it->get());
684
it = it->next();
685
}
686
}
687
List(List &&p_list) {
688
_data = p_list._data;
689
p_list._data = nullptr;
690
}
691
692
List() {}
693
694
List(std::initializer_list<T> p_init) {
695
for (const T &E : p_init) {
696
push_back(E);
697
}
698
}
699
700
~List() {
701
clear();
702
if (_data) {
703
ERR_FAIL_COND(_data->size_cache);
704
memdelete_allocator<_Data, A>(_data);
705
}
706
}
707
};
708
709
template <typename T, typename A>
710
void List<T, A>::Element::transfer_to_back(List<T, A> *p_dst_list) {
711
// Detach from current.
712
713
if (data->first == this) {
714
data->first = data->first->next_ptr;
715
}
716
if (data->last == this) {
717
data->last = data->last->prev_ptr;
718
}
719
if (prev_ptr) {
720
prev_ptr->next_ptr = next_ptr;
721
}
722
if (next_ptr) {
723
next_ptr->prev_ptr = prev_ptr;
724
}
725
data->size_cache--;
726
727
// Attach to the back of the new one.
728
729
if (!p_dst_list->_data) {
730
p_dst_list->_data = memnew_allocator(_Data, A);
731
p_dst_list->_data->first = this;
732
p_dst_list->_data->last = nullptr;
733
p_dst_list->_data->size_cache = 0;
734
prev_ptr = nullptr;
735
} else {
736
p_dst_list->_data->last->next_ptr = this;
737
prev_ptr = p_dst_list->_data->last;
738
}
739
p_dst_list->_data->last = this;
740
next_ptr = nullptr;
741
742
data = p_dst_list->_data;
743
p_dst_list->_data->size_cache++;
744
}
745
746