forked from questor/eastl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslist.h
2121 lines (1676 loc) · 66.4 KB
/
slist.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.
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// An slist is a singly-linked list. The C++ standard library doesn't define
// such a thing as an slist, nor does the C++ TR1. Our implementation of slist
// largely follows the design of the SGI STL slist container, which is also
// found in STLPort. Singly-linked lists use less memory than doubly-linked
// lists, but are less flexible.
//
// In looking at slist, you will notice a lot of references to things like
// 'before first', 'before last', 'insert after', and 'erase after'. This is
// due to the fact that std::list insert and erase works on the node before
// the referenced node, whereas slist is singly linked and operations are only
// efficient if they work on the node after the referenced node. This is because
// with an slist node you know the node after it but not the node before it.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_SLIST_H
#define EASTL_SLIST_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/sort.h>
#include <stddef.h>
#ifdef _MSC_VER
#pragma warning(push, 0)
#include <new>
#pragma warning(pop)
#else
#include <new>
#endif
EA_DISABLE_SN_WARNING(828); // The EDG SN compiler has a bug in its handling of variadic template arguments and mistakenly reports "parameter "args" was never referenced"
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4530) // C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
#pragma warning(disable: 4345) // Behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized
#pragma warning(disable: 4571) // catch(...) semantics changed since Visual C++ 7.1; structured exceptions (SEH) are no longer caught.
#endif
#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_SLIST_DEFAULT_NAME
///
/// Defines a default container name in the absence of a user-provided name.
///
#ifndef EASTL_SLIST_DEFAULT_NAME
#define EASTL_SLIST_DEFAULT_NAME EASTL_DEFAULT_NAME_PREFIX " slist" // Unless the user overrides something, this is "EASTL slist".
#endif
/// EASTL_SLIST_DEFAULT_ALLOCATOR
///
#ifndef EASTL_SLIST_DEFAULT_ALLOCATOR
#define EASTL_SLIST_DEFAULT_ALLOCATOR allocator_type(EASTL_SLIST_DEFAULT_NAME)
#endif
/// SListNodeBase
///
/// This is a standalone struct so that operations on it can be done without templates
/// and so that an empty slist can have an SListNodeBase and thus not create any
/// instances of T.
///
struct SListNodeBase
{
SListNodeBase* mpNext;
} EASTL_LIST_PROXY_MAY_ALIAS;
#if EASTL_LIST_PROXY_ENABLED
/// SListNodeBaseProxy
///
/// In debug builds, we define SListNodeBaseProxy to be the same thing as
/// SListNodeBase, except it is templated on the parent SListNode class.
/// We do this because we want users in debug builds to be able to easily
/// view the slist's contents in a debugger GUI. We do this only in a debug
/// build for the reasons described above: that SListNodeBase needs to be
/// as efficient as possible and not cause code bloat or extra function
/// calls (inlined or not).
///
/// SListNodeBaseProxy *must* be separate from its parent class SListNode
/// because the slist class must have a member node which contains no T value.
/// It is thus incorrect for us to have one single SListNode class which
/// has both mpNext and mValue. So we do a recursive template trick in the
/// definition and use of SListNodeBaseProxy.
///
template <typename SLN>
struct SListNodeBaseProxy
{
SLN* mpNext;
};
template <typename T>
struct SListNode : public SListNodeBaseProxy< SListNode<T> >
{
T mValue;
};
#else
template <typename T>
struct SListNode : public SListNodeBase
{
T mValue;
};
#endif
/// SListIterator
///
template <typename T, typename Pointer, typename Reference>
struct SListIterator
{
typedef SListIterator<T, Pointer, Reference> this_type;
typedef SListIterator<T, T*, T&> iterator;
typedef SListIterator<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 uint32_t.
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef SListNode<T> node_type;
typedef Pointer pointer;
typedef Reference reference;
typedef EASTL_ITC_NS::forward_iterator_tag iterator_category;
public:
node_type* mpNode;
public:
SListIterator();
SListIterator(const SListNodeBase* pNode);
SListIterator(const iterator& x);
reference operator*() const;
pointer operator->() const;
this_type& operator++();
this_type operator++(int);
};
/// SListBase
///
/// See VectorBase (class vector) for an explanation of why we
/// create this separate base class.
///
template <typename T, typename Allocator>
struct SListBase
{
public:
typedef Allocator allocator_type;
typedef SListNode<T> node_type;
typedef eastl_size_t size_type; // See config.h for the definition of eastl_size_t, which defaults to uint32_t.
typedef ptrdiff_t difference_type;
#if EASTL_LIST_PROXY_ENABLED
typedef SListNodeBaseProxy< SListNode<T> > base_node_type;
#else
typedef SListNodeBase base_node_type; // We use SListNodeBase instead of SListNode<T> because we don't want to create a T.
#endif
protected:
base_node_type mNode;
#if EASTL_SLIST_SIZE_CACHE
size_type mSize;
#endif
allocator_type mAllocator; // To do: Use base class optimization to make this go away.
public:
const allocator_type& getAllocator() const EASTL_NOEXCEPT;
allocator_type& getAllocator() EASTL_NOEXCEPT;
void setAllocator(const allocator_type& allocator);
protected:
SListBase();
SListBase(const allocator_type& a);
~SListBase();
node_type* DoAllocateNode();
void DoFreeNode(node_type* pNode);
SListNodeBase* DoEraseAfter(SListNodeBase* pNode);
SListNodeBase* DoEraseAfter(SListNodeBase* pNode, SListNodeBase* pNodeLast);
}; // class SListBase
/// slist
///
/// This is the equivalent of C++11's forward_list.
///
/// -- size() is O(n) --
/// Note that as of this writing, list::size() is an O(n) operation when EASTL_SLIST_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_SLIST_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 slist::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 slist<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 slist : public SListBase<T, Allocator>
{
typedef SListBase<T, Allocator> base_type;
typedef slist<T, Allocator> this_type;
public:
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef SListIterator<T, T*, T&> iterator;
typedef SListIterator<T, const T*, const T&> const_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::mNode;
using base_type::mAllocator;
using base_type::DoEraseAfter;
using base_type::DoAllocateNode;
using base_type::DoFreeNode;
#if EASTL_SLIST_SIZE_CACHE
using base_type::mSize;
#endif
public:
slist();
slist(const allocator_type& allocator);
explicit slist(size_type n, const allocator_type& allocator = EASTL_SLIST_DEFAULT_ALLOCATOR);
slist(size_type n, const value_type& value, const allocator_type& allocator = EASTL_SLIST_DEFAULT_ALLOCATOR);
slist(const this_type& x);
slist(std::initializer_list<value_type> ilist, const allocator_type& allocator = EASTL_SLIST_DEFAULT_ALLOCATOR);
#if EASTL_MOVE_SEMANTICS_ENABLED
slist(this_type&& x);
slist(this_type&& x, const allocator_type& allocator);
#endif
template <typename InputIterator>
slist(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>);
#if EASTL_MOVE_SEMANTICS_ENABLED
this_type& operator=(this_type&& x);
#endif
void swap(this_type& x);
void assign(size_type n, const value_type& value);
void assign(std::initializer_list<value_type> ilist);
template <typename InputIterator>
void assign(InputIterator first, InputIterator last);
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;
iterator before_begin() EASTL_NOEXCEPT;
const_iterator before_begin() const EASTL_NOEXCEPT;
const_iterator cbefore_begin() const EASTL_NOEXCEPT;
iterator previous(const_iterator position);
const_iterator previous(const_iterator position) const;
reference front();
const_reference front() const;
#if EASTL_MOVE_SEMANTICS_ENABLED && EASTL_VARIADIC_TEMPLATES_ENABLED
template <class... Args>
void emplace_front(Args&&... args);
#else
#if EASTL_MOVE_SEMANTICS_ENABLED
void emplace_front(value_type&& value);
#endif
void emplace_front(const value_type& value);
#endif
void pushFront(const value_type& value);
reference pushFront();
#if EASTL_MOVE_SEMANTICS_ENABLED
void pushFront(value_type&& value);
#endif
void popFront();
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);
iterator insert(const_iterator position);
iterator insert(const_iterator position, const value_type& value);
void insert(const_iterator position, size_type n, const value_type& value);
template <typename InputIterator>
void insert(const_iterator position, InputIterator first, InputIterator last);
// Returns an iterator pointing to the last inserted element, or position if insertion count is zero.
iterator insert_after(const_iterator position);
iterator insert_after(const_iterator position, const value_type& value);
iterator insert_after(const_iterator position, size_type n, const value_type& value);
iterator insert_after(const_iterator position, std::initializer_list<value_type> ilist);
#if EASTL_MOVE_SEMANTICS_ENABLED
iterator insert_after(const_iterator position, value_type&& value);
#endif
#if EASTL_MOVE_SEMANTICS_ENABLED && EASTL_VARIADIC_TEMPLATES_ENABLED
template <class... Args>
iterator emplace_after(const_iterator position, Args&&... args);
#else
#if EASTL_MOVE_SEMANTICS_ENABLED
iterator emplace_after(const_iterator position, value_type&& value);
#endif
iterator emplace_after(const_iterator position, const value_type& value);
#endif
template <typename InputIterator>
iterator insert_after(const_iterator position, InputIterator first, InputIterator last);
iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);
iterator erase_after(const_iterator position);
iterator erase_after(const_iterator before_first, const_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.
void remove(const value_type& value);
template <typename Predicate>
void removeIf(Predicate predicate);
void reverse() EASTL_NOEXCEPT;
// splice splices to before position, like with the list container. However, in order to do so
// it must walk the list from beginning to position, which is an O(n) operation that can thus
// be slow. It's recommended that the splice_after functions be used whenever possible as they are O(1).
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);
#if EASTL_MOVE_SEMANTICS_ENABLED
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);
#endif
void splice_after(const_iterator position, this_type& x);
void splice_after(const_iterator position, this_type& x, const_iterator i);
void splice_after(const_iterator position, this_type& x, const_iterator first, const_iterator last);
#if EASTL_MOVE_SEMANTICS_ENABLED
void splice_after(const_iterator position, this_type&& x);
void splice_after(const_iterator position, this_type&& x, const_iterator i);
void splice_after(const_iterator position, this_type&& x, const_iterator first, const_iterator last);
#endif
// The following splice_after funcions are deprecated, as they don't allow for recognizing
// the allocator, cannot maintain the source mSize, and are not in the C++11 Standard definition
// of std::forward_list (which is the equivalent of this class).
void splice_after(const_iterator position, const_iterator before_first, const_iterator before_last); // before_first and before_last come from a source container.
void splice_after(const_iterator position, const_iterator previous); // previous comes from a source container.
// 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 <class Compare>
void sort(Compare compare);
// Not yet implemented:
// void merge(this_type& x);
// void merge(this_type&& x);
// template <class Compare>
// void merge(this_type& x, Compare compare);
// template <class Compare>
// void merge(this_type&& x, Compare compare);
// If these get implemented then make sure to override them in fixed_slist.
bool validate() const;
int validateIterator(const_iterator i) const;
#if EASTL_RESET_ENABLED
void reset() EASTL_NOEXCEPT; // This function name is deprecated; use reset_lose_memory instead.
#endif
protected:
node_type* DoCreateNode();
#if EASTL_MOVE_SEMANTICS_ENABLED && EASTL_VARIADIC_TEMPLATES_ENABLED // If we can do variadic arguments...
template<typename... Args>
node_type* DoCreateNode(Args&&... args);
#else
#if EASTL_MOVE_SEMANTICS_ENABLED
node_type* DoCreateNode(value_type&& value);
#endif
node_type* DoCreateNode(const value_type& value);
#endif
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 InputIterator>
node_type* DoInsertAfter(SListNodeBase* pNode, InputIterator first, InputIterator last);
template <typename Integer>
node_type* DoInsertAfter(SListNodeBase* pNode, Integer n, Integer value, true_type);
template <typename InputIterator>
node_type* DoInsertAfter(SListNodeBase* pNode, InputIterator first, InputIterator last, false_type);
node_type* DoInsertValueAfter(SListNodeBase* pNode);
node_type* DoInsertValuesAfter(SListNodeBase* pNode, size_type n, const value_type& value);
#if EASTL_MOVE_SEMANTICS_ENABLED && EASTL_VARIADIC_TEMPLATES_ENABLED // If we can do variadic arguments...
template<typename... Args>
node_type* DoInsertValueAfter(SListNodeBase* pNode, Args&&... args);
#else
#if EASTL_MOVE_SEMANTICS_ENABLED
node_type* DoInsertValueAfter(SListNodeBase* pNode, value_type&& value);
#endif
node_type* DoInsertValueAfter(SListNodeBase* pNode, const value_type& value);
#endif
void DoSwap(this_type& x);
}; // class slist
///////////////////////////////////////////////////////////////////////
// SListNodeBase functions
///////////////////////////////////////////////////////////////////////
inline SListNodeBase* SListNodeInsertAfter(SListNodeBase* pPrevNode, SListNodeBase* pNode)
{
pNode->mpNext = pPrevNode->mpNext;
pPrevNode->mpNext = pNode;
return pNode;
}
inline SListNodeBase* SListNodeGetPrevious(SListNodeBase* pNodeBase, const SListNodeBase* pNode)
{
while(pNodeBase && (pNodeBase->mpNext != pNode))
pNodeBase = pNodeBase->mpNext;
return pNodeBase;
}
inline const SListNodeBase* SListNodeGetPrevious(const SListNodeBase* pNodeBase, const SListNodeBase* pNode)
{
while(pNodeBase && (pNodeBase->mpNext != pNode))
pNodeBase = pNodeBase->mpNext;
return pNodeBase;
}
inline void SListNodeSpliceAfter(SListNodeBase* pNode, SListNodeBase* pNodeBeforeFirst, SListNodeBase* pNodeBeforeLast)
{
if((pNode != pNodeBeforeFirst) && (pNode != pNodeBeforeLast))
{
SListNodeBase* const pFirst = pNodeBeforeFirst->mpNext;
SListNodeBase* const pPosition = pNode->mpNext;
pNodeBeforeFirst->mpNext = pNodeBeforeLast->mpNext;
pNode->mpNext = pFirst;
pNodeBeforeLast->mpNext = pPosition;
}
}
inline void SListNodeSpliceAfter(SListNodeBase* pNode, SListNodeBase* pNodeBase)
{
SListNodeBase* const pNodeBeforeLast = SListNodeGetPrevious(pNodeBase, NULL);
if(pNodeBeforeLast != pNodeBase)
{
SListNodeBase* const pPosition = pNode->mpNext;
pNode->mpNext = pNodeBase->mpNext;
pNodeBase->mpNext = NULL;
pNodeBeforeLast->mpNext = pPosition;
}
}
inline SListNodeBase* SListNodeReverse(SListNodeBase* pNode)
{
SListNodeBase* pNodeFirst = pNode;
pNode = pNode->mpNext;
pNodeFirst->mpNext = NULL;
while(pNode)
{
SListNodeBase* const pTemp = pNode->mpNext;
pNode->mpNext = pNodeFirst;
pNodeFirst = pNode;
pNode = pTemp;
}
return pNodeFirst;
}
inline uint32_t SListNodeGetSize(SListNodeBase* pNode)
{
uint32_t n = 0;
while(pNode)
{
++n;
pNode = pNode->mpNext;
}
return n;
}
///////////////////////////////////////////////////////////////////////
// SListIterator functions
///////////////////////////////////////////////////////////////////////
template <typename T, typename Pointer, typename Reference>
inline SListIterator<T, Pointer, Reference>::SListIterator()
: mpNode(NULL)
{
// Empty
}
template <typename T, typename Pointer, typename Reference>
inline SListIterator<T, Pointer, Reference>::SListIterator(const SListNodeBase* pNode)
: mpNode(static_cast<node_type*>((SListNode<T>*)const_cast<SListNodeBase*>(pNode))) // All this casting is in the name of making runtime debugging much easier on the user.
{
// Empty
}
template <typename T, typename Pointer, typename Reference>
inline SListIterator<T, Pointer, Reference>::SListIterator(const iterator& x)
: mpNode(const_cast<node_type*>(x.mpNode))
{
// Empty
}
template <typename T, typename Pointer, typename Reference>
inline typename SListIterator<T, Pointer, Reference>::reference
SListIterator<T, Pointer, Reference>::operator*() const
{
return mpNode->mValue;
}
template <typename T, typename Pointer, typename Reference>
inline typename SListIterator<T, Pointer, Reference>::pointer
SListIterator<T, Pointer, Reference>::operator->() const
{
return &mpNode->mValue;
}
template <typename T, typename Pointer, typename Reference>
inline typename SListIterator<T, Pointer, Reference>::this_type&
SListIterator<T, Pointer, Reference>::operator++()
{
mpNode = static_cast<node_type*>(mpNode->mpNext);
return *this;
}
template <typename T, typename Pointer, typename Reference>
inline typename SListIterator<T, Pointer, Reference>::this_type
SListIterator<T, Pointer, Reference>::operator++(int)
{
this_type temp(*this);
mpNode = static_cast<node_type*>(mpNode->mpNext);
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 SListIterator<T, PointerA, ReferenceA>& a,
const SListIterator<T, PointerB, ReferenceB>& b)
{
return a.mpNode == b.mpNode;
}
template <typename T, typename PointerA, typename ReferenceA, typename PointerB, typename ReferenceB>
inline bool operator!=(const SListIterator<T, PointerA, ReferenceA>& a,
const SListIterator<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 SListIterator<T, Pointer, Reference>& a,
const SListIterator<T, Pointer, Reference>& b)
{
return a.mpNode != b.mpNode;
}
///////////////////////////////////////////////////////////////////////
// SListBase functions
///////////////////////////////////////////////////////////////////////
template <typename T, typename Allocator>
inline SListBase<T, Allocator>::SListBase()
: mNode(),
#if EASTL_SLIST_SIZE_CACHE
mSize(0),
#endif
mAllocator(EASTL_SLIST_DEFAULT_NAME)
{
mNode.mpNext = NULL;
}
template <typename T, typename Allocator>
inline SListBase<T, Allocator>::SListBase(const allocator_type& allocator)
: mNode(),
#if EASTL_SLIST_SIZE_CACHE
mSize(0),
#endif
mAllocator(allocator)
{
mNode.mpNext = NULL;
}
template <typename T, typename Allocator>
inline SListBase<T, Allocator>::~SListBase()
{
DoEraseAfter((SListNodeBase*)&mNode, NULL);
}
template <typename T, typename Allocator>
inline const typename SListBase<T, Allocator>::allocator_type&
SListBase<T, Allocator>::getAllocator() const EASTL_NOEXCEPT
{
return mAllocator;
}
template <typename T, typename Allocator>
inline typename SListBase<T, Allocator>::allocator_type&
SListBase<T, Allocator>::getAllocator() EASTL_NOEXCEPT
{
return mAllocator;
}
template <typename T, typename Allocator>
void
SListBase<T, Allocator>::setAllocator(const allocator_type& allocator)
{
EASTL_ASSERT((mAllocator == allocator) || (static_cast<node_type*>(mNode.mpNext) == NULL)); // We can only assign a different allocator if we are empty of elements.
mAllocator = allocator;
}
template <typename T, typename Allocator>
inline SListNode<T>* SListBase<T, Allocator>::DoAllocateNode()
{
return (node_type*)allocate_memory(mAllocator, sizeof(node_type), EASTL_ALIGN_OF(T), 0);
}
template <typename T, typename Allocator>
inline void SListBase<T, Allocator>::DoFreeNode(node_type* pNode)
{
EASTLFree(mAllocator, pNode, sizeof(node_type));
}
template <typename T, typename Allocator>
SListNodeBase* SListBase<T, Allocator>::DoEraseAfter(SListNodeBase* pNode)
{
node_type* const pNodeNext = static_cast<node_type*>((base_node_type*)pNode->mpNext);
SListNodeBase* const pNodeNextNext = (SListNodeBase*)pNodeNext->mpNext;
pNode->mpNext = pNodeNextNext;
pNodeNext->~node_type();
DoFreeNode(pNodeNext);
#if EASTL_SLIST_SIZE_CACHE
--mSize;
#endif
return pNodeNextNext;
}
template <typename T, typename Allocator>
SListNodeBase* SListBase<T, Allocator>::DoEraseAfter(SListNodeBase* pNode, SListNodeBase* pNodeLast)
{
node_type* pNodeCurrent = static_cast<node_type*>((base_node_type*)pNode->mpNext);
while(pNodeCurrent != (base_node_type*)pNodeLast)
{
node_type* const pNodeTemp = pNodeCurrent;
pNodeCurrent = static_cast<node_type*>((base_node_type*)pNodeCurrent->mpNext);
pNodeTemp->~node_type();
DoFreeNode(pNodeTemp);
#if EASTL_SLIST_SIZE_CACHE
--mSize;
#endif
}
pNode->mpNext = pNodeLast;
return pNodeLast;
}
///////////////////////////////////////////////////////////////////////
// slist functions
///////////////////////////////////////////////////////////////////////
template <typename T, typename Allocator>
inline slist<T, Allocator>::slist()
: base_type()
{
// Empty
}
template <typename T, typename Allocator>
inline slist<T, Allocator>::slist(const allocator_type& allocator)
: base_type(allocator)
{
// Empty
}
template <typename T, typename Allocator>
inline slist<T, Allocator>::slist(size_type n, const allocator_type& allocator)
: base_type(allocator)
{
DoInsertValuesAfter((SListNodeBase*)&mNode, n, value_type());
}
template <typename T, typename Allocator>
inline slist<T, Allocator>::slist(size_type n, const value_type& value, const allocator_type& allocator)
: base_type(allocator)
{
DoInsertValuesAfter((SListNodeBase*)&mNode, n, value);
}
template <typename T, typename Allocator>
inline slist<T, Allocator>::slist(const slist& x)
: base_type(x.mAllocator)
{
DoInsertAfter((SListNodeBase*)&mNode, const_iterator((SListNodeBase*)x.mNode.mpNext), const_iterator(NULL), false_type());
}
#if EASTL_MOVE_SEMANTICS_ENABLED
template <typename T, typename Allocator>
slist<T, Allocator>::slist(this_type&& x)
: base_type(x.mAllocator)
{
swap(x);
}
template <typename T, typename Allocator>
slist<T, Allocator>::slist(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.
}
#endif
template <typename T, typename Allocator>
inline slist<T, Allocator>::slist(std::initializer_list<value_type> ilist, const allocator_type& allocator)
: base_type(allocator)
{
DoInsertAfter((SListNodeBase*)&mNode, ilist.begin(), ilist.end());
}
template <typename T, typename Allocator>
template <typename InputIterator>
inline slist<T, Allocator>::slist(InputIterator first, InputIterator last)
: base_type(EASTL_SLIST_DEFAULT_ALLOCATOR)
{
DoInsertAfter((SListNodeBase*)&mNode, first, last);
}
template <typename T, typename Allocator>
inline typename slist<T, Allocator>::iterator
slist<T, Allocator>::begin() EASTL_NOEXCEPT
{
return iterator((SListNodeBase*)mNode.mpNext);
}
template <typename T, typename Allocator>
inline typename slist<T, Allocator>::const_iterator
slist<T, Allocator>::begin() const EASTL_NOEXCEPT
{
return const_iterator((SListNodeBase*)mNode.mpNext);
}
template <typename T, typename Allocator>
inline typename slist<T, Allocator>::const_iterator
slist<T, Allocator>::cbegin() const EASTL_NOEXCEPT
{
return const_iterator((SListNodeBase*)mNode.mpNext);
}
template <typename T, typename Allocator>
inline typename slist<T, Allocator>::iterator
slist<T, Allocator>::end() EASTL_NOEXCEPT
{
return iterator(NULL);
}
template <typename T, typename Allocator>
inline typename slist<T, Allocator>::const_iterator
slist<T, Allocator>::end() const EASTL_NOEXCEPT
{
return const_iterator(NULL);
}
template <typename T, typename Allocator>
inline typename slist<T, Allocator>::const_iterator
slist<T, Allocator>::cend() const EASTL_NOEXCEPT
{
return const_iterator(NULL);
}
template <typename T, typename Allocator>
inline typename slist<T, Allocator>::iterator
slist<T, Allocator>::before_begin() EASTL_NOEXCEPT
{
return iterator((SListNodeBase*)&mNode);
}
template <typename T, typename Allocator>
inline typename slist<T, Allocator>::const_iterator
slist<T, Allocator>::before_begin() const EASTL_NOEXCEPT
{
return const_iterator((SListNodeBase*)&mNode);
}
template <typename T, typename Allocator>
inline typename slist<T, Allocator>::const_iterator
slist<T, Allocator>::cbefore_begin() const EASTL_NOEXCEPT
{
return const_iterator((SListNodeBase*)&mNode);
}
template <typename T, typename Allocator>
inline typename slist<T, Allocator>::iterator
slist<T, Allocator>::previous(const_iterator position)
{
return iterator(SListNodeGetPrevious((SListNodeBase*)&mNode, (SListNodeBase*)position.mpNode));
}
template <typename T, typename Allocator>
inline typename slist<T, Allocator>::const_iterator
slist<T, Allocator>::previous(const_iterator position) const
{
return const_iterator(SListNodeGetPrevious((SListNodeBase*)&mNode, (SListNodeBase*)position.mpNode));
}
template <typename T, typename Allocator>
inline typename slist<T, Allocator>::reference
slist<T, Allocator>::front()
{
#if EASTL_ASSERT_ENABLED
if(EASTL_UNLIKELY(mNode.mpNext == NULL))
EASTL_FAIL_MSG("slist::front -- empty container");
#endif
EA_ANALYSIS_ASSUME(mNode.mpNext != NULL);
return ((node_type*)mNode.mpNext)->mValue;
}
template <typename T, typename Allocator>
inline typename slist<T, Allocator>::const_reference
slist<T, Allocator>::front() const
{
#if EASTL_ASSERT_ENABLED
if(EASTL_UNLIKELY(mNode.mpNext == NULL))
EASTL_FAIL_MSG("slist::front -- empty container");
#endif
EA_ANALYSIS_ASSUME(mNode.mpNext != NULL);
return static_cast<node_type*>(mNode.mpNext)->mValue;
}
#if EASTL_MOVE_SEMANTICS_ENABLED && EASTL_VARIADIC_TEMPLATES_ENABLED
template <typename T, typename Allocator>
template <class... Args>
void slist<T, Allocator>::emplace_front(Args&&... args)
{
DoInsertValueAfter((SListNodeBase*)&mNode, eastl::forward<Args>(args)...);
}
#else
#if EASTL_MOVE_SEMANTICS_ENABLED
template <typename T, typename Allocator>
void slist<T, Allocator>::emplace_front(value_type&& value)
{
DoInsertValueAfter((SListNodeBase*)&mNode, eastl::move(value));
}
#endif
template <typename T, typename Allocator>
void slist<T, Allocator>::emplace_front(const value_type& value)
{
DoInsertValueAfter((SListNodeBase*)&mNode, value);
}
#endif
template <typename T, typename Allocator>
inline void slist<T, Allocator>::pushFront(const value_type& value)
{
SListNodeInsertAfter((SListNodeBase*)&mNode, (SListNodeBase*)DoCreateNode(value));