bstree_algorithms.hpp 78 KB

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