aboutsummaryrefslogtreecommitdiffstats
path: root/include/estd/IntrusiveForwardList.hpp
blob: faa98d116e0ddc879974fb49b0fc44d76d09e276 (plain)
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
/**
 * \file
 * \brief IntrusiveForwardList 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_INTRUSIVEFORWARDLIST_HPP_
#define ESTD_INTRUSIVEFORWARDLIST_HPP_

#include <iterator>

#include <cstddef>

namespace estd
{

namespace internal
{

class IntrusiveForwardListBase;

}

/**
 * \brief IntrusiveForwardListNode class is the node that is needed for the object to be linked in IntrusiveForwardList
 *
 * To some extent, this class can be considered to be a limited (raw) iterator.
 *
 * The object that wants to be linked in IntrusiveForwardList must contain a variable of this type - one for each
 * intrusive forward list that will be used with object.
 */

class IntrusiveForwardListNode
{
public:

	/// AccessKey class is used to limit access to IntrusiveForwardListNode's linkAfter() and unlinkNext() functions -
	/// only internal::IntrusiveForwardListBase can link/unlink nodes
	class AccessKey
	{
		friend class internal::IntrusiveForwardListBase;

		/**
		 * \brief AccessKey's constructor
		 */

		constexpr AccessKey()
		{

		}

		AccessKey(const AccessKey&) = delete;
		AccessKey(AccessKey&&) = delete;
		const AccessKey& operator=(const AccessKey&) = delete;
		AccessKey& operator=(AccessKey&&) = delete;
	};

	/**
	 * \brief IntrusiveForwardListNode's constructor
	 */

	constexpr IntrusiveForwardListNode() :
			nextNode_{}
	{

	}

	/**
	 * \brief IntrusiveForwardListNode's move constructor
	 *
	 * \param [in] other is a rvalue reference to IntrusiveForwardListNode used as source of move construction
	 */

	IntrusiveForwardListNode(IntrusiveForwardListNode&& other) :
			nextNode_{other.nextNode_}
	{
		other.reset();
	}

	/**
	 * \return pointer to next node on the list
	 */

	IntrusiveForwardListNode* getNextNode() const
	{
		return nextNode_;
	}

	/**
	 * \return true if the node is linked in some list, false otherwise
	 */

	bool isLinked() const
	{
		return nextNode_ != nullptr;
	}

	/**
	 * \brief Links the node in the list after \a position.
	 *
	 * \note Access to this function is restricted only to functions from internal::IntrusiveForwardListBase class
	 *
	 * \param [in] position is a pointer to node after which this node will be linked
	 * \param [in] accessKey is used to limit access to this function
	 */

	void linkAfter(IntrusiveForwardListNode* const position, AccessKey)
	{
		nextNode_ = position->getNextNode();
		position->nextNode_ = this;
	}

	/**
	 * \brief Swaps contents with another node.
	 *
	 * \param [in] other is a reference to IntrusiveForwardListNode with which contents of this node will be swapped
	 */

	void swap(IntrusiveForwardListNode& other)
	{
		using std::swap;
		swap(nextNode_, other.nextNode_);
	}

	/**
	 * \brief Unlinks the node following this one from the list.
	 *
	 * \note Access to this function is restricted only to functions from internal::IntrusiveForwardListBase class
	 *
	 * \param [in] accessKey is used to limit access to this function
	 */

	void unlinkNext(AccessKey)
	{
		auto& nextNode = *nextNode_;
		nextNode_ = nextNode.nextNode_;

		nextNode.reset();
	}

	IntrusiveForwardListNode(const IntrusiveForwardListNode&) = delete;
	const IntrusiveForwardListNode& operator=(const IntrusiveForwardListNode&) = delete;
	IntrusiveForwardListNode& operator=(IntrusiveForwardListNode&&) = delete;

private:

	/**
	 * \brief Resets the node to the same state as right after construction.
	 */

	void reset()
	{
		nextNode_ = {};
	}

	/// pointer to next node on the list
	IntrusiveForwardListNode* nextNode_;
};

/**
 * \brief Swaps contents of two nodes.
 *
 * \param [in] left is a reference to IntrusiveForwardListNode with which contents of \a right will be swapped
 * \param [in] right is a reference to IntrusiveForwardListNode with which contents of \a left will be swapped
 */

inline void swap(IntrusiveForwardListNode& left, IntrusiveForwardListNode& right)
{
	left.swap(right);
}

