linear_slist_algorithms.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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/twin.hpp> //for node_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. //A simple struct containing:
  61. //
  62. // typedef node_ptr type;
  63. // node_ptr first;
  64. // node_ptr second;
  65. typedef twin<node_ptr> node_pair;
  66. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  67. //! <b>Effects</b>: Constructs an non-used list element, putting the next
  68. //! pointer to null:
  69. //! <tt>NodeTraits::get_next(this_node) == node_ptr()</tt>
  70. //!
  71. //! <b>Complexity</b>: Constant
  72. //!
  73. //! <b>Throws</b>: Nothing.
  74. static void init(node_ptr this_node) BOOST_NOEXCEPT;
  75. //! <b>Requires</b>: this_node must be in a circular list or be an empty circular list.
  76. //!
  77. //! <b>Effects</b>: Returns true is "this_node" is the only node of a circular list:
  78. //! or it's a not inserted node:
  79. //! <tt>return node_ptr() == NodeTraits::get_next(this_node) || NodeTraits::get_next(this_node) == this_node</tt>
  80. //!
  81. //! <b>Complexity</b>: Constant
  82. //!
  83. //! <b>Throws</b>: Nothing.
  84. static bool unique(const_node_ptr this_node) BOOST_NOEXCEPT;
  85. //! <b>Effects</b>: Returns true is "this_node" has the same state as if
  86. //! it was inited using "init(node_ptr)"
  87. //!
  88. //! <b>Complexity</b>: Constant
  89. //!
  90. //! <b>Throws</b>: Nothing.
  91. static bool inited(const_node_ptr this_node) BOOST_NOEXCEPT;
  92. //! <b>Requires</b>: prev_node must be in a circular list or be an empty circular list.
  93. //!
  94. //! <b>Effects</b>: Unlinks the next node of prev_node from the circular list.
  95. //!
  96. //! <b>Complexity</b>: Constant
  97. //!
  98. //! <b>Throws</b>: Nothing.
  99. static void unlink_after(node_ptr prev_node) BOOST_NOEXCEPT;
  100. //! <b>Requires</b>: prev_node and last_node must be in a circular list
  101. //! or be an empty circular list.
  102. //!
  103. //! <b>Effects</b>: Unlinks the range (prev_node, last_node) from the linear list.
  104. //!
  105. //! <b>Complexity</b>: Constant
  106. //!
  107. //! <b>Throws</b>: Nothing.
  108. static void unlink_after(node_ptr prev_node, node_ptr last_node) BOOST_NOEXCEPT;
  109. //! <b>Requires</b>: prev_node must be a node of a linear list.
  110. //!
  111. //! <b>Effects</b>: Links this_node after prev_node in the linear list.
  112. //!
  113. //! <b>Complexity</b>: Constant
  114. //!
  115. //! <b>Throws</b>: Nothing.
  116. static void link_after(node_ptr prev_node, node_ptr this_node) BOOST_NOEXCEPT;
  117. //! <b>Requires</b>: b and e must be nodes of the same linear list or an empty range.
  118. //! and p must be a node of a different linear list.
  119. //!
  120. //! <b>Effects</b>: Removes the nodes from (b, e] range from their linear list and inserts
  121. //! them after p in p's linear list.
  122. //!
  123. //! <b>Complexity</b>: Constant
  124. //!
  125. //! <b>Throws</b>: Nothing.
  126. static void transfer_after(node_ptr p, node_ptr b, node_ptr e) BOOST_NOEXCEPT;
  127. #else
  128. using base_t::transfer_after;
  129. #endif //#if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  130. //! <b>Effects</b>: Constructs an empty list, making this_node the only
  131. //! node of the circular list:
  132. //! <tt>NodeTraits::get_next(this_node) == this_node</tt>.
  133. //!
  134. //! <b>Complexity</b>: Constant
  135. //!
  136. //! <b>Throws</b>: Nothing.
  137. inline static void init_header(node_ptr this_node) BOOST_NOEXCEPT
  138. { NodeTraits::set_next(this_node, node_ptr()); }
  139. //! <b>Requires</b>: 'p' is the first node of a list.
  140. //!
  141. //! <b>Effects</b>: Returns a pointer to a node that represents the "end" (one past end) node
  142. //!
  143. //! <b>Complexity</b>: Constant time.
  144. //!
  145. //! <b>Throws</b>: Nothing.
  146. inline static node_ptr end_node(const_node_ptr) BOOST_NOEXCEPT
  147. { return node_ptr(); }
  148. //! <b>Effects</b>: Returns true if this_node_points to an empty list.
  149. //!
  150. //! <b>Complexity</b>: Constant
  151. //!
  152. //! <b>Throws</b>: Nothing.
  153. inline static bool is_empty(const_node_ptr this_node) BOOST_NOEXCEPT
  154. { return !NodeTraits::get_next(this_node); }
  155. //! <b>Effects</b>: Returns true if this_node points to a sentinel node.
  156. //!
  157. //! <b>Complexity</b>: Constant
  158. //!
  159. //! <b>Throws</b>: Nothing.
  160. inline static bool is_sentinel(const_node_ptr this_node) BOOST_NOEXCEPT
  161. { return NodeTraits::get_next(this_node) == this_node; }
  162. //! <b>Effects</b>: Marks this node as a "sentinel" node, a special state that is different from "empty",
  163. //! that can be used to mark a special state of the list
  164. //!
  165. //! <b>Complexity</b>: Constant
  166. //!
  167. //! <b>Throws</b>: Nothing.
  168. inline static void set_sentinel(node_ptr this_node) BOOST_NOEXCEPT
  169. { NodeTraits::set_next(this_node, this_node); }
  170. //! <b>Requires</b>: this_node and prev_init_node must be in the same linear list.
  171. //!
  172. //! <b>Effects</b>: Returns the previous node of this_node in the linear list starting.
  173. //! the search from prev_init_node. The first node checked for equality
  174. //! is NodeTraits::get_next(prev_init_node).
  175. //!
  176. //! <b>Complexity</b>: Linear to the number of elements between prev_init_node and this_node.
  177. //!
  178. //! <b>Throws</b>: Nothing.
  179. inline static node_ptr
  180. get_previous_node(node_ptr prev_init_node, node_ptr this_node) BOOST_NOEXCEPT
  181. { return base_t::get_previous_node(prev_init_node, this_node); }
  182. //! <b>Requires</b>: this_node must be in a linear list or be an empty linear list.
  183. //!
  184. //! <b>Effects</b>: Returns the number of nodes in a linear list. If the linear list
  185. //! is empty, returns 1.
  186. //!
  187. //! <b>Complexity</b>: Linear
  188. //!
  189. //! <b>Throws</b>: Nothing.
  190. static std::size_t count(const_node_ptr this_node) BOOST_NOEXCEPT
  191. {
  192. std::size_t result = 0;
  193. const_node_ptr p = this_node;
  194. do{
  195. p = NodeTraits::get_next(p);
  196. ++result;
  197. } while (p);
  198. return result;
  199. }
  200. //! <b>Requires</b>: this_node and other_node must be nodes inserted
  201. //! in linear lists or be empty linear lists.
  202. //!
  203. //! <b>Effects</b>: Moves all the nodes previously chained after this_node after other_node
  204. //! and vice-versa.
  205. //!
  206. //! <b>Complexity</b>: Constant
  207. //!
  208. //! <b>Throws</b>: Nothing.
  209. inline static void swap_trailing_nodes(node_ptr this_node, node_ptr other_node) BOOST_NOEXCEPT
  210. {
  211. node_ptr this_nxt = NodeTraits::get_next(this_node);
  212. node_ptr other_nxt = NodeTraits::get_next(other_node);
  213. NodeTraits::set_next(this_node, other_nxt);
  214. NodeTraits::set_next(other_node, this_nxt);
  215. }
  216. //! <b>Effects</b>: Reverses the order of elements in the list.
  217. //!
  218. //! <b>Returns</b>: The new first node of the list.
  219. //!
  220. //! <b>Throws</b>: Nothing.
  221. //!
  222. //! <b>Complexity</b>: This function is linear to the contained elements.
  223. static node_ptr reverse(node_ptr p) BOOST_NOEXCEPT
  224. {
  225. if(!p) return node_ptr();
  226. node_ptr i = NodeTraits::get_next(p);
  227. node_ptr first(p);
  228. while(i){
  229. node_ptr nxti(NodeTraits::get_next(i));
  230. base_t::unlink_after(p);
  231. NodeTraits::set_next(i, first);
  232. first = i;
  233. i = nxti;
  234. }
  235. return first;
  236. }
  237. //! <b>Effects</b>: Moves the first n nodes starting at p to the end of the list.
  238. //!
  239. //! <b>Returns</b>: A pair containing the new first and last node of the list or
  240. //! if there has been any movement, a null pair if n leads to no movement.
  241. //!
  242. //! <b>Throws</b>: Nothing.
  243. //!
  244. //! <b>Complexity</b>: Linear to the number of elements plus the number moved positions.
  245. static node_pair move_first_n_backwards(node_ptr p, std::size_t n) BOOST_NOEXCEPT
  246. {
  247. node_pair ret;
  248. //Null shift, or count() == 0 or 1, nothing to do
  249. if(!n || !p || !NodeTraits::get_next(p)){
  250. return ret;
  251. }
  252. node_ptr first = p;
  253. bool end_found = false;
  254. node_ptr new_last = node_ptr();
  255. node_ptr old_last = node_ptr();
  256. //Now find the new last node according to the shift count.
  257. //If we find 0 before finding the new last node
  258. //unlink p, shortcut the search now that we know the size of the list
  259. //and continue.
  260. for(std::size_t i = 1; i <= n; ++i){
  261. new_last = first;
  262. first = NodeTraits::get_next(first);
  263. if(first == node_ptr()){
  264. //Shortcut the shift with the modulo of the size of the list
  265. n %= i;
  266. if(!n) return ret;
  267. old_last = new_last;
  268. i = 0;
  269. //Unlink p and continue the new first node search
  270. first = p;
  271. //unlink_after(new_last);
  272. end_found = true;
  273. }
  274. }
  275. //If the p has not been found in the previous loop, find it
  276. //starting in the new first node and unlink it
  277. if(!end_found){
  278. old_last = base_t::get_previous_node(first, node_ptr());
  279. }
  280. //Now link p after the new last node
  281. NodeTraits::set_next(old_last, p);
  282. NodeTraits::set_next(new_last, node_ptr());
  283. ret.first = first;
  284. ret.second = new_last;
  285. return ret;
  286. }
  287. //! <b>Effects</b>: Moves the first n nodes starting at p to the beginning of the list.
  288. //!
  289. //! <b>Returns</b>: A pair containing the new first and last node of the list or
  290. //! if there has been any movement, a null pair if n leads to no movement.
  291. //!
  292. //! <b>Throws</b>: Nothing.
  293. //!
  294. //! <b>Complexity</b>: Linear to the number of elements plus the number moved positions.
  295. static node_pair move_first_n_forward(node_ptr p, std::size_t n) BOOST_NOEXCEPT
  296. {
  297. node_pair ret;
  298. //Null shift, or count() == 0 or 1, nothing to do
  299. if(!n || !p || !NodeTraits::get_next(p))
  300. return ret;
  301. node_ptr first = p;
  302. //Iterate until p is found to know where the current last node is.
  303. //If the shift count is less than the size of the list, we can also obtain
  304. //the position of the new last node after the shift.
  305. node_ptr old_last(first), next_to_it, new_last(p);
  306. std::size_t distance = 1;
  307. while(!!(next_to_it = node_traits::get_next(old_last))){
  308. if(distance++ > n)
  309. new_last = node_traits::get_next(new_last);
  310. old_last = next_to_it;
  311. }
  312. //If the shift was bigger or equal than the size, obtain the equivalent
  313. //forward shifts and find the new last node.
  314. if(distance <= n){
  315. //Now find the equivalent forward shifts.
  316. //Shortcut the shift with the modulo of the size of the list
  317. std::size_t new_before_last_pos = (distance - (n % distance))% distance;
  318. //If the shift is a multiple of the size there is nothing to do
  319. if(!new_before_last_pos)
  320. return ret;
  321. for( new_last = p
  322. ; --new_before_last_pos
  323. ; new_last = node_traits::get_next(new_last)){
  324. //empty
  325. }
  326. }
  327. //Get the first new node
  328. node_ptr new_first(node_traits::get_next(new_last));
  329. //Now put the old beginning after the old end
  330. NodeTraits::set_next(old_last, p);
  331. NodeTraits::set_next(new_last, node_ptr());
  332. ret.first = new_first;
  333. ret.second = new_last;
  334. return ret;
  335. }
  336. //! <b>Requires</b>: other must be a list and p must be a node of a different linear list.
  337. //!
  338. //! <b>Effects</b>: Transfers all nodes from other after p in p's linear list.
  339. //!
  340. //! <b>Complexity</b>: Linear
  341. //!
  342. //! <b>Throws</b>: Nothing.
  343. static void transfer_after(node_ptr p, node_ptr other) BOOST_NOEXCEPT
  344. {
  345. if ((is_empty)(p)) {
  346. (swap_trailing_nodes)(p, other);
  347. }
  348. else {
  349. node_ptr other_last((get_previous_node)(other, node_ptr()));
  350. base_t::transfer_after(p, other, other_last);
  351. }
  352. }
  353. //! <b>Requires</b>: "disposer" must be an object function
  354. //! taking a node_ptr parameter and shouldn't throw.
  355. //!
  356. //! <b>Effects</b>: Unlinks all nodes reachable from p (but not p) and calls
  357. //! <tt>void disposer::operator()(node_ptr)</tt> for every node of the list
  358. //! where p is linked.
  359. //!
  360. //! <b>Returns</b>: The number of disposed nodes
  361. //!
  362. //! <b>Complexity</b>: Linear to the number of element of the list.
  363. //!
  364. //! <b>Throws</b>: Nothing.
  365. template<class Disposer>
  366. inline static std::size_t detach_and_dispose(node_ptr p, Disposer disposer) BOOST_NOEXCEPT
  367. { return base_t::unlink_after_and_dispose(p, node_ptr(), disposer); }
  368. };
  369. /// @cond
  370. template<class NodeTraits>
  371. struct get_algo<LinearSListAlgorithms, NodeTraits>
  372. {
  373. typedef linear_slist_algorithms<NodeTraits> type;
  374. };
  375. /// @endcond
  376. } //namespace intrusive
  377. } //namespace boost
  378. #include <boost/intrusive/detail/config_end.hpp>
  379. #endif //BOOST_INTRUSIVE_LINEAR_SLIST_ALGORITHMS_HPP