Ignore:
Timestamp:
2018-07-05T21:41:23Z (7 years ago)
Author:
Dzejrou <dzejrou@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
27f1bc0
Parents:
6175b78
git-author:
Dzejrou <dzejrou@…> (2018-05-13 22:57:14)
git-committer:
Dzejrou <dzejrou@…> (2018-07-05 21:41:23)
Message:

cpp: revamped rbtree so that it now stores equivalent keys in a list

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/internal/rbtree_policies.hpp

    r6175b78 r73e3791  
    5959        static typename Tree::iterator lower_bound(const Tree& tree, const Key& key)
    6060        {
    61             using iterator = typename Tree::iterator;
     61            using iterator  = typename Tree::iterator;
     62            using node_type = typename Tree::node_type;
    6263
    6364            auto it = lower_bound_const(tree, key);
    6465
    65             return iterator{it.node(), it.end()};
     66            return iterator{const_cast<node_type*>(it.node()), it.end()};
    6667        }
    6768
     
    101102        static typename Tree::iterator upper_bound(const Tree& tree, const Key& key)
    102103        {
    103             using iterator = typename Tree::iterator;
     104            using iterator  = typename Tree::iterator;
     105            using node_type = typename Tree::node_type;
    104106
    105107            auto it = upper_bound_const(tree, key);
    106108
    107             return iterator{it.node(), it.end()};
     109            return iterator{const_cast<node_type*>(it.node()), it.end()};
    108110        }
    109111
     
    113115            /**
    114116             * If key isn't in the tree, we get it's
    115              * successor or tree.end(). If key is
    116              * in the tree, we get it.
    117              * In the first case, the successor is also
    118              * the upper bound, so we just return it,
    119              * otherwise (as long as it != end()) we
    120              * increment.
     117             * predecessor or tree.end(). If key is
     118             * in the tree, we get it. So unless it
     119             * is equal to end(), we can increment it
     120             * to get the upper bound.
    121121             */
    122122            auto it = lower_bound_const(tree, key);
    123123            if (it == tree.end())
    124124                return it;
    125             else if (tree.keys_equal(key, *it))
     125            else
    126126                return ++it;
    127             else
    128                 return it;
    129127        }
    130128
     
    176174
    177175            auto node = new node_type{move(val)};
    178             tree.insert_node(node, parent);
    179 
    180             return make_pair(iterator{node, false}, true);
     176
     177            return insert(tree, node, parent);
    181178        }
    182179
     
    194191
    195192            auto node = new node_type{val};
    196             tree.insert_node(node, parent);
    197 
    198             return make_pair(iterator{node, false}, true);
     193
     194            return insert(tree, node, parent);
    199195        }
    200196
     
    212208
    213209            auto node = new node_type{forward<Value>(val)};
    214             tree.insert_node(node, parent);
     210
     211            return insert(tree, node, parent);
     212        }
     213
     214        template<class Tree>
     215        static pair<
     216            typename Tree::iterator, bool
     217        > insert(
     218            Tree& tree, typename Tree::node_type* node,
     219            typename Tree::node_type* parent
     220        )
     221        {
     222            using iterator  = typename Tree::iterator;
     223
     224            if (!node)
     225                return make_pair(tree.end(), false);
     226
     227            ++tree.size_;
     228            if (!parent)
     229            {
     230                node->color = rbcolor::black;
     231                tree.root_ = node;
     232            }
     233            else
     234            {
     235                if (tree.keys_comp(tree.get_key(node->value), parent->value))
     236                    parent->add_left_child(node);
     237                else
     238                    parent->add_right_child(node);
     239
     240                tree.repair_after_insert_(node);
     241                tree.update_root_(node);
     242            }
    215243
    216244            return make_pair(iterator{node, false}, true);
     
    308336            /**
    309337             * If key isn't in the tree, we get it's
    310              * successor or tree.end(). If key is
    311              * in the tree, we get it.
    312              * In the first case, the successor is also
    313              * the upper bound, so we just return it,
    314              * otherwise (as long as it != end()) we
    315              * increment.
     338             * predecessor or tree.end(). If key is
     339             * in the tree, we get it. So unless it
     340             * is equal to end(), we keep incrementing
     341             * until we get to the next key.
    316342             */
    317343            auto it = lower_bound(tree, key);
     
    320346            else if (tree.keys_equal(tree.get_key(*it), key))
    321347            {
    322                 while (tree.keys_equal(tree.get_key(*it), key))
     348                while (it != tree.end() && tree.keys_equal(tree.get_key(*it), key))
    323349                    ++it;
    324350
     
    384410
    385411        template<class Tree>
    386         static typename Tree::iterator insert(Tree& tree, typename Tree::node_type* node)
    387         {
    388             using iterator  = typename Tree::iterator;
     412        static typename Tree::iterator insert(
     413            Tree& tree, typename Tree::node_type* node,
     414            typename Tree::node_type* = nullptr
     415        )
     416        {
     417            using iterator  = typename Tree::iterator;
     418
     419            if (!node)
     420                return tree.end();
    389421
    390422            auto parent = tree.find_parent_for_insertion(tree.get_key(node->value));
    391             tree.insert_node(node, parent);
     423
     424            ++tree.size_;
     425            if (!parent)
     426            {
     427                node->color = rbcolor::black;
     428                tree.root_ = node;
     429            }
     430            else
     431            {
     432                if (tree.keys_comp(tree.get_key(node->value), parent->value))
     433                    parent->add_left_child(node);
     434                else if (tree.keys_comp(tree.get_key(parent->value), node->value))
     435                    parent->add_right_child(node);
     436                else
     437                {
     438                    parent->add(node); // List of nodes with equivalent keys.
     439                    tree.update_root_(parent);
     440
     441                    return iterator{node, false};
     442                }
     443
     444                tree.repair_after_insert_(node);
     445                tree.update_root_(node);
     446            }
    392447
    393448            return iterator{node, false};
Note: See TracChangeset for help on using the changeset viewer.