bstree_algorithms.hpp 78 KB

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