L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
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;
37
39 struct Fwd
40 {
41 static Node *child(Node const *n, Direction d)
42 { return Bst_node::next<Node>(n, d); }
43
44 static bool cmp(Node const *l, Node const *r)
45 { return Compare()(Get_key::key_of(l), Get_key::key_of(r)); }
46 };
47
49 struct Rev
50 {
51 static Node *child(Node const *n, Direction d)
52 { return Bst_node::next<Node>(n, !d); }
53
54 static bool cmp(Node const *l, Node const *r)
55 { return Compare()(Get_key::key_of(r), Get_key::key_of(l)); }
56 };
57
58public:
60 typedef typename Get_key::Key_type Key_type;
62 typedef typename Type_traits<Key_type>::Param_type Key_param_type;
63
65 typedef Fwd Fwd_iter_ops;
67 typedef Rev Rev_iter_ops;
68
70
72 typedef __Bst_iter<Node, Node, Fwd> Iterator;
74 typedef __Bst_iter<Node, Node const, Fwd> Const_iterator;
76 typedef __Bst_iter<Node, Node, Rev> Rev_iterator;
78 typedef __Bst_iter<Node, Node const, Rev> Const_rev_iterator;
80
81protected:
90
93
95 Bst() : _head(0) {}
96
98 Node *head() const { return static_cast<Node*>(_head); }
99
101 static Key_type k(Bst_node const *n)
102 { return Get_key::key_of(static_cast<Node const *>(n)); }
103
113 {
114 Compare cmp;
115 Dir d(cmp(r, l));
116 if (d == Direction::L && !cmp(l, r))
117 return Direction::N;
118 return d;
119 }
120
129 static Dir dir(Key_param_type l, Bst_node const *r)
130 { return dir(l, k(r)); }
131
134 { return Compare()(r, l); }
135
137 static bool greater(Key_param_type l, Bst_node const *r)
138 { return greater(l, k(r)); }
139
141 static bool greater(Bst_node const *l, Bst_node const *r)
142 { return greater(k(l), k(r)); }
143
144
151 template<typename FUNC>
152 static void remove_tree(Bst_node *head, FUNC &&callback)
153 {
155 remove_tree(n, callback);
156
158 remove_tree(n, callback);
159
160 callback(static_cast<Node *>(head));
161 }
162
163public:
164
178 Const_iterator end() const { return Const_iterator(); }
179
184 Iterator begin() { return Iterator(head()); }
189 Iterator end() { return Iterator(); }
190
201
213
214
219
225 Node *find_node(Key_param_type key) const;
226
234
242
251 template<typename FUNC>
252 void remove_all(FUNC &&callback)
253 {
254 if (!_head)
255 return;
256
258 _head = 0;
259 remove_tree(head, cxx::forward<FUNC>(callback));
260 }
261
262
264};
265
266/* find an element */
267template< typename Node, typename Get_key, class Compare>
268inline
269Node *
271{
272 Dir d;
273
274 for (Bst_node *q = _head; q; q = Bst_node::next(q, d))
275 {
276 d = dir(key, q);
277 if (d == Dir::N)
278 return static_cast<Node*>(q);
279 }
280 return 0;
281}
282
283template< typename Node, typename Get_key, class Compare>
284inline
285Node *
287{
288 Dir d;
289 Bst_node *r = 0;
290
291 for (Bst_node *q = _head; q; q = Bst_node::next(q, d))
292 {
293 d = dir(key, q);
294 if (d == Dir::L)
295 r = q; // found a node greater than key
296 else if (d == Dir::N)
297 return static_cast<Node*>(q);
298 }
299 return static_cast<Node*>(r);
300}
301
302/* find an element */
303template< typename Node, typename Get_key, class Compare>
304inline
307{
308 Bst_node *q = _head;
309 Bst_node *r = q;
310
311 for (Dir d; q; q = Bst_node::next(q, d))
312 {
313 d = dir(key, q);
314 if (d == Dir::N)
315 return Iterator(static_cast<Node*>(q), static_cast<Node *>(r));
316
317 if (d != Dir::L && q == r)
318 r = Bst_node::next(q, d);
319 }
320 return Iterator();
321}
322
323}}
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
__Bst_iter< Node, Node const, Rev > Const_rev_iterator
Constant backward.
Definition bst.h:78
Get_key::Key_type Key_type
The type of key values used to generate the total order of the elements.
Definition bst.h:60
__Bst_iter< Node, Node, Fwd > Iterator
Forward iterator.
Definition bst.h:72
Iterator end()
Get the end marker for the mutable forward iterator.
Definition bst.h:189
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:286
Bst()
Create an empty tree.
Definition bst.h:95
Rev_iterator rend()
Get the end marker for the mutable backward iterator.
Definition bst.h:211
Type_traits< Key_type >::Param_type Key_param_type
The type for key parameters.
Definition bst.h:62
static bool greater(Key_param_type l, Bst_node const *r)
Is l greater than r.
Definition bst.h:137
Const_rev_iterator rend() const
Get the end marker for the constant backward iterator.
Definition bst.h:200
Const_rev_iterator rbegin() const
Get the constant backward iterator for the last element in the set.
Definition bst.h:195
Rev Rev_iter_ops
Helper for building reverse iterators for different wrapper classes.
Definition bst.h:67
Bst_node * _head
The head pointer of the tree.
Definition bst.h:92
Node * find_node(Key_param_type key) const
find the node with the given key.
Definition bst.h:270
__Bst_iter< Node, Node const, Fwd > Const_iterator
Constant forward iterator.
Definition bst.h:74
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:129
static bool greater(Key_param_type l, Key_param_type r)
Is l greater than r.
Definition bst.h:133
Const_iterator find(Key_param_type key) const
find the node with the given key.
Definition bst.h:306
Iterator begin()
Get the mutable forward iterator for the first element of the set.
Definition bst.h:184
void remove_all(FUNC &&callback)
Clear the tree.
Definition bst.h:252
static Key_type k(Bst_node const *n)
Get the key value of n.
Definition bst.h:101
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:112
Const_iterator end() const
Get the end marker for the constant forward iterator.
Definition bst.h:178
__Bst_iter< Node, Node, Rev > Rev_iterator
Backward iterator.
Definition bst.h:76
Fwd Fwd_iter_ops
Helper for building forward iterators for different wrapper classes.
Definition bst.h:65
Const_iterator begin() const
Get the constant forward iterator for the first element in the set.
Definition bst.h:173
Rev_iterator rbegin()
Get the mutable backward iterator for the last element of the set.
Definition bst.h:206
static void remove_tree(Bst_node *head, FUNC &&callback)
Remove all elements in the subtree of head.
Definition bst.h:152
static bool greater(Bst_node const *l, Bst_node const *r)
Is l greater than r.
Definition bst.h:141
Node * head() const
Access the head node as object of type Node.
Definition bst.h:98
Internal helpers for the cxx package.
Definition avl_map:21
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