1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
|
/**
* \file
* \brief IntrusiveList template class header
*
* \author Copyright (C) 2015 Kamil Szczygiel http://www.distortec.com http://www.freddiechopin.info
*
* \par License
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not
* distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef ESTD_INTRUSIVELIST_HPP_
#define ESTD_INTRUSIVELIST_HPP_
#include <iterator>
#include <cstddef>
namespace estd
{
namespace internal
{
class IntrusiveListBase;
}
/**
* \brief IntrusiveListNode class is the node that is needed for the object to be linked in IntrusiveList
*
* To some extent, this class can be considered to be a limited (raw) iterator.
*
* The object that wants to be linked in IntrusiveList must contain a variable of this type - one for each intrusive
* list that will be used with object.
*/
class IntrusiveListNode
{
public:
/// LinkAccessKey class is used to limit access to IntrusiveListNode::link() function - only
/// internal::IntrusiveListBase can link nodes to the list
class LinkAccessKey
{
friend class internal::IntrusiveListBase;
/**
* \brief LinkAccessKey's constructor
*/
constexpr LinkAccessKey()
{
}
LinkAccessKey(const LinkAccessKey&) = delete;
LinkAccessKey(LinkAccessKey&&) = delete;
const LinkAccessKey& operator=(const LinkAccessKey&) = delete;
LinkAccessKey& operator=(LinkAccessKey&&) = delete;
};
/**
* \brief IntrusiveListNode's constructor
*/
constexpr IntrusiveListNode() :
nextNode_{this},
previousNode_{this}
{
}
/**
* \brief IntrusiveListNode's move constructor
*
* \param [in] other is a rvalue reference to IntrusiveListNode used as source of move construction
*/
IntrusiveListNode(IntrusiveListNode&& other)
{
if (other.isLinked() == false)
{
reset();
return;
}
nextNode_ = other.nextNode_;
previousNode_ = other.previousNode_;
nextNode_->previousNode_ = previousNode_->nextNode_ = this;
other.reset();
}
/**
* \brief IntrusiveListNode's destructor
*
* Unlinks the node from the list.
*/
~IntrusiveListNode()
{
unlink();
}
/**
* \return reference to next node on the list
*/
IntrusiveListNode& getNextNode() const
{
return *nextNode_;
}
/**
* \return reference to previous node on the list
*/
IntrusiveListNode& getPreviousNode() const
{
return *previousNode_;
}
/**
* \return true if the node is linked in some list, false otherwise
*/
bool isLinked() const
{
return nextNode_ != this;
}
/**
* \brief Links the node in the list before \a position.
*
* \note Access to this function is restricted only to functions from internal::IntrusiveListBase class
*
* \param [in] position is a reference to node before which this node will be linked
* \param [in] linkAccessKey is used to limit access to this function
*/
void link(IntrusiveListNode& position, LinkAccessKey)
{
unlink();
nextNode_ = &position;
previousNode_ = position.previousNode_;
position.previousNode_->nextNode_ = this;
position.previousNode_ = this;
}
/**
* \brief Swaps contents with another node.
*
* \param [in] other is a reference to IntrusiveListNode with which contents of this node will be swapped
*/
void swap(IntrusiveListNode& other)
{
const auto thisWasLinked = isLinked();
const auto otherWasLinked = other.isLinked();
if (thisWasLinked == true || otherWasLinked == true)
{
using std::swap;
swap(nextNode_, other.nextNode_);
swap(previousNode_, other.previousNode_);
if (thisWasLinked == true)
other.nextNode_->previousNode_ = other.previousNode_->nextNode_ = &other;
else
other.reset();
if (otherWasLinked == true)
nextNode_->previousNode_ = previousNode_->nextNode_ = this;
else
reset();
}
}
/**
* \brief Unlinks the node from the list.
*/
void unlink()
{
previousNode_->nextNode_ = nextNode_;
nextNode_->previousNode_ = previousNode_;
reset();
}
IntrusiveListNode(const IntrusiveListNode&) = delete;
const IntrusiveListNode& operator=(const IntrusiveListNode&) = delete;
IntrusiveListNode& operator=(IntrusiveListNode&&) = delete;
private:
/**
* \brief Resets the node to the same state as right after construction.
*/
void reset()
{
nextNode_ = this;
previousNode_ = this;
}
/// reference to next node on the list
IntrusiveListNode* nextNode_;
/// reference to previous node on the list
IntrusiveListNode* previousNode_;
};
namespace internal
{
/**
* \brief IntrusiveListBase class provides base functionalities for IntrusiveList class, but without any knowledge about
* types
*
* This class tries to provide an interface similar to std::list.
*/
class IntrusiveListBase
{
public:
/**
* \brief IntrusiveListBase's constructor
*/
constexpr IntrusiveListBase() :
rootNode_{}
{
}
/**
* \brief IntrusiveListBase's destructor
*
* Unlinks all nodes from the list.
*/
~IntrusiveListBase()
{
clear();
}
/**
* \return reference to first node on the list
*/
IntrusiveListNode& begin()
{
return rootNode_.getNextNode();
}
/**
* \return const reference to first node on the list
*/
const IntrusiveListNode& begin() const
{
return rootNode_.getNextNode();
}
/**
* \return const reference to first node on the list
*/
const IntrusiveListNode& cbegin() const
{
return begin();
}
/**
* \return const reference to "one past the last" node on the list
*/
const IntrusiveListNode& cend() const
{
return end();
}
/**
* \brief Unlinks all nodes from the list.
*/
void clear()
{
while (empty() == false)
pop_front();
}
/**
* \return true is the list is empty, false otherwise
*/
bool empty() const
{
return &begin() == &end();
}
/**
* \return reference to "one past the last" node on the list
*/
IntrusiveListNode& end()
{
return rootNode_;
}
/**
* \return const reference to "one past the last" node on the list
*/
const IntrusiveListNode& end() const
{
return rootNode_;
}
/**
* \brief Unlinks the last node from the list.
*/
void pop_back()
{
erase(end().getPreviousNode());
}
/**
* \brief Unlinks the first node from the list.
*/
void pop_front()
{
erase(begin());
}
/**
* \brief Links the node at the end of the list.
*
* \param [in] newNode is a reference to node that will be linked in the list
*/
void push_back(IntrusiveListNode& newNode)
{
insert(end(), newNode);
}
/**
* \brief Links the node at the beginning of the list.
*
* \param [in] newNode is a reference to node that will be linked in the list
*/
void push_front(IntrusiveListNode& newNode)
{
insert(begin(), newNode);
}
/**
* \brief Swaps contents with another list.
*
* \param [in] other is a reference to IntrusiveListBase with which contents of this list will be swapped
*/
void swap(IntrusiveListBase& other)
{
rootNode_.swap(other.rootNode_);
}
/**
* \brief Unlinks the node at \a position from the list.
*
* \note No instance of the list is needed for this operation.
*
* \param [in] position is a reference to the node that will be unlinked from the list
*
* \return reference to the node that was following the node which was unlinked
*/
static IntrusiveListNode& erase(IntrusiveListNode& position)
{
auto& next = position.getNextNode();
position.unlink();
return next;
}
/**
* \brief Links the node in the list before \a position.
*
* \note No instance of the list is needed for this operation.
*
* \param [in] position is a reference to node before which \a newNode will be linked
* \param [in] newNode is a reference to node that will be linked in the list
*/
static void insert(IntrusiveListNode& position, IntrusiveListNode& newNode)
{
newNode.link(position, {});
}
/**
* \brief Transfers the node from one list to another list before \a position.
*
* \note No instance of any list is needed for this operation.
*
* \param [in] position is a reference to node before which \a splicedNode will be linked
* \param [in] splicedNode is a reference to node that will be spliced from one list to another
*/
static void splice(IntrusiveListNode& position, IntrusiveListNode& splicedNode)
{
insert(position, splicedNode);
}
IntrusiveListBase(const IntrusiveListBase&) = delete;
IntrusiveListBase(IntrusiveListBase&&) = default;
const IntrusiveListBase& operator=(const IntrusiveListBase&) = delete;
IntrusiveListBase& operator=(IntrusiveListBase&&) = delete;
private:
/// root node of the intrusive list
IntrusiveListNode rootNode_;
};
/**
* \brief Swaps contents of two lists.
*
* \param [in] left is a reference to IntrusiveListBase with which contents of \a right will be swapped
* \param [in] right is a reference to IntrusiveListBase with which contents of \a left will be swapped
*/
inline void swap(IntrusiveListBase& left, IntrusiveListBase& right)
{
left.swap(right);
}
} // namespace internal
/**
* \brief IntrusiveListIterator class is an iterator of elements on IntrusiveList.
*
* This class provides an interface similar to std::list::iterator.
*
* \tparam T is the type that has the IntrusiveListNode variable
* \tparam NodePointer is a pointer-to-member to IntrusiveListNode variable in \a T
* \tparam U is the type that will be stored on the list; it can be different from \a T, but must be implicitly
* convertible to \a T (so usually a type derived from \a T); default - \a T;
*/
template<typename T, IntrusiveListNode T::* NodePointer, typename U = T>
class IntrusiveListIterator
{
public:
/// difference type
using difference_type = ptrdiff_t;
/// category of the iterator
using iterator_category = std::bidirectional_iterator_tag;
/// pointer to object "pointed to" by the iterator
using pointer = U*;
/// reference to object "pointed to" by the iterator
using reference = U&;
/// value "pointed to" by the iterator
using value_type = U;
/**
* \brief IntrusiveListIterator's constructor
*/
constexpr IntrusiveListIterator() :
node_{}
{
}
/**
* \brief IntrusiveListIterator's constructor
*
* \param [in] node is a pointer to IntrusiveListNode of element that will be "pointed to" by the iterator
*/
constexpr explicit IntrusiveListIterator(IntrusiveListNode* const node) :
node_{node}
{
}
/**
* \brief IntrusiveListIterator's constructor
*
* \param [in] element is a reference to element that will be "pointed to" by the iterator
*/
constexpr explicit IntrusiveListIterator(reference element) :
node_{&(element.*NodePointer)}
{
static_assert(std::is_convertible<U, T>::value == true, "U must be implicitly convertible to T!");
}
/**
* \brief IntrusiveListIterator's binary infix pointer member access operator
*
* \return pointer to object "pointed to" by the iterator
*/
pointer operator->() const
{
return getPointer();
}
/**
* \brief IntrusiveListIterator's unary prefix dereference operator
*
* \return reference to object "pointed to" by the iterator
*/
reference operator*() const
{
return *getPointer();
}
/**
* \brief IntrusiveListIterator's unary prefix increment operator
*
* \return reference to "this" iterator
*/
IntrusiveListIterator& operator++()
{
node_ = &node_->getNextNode();
return *this;
}
/**
* \brief IntrusiveListIterator's unary postfix increment operator
*
* \return copy of "this" iterator before increment
*/
IntrusiveListIterator operator++(int)
{
const auto temporary = *this;
node_ = &node_->getNextNode();
return temporary;
}
/**
* \brief IntrusiveListIterator's unary prefix decrement operator
*
* \return reference to "this" iterator
*/
IntrusiveListIterator& operator--()
{
node_ = &node_->getPreviousNode();
return *this;
}
/**
* \brief IntrusiveListIterator's unary postfix decrement operator
*
* \return copy of "this" iterator before decrement
*/
IntrusiveListIterator operator--(int)
{
const auto temporary = *this;
node_ = &node_->getPreviousNode();
return temporary;
}
/**
* \brief IntrusiveListIterator's "equal to" comparison operator
*
* \param [in] other is a const reference to IntrusiveListIterator on right-hand side of comparison operator
*
* \return true if both iterators are equal, false otherwise
*/
bool operator==(const IntrusiveListIterator& other) const
{
return node_ == other.node_;
}
private:
/**
* \brief Converts contained pointer to IntrusiveListNode to pointer to object that contains this node.
*
* \return pointer to object "pointed to" by the iterator
*/
pointer getPointer() const
{
static_assert(std::is_convertible<U, T>::value == true, "U must be implicitly convertible to T!");
const auto offset = reinterpret_cast<size_t>(&(static_cast<pointer>(nullptr)->*NodePointer));
return reinterpret_cast<pointer>(reinterpret_cast<size_t>(node_) - offset);
}
/// pointer to IntrusiveListNode of the object "pointed to" by the iterator
IntrusiveListNode* node_;
};
/**
* \brief IntrusiveListIterator's "not equal to" comparison operator
*
* \tparam T is the type that has the IntrusiveListNode variable
* \tparam NodePointer is a pointer-to-member to IntrusiveListNode variable in \a T
* \tparam U is the type that will be stored on the list; it can be different from \a T, but must be implicitly
* convertible to \a T (so usually a type derived from \a T); default - \a T;
*
* \param [in] left is a const reference to IntrusiveListIterator on left-hand side of comparison operator
* \param [in] right is a const reference to IntrusiveListIterator on right-hand side of comparison operator
*
* \return true if iterators are not equal, false otherwise
*/
template<typename T, IntrusiveListNode T::* NodePointer, typename U = T>
inline bool operator!=(const IntrusiveListIterator<T, NodePointer, U>& left,
const IntrusiveListIterator<T, NodePointer, U>& right)
{
return (left == right) == false;
}
/**
* \brief IntrusiveListConstIterator class is a const iterator of elements on IntrusiveList.
*
* This class provides an interface similar to std::list::const_iterator.
*
* \tparam T is the type that has the IntrusiveListNode variable
* \tparam NodePointer is a const pointer-to-member to IntrusiveListNode variable in \a T
* \tparam U is the type that will be stored on the list; it can be different from \a T, but must be implicitly
* convertible to \a T (so usually a type derived from \a T); default - \a T;
*/
template<typename T, const IntrusiveListNode T::* NodePointer, typename U = T>
class IntrusiveListConstIterator
{
public:
/// difference type
using difference_type = ptrdiff_t;
/// category of the iterator
using iterator_category = std::bidirectional_iterator_tag;
/// pointer to object "pointed to" by the iterator
using pointer = const U*;
/// reference to object "pointed to" by the iterator
using reference = const U&;
/// value "pointed to" by the iterator
using value_type = U;
/**
* \brief IntrusiveListConstIterator's constructor
*/
constexpr IntrusiveListConstIterator() :
node_{}
{
}
/**
* \brief IntrusiveListConstIterator's constructor
*
* \param [in] node is a pointer to const IntrusiveListNode of element that will be "pointed to" by the iterator
*/
constexpr explicit IntrusiveListConstIterator(const IntrusiveListNode* const node) :
node_{node}
{
}
/**
* \brief IntrusiveListConstIterator's constructor
*
* \param [in] element is a const reference to element that will be "pointed to" by the iterator
*/
constexpr explicit IntrusiveListConstIterator(reference element) :
node_{&(element.*NodePointer)}
{
static_assert(std::is_convertible<U, T>::value == true, "U must be implicitly convertible to T!");
}
/**
* \brief IntrusiveListConstIterator's constructor
*
* Converts non-const iterator (IntrusiveListIterator) to const iterator (IntrusiveListConstIterator).
*
* \tparam NonConstNodePointer is a non-const version of \a NodePointer
*
* \param [in] iterator is a const reference to non-const iterator (IntrusiveListIterator)
*/
template<IntrusiveListNode T::* NonConstNodePointer>
constexpr IntrusiveListConstIterator(const IntrusiveListIterator<T, NonConstNodePointer, U>& iterator) :
IntrusiveListConstIterator{*iterator}
{
}
/**
* \brief IntrusiveListConstIterator's binary infix pointer member access operator
*
* \return pointer to object "pointed to" by the iterator
*/
pointer operator->() const
{
return getPointer();
}
/**
* \brief IntrusiveListConstIterator's unary prefix dereference operator
*
* \return reference to object "pointed to" by the iterator
*/
reference operator*() const
{
return *getPointer();
}
/**
* \brief IntrusiveListConstIterator's unary prefix increment operator
*
* \return reference to "this" iterator
*/
IntrusiveListConstIterator& operator++()
{
node_ = &node_->getNextNode();
return *this;
}
/**
* \brief IntrusiveListConstIterator's unary postfix increment operator
*
* \return copy of "this" iterator before increment
*/
IntrusiveListConstIterator operator++(int)
{
const auto temporary = *this;
node_ = &node_->getNextNode();
return temporary;
}
/**
* \brief IntrusiveListConstIterator's unary prefix decrement operator
*
* \return reference to "this" iterator
*/
IntrusiveListConstIterator& operator--()
{
node_ = &node_->getPreviousNode();
return *this;
}
/**
* \brief IntrusiveListConstIterator's unary postfix decrement operator
*
* \return copy of "this" iterator before decrement
*/
IntrusiveListConstIterator operator--(int)
{
const auto temporary = *this;
node_ = &node_->getPreviousNode();
return temporary;
}
/**
* \brief IntrusiveListConstIterator's "equal to" comparison operator
*
* \param [in] other is a const reference to IntrusiveListConstIterator on right-hand side of comparison operator
*
* \return true if both iterators are equal, false otherwise
*/
bool operator==(const IntrusiveListConstIterator& other) const
{
return node_ == other.node_;
}
private:
/**
* \brief Converts contained pointer to IntrusiveListNode to pointer to object that contains this node.
*
* \return pointer to object "pointed to" by the iterator
*/
pointer getPointer() const
{
static_assert(std::is_convertible<U, T>::value == true, "U must be implicitly convertible to T!");
const auto offset = reinterpret_cast<size_t>(&(static_cast<pointer>(nullptr)->*NodePointer));
return reinterpret_cast<pointer>(reinterpret_cast<size_t>(node_) - offset);
}
/// pointer to const IntrusiveListNode of the object "pointed to" by the iterator
const IntrusiveListNode* node_;
};
/**
* \brief IntrusiveListConstIterator's "not equal to" comparison operator
*
* \tparam T is the type that has the IntrusiveListNode variable
* \tparam NodePointer is a const pointer-to-member to IntrusiveListNode variable in \a T
* \tparam U is the type that will be stored on the list; it can be different from \a T, but must be implicitly
* convertible to \a T (so usually a type derived from \a T); default - \a T;
*
* \param [in] left is a const reference to IntrusiveListConstIterator on left-hand side of comparison operator
* \param [in] right is a const reference to IntrusiveListConstIterator on right-hand side of comparison operator
*
* \return true if iterators are not equal, false otherwise
*/
template<typename T, const IntrusiveListNode T::* NodePointer, typename U = T>
inline bool operator!=(const IntrusiveListConstIterator<T, NodePointer, U>& left,
const IntrusiveListConstIterator<T, NodePointer, U>& right)
{
return (left == right) == false;
}
/**
* \brief "Equal to" comparison operator for IntrusiveListIterator and IntrusiveListConstIterator
*
* \tparam T is the type that has the IntrusiveListNode variable
* \tparam NodePointer is a pointer-to-member to IntrusiveListNode variable in \a T
* \tparam ConstNodePointer is a const pointer-to-member to IntrusiveListNode variable in \a T
* \tparam U is the type that will be stored on the list; it can be different from \a T, but must be implicitly
* convertible to \a T (so usually a type derived from \a T); default - \a T;
*
* \param [in] left is a const reference to IntrusiveListIterator on left-hand side of comparison operator
* \param [in] right is a const reference to IntrusiveListConstIterator on right-hand side of comparison operator
*
* \return true if both iterators are equal, false otherwise
*/
template<typename T, IntrusiveListNode T::* NodePointer, const IntrusiveListNode T::* ConstNodePointer, typename U = T>
inline bool operator==(const IntrusiveListIterator<T, NodePointer, U>& left,
const IntrusiveListConstIterator<T, ConstNodePointer, U>& right)
{
return decltype(right){left} == right;
}
/**
* \brief "Not equal to" comparison operator for IntrusiveListIterator and IntrusiveListConstIterator
*
* \tparam T is the type that has the IntrusiveListNode variable
* \tparam NodePointer is a pointer-to-member to IntrusiveListNode variable in \a T
* \tparam ConstNodePointer is a const pointer-to-member to IntrusiveListNode variable in \a T
* \tparam U is the type that will be stored on the list; it can be different from \a T, but must be implicitly
* convertible to \a T (so usually a type derived from \a T); default - \a T;
*
* \param [in] left is a const reference to IntrusiveListIterator on left-hand side of comparison operator
* \param [in] right is a const reference to IntrusiveListConstIterator on right-hand side of comparison operator
*
* \return true if iterators are not equal, false otherwise
*/
template<typename T, IntrusiveListNode T::* NodePointer, const IntrusiveListNode T::* ConstNodePointer, typename U = T>
inline bool operator!=(const IntrusiveListIterator<T, NodePointer, U>& left,
const IntrusiveListConstIterator<T, ConstNodePointer, U>& right)
{
return (left == right) == false;
}
/**
* \brief "Not equal to" comparison operator for IntrusiveListConstIterator and IntrusiveListIterator
*
* \tparam T is the type that has the IntrusiveListNode variable
* \tparam NodePointer is a pointer-to-member to IntrusiveListNode variable in \a T
* \tparam ConstNodePointer is a const pointer-to-member to IntrusiveListNode variable in \a T
* \tparam U is the type that will be stored on the list; it can be different from \a T, but must be implicitly
* convertible to \a T (so usually a type derived from \a T); default - \a T;
*
* \param [in] left is a const reference to IntrusiveListConstIterator on left-hand side of comparison operator
* \param [in] right is a const reference to IntrusiveListIterator on right-hand side of comparison operator
*
* \return true if iterators are not equal, false otherwise
*/
template<typename T, IntrusiveListNode T::* NodePointer, const IntrusiveListNode T::* ConstNodePointer, typename U = T>
inline bool operator!=(const IntrusiveListConstIterator<T, ConstNodePointer, U>& left,
const IntrusiveListIterator<T, NodePointer, U>& right)
{
return right != left;
}
/**
* \brief IntrusiveList class is an intrusive circular doubly linked list.
*
* This class tries to provide an interface similar to std::list.
*
* \tparam T is the type that has the IntrusiveListNode variable
* \tparam NodePointer is a pointer-to-member to IntrusiveListNode variable in \a T
* \tparam U is the type that will be stored on the list; it can be different from \a T, but must be implicitly
* convertible to \a T (so usually a type derived from \a T); default - \a T; using different type than \a T can be used
* to break circular dependencies, because \a T must be fully defined to instantiate this class, but it is enough to
* forward declare \a U - it only needs to be fully defined to use member functions
*/
template<typename T, IntrusiveListNode T::* NodePointer, typename U = T>
class IntrusiveList
{
public:
/// const iterator of elements on the list
using const_iterator = IntrusiveListConstIterator<T, NodePointer, U>;
/// const reverse iterator of elements on the list
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
/// const pointer to value linked in the list
using const_pointer = const U*;
/// const reference to value linked in the list
using const_reference = const U&;
/// iterator of elements on the list
using iterator = IntrusiveListIterator<T, NodePointer, U>;
/// reverse iterator of elements on the list
using reverse_iterator = std::reverse_iterator<iterator>;
/// pointer to value linked in the list
using pointer = U*;
/// reference to value linked in the list
using reference = U&;
/// value linked in the list
using value_type = U;
/**
* \brief IntrusiveList's constructor
*/
constexpr IntrusiveList() :
intrusiveListBase_{}
{
}
/**
* \return reference to last element on the list
*/
reference back()
{
return *--end();
}
/**
* \return const reference to last element on the list
*/
const_reference back() const
{
return *--end();
}
/**
* \return iterator of first element on the list
*/
iterator begin()
{
return iterator{&intrusiveListBase_.begin()};
}
/**
* \return const iterator of first element on the list
*/
const_iterator begin() const
{
return const_iterator{&intrusiveListBase_.begin()};
}
/**
* \return const iterator of first element on the list
*/
const_iterator cbegin() const
{
return begin();
}
/**
* \return const iterator of "one past the last" element on the list
*/
const_iterator cend() const
{
return end();
}
/**
* \brief Unlinks all elements from the list.
*/
void clear()
{
intrusiveListBase_.clear();
}
/**
* \return true is the list is empty, false otherwise
*/
bool empty() const
{
return intrusiveListBase_.empty();
}
/**
* \return iterator of "one past the last" element on the list
*/
iterator end()
{
return iterator{&intrusiveListBase_.end()};
}
/**
* \return const iterator of "one past the last" element on the list
*/
const_iterator end() const
{
return const_iterator{&intrusiveListBase_.end()};
}
/**
* \return reference to first element on the list
*/
reference front()
{
return *begin();
}
/**
* \return const reference to first element on the list
*/
const_reference front() const
{
return *begin();
}
/**
* \brief Unlinks the last element from the list.
*/
void pop_back()
{
erase(--end());
}
/**
* \brief Unlinks the first element from the list.
*/
void pop_front()
{
erase(begin());
}
/**
* \brief Links the element at the end of the list.
*
* \param [in] newElement is a reference to the element that will be linked in the list
*/
void push_back(reference newElement)
{
insert(end(), newElement);
}
/**
* \brief Links the element at the beginning of the list.
*
* \param [in] newElement is a reference to the element that will be linked in the list
*/
void push_front(reference newElement)
{
insert(begin(), newElement);
}
/**
* \brief Swaps contents with another list.
*
* \param [in] other is a reference to IntrusiveList with which contents of this list will be swapped
*/
void swap(IntrusiveList& other)
{
intrusiveListBase_.swap(other.intrusiveListBase_);
}
/**
* \brief Unlinks the element at \a position from the list.
*
* \note No instance of the list is needed for this operation.
*
* \param [in] position is an iterator of the element that will be unlinked from the list
*
* \return iterator of the element that was following the element which was unlinked
*/
static iterator erase(const iterator position)
{
auto& positionNode = (*position).*NodePointer;
auto& nextNode = internal::IntrusiveListBase::erase(positionNode);
return iterator{&nextNode};
}
/**
* \brief Links the element in the list before \a position.
*
* \note No instance of the list is needed for this operation.
*
* \param [in] position is an iterator of the element before which \a newNode will be linked
* \param [in] newElement is a reference to the element that will be linked in the list
*
* \return iterator of \a newElement
*/
static iterator insert(const iterator position, reference newElement)
{
static_assert(std::is_convertible<U, T>::value == true, "U must be implicitly convertible to T!");
auto& positionNode = (*position).*NodePointer;
auto& newElementNode = newElement.*NodePointer;
internal::IntrusiveListBase::insert(positionNode, newElementNode);
return iterator{&newElementNode};
}
/**
* \brief Transfers the element from one list to another list before \a position.
*
* \note No instance of any list is needed for this operation.
*
* \param [in] position is an iterator of the element before which \a splicedElement will be linked
* \param [in] splicedElement is an iterator of the element that will be spliced from one list to another
*/
static void splice(const iterator position, const iterator splicedElement)
{
auto& positionNode = (*position).*NodePointer;
auto& splicedElementNode = (*splicedElement).*NodePointer;
internal::IntrusiveListBase::splice(positionNode, splicedElementNode);
}
IntrusiveList(const IntrusiveList&) = delete;
IntrusiveList(IntrusiveList&&) = default;
const IntrusiveList& operator=(const IntrusiveList&) = delete;
IntrusiveList& operator=(IntrusiveList&&) = delete;
private:
/// internal IntrusiveListBase object
internal::IntrusiveListBase intrusiveListBase_;
};
/**
* \brief Swaps contents of two lists.
*
* \tparam T is the type that has the IntrusiveListNode variable
* \tparam NodePointer is a pointer-to-member to IntrusiveListNode variable in \a T
* \tparam U is the type that will be stored on the list; it can be different from \a T, but must be implicitly
* convertible to \a T (so usually a type derived from \a T); default - \a T;
*
* \param [in] left is a reference to IntrusiveList with which contents of \a right will be swapped
* \param [in] right is a reference to IntrusiveList with which contents of \a left will be swapped
*/
template<typename T, IntrusiveListNode T::* NodePointer, typename U = T>
inline void swap(IntrusiveList<T, NodePointer, U>& left, IntrusiveList<T, NodePointer, U>& right)
{
left.swap(right);
}
} // namespace estd
#endif // ESTD_INTRUSIVELIST_HPP_
|