namespace internal
{

/**
 * \brief IntrusiveForwardListBase class provides base functionalities for IntrusiveForwardList class, but without any
 * knowledge about types
 *
 * This class tries to provide an interface similar to std::forward_list.
 */

class IntrusiveForwardListBase
{
public:

	/**
	 * \brief IntrusiveForwardListBase's constructor
	 */

	constexpr IntrusiveForwardListBase() :
			rootNode_{}
	{

	}

	/**
	 * \brief IntrusiveForwardListBase's destructor
	 *
	 * Unlinks all nodes from the list.
	 */

	~IntrusiveForwardListBase()
	{
		clear();
	}

	/**
	 * \return pointer to "one before the first" node on the list
	 */

	IntrusiveForwardListNode* before_begin()
	{
		return &rootNode_;
	}

	/**
	 * \return const pointer to "one before the first" node on the list
	 */

	const IntrusiveForwardListNode* before_begin() const
	{
		return &rootNode_;
	}

	/**
	 * \return pointer to first node on the list
	 */

	IntrusiveForwardListNode* begin()
	{
		return rootNode_.getNextNode();
	}

	/**
	 * \return const pointer to first node on the list
	 */

	const IntrusiveForwardListNode* begin() const
	{
		return rootNode_.getNextNode();
	}

	/**
	 * \return const pointer to "one before the first" node on the list
	 */

	const IntrusiveForwardListNode* cbefore_begin() const
	{
		return before_begin();
	}

	/**
	 * \return const pointer to first node on the list
	 */

	const IntrusiveForwardListNode* cbegin() const
	{
		return begin();
	}

	/**
	 * \return const pointer to "one past the last" node on the list
	 */

	const IntrusiveForwardListNode* 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 pointer to "one past the last" node on the list
	 */

	IntrusiveForwardListNode* end()
	{
		return nullptr;
	}

	/**
	 * \return const pointer to "one past the last" node on the list
	 */

	const IntrusiveForwardListNode* end() const
	{
		return nullptr;
	}

	/**
	 * \brief Unlinks the first node from the list.
	 */

	void pop_front()
	{
		erase_after(before_begin());
	}

	/**
	 * \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(IntrusiveForwardListNode& newNode)
	{
		insert_after(before_begin(), newNode);
	}

	/**
	 * \brief Swaps contents with another list.
	 *
	 * \param [in] other is a reference to IntrusiveForwardListBase with which contents of this list will be swapped
	 */

	void swap(IntrusiveForwardListBase& other)
	{
		rootNode_.swap(other.rootNode_);
	}

	/**
	 * \brief Unlinks the node following \a position from the list.
	 *
	 * \note No instance of the list is needed for this operation.
	 *
	 * \param [in] position is a pointer to the node preceding the one which will be unlinked from the list
	 *
	 * \return pointer to the node that was following the node which was unlinked
	 */

	static IntrusiveForwardListNode* erase_after(IntrusiveForwardListNode* const position)
	{
		position->unlinkNext({});
		return position->getNextNode();
	}

	/**
	 * \brief Links the node in the list after \a position.
	 *
	 * \note No instance of the list is needed for this operation.
	 *
	 * \param [in] position is a pointer to node after which \a newNode will be linked
	 * \param [in] newNode is a reference to node that will be linked in the list
	 */

	static void insert_after(IntrusiveForwardListNode* const position, IntrusiveForwardListNode& newNode)
	{
		newNode.linkAfter(position, {});
	}

	/**
	 * \brief Transfers the node from one list to another list after \a position.
	 *
	 * \note No instance of any list is needed for this operation.
	 *
	 * \param [in] position is a pointer to node after which spliced node will be linked
	 * \param [in] beforeSplicedNode is a pointer to node preceding the one which will be spliced from one list to
	 * another
	 */

	static void splice_after(IntrusiveForwardListNode* const position,
			IntrusiveForwardListNode* const beforeSplicedNode)
	{
		const auto splicedNode = beforeSplicedNode->getNextNode();
		erase_after(beforeSplicedNode);
		insert_after(position, *splicedNode);
	}

	IntrusiveForwardListBase(const IntrusiveForwardListBase&) = delete;
	IntrusiveForwardListBase(IntrusiveForwardListBase&&) = default;
	const IntrusiveForwardListBase& operator=(const IntrusiveForwardListBase&) = delete;
	IntrusiveForwardListBase& operator=(IntrusiveForwardListBase&&) = delete;

private:

