- Info
1
6
7
13
14 #ifndef _INCLUDE__UTIL__AVL_TREE_H_
15 #define _INCLUDE__UTIL__AVL_TREE_H_
16
17 #include <util/misc_math.h>
18
19 namespace Genode {
20
21 class Avl_node_base
22 {
23 protected:
24
25
30 struct Policy
31 {
32
41 virtual bool higher(Avl_node_base *n1, Avl_node_base *n2) const = 0;
42
43
50 virtual void recompute(Avl_node_base *) { }
51 };
52
53 Avl_node_base *_child[2];
54 Avl_node_base *_parent;
55 unsigned char _depth;
56
57 public:
58
59 typedef bool Side;
60
61 enum { LEFT = false, RIGHT = true };
62
63 private:
64
65
68 inline int _child_depth(Side i) {
69 return _child[i] ? _child[i]->_depth : 0; }
70
71
74 void _recompute_depth(Policy &policy);
75
76
79 inline Side _bias() {
80 return (_child_depth(RIGHT) > _child_depth(LEFT)); }
81
82
85 void _adopt(Avl_node_base *node, Side i, Policy &policy);
86
87
97 void _rotate_subtree(Avl_node_base *node, Side side, Policy &policy);
98
99
106 void _rebalance_subtree(Avl_node_base *node, Policy &policy);
107
108 public:
109
110
113 Avl_node_base();
114
115
118 void insert(Avl_node_base *node, Policy &policy);
119
120
123 void remove(Policy &policy);
124 };
125
126
127
137 template <typename NT>
138 class Avl_node : public Avl_node_base
139 {
140 public:
141
142 inline NT *child(Side i) const { return static_cast<NT *>(_child[i]); }
143
144
147 void recompute() { }
148 };
149
150
151
157 template <typename NT>
158 class Avl_tree : Avl_node<NT>
159 {
160 private:
161
162
165 class Policy : public Avl_node_base::Policy
166 {
167 bool higher(Avl_node_base *n1, Avl_node_base *n2) const
168 {
169 return static_cast<NT *>(n1)->higher(static_cast<NT *>(n2));
170 }
171
172 void recompute(Avl_node_base *node)
173 {
174 static_cast<NT *>(node)->recompute();
175 }
176
177 } _policy;
178
179 public:
180
181
184 void insert(Avl_node<NT> *node) { Avl_node_base::insert(node, _policy); }
185
186
189 void remove(Avl_node<NT> *node) { node->remove(_policy); }
190
191
197 inline NT *first() const { return child(Avl_node<NT>::LEFT); }
198 };
199 }
200
201 #endif