circular_list_algorithms.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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_LIST_ALGORITHMS_HPP
  14. #define BOOST_INTRUSIVE_CIRCULAR_LIST_ALGORITHMS_HPP
  15. #include <boost/intrusive/detail/config_begin.hpp>
  16. #include <boost/intrusive/intrusive_fwd.hpp>
  17. #include <boost/intrusive/detail/algo_type.hpp>
  18. #include <boost/core/no_exceptions_support.hpp>
  19. #include <cstddef>
  20. #if defined(BOOST_HAS_PRAGMA_ONCE)
  21. # pragma once
  22. #endif
  23. namespace boost {
  24. namespace intrusive {
  25. //! circular_list_algorithms provides basic algorithms to manipulate nodes
  26. //! forming a circular doubly linked list. An empty circular list is formed by a node
  27. //! whose pointers point to itself.
  28. //!
  29. //! circular_list_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_previous(const_node_ptr n);</tt>
  44. //!
  45. //! <tt>static void set_previous(node_ptr n, node_ptr prev);</tt>
  46. //!
  47. //! <tt>static node_ptr get_next(const_node_ptr n);</tt>
  48. //!
  49. //! <tt>static void set_next(node_ptr n, node_ptr next);</tt>
  50. template<class NodeTraits>
  51. class circular_list_algorithms
  52. {
  53. public:
  54. typedef typename NodeTraits::node node;
  55. typedef typename NodeTraits::node_ptr node_ptr;
  56. typedef typename NodeTraits::const_node_ptr const_node_ptr;
  57. typedef NodeTraits node_traits;
  58. //! <b>Effects</b>: Constructs an non-used list element, so that
  59. //! inited(this_node) == true
  60. //!
  61. //! <b>Complexity</b>: Constant
  62. //!
  63. //! <b>Throws</b>: Nothing.
  64. BOOST_INTRUSIVE_FORCEINLINE static void init(node_ptr this_node)
  65. {
  66. const node_ptr null_node = node_ptr();
  67. NodeTraits::set_next(this_node, null_node);
  68. NodeTraits::set_previous(this_node, null_node);
  69. }
  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.
  72. //!
  73. //! <b>Complexity</b>: Constant
  74. //!
  75. //! <b>Throws</b>: Nothing.
  76. BOOST_INTRUSIVE_FORCEINLINE static bool inited(const const_node_ptr &this_node)
  77. { return !NodeTraits::get_next(this_node); }
  78. //! <b>Effects</b>: Constructs an empty list, making this_node the only
  79. //! node of the circular list:
  80. //! <tt>NodeTraits::get_next(this_node) == NodeTraits::get_previous(this_node)
  81. //! == this_node</tt>.
  82. //!
  83. //! <b>Complexity</b>: Constant
  84. //!
  85. //! <b>Throws</b>: Nothing.
  86. BOOST_INTRUSIVE_FORCEINLINE static void init_header(node_ptr this_node)
  87. {
  88. NodeTraits::set_next(this_node, this_node);
  89. NodeTraits::set_previous(this_node, this_node);
  90. }
  91. //! <b>Requires</b>: this_node must be in a circular list or be an empty circular list.
  92. //!
  93. //! <b>Effects</b>: Returns true is "this_node" is the only node of a circular list:
  94. //! <tt>return NodeTraits::get_next(this_node) == this_node</tt>
  95. //!
  96. //! <b>Complexity</b>: Constant
  97. //!
  98. //! <b>Throws</b>: Nothing.
  99. BOOST_INTRUSIVE_FORCEINLINE static bool unique(const const_node_ptr &this_node)
  100. {
  101. node_ptr next = NodeTraits::get_next(this_node);
  102. return !next || next == this_node;
  103. }
  104. //! <b>Requires</b>: this_node must be in a circular list or be an empty circular list.
  105. //!
  106. //! <b>Effects</b>: Returns the number of nodes in a circular list. If the circular list
  107. //! is empty, returns 1.
  108. //!
  109. //! <b>Complexity</b>: Linear
  110. //!
  111. //! <b>Throws</b>: Nothing.
  112. static std::size_t count(const const_node_ptr &this_node)
  113. {
  114. std::size_t result = 0;
  115. const_node_ptr p = this_node;
  116. do{
  117. p = NodeTraits::get_next(p);
  118. ++result;
  119. }while (p != this_node);
  120. return result;
  121. }
  122. //! <b>Requires</b>: this_node must be in a circular list or be an empty circular list.
  123. //!
  124. //! <b>Effects</b>: Unlinks the node from the circular list.
  125. //!
  126. //! <b>Complexity</b>: Constant
  127. //!
  128. //! <b>Throws</b>: Nothing.
  129. BOOST_INTRUSIVE_FORCEINLINE static node_ptr unlink(node_ptr this_node)
  130. {
  131. node_ptr next(NodeTraits::get_next(this_node));
  132. node_ptr prev(NodeTraits::get_previous(this_node));
  133. NodeTraits::set_next(prev, next);
  134. NodeTraits::set_previous(next, prev);
  135. return next;
  136. }
  137. //! <b>Requires</b>: b and e must be nodes of the same circular list or an empty range.
  138. //!
  139. //! <b>Effects</b>: Unlinks the node [b, e) from the circular list.
  140. //!
  141. //! <b>Complexity</b>: Constant
  142. //!
  143. //! <b>Throws</b>: Nothing.
  144. BOOST_INTRUSIVE_FORCEINLINE static void unlink(node_ptr b, node_ptr e)
  145. {
  146. if (b != e) {
  147. node_ptr prevb(NodeTraits::get_previous(b));
  148. NodeTraits::set_previous(e, prevb);
  149. NodeTraits::set_next(prevb, e);
  150. }
  151. }
  152. //! <b>Requires</b>: nxt_node must be a node of a circular list.
  153. //!
  154. //! <b>Effects</b>: Links this_node before nxt_node in the circular list.
  155. //!
  156. //! <b>Complexity</b>: Constant
  157. //!
  158. //! <b>Throws</b>: Nothing.
  159. BOOST_INTRUSIVE_FORCEINLINE static void link_before(node_ptr nxt_node, node_ptr this_node)
  160. {
  161. node_ptr prev(NodeTraits::get_previous(nxt_node));
  162. NodeTraits::set_previous(this_node, prev);
  163. NodeTraits::set_next(this_node, nxt_node);
  164. //nxt_node might be an alias for prev->next_
  165. //so use it before NodeTraits::set_next(prev, ...)
  166. //is called and the reference changes its value
  167. NodeTraits::set_previous(nxt_node, this_node);
  168. NodeTraits::set_next(prev, this_node);
  169. }
  170. //! <b>Requires</b>: prev_node must be a node of a circular list.
  171. //!
  172. //! <b>Effects</b>: Links this_node after prev_node in the circular list.
  173. //!
  174. //! <b>Complexity</b>: Constant
  175. //!
  176. //! <b>Throws</b>: Nothing.
  177. BOOST_INTRUSIVE_FORCEINLINE static void link_after(node_ptr prev_node, node_ptr this_node)
  178. {
  179. node_ptr next(NodeTraits::get_next(prev_node));
  180. NodeTraits::set_previous(this_node, prev_node);
  181. NodeTraits::set_next(this_node, next);
  182. //prev_node might be an alias for next->next_
  183. //so use it before update it before NodeTraits::set_previous(next, ...)
  184. //is called and the reference changes it's value
  185. NodeTraits::set_next(prev_node, this_node);
  186. NodeTraits::set_previous(next, this_node);
  187. }
  188. //! <b>Requires</b>: this_node and other_node must be nodes inserted
  189. //! in circular lists or be empty circular lists.
  190. //!
  191. //! <b>Effects</b>: Swaps the position of the nodes: this_node is inserted in
  192. //! other_nodes position in the second circular list and the other_node is inserted
  193. //! in this_node's position in the first circular list.
  194. //!
  195. //! <b>Complexity</b>: Constant
  196. //!
  197. //! <b>Throws</b>: Nothing.
  198. static void swap_nodes(node_ptr this_node, node_ptr other_node)
  199. {
  200. if (other_node == this_node)
  201. return;
  202. bool this_inited = inited(this_node);
  203. bool other_inited = inited(other_node);
  204. if(this_inited){
  205. init_header(this_node);
  206. }
  207. if(other_inited){
  208. init_header(other_node);
  209. }
  210. node_ptr next_this(NodeTraits::get_next(this_node));
  211. node_ptr prev_this(NodeTraits::get_previous(this_node));
  212. node_ptr next_other(NodeTraits::get_next(other_node));
  213. node_ptr prev_other(NodeTraits::get_previous(other_node));
  214. //these first two swaps must happen before the other two
  215. swap_prev(next_this, next_other);
  216. swap_next(prev_this, prev_other);
  217. swap_next(this_node, other_node);
  218. swap_prev(this_node, other_node);
  219. if(this_inited){
  220. init(other_node);
  221. }
  222. if(other_inited){
  223. init(this_node);
  224. }
  225. }
  226. //! <b>Requires</b>: b and e must be nodes of the same circular list or an empty range.
  227. //! and p must be a node of a different circular list or may not be an iterator in
  228. // [b, e).
  229. //!
  230. //! <b>Effects</b>: Removes the nodes from [b, e) range from their circular list and inserts
  231. //! them before p in p's circular list.
  232. //!
  233. //! <b>Complexity</b>: Constant
  234. //!
  235. //! <b>Throws</b>: Nothing.
  236. static void transfer(node_ptr p, node_ptr b, node_ptr e)
  237. {
  238. if (b != e) {
  239. node_ptr prev_p(NodeTraits::get_previous(p));
  240. node_ptr prev_b(NodeTraits::get_previous(b));
  241. node_ptr prev_e(NodeTraits::get_previous(e));
  242. NodeTraits::set_next(prev_e, p);
  243. NodeTraits::set_previous(p, prev_e);
  244. NodeTraits::set_next(prev_b, e);
  245. NodeTraits::set_previous(e, prev_b);
  246. NodeTraits::set_next(prev_p, b);
  247. NodeTraits::set_previous(b, prev_p);
  248. }
  249. }
  250. //! <b>Requires</b>: i must a node of a circular list
  251. //! and p must be a node of a different circular list.
  252. //!
  253. //! <b>Effects</b>: Removes the node i from its circular list and inserts
  254. //! it before p in p's circular list.
  255. //! If p == i or p == NodeTraits::get_next(i), this function is a null operation.
  256. //!
  257. //! <b>Complexity</b>: Constant
  258. //!
  259. //! <b>Throws</b>: Nothing.
  260. static void transfer(node_ptr p, node_ptr i)
  261. {
  262. node_ptr n(NodeTraits::get_next(i));
  263. if(n != p && i != p){
  264. node_ptr prev_p(NodeTraits::get_previous(p));
  265. node_ptr prev_i(NodeTraits::get_previous(i));
  266. NodeTraits::set_next(prev_p, i);
  267. NodeTraits::set_previous(i, prev_p);
  268. NodeTraits::set_next(i, p);
  269. NodeTraits::set_previous(p, i);
  270. NodeTraits::set_previous(n, prev_i);
  271. NodeTraits::set_next(prev_i, n);
  272. }
  273. }
  274. //! <b>Effects</b>: Reverses the order of elements in the list.
  275. //!
  276. //! <b>Throws</b>: Nothing.
  277. //!
  278. //! <b>Complexity</b>: This function is linear time.
  279. static void reverse(node_ptr p)
  280. {
  281. node_ptr f(NodeTraits::get_next(p));
  282. node_ptr i(NodeTraits::get_next(f)), e(p);
  283. while(i != e) {
  284. node_ptr n = i;
  285. i = NodeTraits::get_next(i);
  286. transfer(f, n, i);
  287. f = n;
  288. }
  289. }
  290. //! <b>Effects</b>: Moves the node p n positions towards the end of the list.
  291. //!
  292. //! <b>Throws</b>: Nothing.
  293. //!
  294. //! <b>Complexity</b>: Linear to the number of moved positions.
  295. static void move_backwards(node_ptr p, std::size_t n)
  296. {
  297. //Null shift, nothing to do
  298. if(!n) return;
  299. node_ptr first = NodeTraits::get_next(p);
  300. //size() == 0 or 1, nothing to do
  301. if(first == NodeTraits::get_previous(p)) return;
  302. unlink(p);
  303. //Now get the new first node
  304. while(n--){
  305. first = NodeTraits::get_next(first);
  306. }
  307. link_before(first, p);
  308. }
  309. //! <b>Effects</b>: Moves the node p n positions towards the beginning of the list.
  310. //!
  311. //! <b>Throws</b>: Nothing.
  312. //!
  313. //! <b>Complexity</b>: Linear to the number of moved positions.
  314. static void move_forward(node_ptr p, std::size_t n)
  315. {
  316. //Null shift, nothing to do
  317. if(!n) return;
  318. node_ptr last = NodeTraits::get_previous(p);
  319. //size() == 0 or 1, nothing to do
  320. if(last == NodeTraits::get_next(p)) return;
  321. unlink(p);
  322. //Now get the new last node
  323. while(n--){
  324. last = NodeTraits::get_previous(last);
  325. }
  326. link_after(last, p);
  327. }
  328. //! <b>Requires</b>: f and l must be in a circular list.
  329. //!
  330. //! <b>Effects</b>: Returns the number of nodes in the range [f, l).
  331. //!
  332. //! <b>Complexity</b>: Linear
  333. //!
  334. //! <b>Throws</b>: Nothing.
  335. static std::size_t distance(const const_node_ptr &f, const const_node_ptr &l)
  336. {
  337. const_node_ptr i(f);
  338. std::size_t result = 0;
  339. while(i != l){
  340. i = NodeTraits::get_next(i);
  341. ++result;
  342. }
  343. return result;
  344. }
  345. struct stable_partition_info
  346. {
  347. std::size_t num_1st_partition;
  348. std::size_t num_2nd_partition;
  349. node_ptr beg_2st_partition;
  350. };
  351. template<class Pred>
  352. static void stable_partition(node_ptr beg, node_ptr end, Pred pred, stable_partition_info &info)
  353. {
  354. node_ptr bcur = node_traits::get_previous(beg);
  355. node_ptr cur = beg;
  356. node_ptr new_f = end;
  357. std::size_t num1 = 0, num2 = 0;
  358. while(cur != end){
  359. if(pred(cur)){
  360. ++num1;
  361. bcur = cur;
  362. cur = node_traits::get_next(cur);
  363. }
  364. else{
  365. ++num2;
  366. node_ptr last_to_remove = bcur;
  367. new_f = cur;
  368. bcur = cur;
  369. cur = node_traits::get_next(cur);
  370. BOOST_TRY{
  371. //Main loop
  372. while(cur != end){
  373. if(pred(cur)){ //Might throw
  374. ++num1;
  375. //Process current node
  376. node_traits::set_next (last_to_remove, cur);
  377. node_traits::set_previous(cur, last_to_remove);
  378. last_to_remove = cur;
  379. node_ptr nxt = node_traits::get_next(cur);
  380. node_traits::set_next (bcur, nxt);
  381. node_traits::set_previous(nxt, bcur);
  382. cur = nxt;
  383. }
  384. else{
  385. ++num2;
  386. bcur = cur;
  387. cur = node_traits::get_next(cur);
  388. }
  389. }
  390. }
  391. BOOST_CATCH(...){
  392. node_traits::set_next (last_to_remove, new_f);
  393. node_traits::set_previous(new_f, last_to_remove);
  394. BOOST_RETHROW;
  395. }
  396. BOOST_CATCH_END
  397. node_traits::set_next(last_to_remove, new_f);
  398. node_traits::set_previous(new_f, last_to_remove);
  399. break;
  400. }
  401. }
  402. info.num_1st_partition = num1;
  403. info.num_2nd_partition = num2;
  404. info.beg_2st_partition = new_f;
  405. }
  406. private:
  407. BOOST_INTRUSIVE_FORCEINLINE static void swap_prev(node_ptr this_node, node_ptr other_node)
  408. {
  409. node_ptr temp(NodeTraits::get_previous(this_node));
  410. NodeTraits::set_previous(this_node, NodeTraits::get_previous(other_node));
  411. NodeTraits::set_previous(other_node, temp);
  412. }
  413. BOOST_INTRUSIVE_FORCEINLINE static void swap_next(node_ptr this_node, node_ptr other_node)
  414. {
  415. node_ptr temp(NodeTraits::get_next(this_node));
  416. NodeTraits::set_next(this_node, NodeTraits::get_next(other_node));
  417. NodeTraits::set_next(other_node, temp);
  418. }
  419. };
  420. /// @cond
  421. template<class NodeTraits>
  422. struct get_algo<CircularListAlgorithms, NodeTraits>
  423. {
  424. typedef circular_list_algorithms<NodeTraits> type;
  425. };
  426. /// @endcond
  427. } //namespace intrusive
  428. } //namespace boost
  429. #include <boost/intrusive/detail/config_end.hpp>
  430. #endif //BOOST_INTRUSIVE_CIRCULAR_LIST_ALGORITHMS_HPP