2011-12-22 16:19:25 +01:00
|
|
|
/*
|
|
|
|
* \brief AVL tree
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2006-04-12
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 13:23:52 +01:00
|
|
|
* Copyright (C) 2006-2017 Genode Labs GmbH
|
2011-12-22 16:19:25 +01:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 13:23:52 +01:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2011-12-22 16:19:25 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <util/avl_tree.h>
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
#include <base/log.h>
|
2011-12-22 16:19:25 +01:00
|
|
|
|
|
|
|
using namespace Genode;
|
|
|
|
|
|
|
|
|
|
|
|
inline void Avl_node_base::_recompute_depth(Policy &policy)
|
|
|
|
{
|
|
|
|
unsigned char old_depth = _depth;
|
|
|
|
_depth = max(_child_depth(LEFT), _child_depth(RIGHT)) + 1;
|
|
|
|
|
|
|
|
/* if our own value changes, update parent */
|
|
|
|
if (_depth != old_depth && _parent)
|
|
|
|
_parent->_recompute_depth(policy);
|
|
|
|
|
|
|
|
/* call recompute hook only for valid tree nodes */
|
|
|
|
if (_parent)
|
|
|
|
policy.recompute(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Avl_node_base::_adopt(Avl_node_base *node, Side i, Policy &policy)
|
|
|
|
{
|
|
|
|
_child[i] = node;
|
|
|
|
if (node)
|
|
|
|
node->_parent = this;
|
|
|
|
|
|
|
|
_recompute_depth(policy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Avl_node_base::_rotate_subtree(Avl_node_base *node, Side side, Policy &policy)
|
|
|
|
{
|
|
|
|
int i = (node == _child[0]) ? LEFT : RIGHT;
|
|
|
|
|
|
|
|
Avl_node_base *node_r = node->_child[!side];
|
2018-10-25 12:03:02 +02:00
|
|
|
if (!node_r) {
|
|
|
|
error("avl rotation node_r is invalid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-25 12:03:02 +02:00
|
|
|
Avl_node_base *node_r_l = node_r->_child[side];
|
|
|
|
|
2011-12-22 16:19:25 +01:00
|
|
|
/* simple rotation */
|
|
|
|
if (node_r->_bias() == !side) {
|
|
|
|
|
|
|
|
node->_adopt(node_r_l, !side, policy);
|
|
|
|
node_r->_adopt(node, side, policy);
|
|
|
|
|
|
|
|
_adopt(node_r, i, policy);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* double rotation */
|
|
|
|
else if (node_r_l) {
|
|
|
|
|
|
|
|
Avl_node_base *node_r_l_l = node_r_l->_child[side];
|
|
|
|
Avl_node_base *node_r_l_r = node_r_l->_child[!side];
|
|
|
|
|
|
|
|
node->_adopt(node_r_l_l, !side, policy);
|
|
|
|
node_r->_adopt(node_r_l_r, side, policy);
|
|
|
|
|
|
|
|
node_r_l->_adopt(node, side, policy);
|
|
|
|
node_r_l->_adopt(node_r, !side, policy);
|
|
|
|
|
|
|
|
_adopt(node_r_l, i, policy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Avl_node_base::_rebalance_subtree(Avl_node_base *node, Policy &policy)
|
|
|
|
{
|
|
|
|
int v = node->_child_depth(RIGHT) - node->_child_depth(LEFT);
|
|
|
|
|
|
|
|
/* return if subtree is in balance */
|
|
|
|
if (abs(v) < 2) return;
|
|
|
|
|
|
|
|
_rotate_subtree(node, (v < 0), policy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Avl_node_base::insert(Avl_node_base *node, Policy &policy)
|
|
|
|
{
|
|
|
|
if (node == this) {
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
error("inserting element ", node, " twice into avl tree!");
|
2011-12-22 16:19:25 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Side i = LEFT;
|
|
|
|
|
|
|
|
/* for non-root nodes, decide for a branch */
|
|
|
|
if (_parent)
|
|
|
|
i = policy.higher(this, node);
|
|
|
|
|
|
|
|
if (_child[i])
|
|
|
|
_child[i]->insert(node, policy);
|
|
|
|
else
|
|
|
|
_adopt(node, i, policy);
|
|
|
|
|
|
|
|
/* the inserted node might have changed the depth of the subtree */
|
|
|
|
_recompute_depth(policy);
|
|
|
|
|
|
|
|
if (_parent)
|
|
|
|
_parent->_rebalance_subtree(this, policy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Avl_node_base::remove(Policy &policy)
|
|
|
|
{
|
|
|
|
Avl_node_base *lp = 0;
|
|
|
|
Avl_node_base *l = _child[0];
|
|
|
|
|
2013-02-13 16:01:25 +01:00
|
|
|
if (!_parent)
|
base: avoid use of deprecated base/printf.h
Besides adapting the components to the use of base/log.h, the patch
cleans up a few base headers, i.e., it removes unused includes from
root/component.h, specifically base/heap.h and
ram_session/ram_session.h. Hence, components that relied on the implicit
inclusion of those headers have to manually include those headers now.
While adjusting the log messages, I repeatedly stumbled over the problem
that printing char * arguments is ambiguous. It is unclear whether to
print the argument as pointer or null-terminated string. To overcome
this problem, the patch introduces a new type 'Cstring' that allows the
caller to express that the argument should be handled as null-terminated
string. As a nice side effect, with this type in place, the optional len
argument of the 'String' class could be removed. Instead of supplying a
pair of (char const *, size_t), the constructor accepts a 'Cstring'.
This, in turn, clears the way let the 'String' constructor use the new
output mechanism to assemble a string from multiple arguments (and
thereby getting rid of snprintf within Genode in the near future).
To enforce the explicit resolution of the char * ambiguity, the 'char *'
overload of the 'print' function is marked as deleted.
Issue #1987
2016-07-13 19:07:09 +02:00
|
|
|
error("tried to remove AVL node that is not in an AVL tree");
|
2013-02-13 16:01:25 +01:00
|
|
|
|
2011-12-22 16:19:25 +01:00
|
|
|
if (l) {
|
|
|
|
|
|
|
|
/* find right-most node in left sub tree (l) */
|
|
|
|
while (l && l->_child[1])
|
|
|
|
l = l->_child[1];
|
|
|
|
|
|
|
|
/* isolate right-most node in left sub tree */
|
|
|
|
if (l == _child[0])
|
|
|
|
_adopt(l->_child[0], LEFT, policy);
|
|
|
|
else
|
|
|
|
l->_parent->_adopt(l->_child[0], RIGHT, policy);
|
|
|
|
|
|
|
|
/* consistent state */
|
|
|
|
|
|
|
|
/* remember for rebalancing */
|
|
|
|
if (l->_parent != this)
|
|
|
|
lp = l->_parent;
|
|
|
|
|
|
|
|
/* exchange this and l */
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
if (_parent->_child[i] == this)
|
|
|
|
_parent->_adopt(l, i, policy);
|
|
|
|
|
|
|
|
l->_adopt(_child[0], LEFT, policy);
|
|
|
|
l->_adopt(_child[1], RIGHT, policy);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* no left sub tree, attach our right sub tree to our parent */
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
if (_parent->_child[i] == this)
|
|
|
|
_parent->_adopt(_child[1], i, policy);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* walk the tree towards its root and rebalance sub trees */
|
|
|
|
while (lp && lp->_parent) {
|
|
|
|
Avl_node_base *lpp = lp->_parent;
|
|
|
|
lpp->_rebalance_subtree(lp, policy);
|
|
|
|
lp = lpp;
|
|
|
|
}
|
2013-02-13 16:01:25 +01:00
|
|
|
|
|
|
|
/* reset node pointers */
|
|
|
|
_child[LEFT] = _child[RIGHT] = 0;
|
|
|
|
_parent = 0;
|
2011-12-22 16:19:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Avl_node_base::Avl_node_base() : _parent(0), _depth(1) {
|
|
|
|
_child[LEFT] = _child[RIGHT] = 0; }
|