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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
|
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Electronic Arts Inc. All rights reserved.
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// The intrusive list container is similar to a list, with the primary
// different being that intrusive lists allow you to control memory
// allocation.
//
// * Intrusive lists store the nodes directly in the data items. This
// is done by deriving the object from intrusive_list_node.
//
// * The container does no memory allocation -- it works entirely with
// the submitted nodes. This does mean that it is the client's job to
// free the nodes in an intrusive list, though.
//
// * Valid node pointers can be converted back to iterators in O(1).
// This is because objects in the list are also nodes in the list.
//
// * intrusive_list does not support copy construction or assignment;
// the push, pop, and insert operations take ownership of the
// passed object.
//
// Usage notes:
//
// * You can use an intrusive_list directly with the standard nodes
// if you have some other way of converting the node pointer back
// to your data pointer.
//
// * Remember that the list destructor doesn't deallocate nodes -- it can't.
//
// * The size is not cached; this makes size() linear time but splice() is
// constant time. This does mean that you can remove() an element without
// having to figure out which list it is in, however.
//
// * You can insert a node into multiple intrusive_lists. One way to do so
// is to (ab)use inheritance:
//
// struct NodeA : public intrusive_list_node {};
// struct NodeB : public intrusive_list_node {};
// struct Object : public NodeA, nodeB {};
//
// intrusive_list<NodeA> listA;
// intrusive_list<NodeB> listB;
//
// listA.push_back(obj);
// listB.push_back(obj);
//
// * find() vs. locate()
// The find(v) algorithm returns an iterator p such that *p == v; intrusive_list::locate(v)
// returns an iterator p such that &*p == &v. intrusive_list<> doesn't have find() mainly
// because list<> doesn't have it either, but there's no reason it couldn't. intrusive_list
// uses the name 'find' because:
// - So as not to confuse the member function with the well-defined free function from algorithm.h.
// - Because it is not API-compatible with eastl::find().
// - Because it simply locates an object within the list based on its node entry and doesn't perform before any value-based searches or comparisons.
//
// Differences between intrusive_list and std::list:
//
// Issue std::list intrusive_list
// --------------------------------------------------------------
// Automatic node ctor/dtor Yes No
// Can memmove() container Maybe* No
// Same item in list twice Yes(copy/byref) No
// Can store non-copyable items No Yes
// size() O(1) or O(n) O(n)
// clear() O(n) O(1)
// erase(range) O(n) O(1)
// splice(range) O(1) or O(n) O(1)
// Convert reference to iterator No O(1)
// Remove without container No O(1)
// Nodes in mixed allocators No Yes
//
// *) Not required by standard but can be done with some STL implementations.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_INTRUSIVE_LIST_H
#define EASTL_INTRUSIVE_LIST_H
#include <EASTL/internal/config.h>
#include <EASTL/iterator.h>
#include <EASTL/algorithm.h>
#if defined(EA_PRAGMA_ONCE_SUPPORTED)
#pragma once // Some compilers (e.g. VC++) benefit significantly from using this. We've measured 3-4% build speed improvements in apps as a result.
#endif
namespace eastl
{
/// intrusive_list_node
///
/// By design this must be a POD, as user structs will be inheriting from
/// it and they may wish to remain POD themselves. However, if the
/// EASTL_VALIDATE_INTRUSIVE_LIST option is enabled
///
struct intrusive_list_node
{
intrusive_list_node* mpNext;
intrusive_list_node* mpPrev;
#if EASTL_VALIDATE_INTRUSIVE_LIST
intrusive_list_node() // Implemented inline because GCC can't deal with member functions
{ // of may-alias classes being defined outside the declaration.
mpNext = mpPrev = NULL;
}
~intrusive_list_node()
{
#if EASTL_ASSERT_ENABLED
if(mpNext || mpPrev)
EASTL_FAIL_MSG("~intrusive_list_node(): List is non-empty.");
#endif
}
#endif
} EASTL_MAY_ALIAS; // It's not clear if this really should be needed. An old GCC compatible compiler is generating some crashing optimized code when strict aliasing is enabled, but analysis of it seems to blame the compiler. However, this topic can be tricky.
/// intrusive_list_iterator
///
template <typename T, typename Pointer, typename Reference>
class intrusive_list_iterator
{
public:
typedef intrusive_list_iterator<T, Pointer, Reference> this_type;
typedef intrusive_list_iterator<T, T*, T&> iterator;
typedef intrusive_list_iterator<T, const T*, const T&> const_iterator;
typedef T value_type;
typedef T node_type;
typedef ptrdiff_t difference_type;
typedef Pointer pointer;
typedef Reference reference;
typedef EASTL_ITC_NS::bidirectional_iterator_tag iterator_category;
public:
pointer mpNode; // Needs to be public for operator==() to work
public:
intrusive_list_iterator();
explicit intrusive_list_iterator(pointer pNode); // Note that you can also construct an iterator from T via this, since value_type == node_type.
intrusive_list_iterator(const iterator& x);
reference operator*() const;
pointer operator->() const;
intrusive_list_iterator& operator++();
intrusive_list_iterator& operator--();
intrusive_list_iterator operator++(int);
intrusive_list_iterator operator--(int);
}; // class intrusive_list_iterator
/// intrusive_list_base
///
class intrusive_list_base
{
public:
typedef eastl_size_t size_type; // See config.h for the definition of this, which defaults to size_t.
typedef ptrdiff_t difference_type;
protected:
intrusive_list_node mAnchor; ///< Sentinel node (end). All data nodes are linked in a ring from this node.
public:
intrusive_list_base();
~intrusive_list_base();
bool empty() const EA_NOEXCEPT;
eastl_size_t size() const EA_NOEXCEPT; ///< Returns the number of elements in the list; O(n).
void clear() EA_NOEXCEPT; ///< Clears the list; O(1). No deallocation occurs.
void pop_front(); ///< Removes an element from the front of the list; O(1). The element must exist, but is not deallocated.
void pop_back(); ///< Removes an element from the back of the list; O(1). The element must exist, but is not deallocated.
EASTL_API void reverse() EA_NOEXCEPT; ///< Reverses a list so that front and back are swapped; O(n).
EASTL_API bool validate() const; ///< Scans a list for linkage inconsistencies; O(n) time, O(1) space. Returns false if errors are detected, such as loops or branching.
}; // class intrusive_list_base
/// intrusive_list
///
/// Example usage:
/// struct IntNode : public eastl::intrusive_list_node {
/// int mX;
/// IntNode(int x) : mX(x) { }
/// };
///
/// IntNode nodeA(0);
/// IntNode nodeB(1);
///
/// intrusive_list<IntNode> intList;
/// intList.push_back(nodeA);
/// intList.push_back(nodeB);
/// intList.remove(nodeA);
///
template <typename T = intrusive_list_node>
class intrusive_list : public intrusive_list_base
{
public:
typedef intrusive_list<T> this_type;
typedef intrusive_list_base base_type;
typedef T node_type;
typedef T value_type;
typedef typename base_type::size_type size_type;
typedef typename base_type::difference_type difference_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef intrusive_list_iterator<T, T*, T&> iterator;
typedef intrusive_list_iterator<T, const T*, const T&> const_iterator;
typedef eastl::reverse_iterator<iterator> reverse_iterator;
typedef eastl::reverse_iterator<const_iterator> const_reverse_iterator;
public:
intrusive_list(); ///< Creates an empty list.
intrusive_list(const this_type& x); ///< Creates an empty list; ignores the argument.
//intrusive_list(std::initializer_list<value_type> ilist); To consider: Is this feasible, given how initializer_list works by creating a temporary array? Even if it is feasible, is it a good idea?
this_type& operator=(const this_type& x); ///< Clears the list; ignores the argument.
void swap(this_type&); ///< Swaps the contents of two intrusive lists; O(1).
iterator begin() EA_NOEXCEPT; ///< Returns an iterator pointing to the first element in the list.
const_iterator begin() const EA_NOEXCEPT; ///< Returns a const_iterator pointing to the first element in the list.
const_iterator cbegin() const EA_NOEXCEPT; ///< Returns a const_iterator pointing to the first element in the list.
iterator end() EA_NOEXCEPT; ///< Returns an iterator pointing one-after the last element in the list.
const_iterator end() const EA_NOEXCEPT; ///< Returns a const_iterator pointing one-after the last element in the list.
const_iterator cend() const EA_NOEXCEPT; ///< Returns a const_iterator pointing one-after the last element in the list.
reverse_iterator rbegin() EA_NOEXCEPT; ///< Returns a reverse_iterator pointing at the end of the list (start of the reverse sequence).
const_reverse_iterator rbegin() const EA_NOEXCEPT; ///< Returns a const_reverse_iterator pointing at the end of the list (start of the reverse sequence).
const_reverse_iterator crbegin() const EA_NOEXCEPT; ///< Returns a const_reverse_iterator pointing at the end of the list (start of the reverse sequence).
reverse_iterator rend() EA_NOEXCEPT; ///< Returns a reverse_iterator pointing at the start of the list (end of the reverse sequence).
const_reverse_iterator rend() const EA_NOEXCEPT; ///< Returns a const_reverse_iterator pointing at the start of the list (end of the reverse sequence).
const_reverse_iterator crend() const EA_NOEXCEPT; ///< Returns a const_reverse_iterator pointing at the start of the list (end of the reverse sequence).
reference front(); ///< Returns a reference to the first element. The list must be non-empty.
const_reference front() const; ///< Returns a const reference to the first element. The list must be non-empty.
reference back(); ///< Returns a reference to the last element. The list must be non-empty.
const_reference back() const; ///< Returns a const reference to the last element. The list must be non-empty.
void push_front(value_type& x); ///< Adds an element to the front of the list; O(1). The element is not copied. The element must not be in any other list.
void push_back(value_type& x); ///< Adds an element to the back of the list; O(1). The element is not copied. The element must not be in any other list.
bool contains(const value_type& x) const; ///< Returns true if the given element is in the list; O(n). Equivalent to (locate(x) != end()).
iterator locate(value_type& x); ///< Converts a reference to an object in the list back to an iterator, or returns end() if it is not part of the list. O(n)
const_iterator locate(const value_type& x) const; ///< Converts a const reference to an object in the list back to a const iterator, or returns end() if it is not part of the list. O(n)
iterator insert(const_iterator pos, value_type& x); ///< Inserts an element before the element pointed to by the iterator. O(1)
iterator erase(const_iterator pos); ///< Erases the element pointed to by the iterator. O(1)
iterator erase(const_iterator pos, const_iterator last); ///< Erases elements within the iterator range [pos, last). O(1)
reverse_iterator erase(const_reverse_iterator pos);
reverse_iterator erase(const_reverse_iterator pos, const_reverse_iterator last);
static void remove(value_type& value); ///< Erases an element from a list; O(1). Note that this is static so you don't need to know which list the element, although it must be in some list.
void splice(const_iterator pos, value_type& x);
///< Moves the given element into this list before the element pointed to by pos; O(1).
///< Required: x must be in some list or have first/next pointers that point it itself.
void splice(const_iterator pos, intrusive_list& x);
///< Moves the contents of a list into this list before the element pointed to by pos; O(1).
///< Required: &x != this (same as std::list).
void splice(const_iterator pos, intrusive_list& x, const_iterator i);
///< Moves the given element pointed to i within the list x into the current list before
///< the element pointed to by pos; O(1).
void splice(const_iterator pos, intrusive_list& x, const_iterator first, const_iterator last);
///< Moves the range of elements [first, last) from list x into the current list before
///< the element pointed to by pos; O(1).
///< Required: pos must not be in [first, last). (same as std::list).
public:
// Sorting functionality
// This is independent of the global sort algorithms, as lists are
// linked nodes and can be sorted more efficiently by moving nodes
// around in ways that global sort algorithms aren't privy to.
void merge(this_type& x);
template <typename Compare>
void merge(this_type& x, Compare compare);
void unique();
template <typename BinaryPredicate>
void unique(BinaryPredicate);
void sort();
template<typename Compare>
void sort(Compare compare);
public:
// bool validate() const; // Inherited from parent.
int validate_iterator(const_iterator i) const;
}; // intrusive_list
///////////////////////////////////////////////////////////////////////
// intrusive_list_node
///////////////////////////////////////////////////////////////////////
// Moved to be inline within the class because the may-alias attribute is
// triggering what appears to be a bug in GCC that effectively requires
// may-alias structs to implement inline member functions within the class
// declaration. We don't have a .cpp file for
// #if EASTL_VALIDATE_INTRUSIVE_LIST
// inline intrusive_list_node::intrusive_list_node()
// {
// mpNext = mpPrev = NULL;
// }
//
// inline intrusive_list_node::~intrusive_list_node()
// {
// #if EASTL_ASSERT_ENABLED
// if(mpNext || mpPrev)
// EASTL_FAIL_MSG("~intrusive_list_node(): List is non-empty.");
// #endif
// }
// #endif
///////////////////////////////////////////////////////////////////////
// intrusive_list_iterator
///////////////////////////////////////////////////////////////////////
template <typename T, typename Pointer, typename Reference>
inline intrusive_list_iterator<T, Pointer, Reference>::intrusive_list_iterator()
{
#if EASTL_DEBUG
mpNode = NULL;
#endif
}
template <typename T, typename Pointer, typename Reference>
inline intrusive_list_iterator<T, Pointer, Reference>::intrusive_list_iterator(pointer pNode)
: mpNode(pNode)
{
// Empty
}
template <typename T, typename Pointer, typename Reference>
inline intrusive_list_iterator<T, Pointer, Reference>::intrusive_list_iterator(const iterator& x)
: mpNode(x.mpNode)
{
// Empty
}
template <typename T, typename Pointer, typename Reference>
inline typename intrusive_list_iterator<T, Pointer, Reference>::reference
intrusive_list_iterator<T, Pointer, Reference>::operator*() const
{
return *mpNode;
}
template <typename T, typename Pointer, typename Reference>
inline typename intrusive_list_iterator<T, Pointer, Reference>::pointer
intrusive_list_iterator<T, Pointer, Reference>::operator->() const
{
return mpNode;
}
template <typename T, typename Pointer, typename Reference>
inline typename intrusive_list_iterator<T, Pointer, Reference>::this_type&
intrusive_list_iterator<T, Pointer, Reference>::operator++()
{
mpNode = static_cast<node_type*>(mpNode->mpNext);
return *this;
}
template <typename T, typename Pointer, typename Reference>
inline typename intrusive_list_iterator<T, Pointer, Reference>::this_type
intrusive_list_iterator<T, Pointer, Reference>::operator++(int)
{
intrusive_list_iterator it(*this);
mpNode = static_cast<node_type*>(mpNode->mpNext);
return it;
}
template <typename T, typename Pointer, typename Reference>
inline typename intrusive_list_iterator<T, Pointer, Reference>::this_type&
intrusive_list_iterator<T, Pointer, Reference>::operator--()
{
mpNode = static_cast<node_type*>(mpNode->mpPrev);
return *this;
}
template <typename T, typename Pointer, typename Reference>
inline typename intrusive_list_iterator<T, Pointer, Reference>::this_type
intrusive_list_iterator<T, Pointer, Reference>::operator--(int)
{
intrusive_list_iterator it(*this);
mpNode = static_cast<node_type*>(mpNode->mpPrev);
return it;
}
// The C++ defect report #179 requires that we support comparisons between const and non-const iterators.
// Thus we provide additional template paremeters here to support this. The defect report does not
// require us to support comparisons between reverse_iterators and const_reverse_iterators.
template <typename T, typename PointerA, typename ReferenceA, typename PointerB, typename ReferenceB>
inline bool operator==(const intrusive_list_iterator<T, PointerA, ReferenceA>& a,
const intrusive_list_iterator<T, PointerB, ReferenceB>& b)
{
return a.mpNode == b.mpNode;
}
template <typename T, typename PointerA, typename ReferenceA, typename PointerB, typename ReferenceB>
inline bool operator!=(const intrusive_list_iterator<T, PointerA, ReferenceA>& a,
const intrusive_list_iterator<T, PointerB, ReferenceB>& b)
{
return a.mpNode != b.mpNode;
}
// We provide a version of operator!= for the case where the iterators are of the
// same type. This helps prevent ambiguity errors in the presence of rel_ops.
template <typename T, typename Pointer, typename Reference>
inline bool operator!=(const intrusive_list_iterator<T, Pointer, Reference>& a,
const intrusive_list_iterator<T, Pointer, Reference>& b)
{
return a.mpNode != b.mpNode;
}
///////////////////////////////////////////////////////////////////////
// intrusive_list_base
///////////////////////////////////////////////////////////////////////
inline intrusive_list_base::intrusive_list_base()
{
mAnchor.mpNext = mAnchor.mpPrev = &mAnchor;
}
inline intrusive_list_base::~intrusive_list_base()
{
#if EASTL_VALIDATE_INTRUSIVE_LIST
clear();
mAnchor.mpNext = mAnchor.mpPrev = NULL;
#endif
}
inline bool intrusive_list_base::empty() const EA_NOEXCEPT
{
return mAnchor.mpPrev == &mAnchor;
}
inline intrusive_list_base::size_type intrusive_list_base::size() const EA_NOEXCEPT
{
const intrusive_list_node* p = &mAnchor;
size_type n = (size_type)-1;
do {
++n;
p = p->mpNext;
} while(p != &mAnchor);
return n;
}
inline void intrusive_list_base::clear() EA_NOEXCEPT
{
#if EASTL_VALIDATE_INTRUSIVE_LIST
// Need to clear out all the next/prev pointers in the elements;
// this makes this operation O(n) instead of O(1).
intrusive_list_node* pNode = mAnchor.mpNext;
while(pNode != &mAnchor)
{
intrusive_list_node* const pNextNode = pNode->mpNext;
pNode->mpNext = pNode->mpPrev = NULL;
pNode = pNextNode;
}
#endif
mAnchor.mpNext = mAnchor.mpPrev = &mAnchor;
}
inline void intrusive_list_base::pop_front()
{
#if EASTL_VALIDATE_INTRUSIVE_LIST
intrusive_list_node* const pNode = mAnchor.mpNext;
#endif
mAnchor.mpNext->mpNext->mpPrev = &mAnchor;
mAnchor.mpNext = mAnchor.mpNext->mpNext;
#if EASTL_VALIDATE_INTRUSIVE_LIST
if(pNode != &mAnchor)
pNode->mpNext = pNode->mpPrev = NULL;
#if EASTL_ASSERT_ENABLED
else
EASTL_FAIL_MSG("intrusive_list::pop_front(): empty list.");
#endif
#endif
}
inline void intrusive_list_base::pop_back()
{
#if EASTL_VALIDATE_INTRUSIVE_LIST
intrusive_list_node* const pNode = mAnchor.mpPrev;
#endif
mAnchor.mpPrev->mpPrev->mpNext = &mAnchor;
mAnchor.mpPrev = mAnchor.mpPrev->mpPrev;
#if EASTL_VALIDATE_INTRUSIVE_LIST
if(pNode != &mAnchor)
pNode->mpNext = pNode->mpPrev = NULL;
#if EASTL_ASSERT_ENABLED
else
EASTL_FAIL_MSG("intrusive_list::pop_back(): empty list.");
#endif
#endif
}
///////////////////////////////////////////////////////////////////////
// intrusive_list
///////////////////////////////////////////////////////////////////////
template <typename T>
inline intrusive_list<T>::intrusive_list()
{
}
template <typename T>
inline intrusive_list<T>::intrusive_list(const this_type& /*x*/)
: intrusive_list_base()
{
// We intentionally ignore argument x.
// To consider: Shouldn't this function simply not exist? Is there a useful purpose for having this function?
// There should be a comment here about it, though my first guess is that this exists to quell VC++ level 4/-Wall compiler warnings.
}
template <typename T>
inline typename intrusive_list<T>::this_type& intrusive_list<T>::operator=(const this_type& /*x*/)
{
// We intentionally ignore argument x.
// See notes above in the copy constructor about questioning the existence of this function.
return *this;
}
template <typename T>
inline typename intrusive_list<T>::iterator intrusive_list<T>::begin() EA_NOEXCEPT
{
return iterator(static_cast<T*>(mAnchor.mpNext));
}
template <typename T>
inline typename intrusive_list<T>::const_iterator intrusive_list<T>::begin() const EA_NOEXCEPT
{
return const_iterator(static_cast<T*>(mAnchor.mpNext));
}
template <typename T>
inline typename intrusive_list<T>::const_iterator intrusive_list<T>::cbegin() const EA_NOEXCEPT
{
return const_iterator(static_cast<T*>(mAnchor.mpNext));
}
template <typename T>
inline typename intrusive_list<T>::iterator intrusive_list<T>::end() EA_NOEXCEPT
{
return iterator(static_cast<T*>(&mAnchor));
}
template <typename T>
inline typename intrusive_list<T>::const_iterator intrusive_list<T>::end() const EA_NOEXCEPT
{
return const_iterator(static_cast<const T*>(&mAnchor));
}
template <typename T>
inline typename intrusive_list<T>::const_iterator intrusive_list<T>::cend() const EA_NOEXCEPT
{
return const_iterator(static_cast<const T*>(&mAnchor));
}
template <typename T>
inline typename intrusive_list<T>::reverse_iterator intrusive_list<T>::rbegin() EA_NOEXCEPT
{
return reverse_iterator(iterator(static_cast<T*>(&mAnchor)));
}
template <typename T>
inline typename intrusive_list<T>::const_reverse_iterator intrusive_list<T>::rbegin() const EA_NOEXCEPT
{
return const_reverse_iterator(const_iterator(static_cast<const T*>(&mAnchor)));
}
template <typename T>
inline typename intrusive_list<T>::const_reverse_iterator intrusive_list<T>::crbegin() const EA_NOEXCEPT
{
return const_reverse_iterator(const_iterator(static_cast<const T*>(&mAnchor)));
}
template <typename T>
inline typename intrusive_list<T>::reverse_iterator intrusive_list<T>::rend() EA_NOEXCEPT
{
return reverse_iterator(iterator(static_cast<T*>(mAnchor.mpNext)));
}
template <typename T>
inline typename intrusive_list<T>::const_reverse_iterator intrusive_list<T>::rend() const EA_NOEXCEPT
{
return const_reverse_iterator(const_iterator(static_cast<const T*>(mAnchor.mpNext)));
}
template <typename T>
inline typename intrusive_list<T>::const_reverse_iterator intrusive_list<T>::crend() const EA_NOEXCEPT
{
return const_reverse_iterator(const_iterator(static_cast<const T*>(mAnchor.mpNext)));
}
template <typename T>
inline typename intrusive_list<T>::reference intrusive_list<T>::front()
{
#if EASTL_VALIDATE_INTRUSIVE_LIST && EASTL_ASSERT_ENABLED
if(mAnchor.mpNext == &mAnchor)
EASTL_FAIL_MSG("intrusive_list::front(): empty list.");
#endif
return *static_cast<T*>(mAnchor.mpNext);
}
template <typename T>
inline typename intrusive_list<T>::const_reference intrusive_list<T>::front() const
{
#if EASTL_VALIDATE_INTRUSIVE_LIST && EASTL_ASSERT_ENABLED
if(mAnchor.mpNext == &mAnchor)
EASTL_FAIL_MSG("intrusive_list::front(): empty list.");
#endif
return *static_cast<const T*>(mAnchor.mpNext);
}
template <typename T>
inline typename intrusive_list<T>::reference intrusive_list<T>::back()
{
#if EASTL_VALIDATE_INTRUSIVE_LIST && EASTL_ASSERT_ENABLED
if(mAnchor.mpNext == &mAnchor)
EASTL_FAIL_MSG("intrusive_list::back(): empty list.");
#endif
return *static_cast<T*>(mAnchor.mpPrev);
}
template <typename T>
inline typename intrusive_list<T>::const_reference intrusive_list<T>::back() const
{
#if EASTL_VALIDATE_INTRUSIVE_LIST && EASTL_ASSERT_ENABLED
if(mAnchor.mpNext == &mAnchor)
EASTL_FAIL_MSG("intrusive_list::back(): empty list.");
#endif
return *static_cast<const T*>(mAnchor.mpPrev);
}
template <typename T>
inline void intrusive_list<T>::push_front(value_type& x)
{
#if EASTL_VALIDATE_INTRUSIVE_LIST && EASTL_ASSERT_ENABLED
if(x.mpNext || x.mpPrev)
EASTL_FAIL_MSG("intrusive_list::push_front(): element already on a list.");
#endif
x.mpNext = mAnchor.mpNext;
x.mpPrev = &mAnchor;
mAnchor.mpNext = &x;
x.mpNext->mpPrev = &x;
}
template <typename T>
inline void intrusive_list<T>::push_back(value_type& x)
{
#if EASTL_VALIDATE_INTRUSIVE_LIST && EASTL_ASSERT_ENABLED
if(x.mpNext || x.mpPrev)
EASTL_FAIL_MSG("intrusive_list::push_back(): element already on a list.");
#endif
x.mpPrev = mAnchor.mpPrev;
x.mpNext = &mAnchor;
mAnchor.mpPrev = &x;
x.mpPrev->mpNext = &x;
}
template <typename T>
inline bool intrusive_list<T>::contains(const value_type& x) const
{
for(const intrusive_list_node* p = mAnchor.mpNext; p != &mAnchor; p = p->mpNext)
{
if(p == &x)
return true;
}
return false;
}
template <typename T>
inline typename intrusive_list<T>::iterator intrusive_list<T>::locate(value_type& x)
{
for(intrusive_list_node* p = (T*)mAnchor.mpNext; p != &mAnchor; p = p->mpNext)
{
if(p == &x)
return iterator(static_cast<T*>(p));
}
return iterator((T*)&mAnchor);
}
template <typename T>
inline typename intrusive_list<T>::const_iterator intrusive_list<T>::locate(const value_type& x) const
{
for(const intrusive_list_node* p = mAnchor.mpNext; p != &mAnchor; p = p->mpNext)
{
if(p == &x)
return const_iterator(static_cast<const T*>(p));
}
return const_iterator((T*)&mAnchor);
}
template <typename T>
inline typename intrusive_list<T>::iterator intrusive_list<T>::insert(const_iterator pos, value_type& x)
{
#if EASTL_VALIDATE_INTRUSIVE_LIST && EASTL_ASSERT_ENABLED
if(x.mpNext || x.mpPrev)
EASTL_FAIL_MSG("intrusive_list::insert(): element already on a list.");
#endif
intrusive_list_node& next = *const_cast<node_type*>(pos.mpNode);
intrusive_list_node& prev = *static_cast<node_type*>(next.mpPrev);
prev.mpNext = next.mpPrev = &x;
x.mpPrev = &prev;
x.mpNext = &next;
return iterator(&x);
}
template <typename T>
inline typename intrusive_list<T>::iterator
intrusive_list<T>::erase(const_iterator pos)
{
intrusive_list_node& prev = *static_cast<node_type*>(pos.mpNode->mpPrev);
intrusive_list_node& next = *static_cast<node_type*>(pos.mpNode->mpNext);
prev.mpNext = &next;
next.mpPrev = &prev;
#if EASTL_VALIDATE_INTRUSIVE_LIST
iterator ii(const_cast<node_type*>(pos.mpNode));
ii.mpNode->mpPrev = ii.mpNode->mpNext = NULL;
#endif
return iterator(static_cast<node_type*>(&next));
}
template <typename T>
inline typename intrusive_list<T>::iterator
intrusive_list<T>::erase(const_iterator first, const_iterator last)
{
intrusive_list_node& prev = *static_cast<node_type*>(first.mpNode->mpPrev);
intrusive_list_node& next = *const_cast<node_type*>(last.mpNode);
#if EASTL_VALIDATE_INTRUSIVE_LIST
// need to clear out all the next/prev pointers in the elements;
// this makes this operation O(n) instead of O(1), sadly, although
// it's technically amortized O(1) since you could count yourself
// as paying this cost with each insert.
intrusive_list_node* pCur = const_cast<node_type*>(first.mpNode);
while(pCur != &next)
{
intrusive_list_node* const pCurNext = pCur->mpNext;
pCur->mpPrev = pCur->mpNext = NULL;
pCur = pCurNext;
}
#endif
prev.mpNext = &next;
next.mpPrev = &prev;
return iterator(const_cast<node_type*>(last.mpNode));
}
template <typename T>
inline typename intrusive_list<T>::reverse_iterator
intrusive_list<T>::erase(const_reverse_iterator position)
{
return reverse_iterator(erase((++position).base()));
}
template <typename T>
inline typename intrusive_list<T>::reverse_iterator
intrusive_list<T>::erase(const_reverse_iterator first, const_reverse_iterator last)
{
// Version which erases in order from first to last.
// difference_type i(first.base() - last.base());
// while(i--)
// first = erase(first);
// return first;
// Version which erases in order from last to first, but is slightly more efficient:
return reverse_iterator(erase((++last).base(), (++first).base()));
}
template <typename T>
void intrusive_list<T>::swap(intrusive_list& x)
{
// swap anchors
intrusive_list_node temp(mAnchor);
mAnchor = x.mAnchor;
x.mAnchor = temp;
// Fixup node pointers into the anchor, since the addresses of
// the anchors must stay the same with each list.
if(mAnchor.mpNext == &x.mAnchor)
mAnchor.mpNext = mAnchor.mpPrev = &mAnchor;
else
mAnchor.mpNext->mpPrev = mAnchor.mpPrev->mpNext = &mAnchor;
if(x.mAnchor.mpNext == &mAnchor)
x.mAnchor.mpNext = x.mAnchor.mpPrev = &x.mAnchor;
else
x.mAnchor.mpNext->mpPrev = x.mAnchor.mpPrev->mpNext = &x.mAnchor;
#if EASTL_VALIDATE_INTRUSIVE_LIST
temp.mpPrev = temp.mpNext = NULL;
#endif
}
template <typename T>
void intrusive_list<T>::splice(const_iterator pos, value_type& value)
{
// Note that splice(pos, x, pos) and splice(pos+1, x, pos)
// are valid and need to be handled correctly.
if(pos.mpNode != &value)
{
// Unlink item from old list.
intrusive_list_node& oldNext = *value.mpNext;
intrusive_list_node& oldPrev = *value.mpPrev;
oldNext.mpPrev = &oldPrev;
oldPrev.mpNext = &oldNext;
// Relink item into new list.
intrusive_list_node& newNext = *const_cast<node_type*>(pos.mpNode);
intrusive_list_node& newPrev = *newNext.mpPrev;
newPrev.mpNext = &value;
newNext.mpPrev = &value;
value.mpPrev = &newPrev;
value.mpNext = &newNext;
}
}
template <typename T>
void intrusive_list<T>::splice(const_iterator pos, intrusive_list& x)
{
// Note: &x == this is prohibited, so self-insertion is not a problem.
if(x.mAnchor.mpNext != &x.mAnchor) // If the list 'x' isn't empty...
{
intrusive_list_node& next = *const_cast<node_type*>(pos.mpNode);
intrusive_list_node& prev = *static_cast<node_type*>(next.mpPrev);
intrusive_list_node& insertPrev = *static_cast<node_type*>(x.mAnchor.mpNext);
intrusive_list_node& insertNext = *static_cast<node_type*>(x.mAnchor.mpPrev);
prev.mpNext = &insertPrev;
insertPrev.mpPrev = &prev;
insertNext.mpNext = &next;
next.mpPrev = &insertNext;
x.mAnchor.mpPrev = x.mAnchor.mpNext = &x.mAnchor;
}
}
template <typename T>
void intrusive_list<T>::splice(const_iterator pos, intrusive_list& /*x*/, const_iterator i)
{
// Note: &x == this is prohibited, so self-insertion is not a problem.
// Note that splice(pos, x, pos) and splice(pos + 1, x, pos)
// are valid and need to be handled correctly.
// We don't need to check if the source list is empty, because
// this function expects a valid iterator from the source list,
// and thus the list cannot be empty in such a situation.
iterator ii(const_cast<node_type*>(i.mpNode)); // Make a temporary non-const version.
if(pos != ii)
{
// Unlink item from old list.
intrusive_list_node& oldNext = *ii.mpNode->mpNext;
intrusive_list_node& oldPrev = *ii.mpNode->mpPrev;
oldNext.mpPrev = &oldPrev;
oldPrev.mpNext = &oldNext;
// Relink item into new list.
intrusive_list_node& newNext = *const_cast<node_type*>(pos.mpNode);
intrusive_list_node& newPrev = *newNext.mpPrev;
newPrev.mpNext = ii.mpNode;
newNext.mpPrev = ii.mpNode;
ii.mpNode->mpPrev = &newPrev;
ii.mpNode->mpNext = &newNext;
}
}
template <typename T>
void intrusive_list<T>::splice(const_iterator pos, intrusive_list& /*x*/, const_iterator first, const_iterator last)
{
// Note: &x == this is prohibited, so self-insertion is not a problem.
if(first != last)
{
intrusive_list_node& insertPrev = *const_cast<node_type*>(first.mpNode);
intrusive_list_node& insertNext = *static_cast<node_type*>(last.mpNode->mpPrev);
// remove from old list
insertNext.mpNext->mpPrev = insertPrev.mpPrev;
insertPrev.mpPrev->mpNext = insertNext.mpNext;
// insert into this list
intrusive_list_node& next = *const_cast<node_type*>(pos.mpNode);
intrusive_list_node& prev = *static_cast<node_type*>(next.mpPrev);
prev.mpNext = &insertPrev;
insertPrev.mpPrev = &prev;
insertNext.mpNext = &next;
next.mpPrev = &insertNext;
}
}
template <typename T>
inline void intrusive_list<T>::remove(value_type& value)
{
intrusive_list_node& prev = *value.mpPrev;
intrusive_list_node& next = *value.mpNext;
prev.mpNext = &next;
next.mpPrev = &prev;
#if EASTL_VALIDATE_INTRUSIVE_LIST
value.mpPrev = value.mpNext = NULL;
#endif
}
template <typename T>
void intrusive_list<T>::merge(this_type& x)
{
if(this != &x)
{
iterator first(begin());
iterator firstX(x.begin());
const iterator last(end());
const iterator lastX(x.end());
while((first != last) && (firstX != lastX))
{
if(*firstX < *first)
{
iterator next(firstX);
splice(first, x, firstX, ++next);
firstX = next;
}
else
++first;
}
if(firstX != lastX)
splice(last, x, firstX, lastX);
}
}
template <typename T>
template <typename Compare>
void intrusive_list<T>::merge(this_type& x, Compare compare)
{
if(this != &x)
{
iterator first(begin());
iterator firstX(x.begin());
const iterator last(end());
const iterator lastX(x.end());
while((first != last) && (firstX != lastX))
{
if(compare(*firstX, *first))
{
iterator next(firstX);
splice(first, x, firstX, ++next);
firstX = next;
}
else
++first;
}
if(firstX != lastX)
splice(last, x, firstX, lastX);
}
}
template <typename T>
void intrusive_list<T>::unique()
{
iterator first(begin());
const iterator last(end());
if(first != last)
{
iterator next(first);
while(++next != last)
{
if(*first == *next)
erase(next);
else
first = next;
next = first;
}
}
}
template <typename T>
template <typename BinaryPredicate>
void intrusive_list<T>::unique(BinaryPredicate predicate)
{
iterator first(begin());
const iterator last(end());
if(first != last)
{
iterator next(first);
while(++next != last)
{
if(predicate(*first, *next))
erase(next);
else
first = next;
next = first;
}
}
}
template <typename T>
void intrusive_list<T>::sort()
{
// We implement the algorithm employed by Chris Caulfield whereby we use recursive
// function calls to sort the list. The sorting of a very large list may fail due to stack overflow
// if the stack is exhausted. The limit depends on the platform and the avaialble stack space.
// Easier-to-understand version of the 'if' statement:
// iterator i(begin());
// if((i != end()) && (++i != end())) // If the size is >= 2 (without calling the more expensive size() function)...
// Faster, more inlinable version of the 'if' statement:
if((static_cast<node_type*>(mAnchor.mpNext) != &mAnchor) &&
(static_cast<node_type*>(mAnchor.mpNext) != static_cast<node_type*>(mAnchor.mpPrev)))
{
// Split the array into 2 roughly equal halves.
this_type leftList; // This should cause no memory allocation.
this_type rightList;
// We find an iterator which is in the middle of the list. The fastest way to do
// this is to iterate from the base node both forwards and backwards with two
// iterators and stop when they meet each other. Recall that our size() function
// is not O(1) but is instead O(n), at least when EASTL_LIST_SIZE_CACHE is disabled.
#if EASTL_LIST_SIZE_CACHE
iterator mid(begin());
eastl::advance(mid, size() / 2);
#else
iterator mid(begin()), tail(end());
while((mid != tail) && (++mid != tail))
--tail;
#endif
// Move the left half of this into leftList and the right half into rightList.
leftList.splice(leftList.begin(), *this, begin(), mid);
rightList.splice(rightList.begin(), *this);
// Sort the sub-lists.
leftList.sort();
rightList.sort();
// Merge the two halves into this list.
splice(begin(), leftList);
merge(rightList);
}
}
template <typename T>
template<typename Compare>
void intrusive_list<T>::sort(Compare compare)
{
// We implement the algorithm employed by Chris Caulfield whereby we use recursive
// function calls to sort the list. The sorting of a very large list may fail due to stack overflow
// if the stack is exhausted. The limit depends on the platform and the avaialble stack space.
// Easier-to-understand version of the 'if' statement:
// iterator i(begin());
// if((i != end()) && (++i != end())) // If the size is >= 2 (without calling the more expensive size() function)...
// Faster, more inlinable version of the 'if' statement:
if((static_cast<node_type*>(mAnchor.mpNext) != &mAnchor) &&
(static_cast<node_type*>(mAnchor.mpNext) != static_cast<node_type*>(mAnchor.mpPrev)))
{
// Split the array into 2 roughly equal halves.
this_type leftList; // This should cause no memory allocation.
this_type rightList;
// We find an iterator which is in the middle of the list. The fastest way to do
// this is to iterate from the base node both forwards and backwards with two
// iterators and stop when they meet each other. Recall that our size() function
// is not O(1) but is instead O(n), at least when EASTL_LIST_SIZE_CACHE is disabled.
#if EASTL_LIST_SIZE_CACHE
iterator mid(begin());
eastl::advance(mid, size() / 2);
#else
iterator mid(begin()), tail(end());
while((mid != tail) && (++mid != tail))
--tail;
#endif
// Move the left half of this into leftList and the right half into rightList.
leftList.splice(leftList.begin(), *this, begin(), mid);
rightList.splice(rightList.begin(), *this);
// Sort the sub-lists.
leftList.sort(compare);
rightList.sort(compare);
// Merge the two halves into this list.
splice(begin(), leftList);
merge(rightList, compare);
}
}
template <typename T>
inline int intrusive_list<T>::validate_iterator(const_iterator i) const
{
// To do: Come up with a more efficient mechanism of doing this.
for(const_iterator temp = begin(), tempEnd = end(); temp != tempEnd; ++temp)
{
if(temp == i)
return (isf_valid | isf_current | isf_can_dereference);
}
if(i == end())
return (isf_valid | isf_current);
return isf_none;
}
///////////////////////////////////////////////////////////////////////
// global operators
///////////////////////////////////////////////////////////////////////
template <typename T>
bool operator==(const intrusive_list<T>& a, const intrusive_list<T>& b)
{
// If we store an mSize member for intrusive_list, we want to take advantage of it here.
typename intrusive_list<T>::const_iterator ia = a.begin();
typename intrusive_list<T>::const_iterator ib = b.begin();
typename intrusive_list<T>::const_iterator enda = a.end();
typename intrusive_list<T>::const_iterator endb = b.end();
while((ia != enda) && (ib != endb) && (*ia == *ib))
{
++ia;
++ib;
}
return (ia == enda) && (ib == endb);
}
template <typename T>
bool operator!=(const intrusive_list<T>& a, const intrusive_list<T>& b)
{
return !(a == b);
}
template <typename T>
bool operator<(const intrusive_list<T>& a, const intrusive_list<T>& b)
{
return eastl::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
}
template <typename T>
bool operator>(const intrusive_list<T>& a, const intrusive_list<T>& b)
{
return b < a;
}
template <typename T>
bool operator<=(const intrusive_list<T>& a, const intrusive_list<T>& b)
{
return !(b < a);
}
template <typename T>
bool operator>=(const intrusive_list<T>& a, const intrusive_list<T>& b)
{
return !(a < b);
}
template <typename T>
void swap(intrusive_list<T>& a, intrusive_list<T>& b)
{
a.swap(b);
}
} // namespace eastl
#endif // Header include guard
|