-
Notifications
You must be signed in to change notification settings - Fork 7
/
list.h
2143 lines (1711 loc) · 62.9 KB
/
list.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
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
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Electronic Arts Inc. All rights reserved.
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// This file implements a doubly-linked list, much like the C++ std::list class.
// The primary distinctions between this list and std::list are:
// - list doesn't implement some of the less-frequently used functions
// of std::list. Any required functions can be added at a later time.
// - list has a couple extension functions that increase performance.
// - list can contain objects with alignment requirements. std::list cannot
// do so without a bit of tedious non-portable effort.
// - list has optimizations that don't exist in the STL implementations
// supplied by library vendors for our targeted platforms.
// - list supports debug memory naming natively.
// - list::size() by default is not a constant time function, like the list::size
// in some std implementations such as STLPort and SGI STL but unlike the
// list in Dinkumware and Metrowerks. The EASTL_LIST_SIZE_CACHE option can change this.
// - list provides a guaranteed portable node definition that allows users
// to write custom fixed size node allocators that are portable.
// - list is easier to read, debug, and visualize.
// - list is savvy to an environment that doesn't have exception handling,
// as is sometimes the case with console or embedded environments.
// - list has less deeply nested function calls and allows the user to
// enable forced inlining in debug builds in order to reduce bloat.
// - list doesn't keep a member size variable. This means that list is
// smaller than std::list (depends on std::list) and that for most operations
// it is faster than std::list. However, the list::size function is slower.
// - list::size_type is defined as eastl_size_t instead of size_t in order to
// save memory and run faster on 64 bit systems.
///////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_LIST_H
#define EASTL_LIST_H
#include <eastl/internal/config.h>
#include <eastl/allocator.h>
#include <eastl/type_traits.h>
#include <eastl/iterator.h>
#include <eastl/algorithm.h>
#include <eastl/initializer_list.h>
#include <eastl/bonus/compressed_pair.h>
EA_DISABLE_ALL_VC_WARNINGS()
#include <new>
#include <stddef.h>
EA_RESTORE_ALL_VC_WARNINGS()
// 4530 - C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
// 4345 - Behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized
// 4571 - catch(...) semantics changed since Visual C++ 7.1; structured exceptions (SEH) are no longer caught.
// 4623 - default constructor was implicitly defined as deleted
EA_DISABLE_VC_WARNING(4530 4345 4571 4623);
#if defined(EASTL_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
{
/// EASTL_LIST_DEFAULT_NAME
///
/// Defines a default container name in the absence of a user-provided name.
///
#ifndef EASTL_LIST_DEFAULT_NAME
#define EASTL_LIST_DEFAULT_NAME EASTL_DEFAULT_NAME_PREFIX " list" // Unless the user overrides something, this is "EASTL list".
#endif
/// EASTL_LIST_DEFAULT_ALLOCATOR
///
#ifndef EASTL_LIST_DEFAULT_ALLOCATOR
#define EASTL_LIST_DEFAULT_ALLOCATOR allocator_type(EASTL_LIST_DEFAULT_NAME)
#endif
/// ListNodeBase
///
/// We define a ListNodeBase separately from ListNode (below), because it allows
/// us to have non-templated operations such as insert, remove (below), and it
/// makes it so that the list anchor node doesn't carry a T with it, which would
/// waste space and possibly lead to surprising the user due to extra Ts existing
/// that the user didn't explicitly create. The downside to all of this is that
/// it makes debug viewing of a list harder, given that the node pointers are of
/// type ListNodeBase and not ListNode. However, see ListNodeBaseProxy below.
///
struct ListNodeBase
{
ListNodeBase* mpNext;
ListNodeBase* mpPrev;
void insert(ListNodeBase* pNext) EASTL_NOEXCEPT; // Inserts this standalone node before the node pNext in pNext's list.
void remove() EASTL_NOEXCEPT; // Removes this node from the list it's in. Leaves this node's mpNext/mpPrev invalid.
void splice(ListNodeBase* pFirst, ListNodeBase* pLast) EASTL_NOEXCEPT; // Removes [pFirst,pLast) from the list it's in and inserts it before this in this node's list.
void reverse() EASTL_NOEXCEPT; // Reverses the order of nodes in the circular list this node is a part of.
static void swap(ListNodeBase& a, ListNodeBase& b) EASTL_NOEXCEPT; // Swaps the nodes a and b in the lists to which they belong.
void insert_range(ListNodeBase* pFirst, ListNodeBase* pFinal) EASTL_NOEXCEPT; // Differs from splice in that first/final aren't in another list.
static void remove_range(ListNodeBase* pFirst, ListNodeBase* pFinal) EASTL_NOEXCEPT; //
};
EA_DISABLE_VC_WARNING(4625 4626)
template <typename T>
struct ListNode : public ListNodeBase
{
T mValue;
};
EA_RESTORE_VC_WARNING()
/// ListIterator
///
template <typename T, typename Pointer, typename Reference>
struct ListIterator
{
typedef ListIterator<T, Pointer, Reference> this_type;
typedef ListIterator<T, T*, T&> iterator;
typedef ListIterator<T, const T*, const T&> const_iterator;
typedef eastl_size_t size_type; // See config.h for the definition of eastl_size_t, which defaults to size_t.
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef ListNodeBase base_node_type;
typedef ListNode<T> node_type;
typedef Pointer pointer;
typedef Reference reference;
typedef EASTL_ITC_NS::bidirectional_iterator_tag iterator_category;
public:
base_node_type* mpNode;
public:
ListIterator() EASTL_NOEXCEPT;
ListIterator(const ListNodeBase* pNode) EASTL_NOEXCEPT;
ListIterator(const iterator& x) EASTL_NOEXCEPT;
this_type next() const EASTL_NOEXCEPT;
this_type prev() const EASTL_NOEXCEPT;
reference operator*() const EASTL_NOEXCEPT;
pointer operator->() const EASTL_NOEXCEPT;
this_type& operator++() EASTL_NOEXCEPT;
this_type operator++(int) EASTL_NOEXCEPT;
this_type& operator--() EASTL_NOEXCEPT;
this_type operator--(int) EASTL_NOEXCEPT;
}; // ListIterator
/// ListBase
///
/// See VectorBase (class vector) for an explanation of why we
/// create this separate base class.
///
template <typename T, typename Allocator>
class ListBase
{
public:
typedef T value_type;
typedef Allocator allocator_type;
typedef ListNode<T> node_type;
typedef eastl_size_t size_type; // See config.h for the definition of eastl_size_t, which defaults to size_t.
typedef ptrdiff_t difference_type;
typedef ListNodeBase base_node_type; // We use ListNodeBase instead of ListNode<T> because we don't want to create a T.
protected:
eastl::compressed_pair<base_node_type, allocator_type> mNodeAllocator;
#if EASTL_LIST_SIZE_CACHE
size_type mSize;
#endif
base_node_type& internalNode() EASTL_NOEXCEPT { return mNodeAllocator.first(); }
base_node_type const& internalNode() const EASTL_NOEXCEPT { return mNodeAllocator.first(); }
allocator_type& internalAllocator() EASTL_NOEXCEPT { return mNodeAllocator.second(); }
const allocator_type& internalAllocator() const EASTL_NOEXCEPT { return mNodeAllocator.second(); }
public:
const allocator_type& getAllocator() const EASTL_NOEXCEPT;
allocator_type& getAllocator() EASTL_NOEXCEPT;
void setAllocator(const allocator_type& allocator);
protected:
ListBase();
ListBase(const allocator_type& a);
~ListBase();
node_type* DoAllocateNode();
void DoFreeNode(node_type* pNode);
void DoInit() EASTL_NOEXCEPT;
void DoClear();
}; // ListBase
/// list
///
/// -- size() is O(n) --
/// Note that as of this writing, list::size() is an O(n) operation when EASTL_LIST_SIZE_CACHE is disabled.
/// That is, getting the size of the list is not a fast operation, as it requires traversing the list and
/// counting the nodes. We could make list::size() be fast by having a member mSize variable. There are reasons
/// for having such functionality and reasons for not having such functionality. We currently choose
/// to not have a member mSize variable as it would add four bytes to the class, add a tiny amount
/// of processing to functions such as insert and erase, and would only serve to improve the size
/// function, but no others. The alternative argument is that the C++ standard states that std::list
/// should be an O(1) operation (i.e. have a member size variable), most C++ standard library list
/// implementations do so, the size is but an integer which is quick to update, and many users
/// expect to have a fast size function. The EASTL_LIST_SIZE_CACHE option changes this.
/// To consider: Make size caching an optional template parameter.
///
/// Pool allocation
/// If you want to make a custom memory pool for a list container, your pool
/// needs to contain items of type list::node_type. So if you have a memory
/// pool that has a constructor that takes the size of pool items and the
/// count of pool items, you would do this (assuming that MemoryPool implements
/// the Allocator interface):
/// typedef list<Widget, MemoryPool> WidgetList; // Delare your WidgetList type.
/// MemoryPool myPool(sizeof(WidgetList::node_type), 100); // Make a pool of 100 Widget nodes.
/// WidgetList myList(&myPool); // Create a list that uses the pool.
///
template <typename T, typename Allocator = EASTLAllocatorType>
class list : public ListBase<T, Allocator>
{
typedef ListBase<T, Allocator> base_type;
typedef list<T, Allocator> this_type;
protected:
using base_type::mNodeAllocator;
using base_type::DoAllocateNode;
using base_type::DoFreeNode;
using base_type::DoClear;
using base_type::DoInit;
#if EASTL_LIST_SIZE_CACHE
using base_type::mSize;
#endif
using base_type::internalNode;
using base_type::internalAllocator;
public:
typedef T value_type;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef ListIterator<T, T*, T&> iterator;
typedef ListIterator<T, const T*, const T&> const_iterator;
typedef eastl::reverse_iterator<iterator> reverse_iterator;
typedef eastl::reverse_iterator<const_iterator> const_reverse_iterator;
typedef typename base_type::size_type size_type;
typedef typename base_type::difference_type difference_type;
typedef typename base_type::allocator_type allocator_type;
typedef typename base_type::node_type node_type;
typedef typename base_type::base_node_type base_node_type;
using base_type::getAllocator;
public:
list();
list(const allocator_type& allocator);
explicit list(size_type n, const allocator_type& allocator = EASTL_LIST_DEFAULT_ALLOCATOR);
list(size_type n, const value_type& value, const allocator_type& allocator = EASTL_LIST_DEFAULT_ALLOCATOR);
list(const this_type& x);
list(const this_type& x, const allocator_type& allocator);
list(this_type&& x);
list(this_type&&, const allocator_type&);
list(std::initializer_list<value_type> ilist, const allocator_type& allocator = EASTL_LIST_DEFAULT_ALLOCATOR);
template <typename InputIterator>
list(InputIterator first, InputIterator last); // allocator arg removed because VC7.1 fails on the default arg. To do: Make a second version of this function without a default arg.
this_type& operator=(const this_type& x);
this_type& operator=(std::initializer_list<value_type> ilist);
this_type& operator=(this_type&& x);
// In the case that the two containers' allocators are unequal, swap copies elements instead
// of replacing them in place. In this case swap is an O(n) operation instead of O(1).
void swap(this_type& x);
void assign(size_type n, const value_type& value);
template <typename InputIterator> // It turns out that the C++ std::list specifies a two argument
void assign(InputIterator first, InputIterator last); // version of assign that takes (int size, int value). These are not
// iterators, so we need to do a template compiler trick to do the right thing.
void assign(std::initializer_list<value_type> ilist);
iterator begin() EASTL_NOEXCEPT;
const_iterator begin() const EASTL_NOEXCEPT;
const_iterator cbegin() const EASTL_NOEXCEPT;
iterator end() EASTL_NOEXCEPT;
const_iterator end() const EASTL_NOEXCEPT;
const_iterator cend() const EASTL_NOEXCEPT;
reverse_iterator rbegin() EASTL_NOEXCEPT;
const_reverse_iterator rbegin() const EASTL_NOEXCEPT;
const_reverse_iterator crbegin() const EASTL_NOEXCEPT;
reverse_iterator rend() EASTL_NOEXCEPT;
const_reverse_iterator rend() const EASTL_NOEXCEPT;
const_reverse_iterator crend() const EASTL_NOEXCEPT;
bool empty() const EASTL_NOEXCEPT;
size_type size() const EASTL_NOEXCEPT;
void resize(size_type n, const value_type& value);
void resize(size_type n);
reference front();
const_reference front() const;
reference back();
const_reference back() const;
template <typename... Args>
void emplace_front(Args&&... args);
template <typename... Args>
void emplace_back(Args&&... args);
void pushFront(const value_type& value);
void pushFront(value_type&& x);
reference pushFront();
void* pushFrontUninitialized();
void pushBack(const value_type& value);
void pushBack(value_type&& x);
reference pushBack();
void* pushBackUninitialized();
void popFront();
void popBack();
template <typename... Args>
iterator emplace(const_iterator position, Args&&... args);
iterator insert(const_iterator position);
iterator insert(const_iterator position, const value_type& value);
iterator insert(const_iterator position, value_type&& x);
iterator insert(const_iterator position, std::initializer_list<value_type> ilist);
iterator insert(const_iterator position, size_type n, const value_type& value);
template <typename InputIterator>
iterator insert(const_iterator position, InputIterator first, InputIterator last);
iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);
reverse_iterator erase(const_reverse_iterator position);
reverse_iterator erase(const_reverse_iterator first, const_reverse_iterator last);
void clear() EASTL_NOEXCEPT;
void reset_lose_memory() EASTL_NOEXCEPT; // This is a unilateral reset to an initially empty state. No destructors are called, no deallocation occurs.
size_type remove(const T& x);
template <typename Predicate>
size_type removeIf(Predicate);
void reverse() EASTL_NOEXCEPT;
// splice inserts elements in the range [first,last) before position and removes the elements from x.
// In the case that the two containers' allocators are unequal, splice copies elements
// instead of splicing them. In this case elements are not removed from x, and iterators
// into the spliced elements from x continue to point to the original values in x.
void splice(const_iterator position, this_type& x);
void splice(const_iterator position, this_type& x, const_iterator i);
void splice(const_iterator position, this_type& x, const_iterator first, const_iterator last);
void splice(const_iterator position, this_type&& x);
void splice(const_iterator position, this_type&& x, const_iterator i);
void splice(const_iterator position, this_type&& x, const_iterator first, const_iterator last);
public:
// For merge, see notes for splice regarding the handling of unequal allocators.
void merge(this_type& x);
void merge(this_type&& x);
template <typename Compare>
void merge(this_type& x, Compare compare);
template <typename Compare>
void merge(this_type&& x, Compare compare);
void unique();
template <typename BinaryPredicate>
void unique(BinaryPredicate);
// 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 sort();
template<typename Compare>
void sort(Compare compare);
public:
bool validate() const;
int validateIterator(const_iterator i) const;
protected:
node_type* DoCreateNode();
template<typename... Args>
node_type* DoCreateNode(Args&&... args);
template <typename Integer>
void DoAssign(Integer n, Integer value, true_type);
template <typename InputIterator>
void DoAssign(InputIterator first, InputIterator last, false_type);
void DoAssignValues(size_type n, const value_type& value);
template <typename Integer>
void DoInsert(ListNodeBase* pNode, Integer n, Integer value, true_type);
template <typename InputIterator>
void DoInsert(ListNodeBase* pNode, InputIterator first, InputIterator last, false_type);
void DoInsertValues(ListNodeBase* pNode, size_type n, const value_type& value);
template<typename... Args>
void DoInsertValue(ListNodeBase* pNode, Args&&... args);
void DoErase(ListNodeBase* pNode);
void DoSwap(this_type& x);
template <typename Compare>
iterator DoSort(iterator i1, iterator end2, size_type n, Compare& compare);
}; // class list
///////////////////////////////////////////////////////////////////////
// ListNodeBase
///////////////////////////////////////////////////////////////////////
// Swaps the nodes a and b in the lists to which they belong. This is similar to
// splicing a into b's list and b into a's list at the same time.
// Works by swapping the members of a and b, and fixes up the lists that a and b
// were part of to point to the new members.
inline void ListNodeBase::swap(ListNodeBase& a, ListNodeBase& b) EASTL_NOEXCEPT
{
const ListNodeBase temp(a);
a = b;
b = temp;
if(a.mpNext == &b)
a.mpNext = a.mpPrev = &a;
else
a.mpNext->mpPrev = a.mpPrev->mpNext = &a;
if(b.mpNext == &a)
b.mpNext = b.mpPrev = &b;
else
b.mpNext->mpPrev = b.mpPrev->mpNext = &b;
}
// splices the [first,last) range from its current list into our list before this node.
inline void ListNodeBase::splice(ListNodeBase* first, ListNodeBase* last) EASTL_NOEXCEPT
{
// We assume that [first, last] are not within our list.
last->mpPrev->mpNext = this;
first->mpPrev->mpNext = last;
this->mpPrev->mpNext = first;
ListNodeBase* const pTemp = this->mpPrev;
this->mpPrev = last->mpPrev;
last->mpPrev = first->mpPrev;
first->mpPrev = pTemp;
}
inline void ListNodeBase::reverse() EASTL_NOEXCEPT
{
ListNodeBase* pNode = this;
do
{
EA_ANALYSIS_ASSUME(pNode != NULL);
ListNodeBase* const pTemp = pNode->mpNext;
pNode->mpNext = pNode->mpPrev;
pNode->mpPrev = pTemp;
pNode = pNode->mpPrev;
}
while(pNode != this);
}
inline void ListNodeBase::insert(ListNodeBase* pNext) EASTL_NOEXCEPT
{
mpNext = pNext;
mpPrev = pNext->mpPrev;
pNext->mpPrev->mpNext = this;
pNext->mpPrev = this;
}
// Removes this node from the list that it's in. Assumes that the
// node is within a list and thus that its prev/next pointers are valid.
inline void ListNodeBase::remove() EASTL_NOEXCEPT
{
mpNext->mpPrev = mpPrev;
mpPrev->mpNext = mpNext;
}
// Inserts the standalone range [pFirst, pFinal] before pPosition. Assumes that the
// range is not within a list and thus that it's prev/next pointers are not valid.
// Assumes that this node is within a list and thus that its prev/next pointers are valid.
inline void ListNodeBase::insert_range(ListNodeBase* pFirst, ListNodeBase* pFinal) EASTL_NOEXCEPT
{
mpPrev->mpNext = pFirst;
pFirst->mpPrev = mpPrev;
mpPrev = pFinal;
pFinal->mpNext = this;
}
// Removes the range [pFirst, pFinal] from the list that it's in. Assumes that the
// range is within a list and thus that its prev/next pointers are valid.
inline void ListNodeBase::remove_range(ListNodeBase* pFirst, ListNodeBase* pFinal) EASTL_NOEXCEPT
{
pFinal->mpNext->mpPrev = pFirst->mpPrev;
pFirst->mpPrev->mpNext = pFinal->mpNext;
}
///////////////////////////////////////////////////////////////////////
// ListIterator
///////////////////////////////////////////////////////////////////////
template <typename T, typename Pointer, typename Reference>
inline ListIterator<T, Pointer, Reference>::ListIterator() EASTL_NOEXCEPT
: mpNode() // To consider: Do we really need to intialize mpNode?
{
// Empty
}
template <typename T, typename Pointer, typename Reference>
inline ListIterator<T, Pointer, Reference>::ListIterator(const ListNodeBase* pNode) EASTL_NOEXCEPT
: mpNode(const_cast<base_node_type*>(pNode))
{
// Empty
}
template <typename T, typename Pointer, typename Reference>
inline ListIterator<T, Pointer, Reference>::ListIterator(const iterator& x) EASTL_NOEXCEPT
: mpNode(const_cast<base_node_type*>(x.mpNode))
{
// Empty
}
template <typename T, typename Pointer, typename Reference>
inline typename ListIterator<T, Pointer, Reference>::this_type
ListIterator<T, Pointer, Reference>::next() const EASTL_NOEXCEPT
{
return ListIterator(mpNode->mpNext);
}
template <typename T, typename Pointer, typename Reference>
inline typename ListIterator<T, Pointer, Reference>::this_type
ListIterator<T, Pointer, Reference>::prev() const EASTL_NOEXCEPT
{
return ListIterator(mpNode->mpPrev);
}
template <typename T, typename Pointer, typename Reference>
inline typename ListIterator<T, Pointer, Reference>::reference
ListIterator<T, Pointer, Reference>::operator*() const EASTL_NOEXCEPT
{
return static_cast<node_type*>(mpNode)->mValue;
}
template <typename T, typename Pointer, typename Reference>
inline typename ListIterator<T, Pointer, Reference>::pointer
ListIterator<T, Pointer, Reference>::operator->() const EASTL_NOEXCEPT
{
return &static_cast<node_type*>(mpNode)->mValue;
}
template <typename T, typename Pointer, typename Reference>
inline typename ListIterator<T, Pointer, Reference>::this_type&
ListIterator<T, Pointer, Reference>::operator++() EASTL_NOEXCEPT
{
mpNode = mpNode->mpNext;
return *this;
}
template <typename T, typename Pointer, typename Reference>
inline typename ListIterator<T, Pointer, Reference>::this_type
ListIterator<T, Pointer, Reference>::operator++(int) EASTL_NOEXCEPT
{
this_type temp(*this);
mpNode = mpNode->mpNext;
return temp;
}
template <typename T, typename Pointer, typename Reference>
inline typename ListIterator<T, Pointer, Reference>::this_type&
ListIterator<T, Pointer, Reference>::operator--() EASTL_NOEXCEPT
{
mpNode = mpNode->mpPrev;
return *this;
}
template <typename T, typename Pointer, typename Reference>
inline typename ListIterator<T, Pointer, Reference>::this_type
ListIterator<T, Pointer, Reference>::operator--(int) EASTL_NOEXCEPT
{
this_type temp(*this);
mpNode = mpNode->mpPrev;
return temp;
}
// 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 ListIterator<T, PointerA, ReferenceA>& a,
const ListIterator<T, PointerB, ReferenceB>& b) EASTL_NOEXCEPT
{
return a.mpNode == b.mpNode;
}
template <typename T, typename PointerA, typename ReferenceA, typename PointerB, typename ReferenceB>
inline bool operator!=(const ListIterator<T, PointerA, ReferenceA>& a,
const ListIterator<T, PointerB, ReferenceB>& b) EASTL_NOEXCEPT
{
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 ListIterator<T, Pointer, Reference>& a,
const ListIterator<T, Pointer, Reference>& b) EASTL_NOEXCEPT
{
return a.mpNode != b.mpNode;
}
///////////////////////////////////////////////////////////////////////
// ListBase
///////////////////////////////////////////////////////////////////////
template <typename T, typename Allocator>
inline ListBase<T, Allocator>::ListBase()
: mNodeAllocator(base_node_type(), allocator_type(EASTL_LIST_DEFAULT_NAME))
#if EASTL_LIST_SIZE_CACHE
, mSize(0)
#endif
{
DoInit();
}
template <typename T, typename Allocator>
inline ListBase<T, Allocator>::ListBase(const allocator_type& allocator)
: mNodeAllocator(base_node_type(), allocator)
#if EASTL_LIST_SIZE_CACHE
, mSize(0)
#endif
{
DoInit();
}
template <typename T, typename Allocator>
inline ListBase<T, Allocator>::~ListBase()
{
DoClear();
}
template <typename T, typename Allocator>
const typename ListBase<T, Allocator>::allocator_type&
ListBase<T, Allocator>::getAllocator() const EASTL_NOEXCEPT
{
return internalAllocator();
}
template <typename T, typename Allocator>
typename ListBase<T, Allocator>::allocator_type&
ListBase<T, Allocator>::getAllocator() EASTL_NOEXCEPT
{
return internalAllocator();
}
template <typename T, typename Allocator>
inline void ListBase<T, Allocator>::setAllocator(const allocator_type& allocator)
{
EASTL_ASSERT((internalAllocator() == allocator) || (static_cast<node_type*>(internalNode().mpNext) == &internalNode())); // We can only assign a different allocator if we are empty of elements.
internalAllocator() = allocator;
}
template <typename T, typename Allocator>
inline typename ListBase<T, Allocator>::node_type*
ListBase<T, Allocator>::DoAllocateNode()
{
node_type* pNode = (node_type*)allocate_memory(internalAllocator(), sizeof(node_type), EASTL_ALIGN_OF(node_type), 0);
EASTL_ASSERT(pNode != nullptr);
return pNode;
}
template <typename T, typename Allocator>
inline void ListBase<T, Allocator>::DoFreeNode(node_type* p)
{
EASTLFree(internalAllocator(), p, sizeof(node_type));
}
template <typename T, typename Allocator>
inline void ListBase<T, Allocator>::DoInit() EASTL_NOEXCEPT
{
internalNode().mpNext = &internalNode();
internalNode().mpPrev = &internalNode();
}
template <typename T, typename Allocator>
inline void ListBase<T, Allocator>::DoClear()
{
base_node_type* p = internalNode().mpNext;
while(p != &internalNode())
{
node_type* const pTemp = static_cast<node_type*>(p);
p = p->mpNext;
pTemp->~node_type();
EASTLFree(internalAllocator(), pTemp, sizeof(node_type));
}
}
///////////////////////////////////////////////////////////////////////
// list
///////////////////////////////////////////////////////////////////////
template <typename T, typename Allocator>
inline list<T, Allocator>::list()
: base_type()
{
// Empty
}
template <typename T, typename Allocator>
inline list<T, Allocator>::list(const allocator_type& allocator)
: base_type(allocator)
{
// Empty
}
template <typename T, typename Allocator>
inline list<T, Allocator>::list(size_type n, const allocator_type& allocator)
: base_type(allocator)
{
DoInsertValues(&internalNode(), n, value_type());
}
template <typename T, typename Allocator>
inline list<T, Allocator>::list(size_type n, const value_type& value, const allocator_type& allocator)
: base_type(allocator)
{
DoInsertValues(&internalNode(), n, value);
}
template <typename T, typename Allocator>
inline list<T, Allocator>::list(const this_type& x)
: base_type(x.internalAllocator())
{
DoInsert(&internalNode(), const_iterator(x.internalNode().mpNext), const_iterator(&x.internalNode()), false_type());
}
template <typename T, typename Allocator>
inline list<T, Allocator>::list(const this_type& x, const allocator_type& allocator)
: base_type(allocator)
{
DoInsert(&internalNode(), const_iterator(x.internalNode().mpNext), const_iterator(&x.internalNode()), false_type());
}
template <typename T, typename Allocator>
inline list<T, Allocator>::list(this_type&& x)
: base_type(eastl::move(x.internalAllocator()))
{
swap(x);
}
template <typename T, typename Allocator>
inline list<T, Allocator>::list(this_type&& x, const allocator_type& allocator)
: base_type(allocator)
{
swap(x); // member swap handles the case that x has a different allocator than our allocator by doing a copy.
}
template <typename T, typename Allocator>
inline list<T, Allocator>::list(std::initializer_list<value_type> ilist, const allocator_type& allocator)
: base_type(allocator)
{
DoInsert(&internalNode(), ilist.begin(), ilist.end(), false_type());
}
template <typename T, typename Allocator>
template <typename InputIterator>
list<T, Allocator>::list(InputIterator first, InputIterator last)
: base_type(EASTL_LIST_DEFAULT_ALLOCATOR)
{
//insert(const_iterator(&internalNode()), first, last);
DoInsert(&internalNode(), first, last, is_integral<InputIterator>());
}
template <typename T, typename Allocator>
typename list<T, Allocator>::iterator
inline list<T, Allocator>::begin() EASTL_NOEXCEPT
{
return iterator(internalNode().mpNext);
}
template <typename T, typename Allocator>
inline typename list<T, Allocator>::const_iterator
list<T, Allocator>::begin() const EASTL_NOEXCEPT
{
return const_iterator(internalNode().mpNext);
}
template <typename T, typename Allocator>
inline typename list<T, Allocator>::const_iterator
list<T, Allocator>::cbegin() const EASTL_NOEXCEPT
{
return const_iterator(internalNode().mpNext);
}
template <typename T, typename Allocator>
inline typename list<T, Allocator>::iterator
list<T, Allocator>::end() EASTL_NOEXCEPT
{
return iterator(&internalNode());
}
template <typename T, typename Allocator>
inline typename list<T, Allocator>::const_iterator
list<T, Allocator>::end() const EASTL_NOEXCEPT
{
return const_iterator(&internalNode());
}
template <typename T, typename Allocator>
inline typename list<T, Allocator>::const_iterator
list<T, Allocator>::cend() const EASTL_NOEXCEPT
{
return const_iterator(&internalNode());
}
template <typename T, typename Allocator>
inline typename list<T, Allocator>::reverse_iterator
list<T, Allocator>::rbegin() EASTL_NOEXCEPT
{
return reverse_iterator(&internalNode());
}
template <typename T, typename Allocator>
inline typename list<T, Allocator>::const_reverse_iterator
list<T, Allocator>::rbegin() const EASTL_NOEXCEPT
{
return const_reverse_iterator(&internalNode());
}
template <typename T, typename Allocator>
inline typename list<T, Allocator>::const_reverse_iterator
list<T, Allocator>::crbegin() const EASTL_NOEXCEPT
{
return const_reverse_iterator(&internalNode());
}
template <typename T, typename Allocator>
inline typename list<T, Allocator>::reverse_iterator
list<T, Allocator>::rend() EASTL_NOEXCEPT
{
return reverse_iterator(internalNode().mpNext);
}
template <typename T, typename Allocator>
inline typename list<T, Allocator>::const_reverse_iterator
list<T, Allocator>::rend() const EASTL_NOEXCEPT
{
return const_reverse_iterator(internalNode().mpNext);
}
template <typename T, typename Allocator>
inline typename list<T, Allocator>::const_reverse_iterator
list<T, Allocator>::crend() const EASTL_NOEXCEPT
{
return const_reverse_iterator(internalNode().mpNext);
}
template <typename T, typename Allocator>
inline typename list<T, Allocator>::reference
list<T, Allocator>::front()
{
#if EASTL_ASSERT_ENABLED && EASTL_EMPTY_REFERENCE_ASSERT_ENABLED
if (EASTL_UNLIKELY(static_cast<node_type*>(internalNode().mpNext) == &internalNode()))
EASTL_FAIL_MSG("list::front -- empty container");
#else
// We allow the user to reference an empty container.
#endif
return static_cast<node_type*>(internalNode().mpNext)->mValue;
}
template <typename T, typename Allocator>
inline typename list<T, Allocator>::const_reference
list<T, Allocator>::front() const
{
#if EASTL_ASSERT_ENABLED && EASTL_EMPTY_REFERENCE_ASSERT_ENABLED
if (EASTL_UNLIKELY(static_cast<node_type*>(internalNode().mpNext) == &internalNode()))
EASTL_FAIL_MSG("list::front -- empty container");
#else
// We allow the user to reference an empty container.
#endif
return static_cast<node_type*>(internalNode().mpNext)->mValue;
}
template <typename T, typename Allocator>
inline typename list<T, Allocator>::reference
list<T, Allocator>::back()
{
#if EASTL_ASSERT_ENABLED && EASTL_EMPTY_REFERENCE_ASSERT_ENABLED
if (EASTL_UNLIKELY(static_cast<node_type*>(internalNode().mpNext) == &internalNode()))
EASTL_FAIL_MSG("list::back -- empty container");
#else
// We allow the user to reference an empty container.
#endif
return static_cast<node_type*>(internalNode().mpPrev)->mValue;
}
template <typename T, typename Allocator>
inline typename list<T, Allocator>::const_reference
list<T, Allocator>::back() const