	/// root node of the intrusive forward list
	IntrusiveForwardListNode rootNode_;
};

/**
 * \brief Swaps contents of two lists.
 *
 * \param [in] left is a reference to IntrusiveForwardListBase with which contents of \a right will be swapped
 * \param [in] right is a reference to IntrusiveForwardListBase with which contents of \a left will be swapped
 */

inline void swap(IntrusiveForwardListBase& left, IntrusiveForwardListBase& right)
{
	left.swap(right);
}

}	// namespace internal

/**
 * \brief IntrusiveForwardListIterator class is an iterator of elements on IntrusiveForwardList.
 *
 * This class provides an interface similar to std::forward_list::iterator.
 *
 * \tparam T is the type that has the IntrusiveForwardListNode variable
 * \tparam NodePointer is a pointer-to-member to IntrusiveForwardListNode 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, IntrusiveForwardListNode T::* NodePointer, typename U = T>
class IntrusiveForwardListIterator
{
public:

	/// difference type
	using difference_type = ptrdiff_t;

	/// category of the iterator
	using iterator_category = std::forward_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 IntrusiveForwardListIterator's constructor
	 */

	constexpr IntrusiveForwardListIterator() :
			node_{}
	{

	}

	/**
	 * \brief IntrusiveForwardListIterator's constructor
	 *
	 * \param [in] node is a pointer to IntrusiveForwardListNode of element that will be "pointed to" by the iterator
	 */

	constexpr explicit IntrusiveForwardListIterator(IntrusiveForwardListNode* const node) :
			node_{node}
	{

	}

	/**
	 * \brief IntrusiveForwardListIterator's constructor
	 *
	 * \param [in] element is a reference to element that will be "pointed to" by the iterator
	 */

	constexpr explicit IntrusiveForwardListIterator(reference element) :
			node_{&(element.*NodePointer)}
	{
		static_assert(std::is_convertible<U, T>::value == true, "U must be implicitly convertible to T!");
	}

	/**
	 * \brief IntrusiveForwardListIterator's binary infix pointer member access operator
	 *
	 * \return pointer to object "pointed to" by the iterator
	 */

	pointer operator->() const
	{
		return getPointer();
	}

	/**
	 * \brief IntrusiveForwardListIterator's unary prefix dereference operator
	 *
	 * \return reference to object "pointed to" by the iterator
	 */

	reference operator*() const
	{
		return *getPointer();
	}

	/**
	 * \brief IntrusiveForwardListIterator's unary prefix increment operator
	 *
	 * \return reference to "this" iterator
	 */

	IntrusiveForwardListIterator& operator++()
	{
		node_ = node_->getNextNode();
		return *this;
	}

	/**
	 * \brief IntrusiveForwardListIterator's unary postfix increment operator
	 *
	 * \return copy of "this" iterator before increment
	 */

	IntrusiveForwardListIterator operator++(int)
	{
		const auto temporary = *this;
		node_ = node_->getNextNode();
		return temporary;
	}

	/**
	 * \brief IntrusiveForwardListIterator's "equal to" comparison operator
	 *
	 * \param [in] other is a const reference to IntrusiveForwardListIterator on right-hand side of equality operator
	 *
	 * \return true if both iterators are equal, false otherwise
	 */

	bool operator==(const IntrusiveForwardListIterator& other) const
	{
		return node_ == other.node_;
	}

private:

	/**
	 * \brief Converts contained pointer to IntrusiveForwardListNode 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 IntrusiveForwardListNode of the object "pointed to" by the iterator
	IntrusiveForwardListNode* node_;
};

/**
 * \brief IntrusiveForwardListIterator's "not equal to" comparison operator
 *
 * \tparam T is the type that has the IntrusiveForwardListNode variable
 * \tparam NodePointer is a pointer-to-member to IntrusiveForwardListNode 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 IntrusiveForwardListIterator on left-hand side of comparison operator
 * \param [in] right is a const reference to IntrusiveForwardListIterator on right-hand side of comparison operator
 *
 * \return true if iterators are not equal, false otherwise
 */

template<typename T, IntrusiveForwardListNode T::* NodePointer, typename U = T>
inline bool operator!=(const IntrusiveForwardListIterator<T, NodePointer, U>& left,
		const IntrusiveForwardListIterator<T, NodePointer, U>& right)
{
	return (left == right) == false;
}

