bstree_algorithms.hpp 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2007-2014
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. #ifndef BOOST_INTRUSIVE_BSTREE_ALGORITHMS_HPP
  13. #define BOOST_INTRUSIVE_BSTREE_ALGORITHMS_HPP
  14. #include <cstddef>
  15. #include <boost/intrusive/detail/config_begin.hpp>
  16. #include <boost/intrusive/intrusive_fwd.hpp>
  17. #include <boost/intrusive/detail/bstree_algorithms_base.hpp>
  18. #include <boost/intrusive/detail/assert.hpp>
  19. #include <boost/intrusive/detail/uncast.hpp>
  20. #include <boost/intrusive/detail/math.hpp>
  21. #include <boost/intrusive/detail/algo_type.hpp>
  22. #include <boost/intrusive/detail/minimal_pair_header.hpp>
  23. #if defined(BOOST_HAS_PRAGMA_ONCE)
  24. # pragma once
  25. #endif
  26. namespace boost {
  27. namespace intrusive {
  28. /// @cond
  29. //! This type is the information that will be filled by insert_unique_check
  30. template <class NodePtr>
  31. struct insert_commit_data_t
  32. {
  33. bool link_left;
  34. NodePtr node;
  35. };
  36. template <class NodePtr>
  37. struct data_for_rebalance_t
  38. {
  39. NodePtr x;
  40. NodePtr x_parent;
  41. NodePtr y;
  42. };
  43. namespace detail {
  44. template<class ValueTraits, class NodePtrCompare, class ExtraChecker>
  45. struct bstree_node_checker
  46. : public ExtraChecker
  47. {
  48. typedef ExtraChecker base_checker_t;
  49. typedef ValueTraits value_traits;
  50. typedef typename value_traits::node_traits node_traits;
  51. typedef typename node_traits::const_node_ptr const_node_ptr;
  52. struct return_type
  53. : public base_checker_t::return_type
  54. {
  55. return_type() : min_key_node_ptr(const_node_ptr()), max_key_node_ptr(const_node_ptr()), node_count(0) {}
  56. const_node_ptr min_key_node_ptr;
  57. const_node_ptr max_key_node_ptr;
  58. size_t node_count;
  59. };
  60. bstree_node_checker(const NodePtrCompare& comp, ExtraChecker extra_checker)
  61. : base_checker_t(extra_checker), comp_(comp)
  62. {}
  63. void operator () (const const_node_ptr& p,
  64. const return_type& check_return_left, const return_type& check_return_right,
  65. return_type& check_return)
  66. {
  67. if (check_return_left.max_key_node_ptr)
  68. BOOST_INTRUSIVE_INVARIANT_ASSERT(!comp_(p, check_return_left.max_key_node_ptr));
  69. if (check_return_right.min_key_node_ptr)
  70. BOOST_INTRUSIVE_INVARIANT_ASSERT(!comp_(check_return_right.min_key_node_ptr, p));
  71. check_return.min_key_node_ptr = node_traits::get_left(p)? check_return_left.min_key_node_ptr : p;
  72. check_return.max_key_node_ptr = node_traits::get_right(p)? check_return_right.max_key_node_ptr : p;
  73. check_return.node_count = check_return_left.node_count + check_return_right.node_count + 1;
  74. base_checker_t::operator()(p, check_return_left, check_return_right, check_return);
  75. }
  76. const NodePtrCompare comp_;
  77. };
  78. } // namespace detail
  79. /// @endcond
  80. //! This is an implementation of a binary search tree.
  81. //! A node in the search tree has references to its children and its parent. This
  82. //! is to allow traversal of the whole tree from a given node making the
  83. //! implementation of iterator a pointer to a node.
  84. //! At the top of the tree a node is used specially. This node's parent pointer
  85. //! is pointing to the root of the tree. Its left pointer points to the
  86. //! leftmost node in the tree and the right pointer to the rightmost one.
  87. //! This node is used to represent the end-iterator.
  88. //!
  89. //! +---------+
  90. //! header------------------------------>| |
  91. //! | |
  92. //! +----------(left)--------| |--------(right)---------+
  93. //! | +---------+ |
  94. //! | | |
  95. //! | | (parent) |
  96. //! | | |
  97. //! | | |
  98. //! | +---------+ |
  99. //! root of tree ..|......................> | | |
  100. //! | | D | |
  101. //! | | | |
  102. //! | +-------+---------+-------+ |
  103. //! | | | |
  104. //! | | | |
  105. //! | | | |
  106. //! | | | |
  107. //! | | | |
  108. //! | +---------+ +---------+ |
  109. //! | | | | | |
  110. //! | | B | | F | |
  111. //! | | | | | |
  112. //! | +--+---------+--+ +--+---------+--+ |
  113. //! | | | | | |
  114. //! | | | | | |
  115. //! | | | | | |
  116. //! | +---+-----+ +-----+---+ +---+-----+ +-----+---+ |
  117. //! +-->| | | | | | | |<--+
  118. //! | A | | C | | E | | G |
  119. //! | | | | | | | |
  120. //! +---------+ +---------+ +---------+ +---------+
  121. //!
  122. //! bstree_algorithms is configured with a NodeTraits class, which encapsulates the
  123. //! information about the node to be manipulated. NodeTraits must support the
  124. //! following interface:
  125. //!
  126. //! <b>Typedefs</b>:
  127. //!
  128. //! <tt>node</tt>: The type of the node that forms the binary search tree
  129. //!
  130. //! <tt>node_ptr</tt>: A pointer to a node
  131. //!
  132. //! <tt>const_node_ptr</tt>: A pointer to a const node
  133. //!
  134. //! <b>Static functions</b>:
  135. //!
  136. //! <tt>static node_ptr get_parent(const_node_ptr n);</tt>
  137. //!
  138. //! <tt>static void set_parent(node_ptr n, node_ptr parent);</tt>
  139. //!
  140. //! <tt>static node_ptr get_left(const_node_ptr n);</tt>
  141. //!
  142. //! <tt>static void set_left(node_ptr n, node_ptr left);</tt>
  143. //!
  144. //! <tt>static node_ptr get_right(const_node_ptr n);</tt>
  145. //!
  146. //! <tt>static void set_right(node_ptr n, node_ptr right);</tt>
  147. template<class NodeTraits>
  148. class bstree_algorithms : public bstree_algorithms_base<NodeTraits>
  149. {
  150. public:
  151. typedef typename NodeTraits::node node;
  152. typedef NodeTraits node_traits;
  153. typedef typename NodeTraits::node_ptr node_ptr;
  154. typedef typename NodeTraits::const_node_ptr const_node_ptr;
  155. typedef insert_commit_data_t<node_ptr> insert_commit_data;
  156. typedef data_for_rebalance_t<node_ptr> data_for_rebalance;
  157. /// @cond
  158. typedef bstree_algorithms<NodeTraits> this_type;
  159. typedef bstree_algorithms_base<NodeTraits> base_type;
  160. private:
  161. template<class Disposer>
  162. struct dispose_subtree_disposer
  163. {
  164. dispose_subtree_disposer(Disposer &disp, const node_ptr & subtree)
  165. : disposer_(&disp), subtree_(subtree)
  166. {}
  167. void release()
  168. { disposer_ = 0; }
  169. ~dispose_subtree_disposer()
  170. {
  171. if(disposer_){
  172. dispose_subtree(subtree_, *disposer_);
  173. }
  174. }
  175. Disposer *disposer_;
  176. const node_ptr subtree_;
  177. };
  178. /// @endcond
  179. public:
  180. //! <b>Requires</b>: 'header' is the header node of a tree.
  181. //!
  182. //! <b>Effects</b>: Returns the first node of the tree, the header if the tree is empty.
  183. //!
  184. //! <b>Complexity</b>: Constant time.
  185. //!
  186. //! <b>Throws</b>: Nothing.
  187. static node_ptr begin_node(const const_node_ptr & header)
  188. { return node_traits::get_left(header); }
  189. //! <b>Requires</b>: 'header' is the header node of a tree.
  190. //!
  191. //! <b>Effects</b>: Returns the header of the tree.
  192. //!
  193. //! <b>Complexity</b>: Constant time.
  194. //!
  195. //! <b>Throws</b>: Nothing.
  196. static node_ptr end_node(const const_node_ptr & header)
  197. { return detail::uncast(header); }
  198. //! <b>Requires</b>: 'header' is the header node of a tree.
  199. //!
  200. //! <b>Effects</b>: Returns the root of the tree if any, header otherwise
  201. //!
  202. //! <b>Complexity</b>: Constant time.
  203. //!
  204. //! <b>Throws</b>: Nothing.
  205. static node_ptr root_node(const const_node_ptr & header)
  206. {
  207. node_ptr p = node_traits::get_parent(header);
  208. return p ? p : detail::uncast(header);
  209. }
  210. //! <b>Requires</b>: 'node' is a node of the tree or a node initialized
  211. //! by init(...) or init_node.
  212. //!
  213. //! <b>Effects</b>: Returns true if the node is initialized by init() or init_node().
  214. //!
  215. //! <b>Complexity</b>: Constant time.
  216. //!
  217. //! <b>Throws</b>: Nothing.
  218. static bool unique(const const_node_ptr & node)
  219. { return !NodeTraits::get_parent(node); }
  220. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  221. //! <b>Requires</b>: 'node' is a node of the tree or a header node.
  222. //!
  223. //! <b>Effects</b>: Returns the header of the tree.
  224. //!
  225. //! <b>Complexity</b>: Logarithmic.
  226. //!
  227. //! <b>Throws</b>: Nothing.
  228. static node_ptr get_header(const const_node_ptr & node);
  229. #endif
  230. //! <b>Requires</b>: node1 and node2 can't be header nodes
  231. //! of two trees.
  232. //!
  233. //! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
  234. //! in the position node2 before the function. node2 will be inserted in the
  235. //! position node1 had before the function.
  236. //!
  237. //! <b>Complexity</b>: Logarithmic.
  238. //!
  239. //! <b>Throws</b>: Nothing.
  240. //!
  241. //! <b>Note</b>: This function will break container ordering invariants if
  242. //! node1 and node2 are not equivalent according to the ordering rules.
  243. //!
  244. //!Experimental function
  245. static void swap_nodes(const node_ptr & node1, const node_ptr & node2)
  246. {
  247. if(node1 == node2)
  248. return;
  249. node_ptr header1(base_type::get_header(node1)), header2(base_type::get_header(node2));
  250. swap_nodes(node1, header1, node2, header2);
  251. }
  252. //! <b>Requires</b>: node1 and node2 can't be header nodes
  253. //! of two trees with header header1 and header2.
  254. //!
  255. //! <b>Effects</b>: Swaps two nodes. After the function node1 will be inserted
  256. //! in the position node2 before the function. node2 will be inserted in the
  257. //! position node1 had before the function.
  258. //!
  259. //! <b>Complexity</b>: Constant.
  260. //!
  261. //! <b>Throws</b>: Nothing.
  262. //!
  263. //! <b>Note</b>: This function will break container ordering invariants if
  264. //! node1 and node2 are not equivalent according to the ordering rules.
  265. //!
  266. //!Experimental function
  267. static void swap_nodes(const node_ptr & node1, const node_ptr & header1, const node_ptr & node2, const node_ptr & header2)
  268. {
  269. if(node1 == node2)
  270. return;
  271. //node1 and node2 must not be header nodes
  272. //BOOST_INTRUSIVE_INVARIANT_ASSERT((header1 != node1 && header2 != node2));
  273. if(header1 != header2){
  274. //Update header1 if necessary
  275. if(node1 == NodeTraits::get_left(header1)){
  276. NodeTraits::set_left(header1, node2);
  277. }
  278. if(node1 == NodeTraits::get_right(header1)){
  279. NodeTraits::set_right(header1, node2);
  280. }
  281. if(node1 == NodeTraits::get_parent(header1)){
  282. NodeTraits::set_parent(header1, node2);
  283. }
  284. //Update header2 if necessary
  285. if(node2 == NodeTraits::get_left(header2)){
  286. NodeTraits::set_left(header2, node1);
  287. }
  288. if(node2 == NodeTraits::get_right(header2)){
  289. NodeTraits::set_right(header2, node1);
  290. }
  291. if(node2 == NodeTraits::get_parent(header2)){
  292. NodeTraits::set_parent(header2, node1);
  293. }
  294. }
  295. else{
  296. //If both nodes are from the same tree
  297. //Update header if necessary
  298. if(node1 == NodeTraits::get_left(header1)){
  299. NodeTraits::set_left(header1, node2);
  300. }
  301. else if(node2 == NodeTraits::get_left(header2)){
  302. NodeTraits::set_left(header2, node1);
  303. }
  304. if(node1 == NodeTraits::get_right(header1)){
  305. NodeTraits::set_right(header1, node2);
  306. }
  307. else if(node2 == NodeTraits::get_right(header2)){
  308. NodeTraits::set_right(header2, node1);
  309. }
  310. if(node1 == NodeTraits::get_parent(header1)){
  311. NodeTraits::set_parent(header1, node2);
  312. }
  313. else if(node2 == NodeTraits::get_parent(header2)){
  314. NodeTraits::set_parent(header2, node1);
  315. }
  316. //Adjust data in nodes to be swapped
  317. //so that final link swap works as expected
  318. if(node1 == NodeTraits::get_parent(node2)){
  319. NodeTraits::set_parent(node2, node2);
  320. if(node2 == NodeTraits::get_right(node1)){
  321. NodeTraits::set_right(node1, node1);
  322. }
  323. else{
  324. NodeTraits::set_left(node1, node1);
  325. }
  326. }
  327. else if(node2 == NodeTraits::get_parent(node1)){
  328. NodeTraits::set_parent(node1, node1);
  329. if(node1 == NodeTraits::get_right(node2)){
  330. NodeTraits::set_right(node2, node2);
  331. }
  332. else{
  333. NodeTraits::set_left(node2, node2);
  334. }
  335. }
  336. }
  337. //Now swap all the links
  338. node_ptr temp;
  339. //swap left link
  340. temp = NodeTraits::get_left(node1);
  341. NodeTraits::set_left(node1, NodeTraits::get_left(node2));
  342. NodeTraits::set_left(node2, temp);
  343. //swap right link
  344. temp = NodeTraits::get_right(node1);
  345. NodeTraits::set_right(node1, NodeTraits::get_right(node2));
  346. NodeTraits::set_right(node2, temp);
  347. //swap parent link
  348. temp = NodeTraits::get_parent(node1);
  349. NodeTraits::set_parent(node1, NodeTraits::get_parent(node2));
  350. NodeTraits::set_parent(node2, temp);
  351. //Now adjust adjacent nodes for newly inserted node 1
  352. if((temp = NodeTraits::get_left(node1))){
  353. NodeTraits::set_parent(temp, node1);
  354. }
  355. if((temp = NodeTraits::get_right(node1))){
  356. NodeTraits::set_parent(temp, node1);
  357. }
  358. if((temp = NodeTraits::get_parent(node1)) &&
  359. //The header has been already updated so avoid it
  360. temp != header2){
  361. if(NodeTraits::get_left(temp) == node2){
  362. NodeTraits::set_left(temp, node1);
  363. }
  364. if(NodeTraits::get_right(temp) == node2){
  365. NodeTraits::set_right(temp, node1);
  366. }
  367. }
  368. //Now adjust adjacent nodes for newly inserted node 2
  369. if((temp = NodeTraits::get_left(node2))){
  370. NodeTraits::set_parent(temp, node2);
  371. }
  372. if((temp = NodeTraits::get_right(node2))){
  373. NodeTraits::set_parent(temp, node2);
  374. }
  375. if((temp = NodeTraits::get_parent(node2)) &&
  376. //The header has been already updated so avoid it
  377. temp != header1){
  378. if(NodeTraits::get_left(temp) == node1){
  379. NodeTraits::set_left(temp, node2);
  380. }
  381. if(NodeTraits::get_right(temp) == node1){
  382. NodeTraits::set_right(temp, node2);
  383. }
  384. }
  385. }
  386. //! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
  387. //! and new_node must not be inserted in a tree.
  388. //!
  389. //! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
  390. //! tree with new_node. The tree does not need to be rebalanced
  391. //!
  392. //! <b>Complexity</b>: Logarithmic.
  393. //!
  394. //! <b>Throws</b>: Nothing.
  395. //!
  396. //! <b>Note</b>: This function will break container ordering invariants if
  397. //! new_node is not equivalent to node_to_be_replaced according to the
  398. //! ordering rules. This function is faster than erasing and inserting
  399. //! the node, since no rebalancing and comparison is needed. Experimental function
  400. static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & new_node)
  401. {
  402. if(node_to_be_replaced == new_node)
  403. return;
  404. replace_node(node_to_be_replaced, base_type::get_header(node_to_be_replaced), new_node);
  405. }
  406. //! <b>Requires</b>: node_to_be_replaced must be inserted in a tree
  407. //! with header "header" and new_node must not be inserted in a tree.
  408. //!
  409. //! <b>Effects</b>: Replaces node_to_be_replaced in its position in the
  410. //! tree with new_node. The tree does not need to be rebalanced
  411. //!
  412. //! <b>Complexity</b>: Constant.
  413. //!
  414. //! <b>Throws</b>: Nothing.
  415. //!
  416. //! <b>Note</b>: This function will break container ordering invariants if
  417. //! new_node is not equivalent to node_to_be_replaced according to the
  418. //! ordering rules. This function is faster than erasing and inserting
  419. //! the node, since no rebalancing or comparison is needed. Experimental function
  420. static void replace_node(const node_ptr & node_to_be_replaced, const node_ptr & header, const node_ptr & new_node)
  421. {
  422. if(node_to_be_replaced == new_node)
  423. return;
  424. //Update header if necessary
  425. if(node_to_be_replaced == NodeTraits::get_left(header)){
  426. NodeTraits::set_left(header, new_node);
  427. }
  428. if(node_to_be_replaced == NodeTraits::get_right(header)){
  429. NodeTraits::set_right(header, new_node);
  430. }
  431. if(node_to_be_replaced == NodeTraits::get_parent(header)){
  432. NodeTraits::set_parent(header, new_node);
  433. }
  434. //Now set data from the original node
  435. node_ptr temp;
  436. NodeTraits::set_left(new_node, NodeTraits::get_left(node_to_be_replaced));
  437. NodeTraits::set_right(new_node, NodeTraits::get_right(node_to_be_replaced));
  438. NodeTraits::set_parent(new_node, NodeTraits::get_parent(node_to_be_replaced));
  439. //Now adjust adjacent nodes for newly inserted node
  440. if((temp = NodeTraits::get_left(new_node))){
  441. NodeTraits::set_parent(temp, new_node);
  442. }
  443. if((temp = NodeTraits::get_right(new_node))){
  444. NodeTraits::set_parent(temp, new_node);
  445. }
  446. if((temp = NodeTraits::get_parent(new_node)) &&
  447. //The header has been already updated so avoid it
  448. temp != header){
  449. if(NodeTraits::get_left(temp) == node_to_be_replaced){
  450. NodeTraits::set_left(temp, new_node);
  451. }
  452. if(NodeTraits::get_right(temp) == node_to_be_replaced){
  453. NodeTraits::set_right(temp, new_node);
  454. }
  455. }
  456. }
  457. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  458. //! <b>Requires</b>: 'node' is a node from the tree except the header.
  459. //!
  460. //! <b>Effects</b>: Returns the next node of the tree.
  461. //!
  462. //! <b>Complexity</b>: Average constant time.
  463. //!
  464. //! <b>Throws</b>: Nothing.
  465. static node_ptr next_node(const node_ptr & node);
  466. //! <b>Requires</b>: 'node' is a node from the tree except the leftmost node.
  467. //!
  468. //! <b>Effects</b>: Returns the previous node of the tree.
  469. //!
  470. //! <b>Complexity</b>: Average constant time.
  471. //!
  472. //! <b>Throws</b>: Nothing.
  473. static node_ptr prev_node(const node_ptr & node);
  474. //! <b>Requires</b>: 'node' is a node of a tree but not the header.
  475. //!
  476. //! <b>Effects</b>: Returns the minimum node of the subtree starting at p.
  477. //!
  478. //! <b>Complexity</b>: Logarithmic to the size of the subtree.
  479. //!
  480. //! <b>Throws</b>: Nothing.
  481. static node_ptr minimum(node_ptr node);
  482. //! <b>Requires</b>: 'node' is a node of a tree but not the header.
  483. //!
  484. //! <b>Effects</b>: Returns the maximum node of the subtree starting at p.
  485. //!
  486. //! <b>Complexity</b>: Logarithmic to the size of the subtree.
  487. //!
  488. //! <b>Throws</b>: Nothing.
  489. static node_ptr maximum(node_ptr node);
  490. #endif
  491. //! <b>Requires</b>: 'node' must not be part of any tree.
  492. //!
  493. //! <b>Effects</b>: After the function unique(node) == true.
  494. //!
  495. //! <b>Complexity</b>: Constant.
  496. //!
  497. //! <b>Throws</b>: Nothing.
  498. //!
  499. //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
  500. static void init(const node_ptr & node)
  501. {
  502. NodeTraits::set_parent(node, node_ptr());
  503. NodeTraits::set_left(node, node_ptr());
  504. NodeTraits::set_right(node, node_ptr());
  505. };
  506. //! <b>Effects</b>: Returns true if node is in the same state as if called init(node)
  507. //!
  508. //! <b>Complexity</b>: Constant.
  509. //!
  510. //! <b>Throws</b>: Nothing.
  511. static bool inited(const const_node_ptr & node)
  512. {
  513. return !NodeTraits::get_parent(node) &&
  514. !NodeTraits::get_left(node) &&
  515. !NodeTraits::get_right(node) ;
  516. };
  517. //! <b>Requires</b>: node must not be part of any tree.
  518. //!
  519. //! <b>Effects</b>: Initializes the header to represent an empty tree.
  520. //! unique(header) == true.
  521. //!
  522. //! <b>Complexity</b>: Constant.
  523. //!
  524. //! <b>Throws</b>: Nothing.
  525. //!
  526. //! <b>Nodes</b>: If node is inserted in a tree, this function corrupts the tree.
  527. static void init_header(const node_ptr & header)
  528. {
  529. NodeTraits::set_parent(header, node_ptr());
  530. NodeTraits::set_left(header, header);
  531. NodeTraits::set_right(header, header);
  532. }
  533. //! <b>Requires</b>: "disposer" must be an object function
  534. //! taking a node_ptr parameter and shouldn't throw.
  535. //!
  536. //! <b>Effects</b>: Empties the target tree calling
  537. //! <tt>void disposer::operator()(const node_ptr &)</tt> for every node of the tree
  538. //! except the header.
  539. //!
  540. //! <b>Complexity</b>: Linear to the number of element of the source tree plus the.
  541. //! number of elements of tree target tree when calling this function.
  542. //!
  543. //! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
  544. template<class Disposer>
  545. static void clear_and_dispose(const node_ptr & header, Disposer disposer)
  546. {
  547. node_ptr source_root = NodeTraits::get_parent(header);
  548. if(!source_root)
  549. return;
  550. dispose_subtree(source_root, disposer);
  551. init_header(header);
  552. }
  553. //! <b>Requires</b>: header is the header of a tree.
  554. //!
  555. //! <b>Effects</b>: Unlinks the leftmost node from the tree, and
  556. //! updates the header link to the new leftmost node.
  557. //!
  558. //! <b>Complexity</b>: Average complexity is constant time.
  559. //!
  560. //! <b>Throws</b>: Nothing.
  561. //!
  562. //! <b>Notes</b>: This function breaks the tree and the tree can
  563. //! only be used for more unlink_leftmost_without_rebalance calls.
  564. //! This function is normally used to achieve a step by step
  565. //! controlled destruction of the tree.
  566. static node_ptr unlink_leftmost_without_rebalance(const node_ptr & header)
  567. {
  568. node_ptr leftmost = NodeTraits::get_left(header);
  569. if (leftmost == header)
  570. return node_ptr();
  571. node_ptr leftmost_parent(NodeTraits::get_parent(leftmost));
  572. node_ptr leftmost_right (NodeTraits::get_right(leftmost));
  573. bool is_root = leftmost_parent == header;
  574. if (leftmost_right){
  575. NodeTraits::set_parent(leftmost_right, leftmost_parent);
  576. NodeTraits::set_left(header, base_type::minimum(leftmost_right));
  577. if (is_root)
  578. NodeTraits::set_parent(header, leftmost_right);
  579. else
  580. NodeTraits::set_left(NodeTraits::get_parent(header), leftmost_right);
  581. }
  582. else if (is_root){
  583. NodeTraits::set_parent(header, node_ptr());
  584. NodeTraits::set_left(header, header);
  585. NodeTraits::set_right(header, header);
  586. }
  587. else{
  588. NodeTraits::set_left(leftmost_parent, node_ptr());
  589. NodeTraits::set_left(header, leftmost_parent);
  590. }
  591. return leftmost;
  592. }
  593. //! <b>Requires</b>: node is a node of the tree but it's not the header.
  594. //!
  595. //! <b>Effects</b>: Returns the number of nodes of the subtree.
  596. //!
  597. //! <b>Complexity</b>: Linear time.
  598. //!
  599. //! <b>Throws</b>: Nothing.
  600. static std::size_t size(const const_node_ptr & header)
  601. {
  602. node_ptr beg(begin_node(header));
  603. node_ptr end(end_node(header));
  604. std::size_t i = 0;
  605. for(;beg != end; beg = base_type::next_node(beg)) ++i;
  606. return i;
  607. }
  608. //! <b>Requires</b>: header1 and header2 must be the header nodes
  609. //! of two trees.
  610. //!
  611. //! <b>Effects</b>: Swaps two trees. After the function header1 will contain
  612. //! links to the second tree and header2 will have links to the first tree.
  613. //!
  614. //! <b>Complexity</b>: Constant.
  615. //!
  616. //! <b>Throws</b>: Nothing.
  617. static void swap_tree(const node_ptr & header1, const node_ptr & header2)
  618. {
  619. if(header1 == header2)
  620. return;
  621. node_ptr tmp;
  622. //Parent swap
  623. tmp = NodeTraits::get_parent(header1);
  624. NodeTraits::set_parent(header1, NodeTraits::get_parent(header2));
  625. NodeTraits::set_parent(header2, tmp);
  626. //Left swap
  627. tmp = NodeTraits::get_left(header1);
  628. NodeTraits::set_left(header1, NodeTraits::get_left(header2));
  629. NodeTraits::set_left(header2, tmp);
  630. //Right swap
  631. tmp = NodeTraits::get_right(header1);
  632. NodeTraits::set_right(header1, NodeTraits::get_right(header2));
  633. NodeTraits::set_right(header2, tmp);
  634. //Now test parent
  635. node_ptr h1_parent(NodeTraits::get_parent(header1));
  636. if(h1_parent){
  637. NodeTraits::set_parent(h1_parent, header1);
  638. }
  639. else{
  640. NodeTraits::set_left(header1, header1);
  641. NodeTraits::set_right(header1, header1);
  642. }
  643. node_ptr h2_parent(NodeTraits::get_parent(header2));
  644. if(h2_parent){
  645. NodeTraits::set_parent(h2_parent, header2);
  646. }
  647. else{
  648. NodeTraits::set_left(header2, header2);
  649. NodeTraits::set_right(header2, header2);
  650. }
  651. }
  652. #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
  653. //! <b>Requires</b>: p is a node of a tree.
  654. //!
  655. //! <b>Effects</b>: Returns true if p is the header of the tree.
  656. //!
  657. //! <b>Complexity</b>: Constant.
  658. //!
  659. //! <b>Throws</b>: Nothing.
  660. static bool is_header(const const_node_ptr & p);
  661. #endif
  662. //! <b>Requires</b>: "header" must be the header node of a tree.
  663. //! KeyNodePtrCompare is a function object that induces a strict weak
  664. //! ordering compatible with the strict weak ordering used to create the
  665. //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
  666. //!
  667. //! <b>Effects</b>: Returns a node_ptr to the first element that is equivalent to
  668. //! "key" according to "comp" or "header" if that element does not exist.
  669. //!
  670. //! <b>Complexity</b>: Logarithmic.
  671. //!
  672. //! <b>Throws</b>: If "comp" throws.
  673. template<class KeyType, class KeyNodePtrCompare>
  674. static node_ptr find
  675. (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
  676. {
  677. node_ptr end = detail::uncast(header);
  678. node_ptr y = lower_bound(header, key, comp);
  679. return (y == end || comp(key, y)) ? end : y;
  680. }
  681. //! <b>Requires</b>: "header" must be the header node of a tree.
  682. //! KeyNodePtrCompare is a function object that induces a strict weak
  683. //! ordering compatible with the strict weak ordering used to create the
  684. //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
  685. //! 'lower_key' must not be greater than 'upper_key' according to 'comp'. If
  686. //! 'lower_key' == 'upper_key', ('left_closed' || 'right_closed') must be true.
  687. //!
  688. //! <b>Effects</b>: Returns an a pair with the following criteria:
  689. //!
  690. //! first = lower_bound(lower_key) if left_closed, upper_bound(lower_key) otherwise
  691. //!
  692. //! second = upper_bound(upper_key) if right_closed, lower_bound(upper_key) otherwise
  693. //!
  694. //! <b>Complexity</b>: Logarithmic.
  695. //!
  696. //! <b>Throws</b>: If "comp" throws.
  697. //!
  698. //! <b>Note</b>: This function can be more efficient than calling upper_bound
  699. //! and lower_bound for lower_key and upper_key.
  700. //!
  701. //! <b>Note</b>: Experimental function, the interface might change.
  702. template< class KeyType, class KeyNodePtrCompare>
  703. static std::pair<node_ptr, node_ptr> bounded_range
  704. ( const const_node_ptr & header
  705. , const KeyType &lower_key
  706. , const KeyType &upper_key
  707. , KeyNodePtrCompare comp
  708. , bool left_closed
  709. , bool right_closed)
  710. {
  711. node_ptr y = detail::uncast(header);
  712. node_ptr x = NodeTraits::get_parent(header);
  713. while(x){
  714. //If x is less than lower_key the target
  715. //range is on the right part
  716. if(comp(x, lower_key)){
  717. //Check for invalid input range
  718. BOOST_INTRUSIVE_INVARIANT_ASSERT(comp(x, upper_key));
  719. x = NodeTraits::get_right(x);
  720. }
  721. //If the upper_key is less than x, the target
  722. //range is on the left part
  723. else if(comp(upper_key, x)){
  724. y = x;
  725. x = NodeTraits::get_left(x);
  726. }
  727. else{
  728. //x is inside the bounded range(lower_key <= x <= upper_key),
  729. //so we must split lower and upper searches
  730. //
  731. //Sanity check: if lower_key and upper_key are equal, then both left_closed and right_closed can't be false
  732. BOOST_INTRUSIVE_INVARIANT_ASSERT(left_closed || right_closed || comp(lower_key, x) || comp(x, upper_key));
  733. return std::pair<node_ptr,node_ptr>(
  734. left_closed
  735. //If left_closed, then comp(x, lower_key) is already the lower_bound
  736. //condition so we save one comparison and go to the next level
  737. //following traditional lower_bound algo
  738. ? lower_bound_loop(NodeTraits::get_left(x), x, lower_key, comp)
  739. //If left-open, comp(x, lower_key) is not the upper_bound algo
  740. //condition so we must recheck current 'x' node with upper_bound algo
  741. : upper_bound_loop(x, y, lower_key, comp)
  742. ,
  743. right_closed
  744. //If right_closed, then comp(upper_key, x) is already the upper_bound
  745. //condition so we can save one comparison and go to the next level
  746. //following lower_bound algo
  747. ? upper_bound_loop(NodeTraits::get_right(x), y, upper_key, comp)
  748. //If right-open, comp(upper_key, x) is not the lower_bound algo
  749. //condition so we must recheck current 'x' node with lower_bound algo
  750. : lower_bound_loop(x, y, upper_key, comp)
  751. );
  752. }
  753. }
  754. return std::pair<node_ptr,node_ptr> (y, y);
  755. }
  756. //! <b>Requires</b>: "header" must be the header node of a tree.
  757. //! KeyNodePtrCompare is a function object that induces a strict weak
  758. //! ordering compatible with the strict weak ordering used to create the
  759. //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
  760. //!
  761. //! <b>Effects</b>: Returns the number of elements with a key equivalent to "key"
  762. //! according to "comp".
  763. //!
  764. //! <b>Complexity</b>: Logarithmic.
  765. //!
  766. //! <b>Throws</b>: If "comp" throws.
  767. template<class KeyType, class KeyNodePtrCompare>
  768. static std::size_t count
  769. (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
  770. {
  771. std::pair<node_ptr, node_ptr> ret = equal_range(header, key, comp);
  772. std::size_t n = 0;
  773. while(ret.first != ret.second){
  774. ++n;
  775. ret.first = base_type::next_node(ret.first);
  776. }
  777. return n;
  778. }
  779. //! <b>Requires</b>: "header" must be the header node of a tree.
  780. //! KeyNodePtrCompare is a function object that induces a strict weak
  781. //! ordering compatible with the strict weak ordering used to create the
  782. //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
  783. //!
  784. //! <b>Effects</b>: Returns an a pair of node_ptr delimiting a range containing
  785. //! all elements that are equivalent to "key" according to "comp" or an
  786. //! empty range that indicates the position where those elements would be
  787. //! if there are no equivalent elements.
  788. //!
  789. //! <b>Complexity</b>: Logarithmic.
  790. //!
  791. //! <b>Throws</b>: If "comp" throws.
  792. template<class KeyType, class KeyNodePtrCompare>
  793. static std::pair<node_ptr, node_ptr> equal_range
  794. (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
  795. {
  796. return bounded_range(header, key, key, comp, true, true);
  797. }
  798. //! <b>Requires</b>: "header" must be the header node of a tree.
  799. //! KeyNodePtrCompare is a function object that induces a strict weak
  800. //! ordering compatible with the strict weak ordering used to create the
  801. //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
  802. //!
  803. //! <b>Effects</b>: Returns an a pair of node_ptr delimiting a range containing
  804. //! the first element that is equivalent to "key" according to "comp" or an
  805. //! empty range that indicates the position where that element would be
  806. //! if there are no equivalent elements.
  807. //!
  808. //! <b>Complexity</b>: Logarithmic.
  809. //!
  810. //! <b>Throws</b>: If "comp" throws.
  811. template<class KeyType, class KeyNodePtrCompare>
  812. static std::pair<node_ptr, node_ptr> lower_bound_range
  813. (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
  814. {
  815. node_ptr const lb(lower_bound(header, key, comp));
  816. std::pair<node_ptr, node_ptr> ret_ii(lb, lb);
  817. if(lb != header && !comp(key, lb)){
  818. ret_ii.second = base_type::next_node(ret_ii.second);
  819. }
  820. return ret_ii;
  821. }
  822. //! <b>Requires</b>: "header" must be the header node of a tree.
  823. //! KeyNodePtrCompare is a function object that induces a strict weak
  824. //! ordering compatible with the strict weak ordering used to create the
  825. //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
  826. //!
  827. //! <b>Effects</b>: Returns a node_ptr to the first element that is
  828. //! not less than "key" according to "comp" or "header" if that element does
  829. //! not exist.
  830. //!
  831. //! <b>Complexity</b>: Logarithmic.
  832. //!
  833. //! <b>Throws</b>: If "comp" throws.
  834. template<class KeyType, class KeyNodePtrCompare>
  835. static node_ptr lower_bound
  836. (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
  837. {
  838. return lower_bound_loop(NodeTraits::get_parent(header), detail::uncast(header), key, comp);
  839. }
  840. //! <b>Requires</b>: "header" must be the header node of a tree.
  841. //! KeyNodePtrCompare is a function object that induces a strict weak
  842. //! ordering compatible with the strict weak ordering used to create the
  843. //! the tree. KeyNodePtrCompare can compare KeyType with tree's node_ptrs.
  844. //!
  845. //! <b>Effects</b>: Returns a node_ptr to the first element that is greater
  846. //! than "key" according to "comp" or "header" if that element does not exist.
  847. //!
  848. //! <b>Complexity</b>: Logarithmic.
  849. //!
  850. //! <b>Throws</b>: If "comp" throws.
  851. template<class KeyType, class KeyNodePtrCompare>
  852. static node_ptr upper_bound
  853. (const const_node_ptr & header, const KeyType &key, KeyNodePtrCompare comp)
  854. {
  855. return upper_bound_loop(NodeTraits::get_parent(header), detail::uncast(header), key, comp);
  856. }
  857. //! <b>Requires</b>: "header" must be the header node of a tree.
  858. //! "commit_data" must have been obtained from a previous call to
  859. //! "insert_unique_check". No objects should have been inserted or erased
  860. //! from the set between the "insert_unique_check" that filled "commit_data"
  861. //! and the call to "insert_commit".
  862. //!
  863. //!
  864. //! <b>Effects</b>: Inserts new_node in the set using the information obtained
  865. //! from the "commit_data" that a previous "insert_check" filled.
  866. //!
  867. //! <b>Complexity</b>: Constant time.
  868. //!
  869. //! <b>Throws</b>: Nothing.
  870. //!
  871. //! <b>Notes</b>: This function has only sense if a "insert_unique_check" has been
  872. //! previously executed to fill "commit_data". No value should be inserted or
  873. //! erased between the "insert_check" and "insert_commit" calls.
  874. static void insert_unique_commit
  875. (const node_ptr & header, const node_ptr & new_value, const insert_commit_data &commit_data)
  876. { return insert_commit(header, new_value, commit_data); }
  877. //! <b>Requires</b>: "header" must be the header node of a tree.
  878. //! KeyNodePtrCompare is a function object that induces a strict weak
  879. //! ordering compatible with the strict weak ordering used to create the
  880. //! the tree. NodePtrCompare compares KeyType with a node_ptr.
  881. //!
  882. //! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
  883. //! tree according to "comp" and obtains the needed information to realize
  884. //! a constant-time node insertion if there is no equivalent node.
  885. //!
  886. //! <b>Returns</b>: If there is an equivalent value
  887. //! returns a pair containing a node_ptr to the already present node
  888. //! and false. If there is not equivalent key can be inserted returns true
  889. //! in the returned pair's boolean and fills "commit_data" that is meant to
  890. //! be used with the "insert_commit" function to achieve a constant-time
  891. //! insertion function.
  892. //!
  893. //! <b>Complexity</b>: Average complexity is at most logarithmic.
  894. //!
  895. //! <b>Throws</b>: If "comp" throws.
  896. //!
  897. //! <b>Notes</b>: This function is used to improve performance when constructing
  898. //! a node is expensive and the user does not want to have two equivalent nodes
  899. //! in the tree: if there is an equivalent value
  900. //! the constructed object must be discarded. Many times, the part of the
  901. //! node that is used to impose the order is much cheaper to construct
  902. //! than the node and this function offers the possibility to use that part
  903. //! to check if the insertion will be successful.
  904. //!
  905. //! If the check is successful, the user can construct the node and use
  906. //! "insert_commit" to insert the node in constant-time. This gives a total
  907. //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
  908. //!
  909. //! "commit_data" remains valid for a subsequent "insert_unique_commit" only
  910. //! if no more objects are inserted or erased from the set.
  911. template<class KeyType, class KeyNodePtrCompare>
  912. static std::pair<node_ptr, bool> insert_unique_check
  913. (const const_node_ptr & header, const KeyType &key
  914. ,KeyNodePtrCompare comp, insert_commit_data &commit_data
  915. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  916. , std::size_t *pdepth = 0
  917. #endif
  918. )
  919. {
  920. std::size_t depth = 0;
  921. node_ptr h(detail::uncast(header));
  922. node_ptr y(h);
  923. node_ptr x(NodeTraits::get_parent(y));
  924. node_ptr prev = node_ptr();
  925. //Find the upper bound, cache the previous value and if we should
  926. //store it in the left or right node
  927. bool left_child = true;
  928. while(x){
  929. ++depth;
  930. y = x;
  931. x = (left_child = comp(key, x)) ?
  932. NodeTraits::get_left(x) : (prev = y, NodeTraits::get_right(x));
  933. }
  934. if(pdepth) *pdepth = depth;
  935. //Since we've found the upper bound there is no other value with the same key if:
  936. // - There is no previous node
  937. // - The previous node is less than the key
  938. const bool not_present = !prev || comp(prev, key);
  939. if(not_present){
  940. commit_data.link_left = left_child;
  941. commit_data.node = y;
  942. }
  943. return std::pair<node_ptr, bool>(prev, not_present);
  944. }
  945. //! <b>Requires</b>: "header" must be the header node of a tree.
  946. //! KeyNodePtrCompare is a function object that induces a strict weak
  947. //! ordering compatible with the strict weak ordering used to create the
  948. //! the tree. NodePtrCompare compares KeyType with a node_ptr.
  949. //! "hint" is node from the "header"'s tree.
  950. //!
  951. //! <b>Effects</b>: Checks if there is an equivalent node to "key" in the
  952. //! tree according to "comp" using "hint" as a hint to where it should be
  953. //! inserted and obtains the needed information to realize
  954. //! a constant-time node insertion if there is no equivalent node.
  955. //! If "hint" is the upper_bound the function has constant time
  956. //! complexity (two comparisons in the worst case).
  957. //!
  958. //! <b>Returns</b>: If there is an equivalent value
  959. //! returns a pair containing a node_ptr to the already present node
  960. //! and false. If there is not equivalent key can be inserted returns true
  961. //! in the returned pair's boolean and fills "commit_data" that is meant to
  962. //! be used with the "insert_commit" function to achieve a constant-time
  963. //! insertion function.
  964. //!
  965. //! <b>Complexity</b>: Average complexity is at most logarithmic, but it is
  966. //! amortized constant time if new_node should be inserted immediately before "hint".
  967. //!
  968. //! <b>Throws</b>: If "comp" throws.
  969. //!
  970. //! <b>Notes</b>: This function is used to improve performance when constructing
  971. //! a node is expensive and the user does not want to have two equivalent nodes
  972. //! in the tree: if there is an equivalent value
  973. //! the constructed object must be discarded. Many times, the part of the
  974. //! node that is used to impose the order is much cheaper to construct
  975. //! than the node and this function offers the possibility to use that part
  976. //! to check if the insertion will be successful.
  977. //!
  978. //! If the check is successful, the user can construct the node and use
  979. //! "insert_commit" to insert the node in constant-time. This gives a total
  980. //! logarithmic complexity to the insertion: check(O(log(N)) + commit(O(1)).
  981. //!
  982. //! "commit_data" remains valid for a subsequent "insert_unique_commit" only
  983. //! if no more objects are inserted or erased from the set.
  984. template<class KeyType, class KeyNodePtrCompare>
  985. static std::pair<node_ptr, bool> insert_unique_check
  986. (const const_node_ptr & header, const node_ptr &hint, const KeyType &key
  987. ,KeyNodePtrCompare comp, insert_commit_data &commit_data
  988. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  989. , std::size_t *pdepth = 0
  990. #endif
  991. )
  992. {
  993. //hint must be bigger than the key
  994. if(hint == header || comp(key, hint)){
  995. node_ptr prev(hint);
  996. //Previous value should be less than the key
  997. if(hint == begin_node(header) || comp((prev = base_type::prev_node(hint)), key)){
  998. commit_data.link_left = unique(header) || !NodeTraits::get_left(hint);
  999. commit_data.node = commit_data.link_left ? hint : prev;
  1000. if(pdepth){
  1001. *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1;
  1002. }
  1003. return std::pair<node_ptr, bool>(node_ptr(), true);
  1004. }
  1005. }
  1006. //Hint was wrong, use hintless insertion
  1007. return insert_unique_check(header, key, comp, commit_data, pdepth);
  1008. }
  1009. //! <b>Requires</b>: "header" must be the header node of a tree.
  1010. //! NodePtrCompare is a function object that induces a strict weak
  1011. //! ordering compatible with the strict weak ordering used to create the
  1012. //! the tree. NodePtrCompare compares two node_ptrs. "hint" is node from
  1013. //! the "header"'s tree.
  1014. //!
  1015. //! <b>Effects</b>: Inserts new_node into the tree, using "hint" as a hint to
  1016. //! where it will be inserted. If "hint" is the upper_bound
  1017. //! the insertion takes constant time (two comparisons in the worst case).
  1018. //!
  1019. //! <b>Complexity</b>: Logarithmic in general, but it is amortized
  1020. //! constant time if new_node is inserted immediately before "hint".
  1021. //!
  1022. //! <b>Throws</b>: If "comp" throws.
  1023. template<class NodePtrCompare>
  1024. static node_ptr insert_equal
  1025. (const node_ptr & h, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp
  1026. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1027. , std::size_t *pdepth = 0
  1028. #endif
  1029. )
  1030. {
  1031. insert_commit_data commit_data;
  1032. insert_equal_check(h, hint, new_node, comp, commit_data, pdepth);
  1033. insert_commit(h, new_node, commit_data);
  1034. return new_node;
  1035. }
  1036. //! <b>Requires</b>: "h" must be the header node of a tree.
  1037. //! NodePtrCompare is a function object that induces a strict weak
  1038. //! ordering compatible with the strict weak ordering used to create the
  1039. //! the tree. NodePtrCompare compares two node_ptrs.
  1040. //!
  1041. //! <b>Effects</b>: Inserts new_node into the tree before the upper bound
  1042. //! according to "comp".
  1043. //!
  1044. //! <b>Complexity</b>: Average complexity for insert element is at
  1045. //! most logarithmic.
  1046. //!
  1047. //! <b>Throws</b>: If "comp" throws.
  1048. template<class NodePtrCompare>
  1049. static node_ptr insert_equal_upper_bound
  1050. (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp
  1051. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1052. , std::size_t *pdepth = 0
  1053. #endif
  1054. )
  1055. {
  1056. insert_commit_data commit_data;
  1057. insert_equal_upper_bound_check(h, new_node, comp, commit_data, pdepth);
  1058. insert_commit(h, new_node, commit_data);
  1059. return new_node;
  1060. }
  1061. //! <b>Requires</b>: "h" must be the header node of a tree.
  1062. //! NodePtrCompare is a function object that induces a strict weak
  1063. //! ordering compatible with the strict weak ordering used to create the
  1064. //! the tree. NodePtrCompare compares two node_ptrs.
  1065. //!
  1066. //! <b>Effects</b>: Inserts new_node into the tree before the lower bound
  1067. //! according to "comp".
  1068. //!
  1069. //! <b>Complexity</b>: Average complexity for insert element is at
  1070. //! most logarithmic.
  1071. //!
  1072. //! <b>Throws</b>: If "comp" throws.
  1073. template<class NodePtrCompare>
  1074. static node_ptr insert_equal_lower_bound
  1075. (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp
  1076. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1077. , std::size_t *pdepth = 0
  1078. #endif
  1079. )
  1080. {
  1081. insert_commit_data commit_data;
  1082. insert_equal_lower_bound_check(h, new_node, comp, commit_data, pdepth);
  1083. insert_commit(h, new_node, commit_data);
  1084. return new_node;
  1085. }
  1086. //! <b>Requires</b>: "header" must be the header node of a tree.
  1087. //! "pos" must be a valid iterator or header (end) node.
  1088. //! "pos" must be an iterator pointing to the successor to "new_node"
  1089. //! once inserted according to the order of already inserted nodes. This function does not
  1090. //! check "pos" and this precondition must be guaranteed by the caller.
  1091. //!
  1092. //! <b>Effects</b>: Inserts new_node into the tree before "pos".
  1093. //!
  1094. //! <b>Complexity</b>: Constant-time.
  1095. //!
  1096. //! <b>Throws</b>: Nothing.
  1097. //!
  1098. //! <b>Note</b>: If "pos" is not the successor of the newly inserted "new_node"
  1099. //! tree invariants might be broken.
  1100. static node_ptr insert_before
  1101. (const node_ptr & header, const node_ptr & pos, const node_ptr & new_node
  1102. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1103. , std::size_t *pdepth = 0
  1104. #endif
  1105. )
  1106. {
  1107. insert_commit_data commit_data;
  1108. insert_before_check(header, pos, commit_data, pdepth);
  1109. insert_commit(header, new_node, commit_data);
  1110. return new_node;
  1111. }
  1112. //! <b>Requires</b>: "header" must be the header node of a tree.
  1113. //! "new_node" must be, according to the used ordering no less than the
  1114. //! greatest inserted key.
  1115. //!
  1116. //! <b>Effects</b>: Inserts new_node into the tree before "pos".
  1117. //!
  1118. //! <b>Complexity</b>: Constant-time.
  1119. //!
  1120. //! <b>Throws</b>: Nothing.
  1121. //!
  1122. //! <b>Note</b>: If "new_node" is less than the greatest inserted key
  1123. //! tree invariants are broken. This function is slightly faster than
  1124. //! using "insert_before".
  1125. static void push_back
  1126. (const node_ptr & header, const node_ptr & new_node
  1127. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1128. , std::size_t *pdepth = 0
  1129. #endif
  1130. )
  1131. {
  1132. insert_commit_data commit_data;
  1133. push_back_check(header, commit_data, pdepth);
  1134. insert_commit(header, new_node, commit_data);
  1135. }
  1136. //! <b>Requires</b>: "header" must be the header node of a tree.
  1137. //! "new_node" must be, according to the used ordering, no greater than the
  1138. //! lowest inserted key.
  1139. //!
  1140. //! <b>Effects</b>: Inserts new_node into the tree before "pos".
  1141. //!
  1142. //! <b>Complexity</b>: Constant-time.
  1143. //!
  1144. //! <b>Throws</b>: Nothing.
  1145. //!
  1146. //! <b>Note</b>: If "new_node" is greater than the lowest inserted key
  1147. //! tree invariants are broken. This function is slightly faster than
  1148. //! using "insert_before".
  1149. static void push_front
  1150. (const node_ptr & header, const node_ptr & new_node
  1151. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1152. , std::size_t *pdepth = 0
  1153. #endif
  1154. )
  1155. {
  1156. insert_commit_data commit_data;
  1157. push_front_check(header, commit_data, pdepth);
  1158. insert_commit(header, new_node, commit_data);
  1159. }
  1160. //! <b>Requires</b>: 'node' can't be a header node.
  1161. //!
  1162. //! <b>Effects</b>: Calculates the depth of a node: the depth of a
  1163. //! node is the length (number of edges) of the path from the root
  1164. //! to that node. (The root node is at depth 0.)
  1165. //!
  1166. //! <b>Complexity</b>: Logarithmic to the number of nodes in the tree.
  1167. //!
  1168. //! <b>Throws</b>: Nothing.
  1169. static std::size_t depth(const_node_ptr node)
  1170. {
  1171. std::size_t depth = 0;
  1172. node_ptr p_parent;
  1173. while(node != NodeTraits::get_parent(p_parent = NodeTraits::get_parent(node))){
  1174. ++depth;
  1175. node = p_parent;
  1176. }
  1177. return depth;
  1178. }
  1179. //! <b>Requires</b>: "cloner" must be a function
  1180. //! object taking a node_ptr and returning a new cloned node of it. "disposer" must
  1181. //! take a node_ptr and shouldn't throw.
  1182. //!
  1183. //! <b>Effects</b>: First empties target tree calling
  1184. //! <tt>void disposer::operator()(const node_ptr &)</tt> for every node of the tree
  1185. //! except the header.
  1186. //!
  1187. //! Then, duplicates the entire tree pointed by "source_header" cloning each
  1188. //! source node with <tt>node_ptr Cloner::operator()(const node_ptr &)</tt> to obtain
  1189. //! the nodes of the target tree. If "cloner" throws, the cloned target nodes
  1190. //! are disposed using <tt>void disposer(const node_ptr &)</tt>.
  1191. //!
  1192. //! <b>Complexity</b>: Linear to the number of element of the source tree plus the
  1193. //! number of elements of tree target tree when calling this function.
  1194. //!
  1195. //! <b>Throws</b>: If cloner functor throws. If this happens target nodes are disposed.
  1196. template <class Cloner, class Disposer>
  1197. static void clone
  1198. (const const_node_ptr & source_header, const node_ptr & target_header, Cloner cloner, Disposer disposer)
  1199. {
  1200. if(!unique(target_header)){
  1201. clear_and_dispose(target_header, disposer);
  1202. }
  1203. node_ptr leftmost, rightmost;
  1204. node_ptr new_root = clone_subtree
  1205. (source_header, target_header, cloner, disposer, leftmost, rightmost);
  1206. //Now update header node
  1207. NodeTraits::set_parent(target_header, new_root);
  1208. NodeTraits::set_left (target_header, leftmost);
  1209. NodeTraits::set_right (target_header, rightmost);
  1210. }
  1211. //! <b>Requires</b>: header must be the header of a tree, z a node
  1212. //! of that tree and z != header.
  1213. //!
  1214. //! <b>Effects</b>: Erases node "z" from the tree with header "header".
  1215. //!
  1216. //! <b>Complexity</b>: Amortized constant time.
  1217. //!
  1218. //! <b>Throws</b>: Nothing.
  1219. static void erase(const node_ptr & header, const node_ptr & z)
  1220. {
  1221. data_for_rebalance ignored;
  1222. erase(header, z, ignored);
  1223. }
  1224. //! <b>Requires</b>: node is a tree node but not the header.
  1225. //!
  1226. //! <b>Effects</b>: Unlinks the node and rebalances the tree.
  1227. //!
  1228. //! <b>Complexity</b>: Average complexity is constant time.
  1229. //!
  1230. //! <b>Throws</b>: Nothing.
  1231. static void unlink(const node_ptr & node)
  1232. {
  1233. node_ptr x = NodeTraits::get_parent(node);
  1234. if(x){
  1235. while(!base_type::is_header(x))
  1236. x = NodeTraits::get_parent(x);
  1237. erase(x, node);
  1238. }
  1239. }
  1240. //! <b>Requires</b>: header must be the header of a tree.
  1241. //!
  1242. //! <b>Effects</b>: Rebalances the tree.
  1243. //!
  1244. //! <b>Throws</b>: Nothing.
  1245. //!
  1246. //! <b>Complexity</b>: Linear.
  1247. static void rebalance(const node_ptr & header)
  1248. {
  1249. node_ptr root = NodeTraits::get_parent(header);
  1250. if(root){
  1251. rebalance_subtree(root);
  1252. }
  1253. }
  1254. //! <b>Requires</b>: old_root is a node of a tree. It shall not be null.
  1255. //!
  1256. //! <b>Effects</b>: Rebalances the subtree rooted at old_root.
  1257. //!
  1258. //! <b>Returns</b>: The new root of the subtree.
  1259. //!
  1260. //! <b>Throws</b>: Nothing.
  1261. //!
  1262. //! <b>Complexity</b>: Linear.
  1263. static node_ptr rebalance_subtree(const node_ptr & old_root)
  1264. {
  1265. //Taken from:
  1266. //"Tree rebalancing in optimal time and space"
  1267. //Quentin F. Stout and Bette L. Warren
  1268. //To avoid irregularities in the algorithm (old_root can be a
  1269. //left or right child or even the root of the tree) just put the
  1270. //root as the right child of its parent. Before doing this backup
  1271. //information to restore the original relationship after
  1272. //the algorithm is applied.
  1273. node_ptr super_root = NodeTraits::get_parent(old_root);
  1274. BOOST_INTRUSIVE_INVARIANT_ASSERT(super_root);
  1275. //Get root info
  1276. node_ptr super_root_right_backup = NodeTraits::get_right(super_root);
  1277. bool super_root_is_header = NodeTraits::get_parent(super_root) == old_root;
  1278. bool old_root_is_right = is_right_child(old_root);
  1279. NodeTraits::set_right(super_root, old_root);
  1280. std::size_t size;
  1281. subtree_to_vine(super_root, size);
  1282. vine_to_subtree(super_root, size);
  1283. node_ptr new_root = NodeTraits::get_right(super_root);
  1284. //Recover root
  1285. if(super_root_is_header){
  1286. NodeTraits::set_right(super_root, super_root_right_backup);
  1287. NodeTraits::set_parent(super_root, new_root);
  1288. }
  1289. else if(old_root_is_right){
  1290. NodeTraits::set_right(super_root, new_root);
  1291. }
  1292. else{
  1293. NodeTraits::set_right(super_root, super_root_right_backup);
  1294. NodeTraits::set_left(super_root, new_root);
  1295. }
  1296. return new_root;
  1297. }
  1298. //! <b>Effects</b>: Asserts the integrity of the container with additional checks provided by the user.
  1299. //!
  1300. //! <b>Requires</b>: header must be the header of a tree.
  1301. //!
  1302. //! <b>Complexity</b>: Linear time.
  1303. //!
  1304. //! <b>Note</b>: The method might not have effect when asserts are turned off (e.g., with NDEBUG).
  1305. //! Experimental function, interface might change in future versions.
  1306. template<class Checker>
  1307. static void check(const const_node_ptr& header, Checker checker, typename Checker::return_type& checker_return)
  1308. {
  1309. const_node_ptr root_node_ptr = NodeTraits::get_parent(header);
  1310. if (!root_node_ptr){
  1311. // check left&right header pointers
  1312. BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(header) == header);
  1313. BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(header) == header);
  1314. }
  1315. else{
  1316. // check parent pointer of root node
  1317. BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(root_node_ptr) == header);
  1318. // check subtree from root
  1319. check_subtree(root_node_ptr, checker, checker_return);
  1320. // check left&right header pointers
  1321. const_node_ptr p = root_node_ptr;
  1322. while (NodeTraits::get_left(p)) { p = NodeTraits::get_left(p); }
  1323. BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(header) == p);
  1324. p = root_node_ptr;
  1325. while (NodeTraits::get_right(p)) { p = NodeTraits::get_right(p); }
  1326. BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(header) == p);
  1327. }
  1328. }
  1329. protected:
  1330. static void erase(const node_ptr & header, const node_ptr & z, data_for_rebalance &info)
  1331. {
  1332. node_ptr y(z);
  1333. node_ptr x;
  1334. const node_ptr z_left(NodeTraits::get_left(z));
  1335. const node_ptr z_right(NodeTraits::get_right(z));
  1336. if(!z_left){
  1337. x = z_right; // x might be null.
  1338. }
  1339. else if(!z_right){ // z has exactly one non-null child. y == z.
  1340. x = z_left; // x is not null.
  1341. BOOST_ASSERT(x);
  1342. }
  1343. else{ //make y != z
  1344. // y = find z's successor
  1345. y = base_type::minimum(z_right);
  1346. x = NodeTraits::get_right(y); // x might be null.
  1347. }
  1348. node_ptr x_parent;
  1349. const node_ptr z_parent(NodeTraits::get_parent(z));
  1350. const bool z_is_leftchild(NodeTraits::get_left(z_parent) == z);
  1351. if(y != z){ //has two children and y is the minimum of z
  1352. //y is z's successor and it has a null left child.
  1353. //x is the right child of y (it can be null)
  1354. //Relink y in place of z and link x with y's old parent
  1355. NodeTraits::set_parent(z_left, y);
  1356. NodeTraits::set_left(y, z_left);
  1357. if(y != z_right){
  1358. //Link y with the right tree of z
  1359. NodeTraits::set_right(y, z_right);
  1360. NodeTraits::set_parent(z_right, y);
  1361. //Link x with y's old parent (y must be a left child)
  1362. x_parent = NodeTraits::get_parent(y);
  1363. BOOST_ASSERT(NodeTraits::get_left(x_parent) == y);
  1364. if(x)
  1365. NodeTraits::set_parent(x, x_parent);
  1366. //Since y was the successor and not the right child of z, it must be a left child
  1367. NodeTraits::set_left(x_parent, x);
  1368. }
  1369. else{ //y was the right child of y so no need to fix x's position
  1370. x_parent = y;
  1371. }
  1372. NodeTraits::set_parent(y, z_parent);
  1373. this_type::set_child(header, y, z_parent, z_is_leftchild);
  1374. }
  1375. else { // z has zero or one child, x is one child (it can be null)
  1376. //Just link x to z's parent
  1377. x_parent = z_parent;
  1378. if(x)
  1379. NodeTraits::set_parent(x, z_parent);
  1380. this_type::set_child(header, x, z_parent, z_is_leftchild);
  1381. //Now update leftmost/rightmost in case z was one of them
  1382. if(NodeTraits::get_left(header) == z){
  1383. //z_left must be null because z is the leftmost
  1384. BOOST_ASSERT(!z_left);
  1385. NodeTraits::set_left(header, !z_right ?
  1386. z_parent : // makes leftmost == header if z == root
  1387. base_type::minimum(z_right));
  1388. }
  1389. if(NodeTraits::get_right(header) == z){
  1390. //z_right must be null because z is the rightmost
  1391. BOOST_ASSERT(!z_right);
  1392. NodeTraits::set_right(header, !z_left ?
  1393. z_parent : // makes rightmost == header if z == root
  1394. base_type::maximum(z_left));
  1395. }
  1396. }
  1397. //If z had 0/1 child, y == z and one of its children (and maybe null)
  1398. //If z had 2 children, y is the successor of z and x is the right child of y
  1399. info.x = x;
  1400. info.y = y;
  1401. //If z had 0/1 child, x_parent is the new parent of the old right child of y (z's successor)
  1402. //If z had 2 children, x_parent is the new parent of y (z_parent)
  1403. BOOST_ASSERT(!x || NodeTraits::get_parent(x) == x_parent);
  1404. info.x_parent = x_parent;
  1405. }
  1406. //! <b>Requires</b>: node is a node of the tree but it's not the header.
  1407. //!
  1408. //! <b>Effects</b>: Returns the number of nodes of the subtree.
  1409. //!
  1410. //! <b>Complexity</b>: Linear time.
  1411. //!
  1412. //! <b>Throws</b>: Nothing.
  1413. static std::size_t subtree_size(const const_node_ptr & subtree)
  1414. {
  1415. std::size_t count = 0;
  1416. if (subtree){
  1417. node_ptr n = detail::uncast(subtree);
  1418. node_ptr m = NodeTraits::get_left(n);
  1419. while(m){
  1420. n = m;
  1421. m = NodeTraits::get_left(n);
  1422. }
  1423. while(1){
  1424. ++count;
  1425. node_ptr n_right(NodeTraits::get_right(n));
  1426. if(n_right){
  1427. n = n_right;
  1428. m = NodeTraits::get_left(n);
  1429. while(m){
  1430. n = m;
  1431. m = NodeTraits::get_left(n);
  1432. }
  1433. }
  1434. else {
  1435. do{
  1436. if (n == subtree){
  1437. return count;
  1438. }
  1439. m = n;
  1440. n = NodeTraits::get_parent(n);
  1441. }while(NodeTraits::get_left(n) != m);
  1442. }
  1443. }
  1444. }
  1445. return count;
  1446. }
  1447. //! <b>Requires</b>: p is a node of a tree.
  1448. //!
  1449. //! <b>Effects</b>: Returns true if p is a left child.
  1450. //!
  1451. //! <b>Complexity</b>: Constant.
  1452. //!
  1453. //! <b>Throws</b>: Nothing.
  1454. static bool is_left_child(const node_ptr & p)
  1455. { return NodeTraits::get_left(NodeTraits::get_parent(p)) == p; }
  1456. //! <b>Requires</b>: p is a node of a tree.
  1457. //!
  1458. //! <b>Effects</b>: Returns true if p is a right child.
  1459. //!
  1460. //! <b>Complexity</b>: Constant.
  1461. //!
  1462. //! <b>Throws</b>: Nothing.
  1463. static bool is_right_child(const node_ptr & p)
  1464. { return NodeTraits::get_right(NodeTraits::get_parent(p)) == p; }
  1465. static void insert_before_check
  1466. (const node_ptr &header, const node_ptr & pos
  1467. , insert_commit_data &commit_data
  1468. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1469. , std::size_t *pdepth = 0
  1470. #endif
  1471. )
  1472. {
  1473. node_ptr prev(pos);
  1474. if(pos != NodeTraits::get_left(header))
  1475. prev = base_type::prev_node(pos);
  1476. bool link_left = unique(header) || !NodeTraits::get_left(pos);
  1477. commit_data.link_left = link_left;
  1478. commit_data.node = link_left ? pos : prev;
  1479. if(pdepth){
  1480. *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1;
  1481. }
  1482. }
  1483. static void push_back_check
  1484. (const node_ptr & header, insert_commit_data &commit_data
  1485. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1486. , std::size_t *pdepth = 0
  1487. #endif
  1488. )
  1489. {
  1490. node_ptr prev(NodeTraits::get_right(header));
  1491. if(pdepth){
  1492. *pdepth = prev == header ? 0 : depth(prev) + 1;
  1493. }
  1494. commit_data.link_left = false;
  1495. commit_data.node = prev;
  1496. }
  1497. static void push_front_check
  1498. (const node_ptr & header, insert_commit_data &commit_data
  1499. #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  1500. , std::size_t *pdepth = 0
  1501. #endif
  1502. )
  1503. {
  1504. node_ptr pos(NodeTraits::get_left(header));
  1505. if(pdepth){
  1506. *pdepth = pos == header ? 0 : depth(pos) + 1;
  1507. }
  1508. commit_data.link_left = true;
  1509. commit_data.node = pos;
  1510. }
  1511. template<class NodePtrCompare>
  1512. static void insert_equal_check
  1513. (const node_ptr &header, const node_ptr & hint, const node_ptr & new_node, NodePtrCompare comp
  1514. , insert_commit_data &commit_data
  1515. /// @cond
  1516. , std::size_t *pdepth = 0
  1517. /// @endcond
  1518. )
  1519. {
  1520. if(hint == header || !comp(hint, new_node)){
  1521. node_ptr prev(hint);
  1522. if(hint == NodeTraits::get_left(header) ||
  1523. !comp(new_node, (prev = base_type::prev_node(hint)))){
  1524. bool link_left = unique(header) || !NodeTraits::get_left(hint);
  1525. commit_data.link_left = link_left;
  1526. commit_data.node = link_left ? hint : prev;
  1527. if(pdepth){
  1528. *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1;
  1529. }
  1530. }
  1531. else{
  1532. insert_equal_upper_bound_check(header, new_node, comp, commit_data, pdepth);
  1533. }
  1534. }
  1535. else{
  1536. insert_equal_lower_bound_check(header, new_node, comp, commit_data, pdepth);
  1537. }
  1538. }
  1539. template<class NodePtrCompare>
  1540. static void insert_equal_upper_bound_check
  1541. (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp, insert_commit_data & commit_data, std::size_t *pdepth = 0)
  1542. {
  1543. std::size_t depth = 0;
  1544. node_ptr y(h);
  1545. node_ptr x(NodeTraits::get_parent(y));
  1546. while(x){
  1547. ++depth;
  1548. y = x;
  1549. x = comp(new_node, x) ?
  1550. NodeTraits::get_left(x) : NodeTraits::get_right(x);
  1551. }
  1552. if(pdepth) *pdepth = depth;
  1553. commit_data.link_left = (y == h) || comp(new_node, y);
  1554. commit_data.node = y;
  1555. }
  1556. template<class NodePtrCompare>
  1557. static void insert_equal_lower_bound_check
  1558. (const node_ptr & h, const node_ptr & new_node, NodePtrCompare comp, insert_commit_data & commit_data, std::size_t *pdepth = 0)
  1559. {
  1560. std::size_t depth = 0;
  1561. node_ptr y(h);
  1562. node_ptr x(NodeTraits::get_parent(y));
  1563. while(x){
  1564. ++depth;
  1565. y = x;
  1566. x = !comp(x, new_node) ?
  1567. NodeTraits::get_left(x) : NodeTraits::get_right(x);
  1568. }
  1569. if(pdepth) *pdepth = depth;
  1570. commit_data.link_left = (y == h) || !comp(y, new_node);
  1571. commit_data.node = y;
  1572. }
  1573. static void insert_commit
  1574. (const node_ptr & header, const node_ptr & new_node, const insert_commit_data &commit_data)
  1575. {
  1576. //Check if commit_data has not been initialized by a insert_unique_check call.
  1577. BOOST_INTRUSIVE_INVARIANT_ASSERT(commit_data.node != node_ptr());
  1578. node_ptr parent_node(commit_data.node);
  1579. if(parent_node == header){
  1580. NodeTraits::set_parent(header, new_node);
  1581. NodeTraits::set_right(header, new_node);
  1582. NodeTraits::set_left(header, new_node);
  1583. }
  1584. else if(commit_data.link_left){
  1585. NodeTraits::set_left(parent_node, new_node);
  1586. if(parent_node == NodeTraits::get_left(header))
  1587. NodeTraits::set_left(header, new_node);
  1588. }
  1589. else{
  1590. NodeTraits::set_right(parent_node, new_node);
  1591. if(parent_node == NodeTraits::get_right(header))
  1592. NodeTraits::set_right(header, new_node);
  1593. }
  1594. NodeTraits::set_parent(new_node, parent_node);
  1595. NodeTraits::set_right(new_node, node_ptr());
  1596. NodeTraits::set_left(new_node, node_ptr());
  1597. }
  1598. //Fix header and own's parent data when replacing x with own, providing own's old data with parent
  1599. static void set_child(const node_ptr & header, const node_ptr & new_child, const node_ptr & new_parent, const bool link_left)
  1600. {
  1601. if(new_parent == header)
  1602. NodeTraits::set_parent(header, new_child);
  1603. else if(link_left)
  1604. NodeTraits::set_left(new_parent, new_child);
  1605. else
  1606. NodeTraits::set_right(new_parent, new_child);
  1607. }
  1608. // rotate p to left (no header and p's parent fixup)
  1609. static void rotate_left_no_parent_fix(const node_ptr & p, const node_ptr &p_right)
  1610. {
  1611. node_ptr p_right_left(NodeTraits::get_left(p_right));
  1612. NodeTraits::set_right(p, p_right_left);
  1613. if(p_right_left){
  1614. NodeTraits::set_parent(p_right_left, p);
  1615. }
  1616. NodeTraits::set_left(p_right, p);
  1617. NodeTraits::set_parent(p, p_right);
  1618. }
  1619. // rotate p to left (with header and p's parent fixup)
  1620. static void rotate_left(const node_ptr & p, const node_ptr & p_right, const node_ptr & p_parent, const node_ptr & header)
  1621. {
  1622. const bool p_was_left(NodeTraits::get_left(p_parent) == p);
  1623. rotate_left_no_parent_fix(p, p_right);
  1624. NodeTraits::set_parent(p_right, p_parent);
  1625. set_child(header, p_right, p_parent, p_was_left);
  1626. }
  1627. // rotate p to right (no header and p's parent fixup)
  1628. static void rotate_right_no_parent_fix(const node_ptr & p, const node_ptr &p_left)
  1629. {
  1630. node_ptr p_left_right(NodeTraits::get_right(p_left));
  1631. NodeTraits::set_left(p, p_left_right);
  1632. if(p_left_right){
  1633. NodeTraits::set_parent(p_left_right, p);
  1634. }
  1635. NodeTraits::set_right(p_left, p);
  1636. NodeTraits::set_parent(p, p_left);
  1637. }
  1638. // rotate p to right (with header and p's parent fixup)
  1639. static void rotate_right(const node_ptr & p, const node_ptr & p_left, const node_ptr & p_parent, const node_ptr & header)
  1640. {
  1641. const bool p_was_left(NodeTraits::get_left(p_parent) == p);
  1642. rotate_right_no_parent_fix(p, p_left);
  1643. NodeTraits::set_parent(p_left, p_parent);
  1644. set_child(header, p_left, p_parent, p_was_left);
  1645. }
  1646. private:
  1647. static void subtree_to_vine(node_ptr vine_tail, std::size_t &size)
  1648. {
  1649. //Inspired by LibAVL:
  1650. //It uses a clever optimization for trees with parent pointers.
  1651. //No parent pointer is updated when transforming a tree to a vine as
  1652. //most of them will be overriten during compression rotations.
  1653. //A final pass must be made after the rebalancing to updated those
  1654. //pointers not updated by tree_to_vine + compression calls
  1655. std::size_t len = 0;
  1656. node_ptr remainder = NodeTraits::get_right(vine_tail);
  1657. while(remainder){
  1658. node_ptr tempptr = NodeTraits::get_left(remainder);
  1659. if(!tempptr){ //move vine-tail down one
  1660. vine_tail = remainder;
  1661. remainder = NodeTraits::get_right(remainder);
  1662. ++len;
  1663. }
  1664. else{ //rotate
  1665. NodeTraits::set_left(remainder, NodeTraits::get_right(tempptr));
  1666. NodeTraits::set_right(tempptr, remainder);
  1667. remainder = tempptr;
  1668. NodeTraits::set_right(vine_tail, tempptr);
  1669. }
  1670. }
  1671. size = len;
  1672. }
  1673. static void compress_subtree(node_ptr scanner, std::size_t count)
  1674. {
  1675. while(count--){ //compress "count" spine nodes in the tree with pseudo-root scanner
  1676. node_ptr child = NodeTraits::get_right(scanner);
  1677. node_ptr child_right = NodeTraits::get_right(child);
  1678. NodeTraits::set_right(scanner, child_right);
  1679. //Avoid setting the parent of child_right
  1680. scanner = child_right;
  1681. node_ptr scanner_left = NodeTraits::get_left(scanner);
  1682. NodeTraits::set_right(child, scanner_left);
  1683. if(scanner_left)
  1684. NodeTraits::set_parent(scanner_left, child);
  1685. NodeTraits::set_left(scanner, child);
  1686. NodeTraits::set_parent(child, scanner);
  1687. }
  1688. }
  1689. static void vine_to_subtree(const node_ptr & super_root, std::size_t count)
  1690. {
  1691. const std::size_t one_szt = 1u;
  1692. std::size_t leaf_nodes = count + one_szt - std::size_t(one_szt << detail::floor_log2(count + one_szt));
  1693. compress_subtree(super_root, leaf_nodes); //create deepest leaves
  1694. std::size_t vine_nodes = count - leaf_nodes;
  1695. while(vine_nodes > 1){
  1696. vine_nodes /= 2;
  1697. compress_subtree(super_root, vine_nodes);
  1698. }
  1699. //Update parents of nodes still in the in the original vine line
  1700. //as those have not been updated by subtree_to_vine or compress_subtree
  1701. for ( node_ptr q = super_root, p = NodeTraits::get_right(super_root)
  1702. ; p
  1703. ; q = p, p = NodeTraits::get_right(p)){
  1704. NodeTraits::set_parent(p, q);
  1705. }
  1706. }
  1707. //! <b>Requires</b>: "n" must be a node inserted in a tree.
  1708. //!
  1709. //! <b>Effects</b>: Returns a pointer to the header node of the tree.
  1710. //!
  1711. //! <b>Complexity</b>: Logarithmic.
  1712. //!
  1713. //! <b>Throws</b>: Nothing.
  1714. static node_ptr get_root(const node_ptr & node)
  1715. {
  1716. BOOST_INTRUSIVE_INVARIANT_ASSERT((!inited(node)));
  1717. node_ptr x = NodeTraits::get_parent(node);
  1718. if(x){
  1719. while(!base_type::is_header(x)){
  1720. x = NodeTraits::get_parent(x);
  1721. }
  1722. return x;
  1723. }
  1724. else{
  1725. return node;
  1726. }
  1727. }
  1728. template <class Cloner, class Disposer>
  1729. static node_ptr clone_subtree
  1730. (const const_node_ptr &source_parent, const node_ptr &target_parent
  1731. , Cloner cloner, Disposer disposer
  1732. , node_ptr &leftmost_out, node_ptr &rightmost_out
  1733. )
  1734. {
  1735. node_ptr target_sub_root = target_parent;
  1736. node_ptr source_root = NodeTraits::get_parent(source_parent);
  1737. if(!source_root){
  1738. leftmost_out = rightmost_out = source_root;
  1739. }
  1740. else{
  1741. //We'll calculate leftmost and rightmost nodes while iterating
  1742. node_ptr current = source_root;
  1743. node_ptr insertion_point = target_sub_root = cloner(current);
  1744. //We'll calculate leftmost and rightmost nodes while iterating
  1745. node_ptr leftmost = target_sub_root;
  1746. node_ptr rightmost = target_sub_root;
  1747. //First set the subroot
  1748. NodeTraits::set_left(target_sub_root, node_ptr());
  1749. NodeTraits::set_right(target_sub_root, node_ptr());
  1750. NodeTraits::set_parent(target_sub_root, target_parent);
  1751. dispose_subtree_disposer<Disposer> rollback(disposer, target_sub_root);
  1752. while(true) {
  1753. //First clone left nodes
  1754. if( NodeTraits::get_left(current) &&
  1755. !NodeTraits::get_left(insertion_point)) {
  1756. current = NodeTraits::get_left(current);
  1757. node_ptr temp = insertion_point;
  1758. //Clone and mark as leaf
  1759. insertion_point = cloner(current);
  1760. NodeTraits::set_left (insertion_point, node_ptr());
  1761. NodeTraits::set_right (insertion_point, node_ptr());
  1762. //Insert left
  1763. NodeTraits::set_parent(insertion_point, temp);
  1764. NodeTraits::set_left (temp, insertion_point);
  1765. //Update leftmost
  1766. if(rightmost == target_sub_root)
  1767. leftmost = insertion_point;
  1768. }
  1769. //Then clone right nodes
  1770. else if( NodeTraits::get_right(current) &&
  1771. !NodeTraits::get_right(insertion_point)){
  1772. current = NodeTraits::get_right(current);
  1773. node_ptr temp = insertion_point;
  1774. //Clone and mark as leaf
  1775. insertion_point = cloner(current);
  1776. NodeTraits::set_left (insertion_point, node_ptr());
  1777. NodeTraits::set_right (insertion_point, node_ptr());
  1778. //Insert right
  1779. NodeTraits::set_parent(insertion_point, temp);
  1780. NodeTraits::set_right (temp, insertion_point);
  1781. //Update rightmost
  1782. rightmost = insertion_point;
  1783. }
  1784. //If not, go up
  1785. else if(current == source_root){
  1786. break;
  1787. }
  1788. else{
  1789. //Branch completed, go up searching more nodes to clone
  1790. current = NodeTraits::get_parent(current);
  1791. insertion_point = NodeTraits::get_parent(insertion_point);
  1792. }
  1793. }
  1794. rollback.release();
  1795. leftmost_out = leftmost;
  1796. rightmost_out = rightmost;
  1797. }
  1798. return target_sub_root;
  1799. }
  1800. template<class Disposer>
  1801. static void dispose_subtree(node_ptr x, Disposer disposer)
  1802. {
  1803. while (x){
  1804. node_ptr save(NodeTraits::get_left(x));
  1805. if (save) {
  1806. // Right rotation
  1807. NodeTraits::set_left(x, NodeTraits::get_right(save));
  1808. NodeTraits::set_right(save, x);
  1809. }
  1810. else {
  1811. save = NodeTraits::get_right(x);
  1812. init(x);
  1813. disposer(x);
  1814. }
  1815. x = save;
  1816. }
  1817. }
  1818. template<class KeyType, class KeyNodePtrCompare>
  1819. static node_ptr lower_bound_loop
  1820. (node_ptr x, node_ptr y, const KeyType &key, KeyNodePtrCompare comp)
  1821. {
  1822. while(x){
  1823. if(comp(x, key)){
  1824. x = NodeTraits::get_right(x);
  1825. }
  1826. else{
  1827. y = x;
  1828. x = NodeTraits::get_left(x);
  1829. }
  1830. }
  1831. return y;
  1832. }
  1833. template<class KeyType, class KeyNodePtrCompare>
  1834. static node_ptr upper_bound_loop
  1835. (node_ptr x, node_ptr y, const KeyType &key, KeyNodePtrCompare comp)
  1836. {
  1837. while(x){
  1838. if(comp(key, x)){
  1839. y = x;
  1840. x = NodeTraits::get_left(x);
  1841. }
  1842. else{
  1843. x = NodeTraits::get_right(x);
  1844. }
  1845. }
  1846. return y;
  1847. }
  1848. template<class Checker>
  1849. static void check_subtree(const const_node_ptr& node, Checker checker, typename Checker::return_type& check_return)
  1850. {
  1851. const_node_ptr left = NodeTraits::get_left(node);
  1852. const_node_ptr right = NodeTraits::get_right(node);
  1853. typename Checker::return_type check_return_left;
  1854. typename Checker::return_type check_return_right;
  1855. if (left)
  1856. {
  1857. BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(left) == node);
  1858. check_subtree(left, checker, check_return_left);
  1859. }
  1860. if (right)
  1861. {
  1862. BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(right) == node);
  1863. check_subtree(right, checker, check_return_right);
  1864. }
  1865. checker(node, check_return_left, check_return_right, check_return);
  1866. }
  1867. };
  1868. /// @cond
  1869. template<class NodeTraits>
  1870. struct get_algo<BsTreeAlgorithms, NodeTraits>
  1871. {
  1872. typedef bstree_algorithms<NodeTraits> type;
  1873. };
  1874. template <class ValueTraits, class NodePtrCompare, class ExtraChecker>
  1875. struct get_node_checker<BsTreeAlgorithms, ValueTraits, NodePtrCompare, ExtraChecker>
  1876. {
  1877. typedef detail::bstree_node_checker<ValueTraits, NodePtrCompare, ExtraChecker> type;
  1878. };
  1879. /// @endcond
  1880. } //namespace intrusive
  1881. } //namespace boost
  1882. #include <boost/intrusive/detail/config_end.hpp>
  1883. #endif //BOOST_INTRUSIVE_BSTREE_ALGORITHMS_HPP