-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.h
1151 lines (1016 loc) · 32.1 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
/* THOR - THOR Template Library
* Joshua M. Kriegshauser
*
* list.h
*
* This file defines an STL-compatible linked list container.
*
* Extensions/Changes:
* - Debug builds check that all iterators are valid (i.e. no trying to erase using an iterator from a different container).
* - push_back() has changed but remains compatible with STL usage:
* * push_back() returns a reference to the added item
* * push_back() with zero parameters will default-construct an element
* * push_back() variations exist with 1-4 parameters that will in-place
* construct an element without necessarily needing a copy constructor.
* * push_back_placement() can be used with placement new to construct
* elements with more than 4 parameters.
* - push_front() has changed but remains compatible with STL usage:
* * push_front() returns a reference to the added item
* * push_front() with zero parameters will default-construct an element
* * push_front() variations exist with 1-4 parameters that will in-place
* construct an element without necessarily needing a copy constructor.
* * push_front_placement() can be used with placement new to construct
* elements with more than 4 parameters.
* - insert() has changed but remains compatible with STL usage:
* * insert(pos) will default-construct an element at pos
* * insert(pos,...) variations with 1-4 additional parameters will
* in-place construct an element at pos without necessarily needing a
* copy constructor.
* * insert_placement(pos) can be used with placement new to construct
* elements with more than 4 parameters.
* - Assistance for raw pointer types:
* * delete_all() will call delete on every element and clear() the list.
* * erase_and_delete() can be used to delete an element and erase it from
* the list.
* * pop_front_delete() will delete the first element and pop it from the list.
* * pop_back_delete() will delete the last element and pop it from the list.
* - O(1) size() function (spec says that size() may be O(n))
* - No initial heap allocation since &node is the terminator node (most std::list implementations construct a dead listnode
* from the heap as soon as they're constructed).
* - move() function can be used to move an item in the list in O(1) without
* copying or constructing.
* - The template allows a preallocated number of entries. These entires are included as part
* of the class and are not allocated from the heap.
* * Example: list<int, 5> reserves space for 5 int entries, but can still be
* passed to functions that require list<int>.
* * Growth above the preallocated amount will continue using the preallocated
* amount augmented with heap memory.
* * swap() between preallocated containers is O(n) for each preallocated list.
* Also, swap() will allocate from the heap and ignore preallocated space.
* * swap(), sort(), splice() and merge() will convert preallocated storage to heap storage.
* * splice(pos, list) is O(1) for standard lists but O(n) for lists with preallocated storage.
*/
#ifndef THOR_LIST_H
#define THOR_LIST_H
#pragma once
#ifndef THOR_BASETYPES_H
#include "basetypes.h"
#endif
#ifndef THOR_TYPETRAITS_H
#include "typetraits.h"
#endif
#ifndef THOR_ITERATOR_H
#include "iterator.h"
#endif
#ifndef THOR_FUNCTION_H
#include "function.h"
#endif
#ifndef THOR_ALGORITHM_H
#include "algorithm.h"
#endif
#ifndef THOR_FREELIST_H
#include "freelist.h"
#endif
#ifndef THOR_MEMORY_H
#include "memory.h"
#endif
namespace thor
{
template<class T, thor_size_type T_PREALLOC = 0> class list;
template <class T, class StrictWeakOrdering>
void __listsort(list<T>& L, StrictWeakOrdering order)
{
if (L.size() < 2)
{
return;
}
list<T> carry;
list<T> counter[64];
thor_size_type fill = 0, i;
while (!L.empty())
{
carry.splice(carry.begin(), L, L.begin());
i = 0;
while (i < fill && !counter[i].empty())
{
counter[i].merge(carry, order);
carry.swap(counter[i++]);
}
carry.swap(counter[i]);
if (i == fill)
{
++fill;
}
}
for (i = 1; i < fill; ++i)
{
counter[i].merge(counter[i - 1], order);
}
L.swap(counter[fill - 1]);
}
template <class T> class list<T, 0>
{
protected:
struct list_node;
public:
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef const T* const_pointer;
typedef const T& const_reference;
typedef thor_size_type size_type;
typedef thor_diff_type difference_type;
// iterator definitions
struct iterator_base : public iterator_type<bidirectional_iterator_tag, T>
{
list_node* m_element;
#ifdef THOR_DEBUG
const list* m_list;
iterator_base(list_node* n, const list* o) : m_element(n), m_list(o) {}
#else
iterator_base(list_node* n, const list*) : m_element(n) {}
#endif
void verify_not_end() const { THOR_DEBUG_ASSERT(m_list->end().m_element != m_element); }
void decr() { m_element = m_element->prev; }
void incr() { verify_not_end(); m_element = m_element->next; }
bool operator == (const iterator_base& i) const { THOR_DEBUG_ASSERT(m_list == i.m_list); return m_element == i.m_element; }
bool operator != (const iterator_base& i) const { THOR_DEBUG_ASSERT(m_list == i.m_list); return m_element != i.m_element; }
};
template<class Traits> class fwd_iterator : public iterator_base
{
public:
typedef typename Traits::pointer pointer;
typedef typename Traits::reference reference;
typedef fwd_iterator<nonconst_traits<T> > nonconst_iterator;
typedef fwd_iterator<Traits> selftype;
fwd_iterator(list_node* n = 0, const list* l = 0) : iterator_base(n, l) {}
fwd_iterator(const nonconst_iterator& i) : iterator_base(i) {}
selftype& operator = (const nonconst_iterator& i) { iterator_base::operator = (i); return *this; }
reference operator * () const { verify_not_end(); return m_element->m_value; }
pointer operator -> () const { verify_not_end(); return &(operator*()); }
selftype& operator -- () /* --iterator */ { decr(); return *this; }
selftype operator -- (int) /* iterator-- */ { selftype n(*this); decr(); return n; }
selftype& operator ++ () /* ++iterator */ { incr(); return *this; }
selftype operator ++ (int) /* iterator++ */ { selftype n(*this); incr(); return n; }
};
template<class Traits> class rev_iterator : public iterator_base
{
public:
typedef typename Traits::pointer pointer;
typedef typename Traits::reference reference;
typedef rev_iterator<nonconst_traits<T> > nonconst_iterator;
typedef rev_iterator<Traits> selftype;
rev_iterator(list_node* n = 0, const list* l = 0) : iterator_base(n, l) {}
rev_iterator(const nonconst_iterator& i) : iterator_base(i) {}
selftype& operator = (const nonconst_iterator& i) { iterator_base::operator = (i); return *this; }
reference operator * () const { verify_not_end(); return m_element->m_value; }
pointer operator -> () const { verify_not_end(); return &(operator*()); }
selftype& operator -- () /* --iterator */ { incr(); return *this; }
selftype operator -- (int) /* iterator-- */ { selftype n(*this); incr(); return n; }
selftype& operator ++ () /* ++iterator */ { decr(); return *this; }
selftype operator ++ (int) /* iterator++ */ { selftype n(*this); decr(); return n; }
};
typedef fwd_iterator<nonconst_traits<T> > iterator;
typedef fwd_iterator<const_traits<T> > const_iterator;
typedef rev_iterator<nonconst_traits<T> > reverse_iterator;
typedef rev_iterator<const_traits<T> > const_reverse_iterator;
// Constructors
list() :
m_head(terminator(), terminator()),
m_size(0)
{}
list(size_type n) :
m_head(terminator(), terminator()),
m_size(0)
{
while(n-- != 0)
{
typetraits<T>::construct(alloc_back());
}
}
list(size_type n, const T& t) :
m_head(terminator(), terminator()),
m_size(0)
{
while (n-- != 0)
{
typetraits<T>::construct(alloc_back(), t);
}
}
list(const list& L) :
m_head(terminator(), terminator()),
m_size(0)
{
insert(end(), L.begin(), L.end());
}
template <class InputIterator> list(InputIterator first, InputIterator last) :
m_head(terminator(), terminator()),
m_size(0)
{
insert(end(), first, last);
}
virtual ~list()
{
clear();
}
// Forward iteration
iterator begin()
{
return iterator(m_head.next, this);
}
const_iterator begin() const
{
return const_iterator(m_head.next, this);
}
iterator end()
{
return iterator(terminator(), this);
}
const_iterator end() const
{
return const_iterator(terminator(), this);
}
// Reverse iteration
reverse_iterator rbegin()
{
return reverse_iterator(m_head.prev, this);
}
const_reverse_iterator rbegin() const
{
return const_reverse_iterator(m_head.prev, this);
}
reverse_iterator rend()
{
return reverse_iterator(terminator(), this);
}
const_reverse_iterator rend() const
{
return const_reverse_iterator(terminator(), this);
}
// Size
size_type size() const
{
return m_size;
}
size_type max_size() const
{
return size_type(-1);
}
bool empty() const
{
return m_size == 0;
}
list& operator = (const list& L)
{
if (this != &L)
{
if (empty())
{
insert(end(), L.begin(), L.end());
}
else
{
iterator write = begin();
iterator wend = end();
const_iterator read = L.begin();
const_iterator rend = L.end();
while (write != wend && read != rend)
{
*write = *read;
++write, ++read;
}
if (read != rend)
{
insert(wend, read, rend);
}
else
{
erase(write, wend);
}
}
}
return *this;
}
// Accessing elements
T& front()
{
THOR_ASSERT(!empty());
return m_head.next->m_value;
}
const T& front() const
{
THOR_ASSERT(!empty());
return m_head.next->m_value;
}
T& back()
{
THOR_ASSERT(!empty());
return m_head.prev->m_value;
}
const T& back() const
{
THOR_ASSERT(!empty());
return m_head.prev->m_value;
}
// Adding elements to the front of the list
T& push_front()
{
typetraits<T>::construct(alloc_front());
return front();
}
template <class T1> T& push_front(const T1& t1)
{
typetraits<T>::construct(alloc_front(), t1);
return front();
}
template <class T1, class T2> T& push_front(const T1& t1, const T2& t2)
{
typetraits<T>::construct(alloc_front(), t1, t2);
return front();
}
template <class T1, class T2, class T3> T& push_front(const T1& t1, const T2& t2, const T3& t3)
{
typetraits<T>::construct(alloc_front(), t1, t2, t3);
return front();
}
template <class T1, class T2, class T3, class T4> T& push_front(const T1& t1, const T2& t2, const T3& t3, const T4& t4)
{
typetraits<T>::construct(alloc_front(), t1, t2, t3, t4);
return front();
}
// Requires the use of placement new to construct the element.
// Example: new (l.push_front_placement()) Element(arg1, arg2);
void* push_front_placement()
{
return alloc_front();
}
// Adding elements to the back of the list
T& push_back()
{
typetraits<T>::construct(alloc_back());
return back();
}
template <class T1> T& push_back(const T1& t1)
{
typetraits<T>::construct(alloc_back(), t1);
return back();
}
template <class T1, class T2> T& push_back(const T1& t1, const T2& t2)
{
typetraits<T>::construct(alloc_back(), t1, t2);
return back();
}
template <class T1, class T2, class T3> T& push_back(const T1& t1, const T2& t2, const T3& t3)
{
typetraits<T>::construct(alloc_back(), t1, t2, t3);
return back();
}
template <class T1, class T2, class T3, class T4> T& push_back(const T1& t1, const T2& t2, const T3& t3, const T4& t4)
{
typetraits<T>::construct(alloc_back(), t1, t2, t3, t4);
return back();
}
// Requires the use of placement new to construct the element.
// Example: new (l.push_back_placement()) Element(arg1, arg2);
void* push_back_placement()
{
return alloc_back();
}
void pop_front()
{
THOR_DEBUG_ASSERT(!empty());
if (!empty())
{
erase_node(m_head.next);
}
}
// Like pop_front(), only deletes the front value as well. Only valid
// for pointer types.
void pop_front_delete()
{
if (!empty())
{
delete m_head.next->m_value;
erase_node(m_head.next);
}
}
void pop_back()
{
THOR_DEBUG_ASSERT(!empty());
if (!empty())
{
erase_node(m_head.prev);
}
}
// Like pop_back(), only deletes the back value as well. Only valid
// for pointer types.
void pop_back_delete()
{
if (!empty())
{
delete m_head.prev->m_value;
erase_node(m_head.prev);
}
}
void swap(list& L)
{
THOR_ASSERT(this != &L);
if (!is_always_shareable())
{
make_shareable();
}
if (!L.is_always_shareable())
{
L.make_shareable();
}
// must fix up terminators first
// also note that pointers must be assigned simultaneously (i.e. m_head.prev->next = m_head.next->prev = terminator() doesn't work)
list_node *&Rhead = m_head.next->prev, *&Rtail = m_head.prev->next;
list_node *&Lhead = L.m_head.next->prev, *&Ltail = L.m_head.prev->next;
Rhead = Rtail = L.terminator();
Lhead = Ltail = terminator();
thor::swap(m_head, L.m_head);
thor::swap(m_size, L.m_size);
}
template <class InputIterator> void assign(InputIterator first, InputIterator last)
{
clear();
insert(begin(), first, last);
}
void assign(size_type n, const T& u)
{
clear();
while (n-- > 0)
{
push_back(u);
}
}
iterator insert(iterator pos)
{
iterator ret(insert_node(pos), this);
typetraits<T>::construct(&ret.m_element->m_value);
return ret;
}
template <class T1> iterator insert(iterator pos, const T1& t1)
{
iterator ret(insert_node(pos), this);
typetraits<T>::construct(&ret.m_element->m_value, t1);
return ret;
}
template <class T1, class T2> iterator insert(iterator pos, const T1& t1, const T2& t2)
{
iterator ret(insert_node(pos), this);
typetraits<T>::construct(&ret.m_element->m_value, t1, t2);
return ret;
}
template <class T1, class T2, class T3> iterator insert(iterator pos, const T1& t1, const T2& t2, const T3& t3)
{
iterator ret(insert_node(pos), this);
typetraits<T>::construct(&ret.m_element->m_value, t1, t2, t3);
return ret;
}
template <class T1, class T2, class T3, class T4> iterator insert(iterator pos, const T1& t1, const T2& t2, const T3& t3, const T4& t4)
{
iterator ret(insert_node(pos), this);
typetraits<T>::construct(&ret.m_element->m_value, t1, t2, t3, t4);
return ret;
}
// Requires the use of placement new to construct the element.
// Example: new (l.insert_placement(pos)) Element(arg1, arg2);
void* insert_placement(iterator pos)
{
return &insert_node(pos)->m_value;
}
template <class InputIterator> void insert(iterator pos, InputIterator first, InputIterator last)
{
while (first != last)
{
list_node* node = insert_node(pos);
typetraits<T>::construct(&node->m_value, *first);
++first;
}
}
void insert(iterator pos, size_type n, const T& t)
{
verify_iterator(pos);
while (n-- != 0)
{
list_node* node = insert_node(pos);
typetraits<T>::construct(&node->m_value, t);
}
}
iterator erase(iterator pos)
{
verify_iterator(pos);
pos.verify_not_end();
iterator next(pos.m_element->next, this);
erase_node(pos.m_element);
return next;
}
iterator erase(iterator first, iterator last)
{
verify_iterator(first);
verify_iterator(last);
list_node* n1 = first.m_element;
list_node* n2 = last.m_element;
while (n1 != n2)
{
list_node* next = n1->next;
erase_node(n1);
n1 = next;
}
return last;
}
// Similar to erase(pos), but also deletes the element. Only valid for pointer types.
iterator erase_and_delete(iterator pos)
{
verify_iterator(pos);
pos.verify_not_end();
iterator next(pos.m_element->next, this);
delete pos.m_element->m_value;
erase_node(pos.m_element);
return next;
}
void clear()
{
list_node* node = m_head.next;
m_size = 0;
list_node* last = m_head.prev = m_head.next = terminator();
while (node != last)
{
list_node* next = node->next;
dealloc_node(node);
node = next;
}
}
void resize(size_type n, const T& t = T())
{
if (size() <= n)
{
// Grow
insert(end(), n - m_size, t);
}
else
{
// Shrink
do
{
pop_back();
}
while (n < size());
}
}
void splice(iterator pos, list& L)
{
THOR_ASSERT(this != &L);
verify_iterator(pos);
if(!L.empty() && this != &L)
{
// Steal list_nodes from L
if (L.is_always_shareable())
{
pos.m_element->prev->next = L.m_head.next;
L.m_head.next->prev = pos.m_element->prev;
pos.m_element->prev = L.m_head.prev;
L.m_head.prev->next = pos.m_element;
m_size += L.m_size;
// Reset L
L.m_head.prev = L.m_head.next = L.terminator();
L.m_size = 0;
}
else
{
// Not shareable, so splice individual nodes
iterator first(L.begin()), last(L.end());
while (first != last)
{
splice(pos, L, first++);
}
}
}
}
void splice(iterator pos, list& L, iterator i)
{
THOR_ASSERT(this != &L);
verify_iterator(pos);
L.verify_iterator(i);
i.verify_not_end();
if (L.end() != i)
{
// Remove i from L
i.m_element->prev->next = i.m_element->next;
i.m_element->next->prev = i.m_element->prev;
--L.m_size;
if (L.is_node_shareable(i.m_element))
{
// Add i to this at pos
pos.m_element->prev->next = i.m_element;
i.m_element->prev = pos.m_element->prev;
pos.m_element->prev = i.m_element;
i.m_element->next = pos.m_element;
++m_size;
}
else
{
// Node is not shareable, so must copy and erase
list_node* node = insert_node(pos);
typetraits<T>::construct(&node->m_value, i.m_element->m_value);
L.dealloc_node(i.m_element);
}
}
}
void splice(iterator pos, list& L, iterator first, iterator last)
{
while (first != last)
{
splice(pos, L, first++);
}
}
void remove(const T& value)
{
list_node* n1 = m_head.next;
while (n1 != terminator())
{
list_node* next = n1->next;
if (n1->m_value == value)
{
erase_node(n1);
}
n1 = next;
}
}
// list must be sorted in order to use this
void unique()
{
unique_internal(equal_to<T>());
}
// list must be sorted in order to use this
template <class BinaryPredicate> void unique(BinaryPredicate pred)
{
unique_internal(pred);
}
void merge(list& L)
{
merge_internal(L, less<T>());
}
template <class StrictWeakOrdering> void merge(list& L, StrictWeakOrdering comp)
{
merge_internal(L, comp);
}
void sort()
{
sort_internal(less<T>());
}
template <class Compare> void sort(Compare comp)
{
sort_internal(comp);
}
// extensions
bool validate() const
{
#define THOR_ASSERT_RETURN(expr) THOR_ASSERT(expr); if (!(expr)) return false
THOR_ASSERT_RETURN(m_head.next->prev == terminator());
THOR_ASSERT_RETURN(m_head.prev->next == terminator());
size_type localcount = 0;
list_node* n = m_head.next;
while (n != terminator())
{
THOR_ASSERT_RETURN(n->next->prev == n);
THOR_ASSERT_RETURN(n->prev->next == n);
++localcount;
n = n->next;
}
THOR_ASSERT_RETURN(localcount == m_size);
return true;
#undef THOR_ASSERT_RETURN
}
void delete_all()
{
list_node* node = m_head.next;
m_size = 0;
list_node* last = m_head.prev = m_head.next = terminator();
while (node != last)
{
list_node* next = node->next;
delete node->m_value;
dealloc_node(node);
node = next;
}
}
// Moves the item at 'which' to before 'pos'
void move(iterator which, iterator pos)
{
verify_iterator(which);
verify_iterator(pos);
which.verify_not_end();
if (which != pos && which.m_element != terminator() && which.m_element->next != pos.m_element)
{
// Remove from current
which.m_element->prev->next = which.m_element->next;
which.m_element->next->prev = which.m_element->prev;
// Insert at pos
which.m_element->prev = pos.m_element->prev;
which.m_element->next = pos.m_element;
pos.m_element->prev->next = which.m_element;
pos.m_element->prev = which.m_element;
}
}
protected:
struct list_node_base
{
list_node* next;
list_node* prev;
list_node_base(list_node* n = 0, list_node* p = 0) : next(n), prev(p) {}
};
struct list_node : public list_node_base
{
T m_value;
};
typedef memory::align_alloc<list_node> align_alloc;
// (address of)m_head also happens to be the end() node (see terminator()). m_head.next is the head pointer and m_head.prev is the tail pointer
list_node_base m_head;
size_type m_size;
// The node allocated does not have m_value constructed
T* alloc_front()
{
THOR_ASSERT(m_head.next->prev == terminator());
m_head.next->prev = alloc_node(m_head.next, terminator());
m_head.next = m_head.next->prev;
++m_size;
return &m_head.next->m_value;
}
// The node allocated does not have m_value constructed
T* alloc_back()
{
THOR_ASSERT(m_head.prev->next == terminator());
m_head.prev->next = alloc_node(terminator(), m_head.prev);
m_head.prev = m_head.prev->next;
++m_size;
return &m_head.prev->m_value;
}
list_node* insert_node(const iterator& pos)
{
verify_iterator(pos);
list_node* node = alloc_node(pos.m_element, pos.m_element->prev);
node->next->prev = node;
node->prev->next = node;
++m_size;
return node;
}
void erase_node(list_node* node)
{
THOR_ASSERT(node != terminator());
node->next->prev = node->prev;
node->prev->next = node->next;
dealloc_node(node);
--m_size;
}
// The node does not have m_value constructed
list_node* alloc_node(list_node* next, list_node* prev, bool require_shareable = false)
{
list_node* node = alloc_node(require_shareable);
THOR_DEBUG_ASSERT(align_alloc::is_aligned(node));
return (list_node*)new (node) list_node_base(next, prev);
}
// The node must already be removed from the list
void dealloc_node(list_node* node)
{
THOR_ASSERT(node != terminator());
node->~list_node();
free_node(node);
}
// Only allocates the memory; list_node is not constructed
virtual list_node* alloc_node(bool /*require_shareable*/)
{
return align_alloc::alloc();
}
// Does not destruct list_node; just frees the underlying memory
virtual void free_node(list_node* node)
{
align_alloc::free(node);
}
virtual bool is_always_shareable() const
{
return true;
}
virtual bool is_node_shareable(list_node*)
{
return true;
}
void make_shareable()
{
list_node* node = m_head.next;
while (node != terminator())
{
list_node* next = node->next;
if (!is_node_shareable(node))
{
list_node* newnode = alloc_node(node->next, node->prev, true);
typetraits<T>::construct(&newnode->m_value, node->m_value);
node->next->prev = newnode;
node->prev->next = newnode;
dealloc_node(node);
}
node = next;
}
}
list_node* terminator() const { return (list_node*)&m_head; }
void verify_iterator(const iterator_base& i) const { THOR_UNUSED(i); THOR_ASSERT(i.m_list == this); }
template <class BinaryPredicate> void unique_internal(BinaryPredicate pred)
{
list_node* last = terminator();
if (size() > 1)
{
list_node* first = m_head.next;
for (;;)
{
list_node* prev = first;
first = first->next;
if (first == last)
{
break;
}
if (pred(first->m_value, prev->m_value))
{
erase_node(prev);
}
}
}
}
template <class StrictWeakOrdering> void merge_internal(list& L, StrictWeakOrdering comp)
{
THOR_ASSERT(this != &L);
if (this != &L && !L.empty())
{
if (!empty())
{
list_node* write = m_head.next;
list_node* read = L.m_head.next;
for (;;)
{
if (comp(read->m_value, write->m_value))
{
list_node* readpos = read;
read = read->next;
splice(iterator(write, this), L, iterator(readpos, &L));
if (read == L.terminator())
{
break;
}
}
else
{
write = write->next;
if (write == terminator())
{
break;
}
}
}
}
// Add any remaining elements
splice(end(), L);
}
}
template <class StrictWeakOrdering> void sort_internal(StrictWeakOrdering order)
{
if (size() < 2)
{
return;
}
list carry;
list counter[64];
thor_size_type fill = 0, i;
while (!empty())
{
carry.splice(carry.begin(), *this, begin());
i = 0;
while (i < fill && !counter[i].empty())
{
counter[i].merge(carry, order);