bstree_algorithms.hpp 78 KB

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