1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Olaf Krzikalla 2004-2006.
4 // (C) Copyright Ion Gaztanaga 2006-2008
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 // See http://www.boost.org/libs/intrusive for documentation.
12 /////////////////////////////////////////////////////////////////////////////
14 #ifndef BOOST_INTRUSIVE_CIRCULAR_LIST_ALGORITHMS_HPP
15 #define BOOST_INTRUSIVE_CIRCULAR_LIST_ALGORITHMS_HPP
17 #include <boost/intrusive/detail/config_begin.hpp>
18 #include <boost/intrusive/intrusive_fwd.hpp>
24 //! circular_list_algorithms provides basic algorithms to manipulate nodes
25 //! forming a circular doubly linked list. An empty circular list is formed by a node
26 //! whose pointers point to itself.
28 //! circular_list_algorithms is configured with a NodeTraits class, which encapsulates the
29 //! information about the node to be manipulated. NodeTraits must support the
30 //! following interface:
34 //! <tt>node</tt>: The type of the node that forms the circular list
36 //! <tt>node_ptr</tt>: A pointer to a node
38 //! <tt>const_node_ptr</tt>: A pointer to a const node
40 //! <b>Static functions</b>:
42 //! <tt>static node_ptr get_previous(const_node_ptr n);</tt>
44 //! <tt>static void set_previous(node_ptr n, node_ptr prev);</tt>
46 //! <tt>static node_ptr get_next(const_node_ptr n);</tt>
48 //! <tt>static void set_next(node_ptr n, node_ptr next);</tt>
49 template<class NodeTraits
>
50 class circular_list_algorithms
53 typedef typename
NodeTraits::node node
;
54 typedef typename
NodeTraits::node_ptr node_ptr
;
55 typedef typename
NodeTraits::const_node_ptr const_node_ptr
;
56 typedef NodeTraits node_traits
;
58 //! <b>Effects</b>: Constructs an non-used list element, so that
59 //! inited(this_node) == true
61 //! <b>Complexity</b>: Constant
63 //! <b>Throws</b>: Nothing.
64 static void init(node_ptr this_node
)
66 NodeTraits::set_next(this_node
, node_ptr(0));
67 NodeTraits::set_previous(this_node
, node_ptr(0));
70 //! <b>Effects</b>: Returns true is "this_node" is in a non-used state
71 //! as if it was initialized by the "init" function.
73 //! <b>Complexity</b>: Constant
75 //! <b>Throws</b>: Nothing.
76 static bool inited(const_node_ptr this_node
)
77 { return !NodeTraits::get_next(this_node
); }
79 //! <b>Effects</b>: Constructs an empty list, making this_node the only
80 //! node of the circular list:
81 //! <tt>NodeTraits::get_next(this_node) == NodeTraits::get_previous(this_node)
82 //! == this_node</tt>.
84 //! <b>Complexity</b>: Constant
86 //! <b>Throws</b>: Nothing.
87 static void init_header(node_ptr this_node
)
89 NodeTraits::set_next(this_node
, this_node
);
90 NodeTraits::set_previous(this_node
, this_node
);
94 //! <b>Requires</b>: this_node must be in a circular list or be an empty circular list.
96 //! <b>Effects</b>: Returns true is "this_node" is the only node of a circular list:
97 //! <tt>return NodeTraits::get_next(this_node) == this_node</tt>
99 //! <b>Complexity</b>: Constant
101 //! <b>Throws</b>: Nothing.
102 static bool unique(const_node_ptr this_node
)
104 node_ptr next
= NodeTraits::get_next(this_node
);
105 return !next
|| next
== this_node
;
108 //! <b>Requires</b>: this_node must be in a circular list or be an empty circular list.
110 //! <b>Effects</b>: Returns the number of nodes in a circular list. If the circular list
111 //! is empty, returns 1.
113 //! <b>Complexity</b>: Constant
115 //! <b>Throws</b>: Nothing.
116 static std::size_t count(const_node_ptr this_node
)
118 std::size_t result
= 0;
119 const_node_ptr p
= this_node
;
121 p
= NodeTraits::get_next(p
);
123 }while (p
!= this_node
);
127 //! <b>Requires</b>: this_node must be in a circular list or be an empty circular list.
129 //! <b>Effects</b>: Unlinks the node from the circular list.
131 //! <b>Complexity</b>: Constant
133 //! <b>Throws</b>: Nothing.
134 static node_ptr
unlink(node_ptr this_node
)
136 if(NodeTraits::get_next(this_node
)){
137 node_ptr
next(NodeTraits::get_next(this_node
));
138 node_ptr
prev(NodeTraits::get_previous(this_node
));
139 NodeTraits::set_next(prev
, next
);
140 NodeTraits::set_previous(next
, prev
);
148 //! <b>Requires</b>: b and e must be nodes of the same circular list or an empty range.
150 //! <b>Effects</b>: Unlinks the node [b, e) from the circular list.
152 //! <b>Complexity</b>: Constant
154 //! <b>Throws</b>: Nothing.
155 static void unlink(node_ptr b
, node_ptr e
)
158 node_ptr
prevb(NodeTraits::get_previous(b
));
159 NodeTraits::set_previous(e
, prevb
);
160 NodeTraits::set_next(prevb
, e
);
164 //! <b>Requires</b>: nxt_node must be a node of a circular list.
166 //! <b>Effects</b>: Links this_node before nxt_node in the circular list.
168 //! <b>Complexity</b>: Constant
170 //! <b>Throws</b>: Nothing.
171 static void link_before(node_ptr nxt_node
, node_ptr this_node
)
173 node_ptr
prev(NodeTraits::get_previous(nxt_node
));
174 NodeTraits::set_previous(this_node
, prev
);
175 NodeTraits::set_next(prev
, this_node
);
176 NodeTraits::set_previous(nxt_node
, this_node
);
177 NodeTraits::set_next(this_node
, nxt_node
);
180 //! <b>Requires</b>: prev_node must be a node of a circular list.
182 //! <b>Effects</b>: Links this_node after prev_node in the circular list.
184 //! <b>Complexity</b>: Constant
186 //! <b>Throws</b>: Nothing.
187 static void link_after(node_ptr prev_node
, node_ptr this_node
)
189 node_ptr
next(NodeTraits::get_next(prev_node
));
190 NodeTraits::set_previous(this_node
, prev_node
);
191 NodeTraits::set_next(this_node
, next
);
192 NodeTraits::set_previous(next
, this_node
);
193 NodeTraits::set_next(prev_node
, this_node
);
196 //! <b>Requires</b>: this_node and other_node must be nodes inserted
197 //! in circular lists or be empty circular lists.
199 //! <b>Effects</b>: Swaps the position of the nodes: this_node is inserted in
200 //! other_nodes position in the second circular list and the other_node is inserted
201 //! in this_node's position in the first circular list.
203 //! <b>Complexity</b>: Constant
205 //! <b>Throws</b>: Nothing.
207 static void swap_nodes(node_ptr this_node, node_ptr other_node)
210 if (other_node == this_node)
212 bool empty1 = unique(this_node);
213 bool empty2 = unique(other_node);
215 node_ptr next_this(NodeTraits::get_next(this_node));
216 node_ptr prev_this(NodeTraits::get_previous(this_node));
217 node_ptr next_other(NodeTraits::get_next(other_node));
218 node_ptr prev_other(NodeTraits::get_previous(other_node));
221 NodeTraits::set_next(this_node, next_other);
222 NodeTraits::set_next(other_node, next_this);
224 NodeTraits::set_previous(this_node, prev_other);
225 NodeTraits::set_previous(other_node, prev_this);
231 NodeTraits::set_next(prev_other, this_node);
232 NodeTraits::set_previous(next_other, this_node);
238 NodeTraits::set_next(prev_this, other_node);
239 NodeTraits::set_previous(next_this, other_node);
246 static void swap_prev(node_ptr this_node
, node_ptr other_node
)
248 node_ptr
temp(NodeTraits::get_previous(this_node
));
249 NodeTraits::set_previous(this_node
, NodeTraits::get_previous(other_node
));
250 NodeTraits::set_previous(other_node
, temp
);
252 static void swap_next(node_ptr this_node
, node_ptr other_node
)
254 node_ptr
temp(NodeTraits::get_next(this_node
));
255 NodeTraits::set_next(this_node
, NodeTraits::get_next(other_node
));
256 NodeTraits::set_next(other_node
, temp
);
260 static void swap_nodes(node_ptr this_node
, node_ptr other_node
)
262 if (other_node
== this_node
)
264 bool this_inited
= inited(this_node
);
265 bool other_inited
= inited(other_node
);
267 init_header(this_node
);
270 init_header(other_node
);
273 node_ptr
next_this(NodeTraits::get_next(this_node
));
274 node_ptr
prev_this(NodeTraits::get_previous(this_node
));
275 node_ptr
next_other(NodeTraits::get_next(other_node
));
276 node_ptr
prev_other(NodeTraits::get_previous(other_node
));
277 //these first two swaps must happen before the other two
278 swap_prev(next_this
, next_other
);
279 swap_next(prev_this
, prev_other
);
280 swap_next(this_node
, other_node
);
281 swap_prev(this_node
, other_node
);
291 //! <b>Requires</b>: b and e must be nodes of the same circular list or an empty range.
292 //! and p must be a node of a different circular list or may not be an iterator in
295 //! <b>Effects</b>: Removes the nodes from [b, e) range from their circular list and inserts
296 //! them before p in p's circular list.
298 //! <b>Complexity</b>: Constant
300 //! <b>Throws</b>: Nothing.
301 static void transfer(node_ptr p
, node_ptr b
, node_ptr e
)
304 node_ptr
prev_p(NodeTraits::get_previous(p
));
305 node_ptr
prev_b(NodeTraits::get_previous(b
));
306 node_ptr
prev_e(NodeTraits::get_previous(e
));
307 NodeTraits::set_next(prev_e
, p
);
308 NodeTraits::set_previous(p
, prev_e
);
309 NodeTraits::set_next(prev_b
, e
);
310 NodeTraits::set_previous(e
, prev_b
);
311 NodeTraits::set_next(prev_p
, b
);
312 NodeTraits::set_previous(b
, prev_p
);
316 //! <b>Requires</b>: i must a node of a circular list
317 //! and p must be a node of a different circular list.
319 //! <b>Effects</b>: Removes the node i from its circular list and inserts
320 //! it before p in p's circular list.
321 //! If p == i or p == NodeTraits::get_next(i), this function is a null operation.
323 //! <b>Complexity</b>: Constant
325 //! <b>Throws</b>: Nothing.
326 static void transfer(node_ptr p
, node_ptr i
)
328 node_ptr
n(NodeTraits::get_next(i
));
329 if(n
!= p
&& i
!= p
){
330 node_ptr
prev_p(NodeTraits::get_previous(p
));
331 node_ptr
prev_i(NodeTraits::get_previous(i
));
332 NodeTraits::set_next(prev_p
, i
);
333 NodeTraits::set_previous(i
, prev_p
);
334 NodeTraits::set_next(i
, p
);
335 NodeTraits::set_previous(p
, i
);
336 NodeTraits::set_previous(n
, prev_i
);
337 NodeTraits::set_next(prev_i
, n
);
342 //! <b>Effects</b>: Reverses the order of elements in the list.
344 //! <b>Throws</b>: Nothing.
346 //! <b>Complexity</b>: This function is linear time.
347 static void reverse(node_ptr p
)
349 node_ptr
f(NodeTraits::get_next(p
));
350 node_ptr
i(NodeTraits::get_next(f
)), e(p
);
354 i
= NodeTraits::get_next(i
);
360 //! <b>Effects</b>: Moves the node p n positions towards the end of the list.
362 //! <b>Throws</b>: Nothing.
364 //! <b>Complexity</b>: Linear to the number of moved positions.
365 static void move_backwards(node_ptr p
, std::size_t n
)
367 //Null shift, nothing to do
369 node_ptr first
= NodeTraits::get_next(p
);
370 //size() == 0 or 1, nothing to do
371 if(first
== NodeTraits::get_previous(p
)) return;
373 //Now get the new first node
375 first
= NodeTraits::get_next(first
);
377 link_before(first
, p
);
380 //! <b>Effects</b>: Moves the node p n positions towards the beginning of the list.
382 //! <b>Throws</b>: Nothing.
384 //! <b>Complexity</b>: Linear to the number of moved positions.
385 static void move_forward(node_ptr p
, std::size_t n
)
387 //Null shift, nothing to do
389 node_ptr last
= NodeTraits::get_previous(p
);
390 //size() == 0 or 1, nothing to do
391 if(last
== NodeTraits::get_next(p
)) return;
394 //Now get the new last node
396 last
= NodeTraits::get_previous(last
);
402 } //namespace intrusive
405 #include <boost/intrusive/detail/config_end.hpp>
407 #endif //BOOST_INTRUSIVE_CIRCULAR_LIST_ALGORITHMS_HPP