L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
bst.h
Go to the documentation of this file.
1// vi:ft=cpp
6/*
7 * (c) 2008-2009 Alexander Warg <warg@os.inf.tu-dresden.de>,
8 * Carsten Weinhold <weinhold@os.inf.tu-dresden.de>
9 * economic rights: Technische Universität Dresden (Germany)
10 *
11 * License: see LICENSE.spdx (in this directory or the directories above)
12 */
13#pragma once
14
15#include "../type_traits"
16#include "bst_base.h"
17#include "bst_iter.h"
18
19struct Avl_set_tester;
20
21namespace cxx { namespace Bits {
22
30template< typename Node, typename Get_key, typename Compare >
31class Bst
32{
33 friend struct ::Avl_set_tester;
34
35private:
36 typedef Direction Dir;
38 struct Fwd
39 {
40 static Node *child(Node const *n, Direction d)
41 { return Bst_node::next<Node>(n, d); }
42
43 static bool cmp(Node const *l, Node const *r)
44 { return Compare()(Get_key::key_of(l), Get_key::key_of(r)); }
45 };
46
48 struct Rev
49 {
50 static Node *child(Node const *n, Direction d)
51 { return Bst_node::next<Node>(n, !d); }
52
53 static bool cmp(Node const *l, Node const *r)
54 { return Compare()(Get_key::key_of(r), Get_key::key_of(l)); }
55 };
56
57public:
59 typedef typename Get_key::Key_type Key_type;
61 typedef typename Type_traits<Key_type>::Param_type Key_param_type;
62
64 typedef Fwd Fwd_iter_ops;
66 typedef Rev Rev_iter_ops;
67
69
71 typedef __Bst_iter<Node, Node, Fwd> Iterator;
73 typedef __Bst_iter<Node, Node const, Fwd> Const_iterator;
75 typedef __Bst_iter<Node, Node, Rev> Rev_iterator;
77 typedef __Bst_iter<Node, Node const, Rev> Const_rev_iterator;
80protected:
92
94 Bst() : _head(0) {}
95
97 Node *head() const { return static_cast<Node*>(_head); }
98
100 static Key_type k(Bst_node const *n)
101 { return Get_key::key_of(static_cast<Node const *>(n)); }
102
112 {
113 Compare cmp;
114 Dir d(cmp(r, l));
115 if (d == Direction::L && !cmp(l, r))
116 return Direction::N;
117 return d;
118 }
119
128 static Dir dir(Key_param_type l, Bst_node const *r)
129 { return dir(l, k(r)); }
130
133 { return Compare()(r, l); }
134
136 static bool greater(Key_param_type l, Bst_node const *r)
137 { return greater(l, k(r)); }
138
140 static bool greater(Bst_node const *l, Bst_node const *r)
141 { return greater(k(l), k(r)); }
150 template<typename FUNC>
151 static void remove_tree(Bst_node *head, FUNC &&callback)
152 {
154 remove_tree(n, callback);
155
157 remove_tree(n, callback);
158
159 callback(static_cast<Node *>(head));
160 }
161
162public:
163
177 Const_iterator end() const { return Const_iterator(); }
178
183 Iterator begin() { return Iterator(head()); }
188 Iterator end() { return Iterator(); }
189
200
218
224 Node *find_node(Key_param_type key) const;
225
233
241
250 template<typename FUNC>
251 void remove_all(FUNC &&callback)
252 {
253 if (!_head)
254 return;
255
257 _head = 0;
258 remove_tree(head, cxx::forward<FUNC>(callback));
259 }
260
261
263};
264
265/* find an element */
266template< typename Node, typename Get_key, class Compare>
267inline
268Node *
270{
271 Dir d;
272
273 for (Bst_node *q = _head; q; q = Bst_node::next(q, d))
274 {
275 d = dir(key, q);
276 if (d == Dir::N)
277 return static_cast<Node*>(q);
278 }
279 return 0;
280}
281
282template< typename Node, typename Get_key, class Compare>
283inline
284Node *
286{
287 Dir d;
288 Bst_node *r = 0;
289
290 for (Bst_node *q = _head; q; q = Bst_node::next(q, d))
291 {
292 d = dir(key, q);
293 if (d == Dir::L)
294 r = q; // found a node greater than key
295 else if (d == Dir::N)
296 return static_cast<Node*>(q);
297 }
298 return static_cast<Node*>(r);
299}
300
301/* find an element */
302template< typename Node, typename Get_key, class Compare>
303inline
306{
307 Bst_node *q = _head;
308 Bst_node *r = q;
309
310 for (Dir d; q; q = Bst_node::next(q, d))
311 {
312 d = dir(key, q);
313 if (d == Dir::N)
314 return Iterator(static_cast<Node*>(q), static_cast<Node *>(r));
315
316 if (d != Dir::L && q == r)
317 r = Bst_node::next(q, d);
318 }
319 return Iterator();
320}
321
322}}
AVL tree.
AVL tree.
Basic type of a node in a binary search tree (BST).
Definition bst_base.h:71
static Bst_node * next(Bst_node const *p, Direction d)
Get next node in direction d.
Definition bst_base.h:87
Basic binary search tree (BST).
Definition bst.h:32
__Bst_iter< Node, Node const, Rev > Const_rev_iterator
Constant backward.
Definition bst.h:77
Get_key::Key_type Key_type
The type of key values used to generate the total order of the elements.
Definition bst.h:59
__Bst_iter< Node, Node, Fwd > Iterator
Forward iterator.
Definition bst.h:71
Iterator end()
Get the end marker for the mutable forward iterator.
Definition bst.h:188
Node * lower_bound_node(Key_param_type key) const
Find the first node with a key not less than the given key.
Definition bst.h:285
Bst()
Create an empty tree.
Definition bst.h:94
Rev_iterator rend()
Get the end marker for the mutable backward iterator.
Definition bst.h:210
Type_traits< Key_type >::Param_type Key_param_type
The type for key parameters.
Definition bst.h:61
static bool greater(Key_param_type l, Bst_node const *r)
Is l greater than r.
Definition bst.h:136
Const_rev_iterator rend() const
Get the end marker for the constant backward iterator.
Definition bst.h:199
Const_rev_iterator rbegin() const
Get the constant backward iterator for the last element in the set.
Definition bst.h:194
Rev Rev_iter_ops
Helper for building reverse iterators for different wrapper classes.
Definition bst.h:66
Bst_node * _head
The head pointer of the tree.
Definition bst.h:91
Node * find_node(Key_param_type key) const
find the node with the given key.
Definition bst.h:269
__Bst_iter< Node, Node const, Fwd > Const_iterator
Constant forward iterator.
Definition bst.h:73
static Dir dir(Key_param_type l, Bst_node const *r)
Get the direction to go from l to search for r.
Definition bst.h:128
static bool greater(Key_param_type l, Key_param_type r)
Is l greater than r.
Definition bst.h:132
Const_iterator find(Key_param_type key) const
find the node with the given key.
Definition bst.h:305
Iterator begin()
Get the mutable forward iterator for the first element of the set.
Definition bst.h:183
void remove_all(FUNC &&callback)
Clear the tree.
Definition bst.h:251
static Key_type k(Bst_node const *n)
Get the key value of n.
Definition bst.h:100
static Dir dir(Key_param_type l, Key_param_type r)
Get the direction to go from l to search for r.
Definition bst.h:111
Const_iterator end() const
Get the end marker for the constant forward iterator.
Definition bst.h:177
__Bst_iter< Node, Node, Rev > Rev_iterator
Backward iterator.
Definition bst.h:75
Fwd Fwd_iter_ops
Helper for building forward iterators for different wrapper classes.
Definition bst.h:64
Const_iterator begin() const
Get the constant forward iterator for the first element in the set.
Definition bst.h:172
Rev_iterator rbegin()
Get the mutable backward iterator for the last element of the set.
Definition bst.h:205
static void remove_tree(Bst_node *head, FUNC &&callback)
Remove all elements in the subtree of head.
Definition bst.h:151
static bool greater(Bst_node const *l, Bst_node const *r)
Is l greater than r.
Definition bst.h:140
Node * head() const
Access the head node as object of type Node.
Definition bst.h:97
Our C++ library.
Definition arith:11
The direction to go in a binary search tree.
Definition bst_base.h:29
@ L
Go to the left child.
Definition bst_base.h:33
@ R
Go to the right child.
Definition bst_base.h:34