/**
 * \brief IntrusiveForwardListConstIterator class is a const iterator of elements on IntrusiveForwardList.
 *
 * This class provides an interface similar to std::forward_list::const_iterator.
 *
 * \tparam T is the type that has the IntrusiveForwardListNode variable
 * \tparam NodePointer is a const pointer-to-member to IntrusiveForwardListNode 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 IntrusiveForwardListNode T::* NodePointer, typename U = T>
class IntrusiveForwardListConstIterator
{
public:

	/// difference type
	using difference_type = ptrdiff_t;

	/// category of the iterator
	using iterator_category = std::forward_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 IntrusiveForwardListConstIterator's constructor
	 */

	constexpr IntrusiveForwardListConstIterator() :
			node_{}
	{

	}

	/**
	 * \brief IntrusiveForwardListConstIterator's constructor
	 *
	 * \param [in] node is a pointer to const IntrusiveForwardListNode of element that will be "pointed to" by the
	 * iterator
	 */

	constexpr explicit IntrusiveForwardListConstIterator(const IntrusiveForwardListNode* const node) :
			node_{node}
	{

	}

	/**
	 * \brief IntrusiveForwardListConstIterator's constructor
	 *
	 * \param [in] element is a const reference to element that will be "pointed to" by the iterator
	 */

	constexpr explicit IntrusiveForwardListConstIterator(reference element) :
			node_{&(element.*NodePointer)}
	{
		static_assert(std::is_convertible<U, T>::value == true, "U must be implicitly convertible to T!");
	}

	/**
	 * \brief IntrusiveForwardListConstIterator's constructor
	 *
	 * Converts non-const iterator (IntrusiveForwardListIterator) to const iterator (IntrusiveForwardListConstIterator).
	 *
	 * \tparam NonConstNodePointer is a non-const version of \a NodePointer
	 *
	 * \param [in] iterator is a const reference to non-const iterator (IntrusiveForwardListIterator)
	 */

	template<IntrusiveForwardListNode T::* NonConstNodePointer>
	constexpr
	IntrusiveForwardListConstIterator(const IntrusiveForwardListIterator<T, NonConstNodePointer, U>& iterator) :
			IntrusiveForwardListConstIterator{*iterator}
	{

	}

	/**
	 * \brief IntrusiveForwardListConstIterator's binary infix pointer member access operator
	 *
	 * \return pointer to object "pointed to" by the iterator
	 */

	pointer operator->() const
	{
		return getPointer();
	}

	/**
	 * \brief IntrusiveForwardListConstIterator's unary prefix dereference operator
	 *
	 * \return reference to object "pointed to" by the iterator
	 */

	reference operator*() const
	{
		return *getPointer();
	}

	/**
	 * \brief IntrusiveForwardListConstIterator's unary prefix increment operator
	 *
	 * \return reference to "this" iterator
	 */

	IntrusiveForwardListConstIterator& operator++()
	{
		node_ = node_->getNextNode();
		return *this;
	}

	/**
	 * \brief IntrusiveForwardListConstIterator's unary postfix increment operator
	 *
	 * \return copy of "this" iterator before increment
	 */

	IntrusiveForwardListConstIterator operator++(int)
	{
		const auto temporary = *this;
		node_ = node_->getNextNode();
		return temporary;
	}

	/**
	 * \brief IntrusiveForwardListConstIterator's "equal to" comparison operator
	 *
	 * \param [in] other is a const reference to IntrusiveForwardListConstIterator on right-hand side of equality
	 * operator
	 *
	 * \return true if both iterators are equal, false otherwise
	 */

	bool operator==(const IntrusiveForwardListConstIterator& other) const
	{
		return node_ == other.node_;
	}

private:

	/**
	 * \brief Converts contained pointer to IntrusiveForwardListNode 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 IntrusiveForwardListNode of the object "pointed to" by the iterator
	const IntrusiveForwardListNode* node_;
};

/**
 * \brief IntrusiveForwardListConstIterator's "not equal to" comparison operator
 *
 * \tparam T is the type that has the IntrusiveForwardListNode variable
 * \tparam NodePointer is a const pointer-to-member to IntrusiveForwardListNode 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 IntrusiveForwardListConstIterator on left-hand side of comparison operator
 * \param [in] right is a const reference to IntrusiveForwardListConstIterator on right-hand side of comparison operator
 *
 * \return true if iterators are not equal, false otherwise
 */

