linear_slist_algorithms.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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_LINEAR_SLIST_ALGORITHMS_HPP
  14. #define BOOST_INTRUSIVE_LINEAR_SLIST_ALGORITHMS_HPP
  15. #include <boost/intrusive/detail/config_begin.hpp>
  16. #include <boost/intrusive/intrusive_fwd.hpp>
  17. #include <boost/intrusive/detail/common_slist_algorithms.hpp>
  18. #include <boost/intrusive/detail/algo_type.hpp>
  19. #include <cstddef>
  20. #include <boost/intrusive/detail/minimal_pair_header.hpp> //std::pair
  21. #if defined(BOOST_HAS_PRAGMA_ONCE)
  22. # pragma once
  23. #endif
  24. namespace boost {
  25. namespace intrusive {
  26. //! linear_slist_algorithms provides basic algorithms to manipulate nodes
  27. //! forming a linear singly linked list.
  28. //!
  29. //! linear_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 linear 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 linear_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(const 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 if
  80. //! 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(const 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 linear list.
  98. //!
  99. //! <b>Complexity</b>: Constant
  100. //!
  101. //! <b>Throws</b>: Nothing.
  102. static void unlink_after(const node_ptr & prev_node, const node_ptr & last_node);
  103. //! <b>Requires</b>: prev_node must be a node of a linear list.
  104. //!
  105. //! <b>Effects</b>: Links this_node after prev_node in the linear list.
  106. //!
  107. //! <b>Complexity</b>: Constant
  108. //!
  109. //! <b>Throws</b>: Nothing.
  110. static void link_after(const node_ptr & prev_node, const node_ptr & this_node);
  111. //! <b>Requires</b>: b and e must be nodes of the same linear list or an empty range.
  112. //! and p must be a node of a different linear list.
  113. //!
  114. //! <b>Effects</b>: Removes the nodes from (b, e] range from their linear list and inserts
  115. //! them after p in p's linear list.
  116. //!
  117. //! <b>Complexity</b>: Constant
  118. //!
  119. //! <b>Throws</b>: Nothing.
  120. static void transfer_after(const node_ptr & p, const node_ptr & b, const 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(const node_ptr & this_node)
  130. { NodeTraits::set_next(this_node, node_ptr ()); }
  131. //! <b>Requires</b>: this_node and prev_init_node must be in the same linear list.
  132. //!
  133. //! <b>Effects</b>: Returns the previous node of this_node in the linear 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 linear list or be an empty linear list.
  143. //!
  144. //! <b>Effects</b>: Returns the number of nodes in a linear list. If the linear list
  145. //! is empty, returns 1.
  146. //!
  147. //! <b>Complexity</b>: Linear
  148. //!
  149. //! <b>Throws</b>: Nothing.
  150. static std::size_t count(const const_node_ptr & this_node)
  151. {
  152. std::size_t result = 0;
  153. const_node_ptr p = this_node;
  154. do{
  155. p = NodeTraits::get_next(p);
  156. ++result;
  157. } while (p);
  158. return result;
  159. }
  160. //! <b>Requires</b>: this_node and other_node must be nodes inserted
  161. //! in linear lists or be empty linear lists.
  162. //!
  163. //! <b>Effects</b>: Moves all the nodes previously chained after this_node after other_node
  164. //! and vice-versa.
  165. //!
  166. //! <b>Complexity</b>: Constant
  167. //!
  168. //! <b>Throws</b>: Nothing.
  169. static void swap_trailing_nodes(node_ptr this_node, node_ptr other_node)
  170. {
  171. node_ptr this_nxt = NodeTraits::get_next(this_node);
  172. node_ptr other_nxt = NodeTraits::get_next(other_node);
  173. NodeTraits::set_next(this_node, other_nxt);
  174. NodeTraits::set_next(other_node, this_nxt);
  175. }
  176. //! <b>Effects</b>: Reverses the order of elements in the list.
  177. //!
  178. //! <b>Returns</b>: The new first node of the list.
  179. //!
  180. //! <b>Throws</b>: Nothing.
  181. //!
  182. //! <b>Complexity</b>: This function is linear to the contained elements.
  183. static node_ptr reverse(node_ptr p)
  184. {
  185. if(!p) return node_ptr();
  186. node_ptr i = NodeTraits::get_next(p);
  187. node_ptr first(p);
  188. while(i){
  189. node_ptr nxti(NodeTraits::get_next(i));
  190. base_t::unlink_after(p);
  191. NodeTraits::set_next(i, first);
  192. first = i;
  193. i = nxti;
  194. }
  195. return first;
  196. }
  197. //! <b>Effects</b>: Moves the first n nodes starting at p to the end of the list.
  198. //!
  199. //! <b>Returns</b>: A pair containing the new first and last node of the list or
  200. //! if there has been any movement, a null pair if n leads to no movement.
  201. //!
  202. //! <b>Throws</b>: Nothing.
  203. //!
  204. //! <b>Complexity</b>: Linear to the number of elements plus the number moved positions.
  205. static std::pair<node_ptr, node_ptr> move_first_n_backwards(node_ptr p, std::size_t n)
  206. {
  207. std::pair<node_ptr, node_ptr> ret;
  208. //Null shift, or count() == 0 or 1, nothing to do
  209. if(!n || !p || !NodeTraits::get_next(p)){
  210. return ret;
  211. }
  212. node_ptr first = p;
  213. bool end_found = false;
  214. node_ptr new_last = node_ptr();
  215. node_ptr old_last = node_ptr();
  216. //Now find the new last node according to the shift count.
  217. //If we find 0 before finding the new last node
  218. //unlink p, shortcut the search now that we know the size of the list
  219. //and continue.
  220. for(std::size_t i = 1; i <= n; ++i){
  221. new_last = first;
  222. first = NodeTraits::get_next(first);
  223. if(first == node_ptr()){
  224. //Shortcut the shift with the modulo of the size of the list
  225. n %= i;
  226. if(!n) return ret;
  227. old_last = new_last;
  228. i = 0;
  229. //Unlink p and continue the new first node search
  230. first = p;
  231. //unlink_after(new_last);
  232. end_found = true;
  233. }
  234. }
  235. //If the p has not been found in the previous loop, find it
  236. //starting in the new first node and unlink it
  237. if(!end_found){
  238. old_last = base_t::get_previous_node(first, node_ptr());
  239. }
  240. //Now link p after the new last node
  241. NodeTraits::set_next(old_last, p);
  242. NodeTraits::set_next(new_last, node_ptr());
  243. ret.first = first;
  244. ret.second = new_last;
  245. return ret;
  246. }
  247. //! <b>Effects</b>: Moves the first n nodes starting at p to the beginning of the list.
  248. //!
  249. //! <b>Returns</b>: A pair containing the new first and last node of the list or
  250. //! if there has been any movement, a null pair if n leads to no movement.
  251. //!
  252. //! <b>Throws</b>: Nothing.
  253. //!
  254. //! <b>Complexity</b>: Linear to the number of elements plus the number moved positions.
  255. static std::pair<node_ptr, node_ptr> move_first_n_forward(node_ptr p, std::size_t n)
  256. {
  257. std::pair<node_ptr, node_ptr> ret;
  258. //Null shift, or count() == 0 or 1, nothing to do
  259. if(!n || !p || !NodeTraits::get_next(p))
  260. return ret;
  261. node_ptr first = p;
  262. //Iterate until p is found to know where the current last node is.
  263. //If the shift count is less than the size of the list, we can also obtain
  264. //the position of the new last node after the shift.
  265. node_ptr old_last(first), next_to_it, new_last(p);
  266. std::size_t distance = 1;
  267. while(!!(next_to_it = node_traits::get_next(old_last))){
  268. if(distance++ > n)
  269. new_last = node_traits::get_next(new_last);
  270. old_last = next_to_it;
  271. }
  272. //If the shift was bigger or equal than the size, obtain the equivalent
  273. //forward shifts and find the new last node.
  274. if(distance <= n){
  275. //Now find the equivalent forward shifts.
  276. //Shortcut the shift with the modulo of the size of the list
  277. std::size_t new_before_last_pos = (distance - (n % distance))% distance;
  278. //If the shift is a multiple of the size there is nothing to do
  279. if(!new_before_last_pos)
  280. return ret;
  281. for( new_last = p
  282. ; --new_before_last_pos
  283. ; new_last = node_traits::get_next(new_last)){
  284. //empty
  285. }
  286. }
  287. //Get the first new node
  288. node_ptr new_first(node_traits::get_next(new_last));
  289. //Now put the old beginning after the old end
  290. NodeTraits::set_next(old_last, p);
  291. NodeTraits::set_next(new_last, node_ptr());
  292. ret.first = new_first;
  293. ret.second = new_last;
  294. return ret;
  295. }
  296. };
  297. /// @cond
  298. template<class NodeTraits>
  299. struct get_algo<LinearSListAlgorithms, NodeTraits>
  300. {
  301. typedef linear_slist_algorithms<NodeTraits> type;
  302. };
  303. /// @endcond
  304. } //namespace intrusive
  305. } //namespace boost
  306. #include <boost/intrusive/detail/config_end.hpp>
  307. #endif //BOOST_INTRUSIVE_LINEAR_SLIST_ALGORITHMS_HPP