circular_slist_algorithms.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Olaf Krzikalla 2004-2006.
  4. // (C) Copyright Ion Gaztanaga 2006-2014
  5. //
  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)
  9. //
  10. // See http://www.boost.org/libs/intrusive for documentation.
  11. //
  12. /////////////////////////////////////////////////////////////////////////////
  13. #ifndef BOOST_INTRUSIVE_CIRCULAR_SLIST_ALGORITHMS_HPP
  14. #define BOOST_INTRUSIVE_CIRCULAR_SLIST_ALGORITHMS_HPP
  15. #include <cstddef>
  16. #include <boost/intrusive/detail/config_begin.hpp>
  17. #include <boost/intrusive/intrusive_fwd.hpp>
  18. #include <boost/intrusive/detail/common_slist_algorithms.hpp>
  19. #include <boost/intrusive/detail/algo_type.hpp>
  20. #if defined(BOOST_HAS_PRAGMA_ONCE)
  21. # pragma once
  22. #endif
  23. namespace boost {
  24. namespace intrusive {
  25. //! circular_slist_algorithms provides basic algorithms to manipulate nodes
  26. //! forming a circular singly linked list. An empty circular list is formed by a node
  27. //! whose pointer to the next node points to itself.
  28. //!
  29. //! circular_slist_algorithms is configured with a NodeTraits class, which encapsulates the
  30. //! information about the node to be manipulated. NodeTraits must support the
  31. //! following interface:
  32. //!
  33. //! <b>Typedefs</b>:
  34. //!
  35. //! <tt>node</tt>: The type of the node that forms the circular list
  36. //!
  37. //! <tt>node_ptr</tt>: A pointer to a node
  38. //!
  39. //! <tt>const_node_ptr</tt>: A pointer to a const node
  40. //!
  41. //! <b>Static functions</b>:
  42. //!
  43. //! <tt>static node_ptr get_next(const_node_ptr n);</tt>
  44. //!
  45. //! <tt>static void set_next(node_ptr n, node_ptr next);</tt>
  46. template<class NodeTraits>
  47. class circular_slist_algorithms
  48. /// @cond
  49. : public detail::common_slist_algorithms<NodeTraits>
  50. /// @endcond
  51. {
  52. /// @cond
  53. typedef detail::common_slist_algorithms<NodeTraits> base_t;
  54. /// @endcond
  55. public:
  56. typedef typename NodeTraits::node node;
  57. typedef typename NodeTraits::node_ptr node_ptr;
  58. typedef typename NodeTraits::const_node_ptr const_node_ptr;
  59. typedef NodeTraits node_traits;
  60. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  61. //! <b>Effects</b>: Constructs an non-used list element, putting the next
  62. //! pointer to null:
  63. //! <tt>NodeTraits::get_next(this_node) == node_ptr()</tt>
  64. //!
  65. //! <b>Complexity</b>: Constant
  66. //!
  67. //! <b>Throws</b>: Nothing.
  68. static void init(node_ptr this_node);
  69. //! <b>Requires</b>: this_node must be in a circular list or be an empty circular list.
  70. //!
  71. //! <b>Effects</b>: Returns true is "this_node" is the only node of a circular list:
  72. //! or it's a not inserted node:
  73. //! <tt>return node_ptr() == NodeTraits::get_next(this_node) || NodeTraits::get_next(this_node) == this_node</tt>
  74. //!
  75. //! <b>Complexity</b>: Constant
  76. //!
  77. //! <b>Throws</b>: Nothing.
  78. static bool unique(const_node_ptr this_node);
  79. //! <b>Effects</b>: Returns true is "this_node" has the same state as
  80. //! if it was inited using "init(node_ptr)"
  81. //!
  82. //! <b>Complexity</b>: Constant
  83. //!
  84. //! <b>Throws</b>: Nothing.
  85. static bool inited(const_node_ptr this_node);
  86. //! <b>Requires</b>: prev_node must be in a circular list or be an empty circular list.
  87. //!
  88. //! <b>Effects</b>: Unlinks the next node of prev_node from the circular list.
  89. //!
  90. //! <b>Complexity</b>: Constant
  91. //!
  92. //! <b>Throws</b>: Nothing.
  93. static void unlink_after(node_ptr prev_node);
  94. //! <b>Requires</b>: prev_node and last_node must be in a circular list
  95. //! or be an empty circular list.
  96. //!
  97. //! <b>Effects</b>: Unlinks the range (prev_node, last_node) from the circular list.
  98. //!
  99. //! <b>Complexity</b>: Constant
  100. //!
  101. //! <b>Throws</b>: Nothing.
  102. static void unlink_after(node_ptr prev_node, node_ptr last_node);
  103. //! <b>Requires</b>: prev_node must be a node of a circular list.
  104. //!
  105. //! <b>Effects</b>: Links this_node after prev_node in the circular list.
  106. //!
  107. //! <b>Complexity</b>: Constant
  108. //!
  109. //! <b>Throws</b>: Nothing.
  110. static void link_after(node_ptr prev_node, node_ptr this_node);
  111. //! <b>Requires</b>: b and e must be nodes of the same circular list or an empty range.
  112. //! and p must be a node of a different circular list.
  113. //!
  114. //! <b>Effects</b>: Removes the nodes from (b, e] range from their circular list and inserts
  115. //! them after p in p's circular list.
  116. //!
  117. //! <b>Complexity</b>: Constant
  118. //!
  119. //! <b>Throws</b>: Nothing.
  120. static void transfer_after(node_ptr p, node_ptr b, node_ptr e);
  121. #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  122. //! <b>Effects</b>: Constructs an empty list, making this_node the only
  123. //! node of the circular list:
  124. //! <tt>NodeTraits::get_next(this_node) == this_node</tt>.
  125. //!
  126. //! <b>Complexity</b>: Constant
  127. //!
  128. //! <b>Throws</b>: Nothing.
  129. BOOST_INTRUSIVE_FORCEINLINE static void init_header(node_ptr this_node)
  130. { NodeTraits::set_next(this_node, this_node); }
  131. //! <b>Requires</b>: this_node and prev_init_node must be in the same circular list.
  132. //!
  133. //! <b>Effects</b>: Returns the previous node of this_node in the circular list starting.
  134. //! the search from prev_init_node. The first node checked for equality
  135. //! is NodeTraits::get_next(prev_init_node).
  136. //!
  137. //! <b>Complexity</b>: Linear to the number of elements between prev_init_node and this_node.
  138. //!
  139. //! <b>Throws</b>: Nothing.
  140. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_previous_node(const node_ptr &prev_init_node, const node_ptr &this_node)
  141. { return base_t::get_previous_node(prev_init_node, this_node); }
  142. //! <b>Requires</b>: this_node must be in a circular list or be an empty circular list.
  143. //!
  144. //! <b>Effects</b>: Returns the previous node of this_node in the circular list.
  145. //!
  146. //! <b>Complexity</b>: Linear to the number of elements in the circular list.
  147. //!
  148. //! <b>Throws</b>: Nothing.
  149. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_previous_node(const node_ptr & this_node)
  150. { return base_t::get_previous_node(this_node, this_node); }
  151. //! <b>Requires</b>: this_node must be in a circular list or be an empty circular list.
  152. //!
  153. //! <b>Effects</b>: Returns the previous node of the previous node of this_node in the circular list.
  154. //!
  155. //! <b>Complexity</b>: Linear to the number of elements in the circular list.
  156. //!
  157. //! <b>Throws</b>: Nothing.
  158. BOOST_INTRUSIVE_FORCEINLINE static node_ptr get_previous_previous_node(const node_ptr & this_node)
  159. { return get_previous_previous_node(this_node, this_node); }
  160. //! <b>Requires</b>: this_node and p must be in the same circular list.
  161. //!
  162. //! <b>Effects</b>: Returns the previous node of the previous node of this_node in the
  163. //! circular list starting. the search from p. The first node checked
  164. //! for equality is NodeTraits::get_next((NodeTraits::get_next(p)).
  165. //!
  166. //! <b>Complexity</b>: Linear to the number of elements in the circular list.
  167. //!
  168. //! <b>Throws</b>: Nothing.
  169. static node_ptr get_previous_previous_node(node_ptr p, const node_ptr & this_node)
  170. {
  171. node_ptr p_next = NodeTraits::get_next(p);
  172. node_ptr p_next_next = NodeTraits::get_next(p_next);
  173. while (this_node != p_next_next){
  174. p = p_next;
  175. p_next = p_next_next;
  176. p_next_next = NodeTraits::get_next(p_next);
  177. }
  178. return p;
  179. }
  180. //! <b>Requires</b>: this_node must be in a circular list or be an empty circular list.
  181. //!
  182. //! <b>Effects</b>: Returns the number of nodes in a circular list. If the circular list
  183. //! is empty, returns 1.
  184. //!
  185. //! <b>Complexity</b>: Linear
  186. //!
  187. //! <b>Throws</b>: Nothing.
  188. static std::size_t count(const const_node_ptr & this_node)
  189. {
  190. std::size_t result = 0;
  191. const_node_ptr p = this_node;
  192. do{
  193. p = NodeTraits::get_next(p);
  194. ++result;
  195. } while (p != this_node);
  196. return result;
  197. }
  198. //! <b>Requires</b>: this_node must be in a circular list, be an empty circular list or be inited.
  199. //!
  200. //! <b>Effects</b>: Unlinks the node from the circular list.
  201. //!
  202. //! <b>Complexity</b>: Linear to the number of elements in the circular list
  203. //!
  204. //! <b>Throws</b>: Nothing.
  205. BOOST_INTRUSIVE_FORCEINLINE static void unlink(node_ptr this_node)
  206. {
  207. if(NodeTraits::get_next(this_node))
  208. base_t::unlink_after(get_previous_node(this_node));
  209. }
  210. //! <b>Requires</b>: nxt_node must be a node of a circular list.
  211. //!
  212. //! <b>Effects</b>: Links this_node before nxt_node in the circular list.
  213. //!
  214. //! <b>Complexity</b>: Linear to the number of elements in the circular list.
  215. //!
  216. //! <b>Throws</b>: Nothing.
  217. BOOST_INTRUSIVE_FORCEINLINE static void link_before (node_ptr nxt_node, node_ptr this_node)
  218. { base_t::link_after(get_previous_node(nxt_node), this_node); }
  219. //! <b>Requires</b>: this_node and other_node must be nodes inserted
  220. //! in circular lists or be empty circular lists.
  221. //!
  222. //! <b>Effects</b>: Swaps the position of the nodes: this_node is inserted in
  223. //! other_nodes position in the second circular list and the other_node is inserted
  224. //! in this_node's position in the first circular list.
  225. //!
  226. //! <b>Complexity</b>: Linear to number of elements of both lists
  227. //!
  228. //! <b>Throws</b>: Nothing.
  229. static void swap_nodes(node_ptr this_node, node_ptr other_node)
  230. {
  231. if (other_node == this_node)
  232. return;
  233. const node_ptr this_next = NodeTraits::get_next(this_node);
  234. const node_ptr other_next = NodeTraits::get_next(other_node);
  235. const bool this_null = !this_next;
  236. const bool other_null = !other_next;
  237. const bool this_empty = this_next == this_node;
  238. const bool other_empty = other_next == other_node;
  239. if(!(other_null || other_empty)){
  240. NodeTraits::set_next(this_next == other_node ? other_node : get_previous_node(other_node), this_node );
  241. }
  242. if(!(this_null | this_empty)){
  243. NodeTraits::set_next(other_next == this_node ? this_node : get_previous_node(this_node), other_node );
  244. }
  245. NodeTraits::set_next(this_node, other_empty ? this_node : (other_next == this_node ? other_node : other_next) );
  246. NodeTraits::set_next(other_node, this_empty ? other_node : (this_next == other_node ? this_node : this_next ) );
  247. }
  248. //! <b>Effects</b>: Reverses the order of elements in the list.
  249. //!
  250. //! <b>Throws</b>: Nothing.
  251. //!
  252. //! <b>Complexity</b>: This function is linear to the contained elements.
  253. static void reverse(node_ptr p)
  254. {
  255. node_ptr i = NodeTraits::get_next(p), e(p);
  256. for (;;) {
  257. node_ptr nxt(NodeTraits::get_next(i));
  258. if (nxt == e)
  259. break;
  260. base_t::transfer_after(e, i, nxt);
  261. }
  262. }
  263. //! <b>Effects</b>: Moves the node p n positions towards the end of the list.
  264. //!
  265. //! <b>Returns</b>: The previous node of p after the function if there has been any movement,
  266. //! Null if n leads to no movement.
  267. //!
  268. //! <b>Throws</b>: Nothing.
  269. //!
  270. //! <b>Complexity</b>: Linear to the number of elements plus the number moved positions.
  271. static node_ptr move_backwards(node_ptr p, std::size_t n)
  272. {
  273. //Null shift, nothing to do
  274. if(!n) return node_ptr();
  275. node_ptr first = NodeTraits::get_next(p);
  276. //count() == 1 or 2, nothing to do
  277. if(NodeTraits::get_next(first) == p)
  278. return node_ptr();
  279. bool end_found = false;
  280. node_ptr new_last = node_ptr();
  281. //Now find the new last node according to the shift count.
  282. //If we find p before finding the new last node
  283. //unlink p, shortcut the search now that we know the size of the list
  284. //and continue.
  285. for(std::size_t i = 1; i <= n; ++i){
  286. new_last = first;
  287. first = NodeTraits::get_next(first);
  288. if(first == p){
  289. //Shortcut the shift with the modulo of the size of the list
  290. n %= i;
  291. if(!n)
  292. return node_ptr();
  293. i = 0;
  294. //Unlink p and continue the new first node search
  295. first = NodeTraits::get_next(p);
  296. base_t::unlink_after(new_last);
  297. end_found = true;
  298. }
  299. }
  300. //If the p has not been found in the previous loop, find it
  301. //starting in the new first node and unlink it
  302. if(!end_found){
  303. base_t::unlink_after(base_t::get_previous_node(first, p));
  304. }
  305. //Now link p after the new last node
  306. base_t::link_after(new_last, p);
  307. return new_last;
  308. }
  309. //! <b>Effects</b>: Moves the node p n positions towards the beginning of the list.
  310. //!
  311. //! <b>Returns</b>: The previous node of p after the function if there has been any movement,
  312. //! Null if n leads equals to no movement.
  313. //!
  314. //! <b>Throws</b>: Nothing.
  315. //!
  316. //! <b>Complexity</b>: Linear to the number of elements plus the number moved positions.
  317. static node_ptr move_forward(node_ptr p, std::size_t n)
  318. {
  319. //Null shift, nothing to do
  320. if(!n) return node_ptr();
  321. node_ptr first = node_traits::get_next(p);
  322. //count() == 1 or 2, nothing to do
  323. if(node_traits::get_next(first) == p) return node_ptr();
  324. //Iterate until p is found to know where the current last node is.
  325. //If the shift count is less than the size of the list, we can also obtain
  326. //the position of the new last node after the shift.
  327. node_ptr old_last(first), next_to_it, new_last(p);
  328. std::size_t distance = 1;
  329. while(p != (next_to_it = node_traits::get_next(old_last))){
  330. if(++distance > n)
  331. new_last = node_traits::get_next(new_last);
  332. old_last = next_to_it;
  333. }
  334. //If the shift was bigger or equal than the size, obtain the equivalent
  335. //forward shifts and find the new last node.
  336. if(distance <= n){
  337. //Now find the equivalent forward shifts.
  338. //Shortcut the shift with the modulo of the size of the list
  339. std::size_t new_before_last_pos = (distance - (n % distance))% distance;
  340. //If the shift is a multiple of the size there is nothing to do
  341. if(!new_before_last_pos) return node_ptr();
  342. for( new_last = p
  343. ; new_before_last_pos--
  344. ; new_last = node_traits::get_next(new_last)){
  345. //empty
  346. }
  347. }
  348. //Now unlink p and link it after the new last node
  349. base_t::unlink_after(old_last);
  350. base_t::link_after(new_last, p);
  351. return new_last;
  352. }
  353. };
  354. /// @cond
  355. template<class NodeTraits>
  356. struct get_algo<CircularSListAlgorithms, NodeTraits>
  357. {
  358. typedef circular_slist_algorithms<NodeTraits> type;
  359. };
  360. /// @endcond
  361. } //namespace intrusive
  362. } //namespace boost
  363. #include <boost/intrusive/detail/config_end.hpp>
  364. #endif //BOOST_INTRUSIVE_CIRCULAR_SLIST_ALGORITHMS_HPP