template<typename T, const IntrusiveForwardListNode T::* NodePointer, typename U = T>
inline bool operator!=(const IntrusiveForwardListConstIterator<T, NodePointer, U>& left,
		const IntrusiveForwardListConstIterator<T, NodePointer, U>& right)
{
	return (left == right) == false;
}

/**
 * \brief "Equal to" comparison operator for IntrusiveForwardListIterator and IntrusiveForwardListConstIterator
 *
 * \tparam T is the type that has the IntrusiveForwardListNode variable
 * \tparam NodePointer is a pointer-to-member to IntrusiveForwardListNode variable in \a T
 * \tparam ConstNodePointer is a const pointer-to-member to IntrusiveForwardListNode 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 IntrusiveForwardListIterator on left-hand side of comparison operator
 * \param [in] right is a const reference to IntrusiveForwardListConstIterator on right-hand side of comparison operator
 *
 * \return true if both iterators are equal, false otherwise
 */

template<typename T, IntrusiveForwardListNode T::* NodePointer, const IntrusiveForwardListNode T::* ConstNodePointer,
		typename U = T>
inline bool operator==(const IntrusiveForwardListIterator<T, NodePointer, U>& left,
		const IntrusiveForwardListConstIterator<T, ConstNodePointer, U>& right)
{
	return decltype(right){left} == right;
}

/**
 * \brief "Not equal to" comparison operator for IntrusiveForwardListIterator and IntrusiveForwardListConstIterator
 *
 * \tparam T is the type that has the IntrusiveForwardListNode variable
 * \tparam NodePointer is a pointer-to-member to IntrusiveForwardListNode variable in \a T
 * \tparam ConstNodePointer is a const pointer-to-member to IntrusiveForwardListNode 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 IntrusiveForwardListIterator on left-hand side of comparison operator
 * \param [in] right is a const reference to IntrusiveForwardListConstIterator on right-hand side of comparison operator
 *
 * \return true if iterators are not equal, false otherwise
 */

template<typename T, IntrusiveForwardListNode T::* NodePointer, const IntrusiveForwardListNode T::* ConstNodePointer,
		typename U = T>
inline bool operator!=(const IntrusiveForwardListIterator<T, NodePointer, U>& left,
		const IntrusiveForwardListConstIterator<T, ConstNodePointer, U>& right)
{
	return (left == right) == false;
}

/**
 * \brief "Not equal to" comparison operator for IntrusiveForwardListConstIterator and IntrusiveForwardListIterator
 *
 * \tparam T is the type that has the IntrusiveForwardListNode variable
 * \tparam NodePointer is a pointer-to-member to IntrusiveForwardListNode variable in \a T
 * \tparam ConstNodePointer is a const pointer-to-member to IntrusiveForwardListNode 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 IntrusiveForwardListConstIterator on left-hand side of comparison operator
 * \param [in] right is a const reference to IntrusiveForwardListIterator on right-hand side of comparison operator
 *
 * \return true if iterators are not equal, false otherwise
 */

template<typename T, IntrusiveForwardListNode T::* NodePointer, const IntrusiveForwardListNode T::* ConstNodePointer,
		typename U = T>
inline bool operator!=(const IntrusiveForwardListConstIterator<T, ConstNodePointer, U>& left,
		const IntrusiveForwardListIterator<T, NodePointer, U>& right)
{
	return right != left;
}

