Intrusive Containers
Loading...
Searching...
No Matches
SortListIn.hpp
Go to the documentation of this file.
1#ifndef SortListIn_HPP
2#define SortListIn_HPP
3
4/**
5 @file SortListIn.hpp
6 @brief Intrusive Double Linked List, Implementation.
7
8 This file defines the implementation for a pair of templates (DListInRoot and DListInNode) that
9 implement an intrusive double linked list.
10
11@copyright (c) 2023-2024 Richard Damon
12@parblock
13MIT License:
14
15Permission is hereby granted, free of charge, to any person obtaining a copy
16of this software and associated documentation files (the "Software"), to deal
17in the Software without restriction, including without limitation the rights
18to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19copies of the Software, and to permit persons to whom the Software is
20furnished to do so, subject to the following conditions:
21
22The above copyright notice and this permission notice shall be included in
23all copies or substantial portions of the Software.
24
25THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31THE SOFTWARE.
32@endparblock
33
34@see DListIn.h
35@see ListIn.h
36@ingroup IntrusiveContainers
37*/
38
39#include "SortListIn.h"
40#include "ListIn.hpp"
41
42/******************************************************************************************
43Implementation
44
45Note that when we need to convert a List or Node pointer/reference to a DListInRoot/ListInNode
46pointer/reference we need to do an explicit static_cast, to avoid ambiguity in the case of
47classes deriving from multiple versions of DListInRoot/ListInNode
48*/
49
50
51template <class R, class N, ContainerThreadSafety s, int n>
53 bool flag = Base::check();
54 // We presume that ListInRoot::check will scan the list
55 return flag;
56}
57
58template <class R, class N, ContainerThreadSafety s, int n>
60 bool flag = Base::check();
61 flag &= static_cast<Root*>(root())->compare(*static_cast<N const*>(this), *next()) >= 0;
62 flag &= static_cast<Root*>(root())->compare(*next(), *static_cast<N const*>(this)) <= 0;
63 return flag;
64}
65
66/**
67Add node to list based on sorting function in Root.
68
69The node is placed such that compare(node, this) will be positive (or zero) for all
70 nodes before this one, and negative for nodes after (or zero if they were added after us)
71
72@param node The node to add to the list
73If node is currently on a list (even us) it is removed before being added to the list
74*/
75template<class R, class N, ContainerThreadSafety s, int n_>
77 Node& mynode = node;
78 // If node is on a list, remove it.
79 // Don't bypass same list, as might be used to update sorting.
80 if(mynode.root() != nullptr) mynode.remove();
81
82 unsigned save = readLock(true);
83
84 N* node1 = nullptr;
85 N* node2 = first();
86 while(node2 && this->compare(*node2, node) >= 0) {
87 node1 = node2;
88 node2 = static_cast<Node*>(node2)->next();
89 }
90
91 // node1 is the node that should be before the provided node
92
93 if (node1) {
94 node1->Node::addAfter(node, true);
95 } else {
96 Base::addFirst(node, true);
97 }
98 readUnlock(save);
99}
100
101
102/**
103Add node to our list, at "natural" point.
104Note that this is the front for singly linked lists and the end for doubly linked lists.
105
106@param node The node to add to the list
107If node is null, Nothing is done.
108If node is currently on a list (even us) it is removed before being added to the list
109*/
110template<class R, class N, ContainerThreadSafety s, int n_>
112 if (node != nullptr) add(*node);
113}
114
115/**
116Add ourself to a list at "natural" position.
117Note that this is the front for singly linked lists, and the end for doubly linked lists.
118
119@param myRoot List to add to.
120*/
121template<class R, class N, ContainerThreadSafety s, int n>
123 static_cast<Root&>(myRoot).add(static_cast<N*>(this));
124}
125
126/**
127Add ourself to a list at "natural" position.
128Note that this is the front for singly linked lists, and the end for doubly linked lists.
129
130@param myRoot List to add to.
131*/
132template<class R, class N, ContainerThreadSafety s, int n>
134 if(myRoot) {
135 static_cast<Root*>(myRoot)->add(static_cast<N*>(this));
136 } else {
137 remove();
138 }
139}
140
141
142/**
143Constructor.
144
145Starts us as an empty list.
146*/
147template <class R, class N, ContainerThreadSafety s, int n>
150
151/**
152Destructor.
153
154Removes all nodes attached to us.
155*/
156template <class R, class N, ContainerThreadSafety s, int n>
158 while (first()) remove(first());
159}
160
161/**
162Constructor.
163
164@param myRoot Pointer to list for node to be added to (if not NULL).
165*/
166template <class R, class N, ContainerThreadSafety s, int n>
168{
169 if (myRoot) addTo(myRoot);
170}
171
172/**
173Constructor.
174
175@param myRoot list we are to be added to.
176*/
177template <class R, class N, ContainerThreadSafety s, int n>
179{
180 addTo(myRoot);
181}
182
183/**
184Destructor.
185
186Remove us from we are on, if any.
187*/
188template <class R, class N, ContainerThreadSafety s, int n>
192#endif
Intrusive Singly Linked List, Implementation.
Intrusive Sorted Double Linked List.
void addTo(R &root)
Add ourself to a list at "natural" position.
Definition SortListIn.hpp:122
SortListInNode(R *root=nullptr)
Constructor.
Definition SortListIn.hpp:167
class SortListInRoot< R, N, s, n > Root
Type of DListInRoot.
Definition SortListIn.h:225
bool check() const override
Definition SortListIn.hpp:59
~SortListInNode()
Destructor.
Definition SortListIn.hpp:189
void add(N &node)
Add node to list based on sorting function in Root.
Definition SortListIn.hpp:76
~SortListInRoot()
Destructor.
Definition SortListIn.hpp:157
SortListInRoot()
Constructor.
Definition SortListIn.hpp:148
class SortListInNode< R, N, s, n > Node
Type of DListIInNode.
Definition SortListIn.h:152
bool check() const override
Definition SortListIn.hpp:52