123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103 |
- #ifndef BOOST_INTRUSIVE_BSTREE_ALGORITHMS_HPP
- #define BOOST_INTRUSIVE_BSTREE_ALGORITHMS_HPP
- #include <cstddef>
- #include <boost/intrusive/detail/config_begin.hpp>
- #include <boost/intrusive/intrusive_fwd.hpp>
- #include <boost/intrusive/detail/bstree_algorithms_base.hpp>
- #include <boost/intrusive/detail/assert.hpp>
- #include <boost/intrusive/detail/uncast.hpp>
- #include <boost/intrusive/detail/math.hpp>
- #include <boost/intrusive/detail/algo_type.hpp>
- #include <boost/intrusive/detail/minimal_pair_header.hpp>
- #if defined(BOOST_HAS_PRAGMA_ONCE)
- # pragma once
- #endif
- namespace boost {
- namespace intrusive {
- template <class NodePtr>
- struct insert_commit_data_t
- {
- inline insert_commit_data_t()
- : link_left(false), node()
- {}
- bool link_left;
- NodePtr node;
- };
- template <class NodePtr>
- struct data_for_rebalance_t
- {
- NodePtr x;
- NodePtr x_parent;
- NodePtr y;
- };
- namespace detail {
- template<class ValueTraits, class NodePtrCompare, class ExtraChecker>
- struct bstree_node_checker
- : public ExtraChecker
- {
- typedef ExtraChecker base_checker_t;
- typedef ValueTraits value_traits;
- typedef typename value_traits::node_traits node_traits;
- typedef typename node_traits::const_node_ptr const_node_ptr;
- struct return_type
- : public base_checker_t::return_type
- {
- inline return_type()
- : min_key_node_ptr(const_node_ptr()), max_key_node_ptr(const_node_ptr()), node_count(0)
- {}
- const_node_ptr min_key_node_ptr;
- const_node_ptr max_key_node_ptr;
- size_t node_count;
- };
- inline bstree_node_checker(const NodePtrCompare& comp, ExtraChecker extra_checker)
- : base_checker_t(extra_checker), comp_(comp)
- {}
- void operator () (const_node_ptr p,
- const return_type& check_return_left, const return_type& check_return_right,
- return_type& check_return)
- {
- BOOST_INTRUSIVE_INVARIANT_ASSERT(!check_return_left.max_key_node_ptr || !comp_(p, check_return_left.max_key_node_ptr));
- BOOST_INTRUSIVE_INVARIANT_ASSERT(!check_return_right.min_key_node_ptr || !comp_(check_return_right.min_key_node_ptr, p));
- check_return.min_key_node_ptr = node_traits::get_left(p)? check_return_left.min_key_node_ptr : p;
- check_return.max_key_node_ptr = node_traits::get_right(p)? check_return_right.max_key_node_ptr : p;
- check_return.node_count = check_return_left.node_count + check_return_right.node_count + 1;
- base_checker_t::operator()(p, check_return_left, check_return_right, check_return);
- }
- const NodePtrCompare comp_;
- };
- }
- template<class NodeTraits>
- class bstree_algorithms : public bstree_algorithms_base<NodeTraits>
- {
- public:
- typedef typename NodeTraits::node node;
- typedef NodeTraits node_traits;
- typedef typename NodeTraits::node_ptr node_ptr;
- typedef typename NodeTraits::const_node_ptr const_node_ptr;
- typedef insert_commit_data_t<node_ptr> insert_commit_data;
- typedef data_for_rebalance_t<node_ptr> data_for_rebalance;
-
- typedef bstree_algorithms<NodeTraits> this_type;
- typedef bstree_algorithms_base<NodeTraits> base_type;
- private:
- template<class Disposer>
- struct dispose_subtree_disposer
- {
- inline dispose_subtree_disposer(Disposer &disp, node_ptr subtree)
- : disposer_(&disp), subtree_(subtree)
- {}
- inline void release()
- { disposer_ = 0; }
- inline ~dispose_subtree_disposer()
- {
- if(disposer_){
- dispose_subtree(subtree_, *disposer_);
- }
- }
- Disposer *disposer_;
- const node_ptr subtree_;
- };
-
- public:
-
-
-
-
-
-
-
- inline static node_ptr begin_node(const_node_ptr header) BOOST_NOEXCEPT
- { return node_traits::get_left(header); }
-
-
-
-
-
-
-
- inline static node_ptr end_node(const_node_ptr header) BOOST_NOEXCEPT
- { return detail::uncast(header); }
-
-
-
-
-
-
-
- inline static node_ptr root_node(const_node_ptr header) BOOST_NOEXCEPT
- {
- node_ptr p = node_traits::get_parent(header);
- return p ? p : detail::uncast(header);
- }
-
-
-
-
-
-
-
-
- inline static bool unique(const_node_ptr n) BOOST_NOEXCEPT
- { return !NodeTraits::get_parent(n); }
- #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
-
-
-
-
-
-
-
- static node_ptr get_header(const_node_ptr n);
- #endif
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- static void swap_nodes(node_ptr node1, node_ptr node2) BOOST_NOEXCEPT
- {
- if(node1 == node2)
- return;
- node_ptr header1(base_type::get_header(node1)), header2(base_type::get_header(node2));
- swap_nodes(node1, header1, node2, header2);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- static void swap_nodes(node_ptr node1, node_ptr header1, node_ptr node2, node_ptr header2) BOOST_NOEXCEPT
- {
- if(node1 == node2)
- return;
-
-
- if(header1 != header2){
-
- if(node1 == NodeTraits::get_left(header1)){
- NodeTraits::set_left(header1, node2);
- }
- if(node1 == NodeTraits::get_right(header1)){
- NodeTraits::set_right(header1, node2);
- }
- if(node1 == NodeTraits::get_parent(header1)){
- NodeTraits::set_parent(header1, node2);
- }
-
- if(node2 == NodeTraits::get_left(header2)){
- NodeTraits::set_left(header2, node1);
- }
- if(node2 == NodeTraits::get_right(header2)){
- NodeTraits::set_right(header2, node1);
- }
- if(node2 == NodeTraits::get_parent(header2)){
- NodeTraits::set_parent(header2, node1);
- }
- }
- else{
-
-
- if(node1 == NodeTraits::get_left(header1)){
- NodeTraits::set_left(header1, node2);
- }
- else if(node2 == NodeTraits::get_left(header2)){
- NodeTraits::set_left(header2, node1);
- }
- if(node1 == NodeTraits::get_right(header1)){
- NodeTraits::set_right(header1, node2);
- }
- else if(node2 == NodeTraits::get_right(header2)){
- NodeTraits::set_right(header2, node1);
- }
- if(node1 == NodeTraits::get_parent(header1)){
- NodeTraits::set_parent(header1, node2);
- }
- else if(node2 == NodeTraits::get_parent(header2)){
- NodeTraits::set_parent(header2, node1);
- }
-
-
- if(node1 == NodeTraits::get_parent(node2)){
- NodeTraits::set_parent(node2, node2);
- if(node2 == NodeTraits::get_right(node1)){
- NodeTraits::set_right(node1, node1);
- }
- else{
- NodeTraits::set_left(node1, node1);
- }
- }
- else if(node2 == NodeTraits::get_parent(node1)){
- NodeTraits::set_parent(node1, node1);
- if(node1 == NodeTraits::get_right(node2)){
- NodeTraits::set_right(node2, node2);
- }
- else{
- NodeTraits::set_left(node2, node2);
- }
- }
- }
-
- node_ptr temp;
-
- temp = NodeTraits::get_left(node1);
- NodeTraits::set_left(node1, NodeTraits::get_left(node2));
- NodeTraits::set_left(node2, temp);
-
- temp = NodeTraits::get_right(node1);
- NodeTraits::set_right(node1, NodeTraits::get_right(node2));
- NodeTraits::set_right(node2, temp);
-
- temp = NodeTraits::get_parent(node1);
- NodeTraits::set_parent(node1, NodeTraits::get_parent(node2));
- NodeTraits::set_parent(node2, temp);
-
- if((temp = NodeTraits::get_left(node1))){
- NodeTraits::set_parent(temp, node1);
- }
- if((temp = NodeTraits::get_right(node1))){
- NodeTraits::set_parent(temp, node1);
- }
-
- if((temp = NodeTraits::get_left(node2))){
- NodeTraits::set_parent(temp, node2);
- }
- if((temp = NodeTraits::get_right(node2))){
- NodeTraits::set_parent(temp, node2);
- }
-
- if ((temp = NodeTraits::get_parent(node1)) == NodeTraits::get_parent(node2)) {
-
- const node_ptr left = NodeTraits::get_left(temp);
- NodeTraits::set_left(temp, NodeTraits::get_right(temp));
- NodeTraits::set_right(temp, left);
- } else {
- if ((temp = NodeTraits::get_parent(node1)) &&
-
- temp != header2) {
- if (NodeTraits::get_left(temp) == node2) {
- NodeTraits::set_left(temp, node1);
- }
- if (NodeTraits::get_right(temp) == node2) {
- NodeTraits::set_right(temp, node1);
- }
- }
- if ((temp = NodeTraits::get_parent(node2)) &&
-
- temp != header1) {
- if (NodeTraits::get_left(temp) == node1) {
- NodeTraits::set_left(temp, node2);
- }
- if (NodeTraits::get_right(temp) == node1) {
- NodeTraits::set_right(temp, node2);
- }
- }
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- inline static void replace_node(node_ptr node_to_be_replaced, node_ptr new_node) BOOST_NOEXCEPT
- {
- replace_node(node_to_be_replaced, base_type::get_header(node_to_be_replaced), new_node);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- static void replace_node(node_ptr node_to_be_replaced, node_ptr header, node_ptr new_node) BOOST_NOEXCEPT
- {
- BOOST_ASSERT(node_to_be_replaced != new_node);
-
- if(node_to_be_replaced == NodeTraits::get_left(header)){
- NodeTraits::set_left(header, new_node);
- }
- if(node_to_be_replaced == NodeTraits::get_right(header)){
- NodeTraits::set_right(header, new_node);
- }
- if(node_to_be_replaced == NodeTraits::get_parent(header)){
- NodeTraits::set_parent(header, new_node);
- }
-
- node_ptr temp;
- NodeTraits::set_left(new_node, NodeTraits::get_left(node_to_be_replaced));
- NodeTraits::set_right(new_node, NodeTraits::get_right(node_to_be_replaced));
- NodeTraits::set_parent(new_node, NodeTraits::get_parent(node_to_be_replaced));
-
- if((temp = NodeTraits::get_left(new_node))){
- NodeTraits::set_parent(temp, new_node);
- }
- if((temp = NodeTraits::get_right(new_node))){
- NodeTraits::set_parent(temp, new_node);
- }
- if((temp = NodeTraits::get_parent(new_node)) &&
-
- temp != header){
- if(NodeTraits::get_left(temp) == node_to_be_replaced){
- NodeTraits::set_left(temp, new_node);
- }
- if(NodeTraits::get_right(temp) == node_to_be_replaced){
- NodeTraits::set_right(temp, new_node);
- }
- }
- }
- #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
-
-
-
-
-
-
-
- static node_ptr next_node(node_ptr n) BOOST_NOEXCEPT;
-
-
-
-
-
-
-
- static node_ptr prev_node(node_ptr n) BOOST_NOEXCEPT;
-
-
-
-
-
-
-
- static node_ptr minimum(node_ptr n);
-
-
-
-
-
-
-
- static node_ptr maximum(node_ptr n);
- #endif
-
-
-
-
-
-
-
-
-
- static void init(node_ptr n) BOOST_NOEXCEPT
- {
- NodeTraits::set_parent(n, node_ptr());
- NodeTraits::set_left(n, node_ptr());
- NodeTraits::set_right(n, node_ptr());
- }
-
-
-
-
-
- static bool inited(const_node_ptr n)
- {
- return !NodeTraits::get_parent(n) &&
- !NodeTraits::get_left(n) &&
- !NodeTraits::get_right(n) ;
- }
-
-
-
-
-
-
-
-
-
-
- static void init_header(node_ptr header) BOOST_NOEXCEPT
- {
- NodeTraits::set_parent(header, node_ptr());
- NodeTraits::set_left(header, header);
- NodeTraits::set_right(header, header);
- }
-
-
-
-
-
-
-
-
-
-
-
- template<class Disposer>
- static void clear_and_dispose(node_ptr header, Disposer disposer) BOOST_NOEXCEPT
- {
- node_ptr source_root = NodeTraits::get_parent(header);
- if(!source_root)
- return;
- dispose_subtree(source_root, disposer);
- init_header(header);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- static node_ptr unlink_leftmost_without_rebalance(node_ptr header) BOOST_NOEXCEPT
- {
- node_ptr leftmost = NodeTraits::get_left(header);
- if (leftmost == header)
- return node_ptr();
- node_ptr leftmost_parent(NodeTraits::get_parent(leftmost));
- node_ptr leftmost_right (NodeTraits::get_right(leftmost));
- bool is_root = leftmost_parent == header;
- if (leftmost_right){
- NodeTraits::set_parent(leftmost_right, leftmost_parent);
- NodeTraits::set_left(header, base_type::minimum(leftmost_right));
- if (is_root)
- NodeTraits::set_parent(header, leftmost_right);
- else
- NodeTraits::set_left(NodeTraits::get_parent(header), leftmost_right);
- }
- else if (is_root){
- NodeTraits::set_parent(header, node_ptr());
- NodeTraits::set_left(header, header);
- NodeTraits::set_right(header, header);
- }
- else{
- NodeTraits::set_left(leftmost_parent, node_ptr());
- NodeTraits::set_left(header, leftmost_parent);
- }
- return leftmost;
- }
-
-
-
-
-
-
-
- static std::size_t size(const_node_ptr header) BOOST_NOEXCEPT
- {
- node_ptr beg(begin_node(header));
- node_ptr end(end_node(header));
- std::size_t i = 0;
- for(;beg != end; beg = base_type::next_node(beg)) ++i;
- return i;
- }
-
-
-
-
-
-
-
-
-
- static void swap_tree(node_ptr header1, node_ptr header2) BOOST_NOEXCEPT
- {
- if(header1 == header2)
- return;
- node_ptr tmp;
-
- tmp = NodeTraits::get_parent(header1);
- NodeTraits::set_parent(header1, NodeTraits::get_parent(header2));
- NodeTraits::set_parent(header2, tmp);
-
- tmp = NodeTraits::get_left(header1);
- NodeTraits::set_left(header1, NodeTraits::get_left(header2));
- NodeTraits::set_left(header2, tmp);
-
- tmp = NodeTraits::get_right(header1);
- NodeTraits::set_right(header1, NodeTraits::get_right(header2));
- NodeTraits::set_right(header2, tmp);
-
- node_ptr h1_parent(NodeTraits::get_parent(header1));
- if(h1_parent){
- NodeTraits::set_parent(h1_parent, header1);
- }
- else{
- NodeTraits::set_left(header1, header1);
- NodeTraits::set_right(header1, header1);
- }
- node_ptr h2_parent(NodeTraits::get_parent(header2));
- if(h2_parent){
- NodeTraits::set_parent(h2_parent, header2);
- }
- else{
- NodeTraits::set_left(header2, header2);
- NodeTraits::set_right(header2, header2);
- }
- }
- #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
-
-
-
-
-
-
-
- static bool is_header(const_node_ptr p) BOOST_NOEXCEPT;
- #endif
-
-
-
-
-
-
-
-
-
-
-
- template<class KeyType, class KeyNodePtrCompare>
- static node_ptr find
- (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
- {
- node_ptr end = detail::uncast(header);
- node_ptr y = lower_bound(header, key, comp);
- return (y == end || comp(key, y)) ? end : y;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template< class KeyType, class KeyNodePtrCompare>
- static std::pair<node_ptr, node_ptr> bounded_range
- ( const_node_ptr header
- , const KeyType &lower_key
- , const KeyType &upper_key
- , KeyNodePtrCompare comp
- , bool left_closed
- , bool right_closed)
- {
- node_ptr y = detail::uncast(header);
- node_ptr x = NodeTraits::get_parent(header);
- while(x){
-
-
- if(comp(x, lower_key)){
-
- BOOST_INTRUSIVE_INVARIANT_ASSERT(comp(x, upper_key));
- x = NodeTraits::get_right(x);
- }
-
-
- else if(comp(upper_key, x)){
- y = x;
- x = NodeTraits::get_left(x);
- }
- else{
-
-
-
-
- BOOST_INTRUSIVE_INVARIANT_ASSERT(left_closed || right_closed || comp(lower_key, x) || comp(x, upper_key));
- return std::pair<node_ptr,node_ptr>(
- left_closed
-
-
-
- ? lower_bound_loop(NodeTraits::get_left(x), x, lower_key, comp)
-
-
- : upper_bound_loop(x, y, lower_key, comp)
- ,
- right_closed
-
-
-
- ? upper_bound_loop(NodeTraits::get_right(x), y, upper_key, comp)
-
-
- : lower_bound_loop(x, y, upper_key, comp)
- );
- }
- }
- return std::pair<node_ptr,node_ptr> (y, y);
- }
-
-
-
-
-
-
-
-
-
-
-
- template<class KeyType, class KeyNodePtrCompare>
- static std::size_t count
- (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
- {
- std::pair<node_ptr, node_ptr> ret = equal_range(header, key, comp);
- std::size_t n = 0;
- while(ret.first != ret.second){
- ++n;
- ret.first = base_type::next_node(ret.first);
- }
- return n;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- template<class KeyType, class KeyNodePtrCompare>
- inline static std::pair<node_ptr, node_ptr> equal_range
- (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
- {
- return bounded_range(header, key, key, comp, true, true);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- template<class KeyType, class KeyNodePtrCompare>
- static std::pair<node_ptr, node_ptr> lower_bound_range
- (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
- {
- node_ptr const lb(lower_bound(header, key, comp));
- std::pair<node_ptr, node_ptr> ret_ii(lb, lb);
- if(lb != header && !comp(key, lb)){
- ret_ii.second = base_type::next_node(ret_ii.second);
- }
- return ret_ii;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- template<class KeyType, class KeyNodePtrCompare>
- inline static node_ptr lower_bound
- (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
- {
- return lower_bound_loop(NodeTraits::get_parent(header), detail::uncast(header), key, comp);
- }
-
-
-
-
-
-
-
-
-
-
-
- template<class KeyType, class KeyNodePtrCompare>
- inline static node_ptr upper_bound
- (const_node_ptr header, const KeyType &key, KeyNodePtrCompare comp)
- {
- return upper_bound_loop(NodeTraits::get_parent(header), detail::uncast(header), key, comp);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- inline static void insert_unique_commit
- (node_ptr header, node_ptr new_value, const insert_commit_data &commit_data) BOOST_NOEXCEPT
- { return insert_commit(header, new_value, commit_data); }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template<class KeyType, class KeyNodePtrCompare>
- static std::pair<node_ptr, bool> insert_unique_check
- (const_node_ptr header, const KeyType &key
- ,KeyNodePtrCompare comp, insert_commit_data &commit_data
- #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
- , std::size_t *pdepth = 0
- #endif
- )
- {
- std::size_t depth = 0;
- node_ptr h(detail::uncast(header));
- node_ptr y(h);
- node_ptr x(NodeTraits::get_parent(y));
- node_ptr prev = node_ptr();
-
-
- bool left_child = true;
- while(x){
- ++depth;
- y = x;
- left_child = comp(key, x);
- x = left_child ?
- NodeTraits::get_left(x) : (prev = y, NodeTraits::get_right(x));
- }
- if(pdepth) *pdepth = depth;
-
-
-
- const bool not_present = !prev || comp(prev, key);
- if(not_present){
- commit_data.link_left = left_child;
- commit_data.node = y;
- }
- return std::pair<node_ptr, bool>(prev, not_present);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template<class KeyType, class KeyNodePtrCompare>
- static std::pair<node_ptr, bool> insert_unique_check
- (const_node_ptr header, node_ptr hint, const KeyType &key
- ,KeyNodePtrCompare comp, insert_commit_data &commit_data
- #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
- , std::size_t *pdepth = 0
- #endif
- )
- {
-
- if(hint == header || comp(key, hint)){
- node_ptr prev(hint);
-
- if(hint == begin_node(header) || comp((prev = base_type::prev_node(hint)), key)){
- commit_data.link_left = unique(header) || !NodeTraits::get_left(hint);
- commit_data.node = commit_data.link_left ? hint : prev;
- if(pdepth){
- *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1;
- }
- return std::pair<node_ptr, bool>(node_ptr(), true);
- }
- }
-
- return insert_unique_check(header, key, comp, commit_data, pdepth);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template<class NodePtrCompare>
- static node_ptr insert_equal
- (node_ptr h, node_ptr hint, node_ptr new_node, NodePtrCompare comp
- #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
- , std::size_t *pdepth = 0
- #endif
- )
- {
- insert_commit_data commit_data;
- insert_equal_check(h, hint, new_node, comp, commit_data, pdepth);
- insert_commit(h, new_node, commit_data);
- return new_node;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- template<class NodePtrCompare>
- static node_ptr insert_equal_upper_bound
- (node_ptr h, node_ptr new_node, NodePtrCompare comp
- #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
- , std::size_t *pdepth = 0
- #endif
- )
- {
- insert_commit_data commit_data;
- insert_equal_upper_bound_check(h, new_node, comp, commit_data, pdepth);
- insert_commit(h, new_node, commit_data);
- return new_node;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- template<class NodePtrCompare>
- static node_ptr insert_equal_lower_bound
- (node_ptr h, node_ptr new_node, NodePtrCompare comp
- #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
- , std::size_t *pdepth = 0
- #endif
- )
- {
- insert_commit_data commit_data;
- insert_equal_lower_bound_check(h, new_node, comp, commit_data, pdepth);
- insert_commit(h, new_node, commit_data);
- return new_node;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- static node_ptr insert_before
- (node_ptr header, node_ptr pos, node_ptr new_node
- #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
- , std::size_t *pdepth = 0
- #endif
- ) BOOST_NOEXCEPT
- {
- insert_commit_data commit_data;
- insert_before_check(header, pos, commit_data, pdepth);
- insert_commit(header, new_node, commit_data);
- return new_node;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- static void push_back
- (node_ptr header, node_ptr new_node
- #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
- , std::size_t *pdepth = 0
- #endif
- ) BOOST_NOEXCEPT
- {
- insert_commit_data commit_data;
- push_back_check(header, commit_data, pdepth);
- insert_commit(header, new_node, commit_data);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- static void push_front
- (node_ptr header, node_ptr new_node
- #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
- , std::size_t *pdepth = 0
- #endif
- ) BOOST_NOEXCEPT
- {
- insert_commit_data commit_data;
- push_front_check(header, commit_data, pdepth);
- insert_commit(header, new_node, commit_data);
- }
-
-
-
-
-
-
-
-
-
- static std::size_t depth(const_node_ptr n) BOOST_NOEXCEPT
- {
- std::size_t depth = 0;
- node_ptr p_parent;
- while(n != NodeTraits::get_parent(p_parent = NodeTraits::get_parent(n))){
- ++depth;
- n = p_parent;
- }
- return depth;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- template <class Cloner, class Disposer>
- static void clone
- (const_node_ptr source_header, node_ptr target_header, Cloner cloner, Disposer disposer)
- {
- if(!unique(target_header)){
- clear_and_dispose(target_header, disposer);
- }
- node_ptr leftmost, rightmost;
- node_ptr new_root = clone_subtree
- (source_header, target_header, cloner, disposer, leftmost, rightmost);
-
- NodeTraits::set_parent(target_header, new_root);
- NodeTraits::set_left (target_header, leftmost);
- NodeTraits::set_right (target_header, rightmost);
- }
-
-
-
-
-
-
-
-
- inline static void erase(node_ptr header, node_ptr z) BOOST_NOEXCEPT
- {
- data_for_rebalance ignored;
- erase(header, z, ignored);
- }
-
-
-
-
-
-
-
-
-
-
-
-
- template<class NodePtrCompare>
- inline static bool transfer_unique
- (node_ptr header1, NodePtrCompare comp, node_ptr header2, node_ptr z)
- {
- data_for_rebalance ignored;
- return transfer_unique(header1, comp, header2, z, ignored);
- }
-
-
-
-
-
-
-
-
-
- template<class NodePtrCompare>
- inline static void transfer_equal
- (node_ptr header1, NodePtrCompare comp, node_ptr header2, node_ptr z)
- {
- data_for_rebalance ignored;
- transfer_equal(header1, comp, header2, z, ignored);
- }
-
-
-
-
-
-
-
- static void unlink(node_ptr n) BOOST_NOEXCEPT
- {
- node_ptr x = NodeTraits::get_parent(n);
- if(x){
- while(!base_type::is_header(x))
- x = NodeTraits::get_parent(x);
- erase(x, n);
- }
- }
-
-
-
-
-
-
-
- static void rebalance(node_ptr header) BOOST_NOEXCEPT
- {
- node_ptr root = NodeTraits::get_parent(header);
- if(root){
- rebalance_subtree(root);
- }
- }
-
-
-
-
-
-
-
-
-
- static node_ptr rebalance_subtree(node_ptr old_root) BOOST_NOEXCEPT
- {
-
-
-
-
-
-
-
-
- node_ptr super_root = NodeTraits::get_parent(old_root);
- BOOST_INTRUSIVE_INVARIANT_ASSERT(super_root);
-
- node_ptr super_root_right_backup = NodeTraits::get_right(super_root);
- bool super_root_is_header = NodeTraits::get_parent(super_root) == old_root;
- bool old_root_is_right = is_right_child(old_root);
- NodeTraits::set_right(super_root, old_root);
- std::size_t size;
- subtree_to_vine(super_root, size);
- vine_to_subtree(super_root, size);
- node_ptr new_root = NodeTraits::get_right(super_root);
-
- if(super_root_is_header){
- NodeTraits::set_right(super_root, super_root_right_backup);
- NodeTraits::set_parent(super_root, new_root);
- }
- else if(old_root_is_right){
- NodeTraits::set_right(super_root, new_root);
- }
- else{
- NodeTraits::set_right(super_root, super_root_right_backup);
- NodeTraits::set_left(super_root, new_root);
- }
- return new_root;
- }
-
-
-
-
-
-
-
-
- template<class Checker>
- static void check(const_node_ptr header, Checker checker, typename Checker::return_type& checker_return)
- {
- const_node_ptr root_node_ptr = NodeTraits::get_parent(header);
- if (!root_node_ptr){
-
- BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(header) == header);
- BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(header) == header);
- }
- else{
-
- BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(root_node_ptr) == header);
-
- check_subtree(root_node_ptr, checker, checker_return);
-
- const_node_ptr p = root_node_ptr;
- while (NodeTraits::get_left(p)) { p = NodeTraits::get_left(p); }
- BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_left(header) == p);
- p = root_node_ptr;
- while (NodeTraits::get_right(p)) { p = NodeTraits::get_right(p); }
- BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_right(header) == p);
- }
- }
- protected:
- template<class NodePtrCompare>
- static bool transfer_unique
- (node_ptr header1, NodePtrCompare comp, node_ptr header2, node_ptr z, data_for_rebalance &info)
- {
- insert_commit_data commit_data;
- bool const transferable = insert_unique_check(header1, z, comp, commit_data).second;
- if(transferable){
- erase(header2, z, info);
- insert_commit(header1, z, commit_data);
- }
- return transferable;
- }
- template<class NodePtrCompare>
- static void transfer_equal
- (node_ptr header1, NodePtrCompare comp, node_ptr header2, node_ptr z, data_for_rebalance &info)
- {
- insert_commit_data commit_data;
- insert_equal_upper_bound_check(header1, z, comp, commit_data);
- erase(header2, z, info);
- insert_commit(header1, z, commit_data);
- }
- static void erase(node_ptr header, node_ptr z, data_for_rebalance &info)
- {
- node_ptr y(z);
- node_ptr x;
- const node_ptr z_left(NodeTraits::get_left(z));
- const node_ptr z_right(NodeTraits::get_right(z));
- if(!z_left){
- x = z_right;
- }
- else if(!z_right){
- x = z_left;
- BOOST_ASSERT(x);
- }
- else{
-
- y = base_type::minimum(z_right);
- x = NodeTraits::get_right(y);
- }
- node_ptr x_parent;
- const node_ptr z_parent(NodeTraits::get_parent(z));
- const bool z_is_leftchild(NodeTraits::get_left(z_parent) == z);
- if(y != z){
-
-
-
- NodeTraits::set_parent(z_left, y);
- NodeTraits::set_left(y, z_left);
- if(y != z_right){
-
- NodeTraits::set_right(y, z_right);
- NodeTraits::set_parent(z_right, y);
-
- x_parent = NodeTraits::get_parent(y);
- BOOST_ASSERT(NodeTraits::get_left(x_parent) == y);
- if(x)
- NodeTraits::set_parent(x, x_parent);
-
- NodeTraits::set_left(x_parent, x);
- }
- else{
- x_parent = y;
- }
- NodeTraits::set_parent(y, z_parent);
- this_type::set_child(header, y, z_parent, z_is_leftchild);
- }
- else {
-
- x_parent = z_parent;
- if(x)
- NodeTraits::set_parent(x, z_parent);
- this_type::set_child(header, x, z_parent, z_is_leftchild);
-
- if(NodeTraits::get_left(header) == z){
-
- BOOST_ASSERT(!z_left);
- NodeTraits::set_left(header, !z_right ?
- z_parent :
- base_type::minimum(z_right));
- }
- if(NodeTraits::get_right(header) == z){
-
- BOOST_ASSERT(!z_right);
- NodeTraits::set_right(header, !z_left ?
- z_parent :
- base_type::maximum(z_left));
- }
- }
-
-
- info.x = x;
- info.y = y;
-
-
- BOOST_ASSERT(!x || NodeTraits::get_parent(x) == x_parent);
- info.x_parent = x_parent;
- }
-
-
-
-
-
-
-
- static std::size_t subtree_size(const_node_ptr subtree) BOOST_NOEXCEPT
- {
- std::size_t count = 0;
- if (subtree){
- node_ptr n = detail::uncast(subtree);
- node_ptr m = NodeTraits::get_left(n);
- while(m){
- n = m;
- m = NodeTraits::get_left(n);
- }
- while(1){
- ++count;
- node_ptr n_right(NodeTraits::get_right(n));
- if(n_right){
- n = n_right;
- m = NodeTraits::get_left(n);
- while(m){
- n = m;
- m = NodeTraits::get_left(n);
- }
- }
- else {
- do{
- if (n == subtree){
- return count;
- }
- m = n;
- n = NodeTraits::get_parent(n);
- }while(NodeTraits::get_left(n) != m);
- }
- }
- }
- return count;
- }
-
-
-
-
-
-
-
- inline static bool is_left_child(node_ptr p) BOOST_NOEXCEPT
- { return NodeTraits::get_left(NodeTraits::get_parent(p)) == p; }
-
-
-
-
-
-
-
- inline static bool is_right_child(node_ptr p) BOOST_NOEXCEPT
- { return NodeTraits::get_right(NodeTraits::get_parent(p)) == p; }
- static void insert_before_check
- (node_ptr header, node_ptr pos
- , insert_commit_data &commit_data
- #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
- , std::size_t *pdepth = 0
- #endif
- )
- {
- node_ptr prev(pos);
- if(pos != NodeTraits::get_left(header))
- prev = base_type::prev_node(pos);
- bool link_left = unique(header) || !NodeTraits::get_left(pos);
- commit_data.link_left = link_left;
- commit_data.node = link_left ? pos : prev;
- if(pdepth){
- *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1;
- }
- }
- static void push_back_check
- (node_ptr header, insert_commit_data &commit_data
- #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
- , std::size_t *pdepth = 0
- #endif
- ) BOOST_NOEXCEPT
- {
- node_ptr prev(NodeTraits::get_right(header));
- if(pdepth){
- *pdepth = prev == header ? 0 : depth(prev) + 1;
- }
- commit_data.link_left = false;
- commit_data.node = prev;
- }
- static void push_front_check
- (node_ptr header, insert_commit_data &commit_data
- #ifndef BOOST_INTRUSIVE_DOXYGEN_INVOKED
- , std::size_t *pdepth = 0
- #endif
- ) BOOST_NOEXCEPT
- {
- node_ptr pos(NodeTraits::get_left(header));
- if(pdepth){
- *pdepth = pos == header ? 0 : depth(pos) + 1;
- }
- commit_data.link_left = true;
- commit_data.node = pos;
- }
- template<class NodePtrCompare>
- static void insert_equal_check
- (node_ptr header, node_ptr hint, node_ptr new_node, NodePtrCompare comp
- , insert_commit_data &commit_data
- /// @cond
- , std::size_t *pdepth = 0
- /// @endcond
- )
- {
- if(hint == header || !comp(hint, new_node)){
- node_ptr prev(hint);
- if(hint == NodeTraits::get_left(header) ||
- !comp(new_node, (prev = base_type::prev_node(hint)))){
- bool link_left = unique(header) || !NodeTraits::get_left(hint);
- commit_data.link_left = link_left;
- commit_data.node = link_left ? hint : prev;
- if(pdepth){
- *pdepth = commit_data.node == header ? 0 : depth(commit_data.node) + 1;
- }
- }
- else{
- insert_equal_upper_bound_check(header, new_node, comp, commit_data, pdepth);
- }
- }
- else{
- insert_equal_lower_bound_check(header, new_node, comp, commit_data, pdepth);
- }
- }
- template<class NodePtrCompare>
- static void insert_equal_upper_bound_check
- (node_ptr h, node_ptr new_node, NodePtrCompare comp, insert_commit_data & commit_data, std::size_t *pdepth = 0)
- {
- std::size_t depth = 0;
- node_ptr y(h);
- node_ptr x(NodeTraits::get_parent(y));
- while(x){
- ++depth;
- y = x;
- x = comp(new_node, x) ?
- NodeTraits::get_left(x) : NodeTraits::get_right(x);
- }
- if(pdepth) *pdepth = depth;
- commit_data.link_left = (y == h) || comp(new_node, y);
- commit_data.node = y;
- }
- template<class NodePtrCompare>
- static void insert_equal_lower_bound_check
- (node_ptr h, node_ptr new_node, NodePtrCompare comp, insert_commit_data & commit_data, std::size_t *pdepth = 0)
- {
- std::size_t depth = 0;
- node_ptr y(h);
- node_ptr x(NodeTraits::get_parent(y));
- while(x){
- ++depth;
- y = x;
- x = !comp(x, new_node) ?
- NodeTraits::get_left(x) : NodeTraits::get_right(x);
- }
- if(pdepth) *pdepth = depth;
- commit_data.link_left = (y == h) || !comp(y, new_node);
- commit_data.node = y;
- }
- static void insert_commit
- (node_ptr header, node_ptr new_node, const insert_commit_data &commit_data) BOOST_NOEXCEPT
- {
-
- BOOST_INTRUSIVE_INVARIANT_ASSERT(commit_data.node != node_ptr());
- node_ptr parent_node(commit_data.node);
- if(parent_node == header){
- NodeTraits::set_parent(header, new_node);
- NodeTraits::set_right(header, new_node);
- NodeTraits::set_left(header, new_node);
- }
- else if(commit_data.link_left){
- NodeTraits::set_left(parent_node, new_node);
- if(parent_node == NodeTraits::get_left(header))
- NodeTraits::set_left(header, new_node);
- }
- else{
- NodeTraits::set_right(parent_node, new_node);
- if(parent_node == NodeTraits::get_right(header))
- NodeTraits::set_right(header, new_node);
- }
- NodeTraits::set_parent(new_node, parent_node);
- NodeTraits::set_right(new_node, node_ptr());
- NodeTraits::set_left(new_node, node_ptr());
- }
-
- static void set_child(node_ptr header, node_ptr new_child, node_ptr new_parent, const bool link_left) BOOST_NOEXCEPT
- {
- if(new_parent == header)
- NodeTraits::set_parent(header, new_child);
- else if(link_left)
- NodeTraits::set_left(new_parent, new_child);
- else
- NodeTraits::set_right(new_parent, new_child);
- }
-
- static void rotate_left_no_parent_fix(node_ptr p, node_ptr p_right) BOOST_NOEXCEPT
- {
- node_ptr p_right_left(NodeTraits::get_left(p_right));
- NodeTraits::set_right(p, p_right_left);
- if(p_right_left){
- NodeTraits::set_parent(p_right_left, p);
- }
- NodeTraits::set_left(p_right, p);
- NodeTraits::set_parent(p, p_right);
- }
-
- static void rotate_left(node_ptr p, node_ptr p_right, node_ptr p_parent, node_ptr header) BOOST_NOEXCEPT
- {
- const bool p_was_left(NodeTraits::get_left(p_parent) == p);
- rotate_left_no_parent_fix(p, p_right);
- NodeTraits::set_parent(p_right, p_parent);
- set_child(header, p_right, p_parent, p_was_left);
- }
-
- static void rotate_right_no_parent_fix(node_ptr p, node_ptr p_left) BOOST_NOEXCEPT
- {
- node_ptr p_left_right(NodeTraits::get_right(p_left));
- NodeTraits::set_left(p, p_left_right);
- if(p_left_right){
- NodeTraits::set_parent(p_left_right, p);
- }
- NodeTraits::set_right(p_left, p);
- NodeTraits::set_parent(p, p_left);
- }
-
- static void rotate_right(node_ptr p, node_ptr p_left, node_ptr p_parent, node_ptr header) BOOST_NOEXCEPT
- {
- const bool p_was_left(NodeTraits::get_left(p_parent) == p);
- rotate_right_no_parent_fix(p, p_left);
- NodeTraits::set_parent(p_left, p_parent);
- set_child(header, p_left, p_parent, p_was_left);
- }
- private:
- static void subtree_to_vine(node_ptr vine_tail, std::size_t &size) BOOST_NOEXCEPT
- {
-
-
-
-
-
-
- std::size_t len = 0;
- node_ptr remainder = NodeTraits::get_right(vine_tail);
- while(remainder){
- node_ptr tempptr = NodeTraits::get_left(remainder);
- if(!tempptr){
- vine_tail = remainder;
- remainder = NodeTraits::get_right(remainder);
- ++len;
- }
- else{
- NodeTraits::set_left(remainder, NodeTraits::get_right(tempptr));
- NodeTraits::set_right(tempptr, remainder);
- remainder = tempptr;
- NodeTraits::set_right(vine_tail, tempptr);
- }
- }
- size = len;
- }
- static void compress_subtree(node_ptr scanner, std::size_t count) BOOST_NOEXCEPT
- {
- while(count--){
- node_ptr child = NodeTraits::get_right(scanner);
- node_ptr child_right = NodeTraits::get_right(child);
- NodeTraits::set_right(scanner, child_right);
-
- scanner = child_right;
- node_ptr scanner_left = NodeTraits::get_left(scanner);
- NodeTraits::set_right(child, scanner_left);
- if(scanner_left)
- NodeTraits::set_parent(scanner_left, child);
- NodeTraits::set_left(scanner, child);
- NodeTraits::set_parent(child, scanner);
- }
- }
- static void vine_to_subtree(node_ptr super_root, std::size_t count) BOOST_NOEXCEPT
- {
- const std::size_t one_szt = 1u;
- std::size_t leaf_nodes = count + one_szt - std::size_t(one_szt << detail::floor_log2(count + one_szt));
- compress_subtree(super_root, leaf_nodes);
- std::size_t vine_nodes = count - leaf_nodes;
- while(vine_nodes > 1){
- vine_nodes /= 2;
- compress_subtree(super_root, vine_nodes);
- }
-
-
- for ( node_ptr q = super_root, p = NodeTraits::get_right(super_root)
- ; p
- ; q = p, p = NodeTraits::get_right(p)){
- NodeTraits::set_parent(p, q);
- }
- }
-
-
-
-
-
-
-
- static node_ptr get_root(node_ptr n) BOOST_NOEXCEPT
- {
- BOOST_INTRUSIVE_INVARIANT_ASSERT((!inited(n)));
- node_ptr x = NodeTraits::get_parent(n);
- if(x){
- while(!base_type::is_header(x)){
- x = NodeTraits::get_parent(x);
- }
- return x;
- }
- else{
- return n;
- }
- }
- template <class Cloner, class Disposer>
- static node_ptr clone_subtree
- (const_node_ptr source_parent, node_ptr target_parent
- , Cloner cloner, Disposer disposer
- , node_ptr &leftmost_out, node_ptr &rightmost_out
- )
- {
- node_ptr target_sub_root = target_parent;
- node_ptr source_root = NodeTraits::get_parent(source_parent);
- if(!source_root){
- leftmost_out = rightmost_out = source_root;
- }
- else{
-
- node_ptr current = source_root;
- node_ptr insertion_point = target_sub_root = cloner(current);
-
- node_ptr leftmost = target_sub_root;
- node_ptr rightmost = target_sub_root;
-
- NodeTraits::set_left(target_sub_root, node_ptr());
- NodeTraits::set_right(target_sub_root, node_ptr());
- NodeTraits::set_parent(target_sub_root, target_parent);
- dispose_subtree_disposer<Disposer> rollback(disposer, target_sub_root);
- while(true) {
-
- if( NodeTraits::get_left(current) &&
- !NodeTraits::get_left(insertion_point)) {
- current = NodeTraits::get_left(current);
- node_ptr temp = insertion_point;
-
- insertion_point = cloner(current);
- NodeTraits::set_left (insertion_point, node_ptr());
- NodeTraits::set_right (insertion_point, node_ptr());
-
- NodeTraits::set_parent(insertion_point, temp);
- NodeTraits::set_left (temp, insertion_point);
-
- if(rightmost == target_sub_root)
- leftmost = insertion_point;
- }
-
- else if( NodeTraits::get_right(current) &&
- !NodeTraits::get_right(insertion_point)){
- current = NodeTraits::get_right(current);
- node_ptr temp = insertion_point;
-
- insertion_point = cloner(current);
- NodeTraits::set_left (insertion_point, node_ptr());
- NodeTraits::set_right (insertion_point, node_ptr());
-
- NodeTraits::set_parent(insertion_point, temp);
- NodeTraits::set_right (temp, insertion_point);
-
- rightmost = insertion_point;
- }
-
- else if(current == source_root){
- break;
- }
- else{
-
- current = NodeTraits::get_parent(current);
- insertion_point = NodeTraits::get_parent(insertion_point);
- }
- }
- rollback.release();
- leftmost_out = leftmost;
- rightmost_out = rightmost;
- }
- return target_sub_root;
- }
- template<class Disposer>
- static void dispose_subtree(node_ptr x, Disposer disposer) BOOST_NOEXCEPT
- {
- while (x){
- node_ptr save(NodeTraits::get_left(x));
- if (save) {
-
- NodeTraits::set_left(x, NodeTraits::get_right(save));
- NodeTraits::set_right(save, x);
- }
- else {
- save = NodeTraits::get_right(x);
- init(x);
- disposer(x);
- }
- x = save;
- }
- }
- template<class KeyType, class KeyNodePtrCompare>
- static node_ptr lower_bound_loop
- (node_ptr x, node_ptr y, const KeyType &key, KeyNodePtrCompare comp)
- {
- while(x){
- if(comp(x, key)){
- x = NodeTraits::get_right(x);
- }
- else{
- y = x;
- x = NodeTraits::get_left(x);
- }
- }
- return y;
- }
- template<class KeyType, class KeyNodePtrCompare>
- static node_ptr upper_bound_loop
- (node_ptr x, node_ptr y, const KeyType &key, KeyNodePtrCompare comp)
- {
- while(x){
- if(comp(key, x)){
- y = x;
- x = NodeTraits::get_left(x);
- }
- else{
- x = NodeTraits::get_right(x);
- }
- }
- return y;
- }
- template<class Checker>
- static void check_subtree(const_node_ptr n, Checker checker, typename Checker::return_type& check_return)
- {
- const_node_ptr left = NodeTraits::get_left(n);
- const_node_ptr right = NodeTraits::get_right(n);
- typename Checker::return_type check_return_left;
- typename Checker::return_type check_return_right;
- if (left)
- {
- BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(left) == n);
- check_subtree(left, checker, check_return_left);
- }
- if (right)
- {
- BOOST_INTRUSIVE_INVARIANT_ASSERT(NodeTraits::get_parent(right) == n);
- check_subtree(right, checker, check_return_right);
- }
- checker(n, check_return_left, check_return_right, check_return);
- }
- };
- template<class NodeTraits>
- struct get_algo<BsTreeAlgorithms, NodeTraits>
- {
- typedef bstree_algorithms<NodeTraits> type;
- };
- template <class ValueTraits, class NodePtrCompare, class ExtraChecker>
- struct get_node_checker<BsTreeAlgorithms, ValueTraits, NodePtrCompare, ExtraChecker>
- {
- typedef detail::bstree_node_checker<ValueTraits, NodePtrCompare, ExtraChecker> type;
- };
- }
- }
- #include <boost/intrusive/detail/config_end.hpp>
- #endif
|