/**
 * \brief IntrusiveForwardList class is an intrusive linear singly linked list.
 *
 * This class tries to provide an interface similar to std::forward_list.
 *
 * \tparam T is the type that has the IntrusiveForwardListNode variable
 * \tparam NodePointer is a pointer-to-member to IntrusiveForwardListNode 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, IntrusiveForwardListNode T::* NodePointer, typename U = T>
class IntrusiveForwardList
{
public:

	/// const iterator of elements on the list
	using const_iterator = IntrusiveForwardListConstIterator<T, NodePointer, U>;

	/// 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 = IntrusiveForwardListIterator<T, NodePointer, U>;

	/// 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 IntrusiveForwardList's constructor
	 */

	constexpr IntrusiveForwardList() :
			intrusiveForwardListBase_{}
	{

	}

	/**
	 * \return iterator of "one before the first" element on the list
	 */

	iterator before_begin()
	{
		return iterator{intrusiveForwardListBase_.before_begin()};
	}

	/**
	 * \return const iterator of "one before the first" element on the list
	 */

	const_iterator before_begin() const
	{
		return const_iterator{intrusiveForwardListBase_.before_begin()};
	}

	/**
	 * \return iterator of first element on the list
	 */

	iterator begin()
	{
		return iterator{intrusiveForwardListBase_.begin()};
	}

	/**
	 * \return const iterator of first element on the list
	 */

	const_iterator begin() const
	{
		return const_iterator{intrusiveForwardListBase_.begin()};
	}

	/**
	 * \return const iterator of "one before the first" element on the list
	 */

	const_iterator cbefore_begin() const
	{
		return before_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()
	{
		intrusiveForwardListBase_.clear();
	}

	/**
	 * \return true is the list is empty, false otherwise
	 */

	bool empty() const
	{
		return intrusiveForwardListBase_.empty();
	}

	/**
	 * \return iterator of "one past the last" element on the list
	 */

	iterator end()
	{
		return iterator{intrusiveForwardListBase_.end()};
	}

	/**
	 * \return const iterator of "one past the last" element on the list
	 */

	const_iterator end() const
	{
		return const_iterator{intrusiveForwardListBase_.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 first element from the list.
	 */

	void pop_front()
	{
		erase_after(before_begin());
	}

	/**
	 * \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_after(before_begin(), newElement);
	}

	/**
	 * \brief Swaps contents with another list.
	 *
	 * \param [in] other is a reference to IntrusiveForwardList with which contents of this list will be swapped
	 */

	void swap(IntrusiveForwardList& other)
	{
		intrusiveForwardListBase_.swap(other.intrusiveForwardListBase_);
	}

	/**
	 * \brief Unlinks the element following \a position from the list.
	 *
	 * \note No instance of the list is needed for this operation.
	 *
	 * \param [in] position is an iterator preceding the element which will be unlinked from the list
	 *
	 * \return iterator of the element that was following the element which was unlinked
	 */

	static iterator erase_after(const iterator position)
	{
		auto& positionNode = (*position).*NodePointer;
		const auto afterNextNode = internal::IntrusiveForwardListBase::erase_after(&positionNode);
		return iterator{afterNextNode};
	}

	/**
	 * \brief Links the element in the list after \a position.
	 *
	 * \note No instance of the list is needed for this operation.
	 *
	 * \param [in] position is an iterator of the element after 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_after(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::IntrusiveForwardListBase::insert_after(&positionNode, newElementNode);
		return iterator{&newElementNode};
	}

	/**
	 * \brief Transfers the element from one list to another list after \a position.
	 *
	 * \note No instance of any list is needed for this operation.
	 *
	 * \param [in] position is an iterator of the element after which spliced element will be linked
	 * \param [in] beforeSplicedElement is an iterator of the element preceding the one which will be spliced from one
	 * list to another
	 */

	static void splice_after(const iterator position, const iterator beforeSplicedElement)
	{
		auto& positionNode = (*position).*NodePointer;
		auto& beforeSplicedElementNode = (*beforeSplicedElement).*NodePointer;
		internal::IntrusiveForwardListBase::splice_after(&positionNode, &beforeSplicedElementNode);
	}

	IntrusiveForwardList(const IntrusiveForwardList&) = delete;
	IntrusiveForwardList(IntrusiveForwardList&&) = default;
	const IntrusiveForwardList& operator=(const IntrusiveForwardList&) = delete;
	IntrusiveForwardList& operator=(IntrusiveForwardList&&) = delete;

private:

	/// internal IntrusiveForwardListBase object
	internal::IntrusiveForwardListBase intrusiveForwardListBase_;
};

/**
 * \brief Swaps contents of two lists.
 *
 * \tparam T is the type that has the IntrusiveForwardListNode variable
 * \tparam NodePointer is a pointer-to-member to IntrusiveForwardListNode 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 IntrusiveForwardList with which contents of \a right will be swapped
 * \param [in] right is a reference to IntrusiveForwardList with which contents of \a left will be swapped
 */

template<typename T, IntrusiveForwardListNode T::* NodePointer, typename U = T>
inline void swap(IntrusiveForwardList<T, NodePointer, U>& left, IntrusiveForwardList<T, NodePointer, U>& right)
{
	left.swap(right);
}

}	// namespace estd

#endif	// ESTD_INTRUSIVEFORWARDLIST_HPP_