L4Re Operating System Framework – Interface and Usage Documentation
Loading...
Searching...
No Matches
avl_set
Go to the documentation of this file.
1// vi:set ft=cpp: -*- Mode: C++ -*-
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 * This file is part of TUD:OS and distributed under the terms of the
12 * GNU General Public License 2.
13 * Please see the COPYING-GPL-2 file for details.
14 *
15 * As a special exception, you may use this file as part of a free software
16 * library without restriction. Specifically, if other files instantiate
17 * templates or use macros or inline functions from this file, or you compile
18 * this file and link it with other files to produce an executable, this
19 * file does not by itself cause the resulting executable to be covered by
20 * the GNU General Public License. This exception does not however
21 * invalidate any other reasons why the executable file might be covered by
22 * the GNU General Public License.
23 */
24
25#pragma once
26
27#include <l4/cxx/std_alloc>
28#include <l4/cxx/std_ops>
29#include <l4/cxx/type_traits>
30#include <l4/cxx/avl_tree>
31
32namespace cxx {
33namespace Bits {
42template< typename Node, typename Key, typename Node_op >
43class Avl_set_iter : public __Bst_iter_b<Node, Node_op>
44{
45private:
47 typedef __Bst_iter_b<Node, Node_op> Base;
48
50 typedef typename Type_traits<Key>::Non_const_type Non_const_key;
51
53 typedef Avl_set_iter<Node, Non_const_key, Node_op> Non_const_iter;
54
55 using Base::_n;
56 using Base::_r;
57 using Base::inc;
58
59public:
61 Avl_set_iter() = default;
62
67 Avl_set_iter(Node const *t) : Base(t) {}
68
73 Avl_set_iter(Base const &o) : Base(o) {}
74
76 Avl_set_iter(Non_const_iter const &o)
77 : Base(o) {}
78
80 Avl_set_iter &operator = (Non_const_iter const &o)
81 { Base::operator = (o); return *this; }
82
87 Key &operator * () const { return const_cast<Node*>(_n)->item; }
92 Key *operator -> () const { return &const_cast<Node*>(_n)->item; }
96 Avl_set_iter &operator ++ () { inc(); return *this; }
100 Avl_set_iter operator ++ (int)
101 { Avl_set_iter tmp = *this; inc(); return tmp; }
102
103};
104
106template<typename KEY_TYPE>
108{
109 typedef KEY_TYPE Key_type;
110 template<typename NODE>
111 static Key_type const &key_of(NODE const *n)
112 { return n->item; }
113};
114
115
128template< typename ITEM_TYPE, class COMPARE,
129 template<typename A> class ALLOC,
130 typename GET_KEY>
132{
133public:
140 enum
141 {
143 E_exist = 17,
144 E_nomem = 12,
145 E_inval = 22
146 };
148 typedef ITEM_TYPE Item_type;
150 typedef GET_KEY Get_key;
152 typedef typename GET_KEY::Key_type Key_type;
154 typedef typename Type_traits<Item_type>::Const_type Const_item_type;
156 typedef COMPARE Item_compare;
157
158private:
160 class _Node : public Avl_tree_node
161 {
162 public:
164 Item_type item;
165
166 _Node() = default;
167
168 _Node(Item_type const &item) : Avl_tree_node(), item(item) {}
169 };
170
171public:
175 class Node
176 {
177 private:
178 struct No_type;
179 friend class Base_avl_set<ITEM_TYPE, COMPARE, ALLOC, GET_KEY>;
180 _Node const *_n;
181 explicit Node(_Node const *n) : _n(n) {}
182
183 public:
185 Node() : _n(0) {}
186
192 Item_type const &operator * () { return _n->item; }
198 Item_type const *operator -> () { return &_n->item; }
199
204 bool valid() const { return _n; }
205
207 operator Item_type const * () { return _n ? &_n->item : 0; }
208 };
209
211 typedef ALLOC<_Node> Node_allocator;
212
213private:
215 Tree _tree;
217 Node_allocator _alloc;
218
219 Base_avl_set &operator = (Base_avl_set const &) = delete;
220
221 typedef typename Tree::Fwd_iter_ops Fwd;
222 typedef typename Tree::Rev_iter_ops Rev;
223
224public:
225 typedef typename Type_traits<Item_type>::Param_type Item_param_type;
226
228 typedef Avl_set_iter<_Node, Item_type, Fwd> Iterator;
229 typedef Iterator iterator;
231 typedef Avl_set_iter<_Node, Const_item_type, Fwd> Const_iterator;
232 typedef Const_iterator const_iterator;
234 typedef Avl_set_iter<_Node, Item_type, Rev> Rev_iterator;
235 typedef Rev_iterator reverse_iterator;
237 typedef Avl_set_iter<_Node, Const_item_type, Rev> Const_rev_iterator;
238 typedef Const_rev_iterator const_reverse_iterator;
239
246 explicit Base_avl_set(Node_allocator const &alloc = Node_allocator())
247 : _tree(), _alloc(alloc)
248 {}
249
251 {
252 _tree.remove_all([this](_Node *n)
253 {
254 n->~_Node();
255 _alloc.free(n);
256 });
257 }
258
266 inline Base_avl_set(Base_avl_set const &o);
267
286
295 int remove(Key_type const &item)
296 {
297 _Node *n = _tree.remove(item);
298
299 if (n)
300 {
301 n->~_Node();
302 _alloc.free(n);
303 return 0;
304 }
305
306 return -E_noent;
307 }
308
313 int erase(Key_type const &item)
314 { return remove(item); }
315
324 Node find_node(Key_type const &item) const
325 { return Node(_tree.find_node(item)); }
326
336 { return Node(_tree.lower_bound_node(key)); }
337
338 Node lower_bound_node(Key_type &&key) const
339 { return Node(_tree.lower_bound_node(key)); }
340
345 Const_iterator begin() const { return _tree.begin(); }
350 Const_iterator end() const { return _tree.end(); }
351
356 Iterator begin() { return _tree.begin(); }
361 Iterator end() { return _tree.end(); }
362
367 Const_rev_iterator rbegin() const { return _tree.rbegin(); }
372 Const_rev_iterator rend() const { return _tree.rend(); }
373
378 Rev_iterator rbegin() { return _tree.rbegin(); }
383 Rev_iterator rend() { return _tree.rend(); }
384
385 Const_iterator find(Key_type const &item) const
386 { return _tree.find(item); }
387
388#ifdef __DEBUG_L4_AVL
389 bool rec_dump(bool print)
390 {
391 return _tree.rec_dump(print);
392 }
393#endif
394};
395
396
397//----------------------------------------------------------------------------
398/* Implementation of AVL Tree */
399
400/* Create a copy */
401template< typename Item, class Compare, template<typename A> class Alloc, typename KEY_TYPE>
403 : _tree(), _alloc(o._alloc)
404{
405 for (Const_iterator i = o.begin(); i != o.end(); ++i)
406 insert(*i);
407}
408
409/* Insert new _Node. */
410template< typename Item, class Compare, template< typename A > class Alloc, typename KEY_TYPE>
413{
414 _Node *n = _alloc.alloc();
415 if (!n)
416 return cxx::pair(end(), -E_nomem);
417
418 new (n, Nothrow()) _Node(item);
419 Pair<_Node *, bool> err = _tree.insert(n);
420 if (!err.second)
421 {
422 n->~_Node();
423 _alloc.free(n);
424 }
425
426 return cxx::pair(Iterator(typename Tree::Iterator(err.first, err.first)), err.second ? 0 : -E_exist);
427}
428
429} // namespace Bits
430
442template< typename ITEM_TYPE, class COMPARE = Lt_functor<ITEM_TYPE>,
443 template<typename A> class ALLOC = New_allocator>
444class Avl_set :
445 public Bits::Base_avl_set<ITEM_TYPE, COMPARE, ALLOC,
446 Bits::Avl_set_get_key<ITEM_TYPE> >
447{
448private:
449 typedef Bits::Base_avl_set<ITEM_TYPE, COMPARE, ALLOC,
451
452public:
453 typedef typename Base::Node_allocator Node_allocator;
454 Avl_set() = default;
455 Avl_set(Node_allocator const &alloc)
456 : Base(alloc)
457 {}
458};
459
460} // namespace cxx
AVL tree.
AVL set for simple compareable items.
Definition avl_set:447
Node of an AVL tree.
Definition avl_tree:39
Avl_tree_node()=default
Create an uninitialized node, this is what you should do.
A generic AVL tree.
Definition avl_tree:108
Pair< Node *, bool > insert(Node *new_node)
Insert a new node into this AVL tree.
Definition avl_tree:227
Node * remove(Key_param_type key)
Remove the node with key from the tree.
Definition avl_tree:289
A smart pointer to a tree item.
Definition avl_set:176
bool valid() const
Validity check.
Definition avl_set:204
Item_type const * operator->()
Dereferenced member access.
Definition avl_set:198
Node()
Default construction for NIL pointer.
Definition avl_set:185
Item_type const & operator*()
Dereference the pointer.
Definition avl_set:192
Internal: AVL set with internally managed nodes.
Definition avl_set:132
Avl_set_iter< _Node, Item_type, Fwd > Iterator
Forward iterator for the set.
Definition avl_set:228
Node find_node(Key_type const &item) const
Lookup a node equal to item.
Definition avl_set:324
Avl_set_iter< _Node, Item_type, Rev > Rev_iterator
Backward iterator for the set.
Definition avl_set:234
Const_rev_iterator rbegin() const
Get the constant backward iterator for the last element in the set.
Definition avl_set:367
Iterator end()
Get the end marker for the mutable forward iterator.
Definition avl_set:361
Rev_iterator rbegin()
Get the mutable backward iterator for the last element of the set.
Definition avl_set:378
@ E_noent
Item does not exist.
Definition avl_set:142
@ E_nomem
Memory allocation failed.
Definition avl_set:144
@ E_exist
Item exists already.
Definition avl_set:143
@ E_inval
Internal error.
Definition avl_set:145
GET_KEY::Key_type Key_type
Type of the sort key used for the items.
Definition avl_set:152
ITEM_TYPE Item_type
Type for the items store in the set.
Definition avl_set:148
int remove(Key_type const &item)
Remove an item from the set.
Definition avl_set:295
COMPARE Item_compare
Type for the comparison functor.
Definition avl_set:156
Base_avl_set(Node_allocator const &alloc=Node_allocator())
Create a AVL-tree based set.
Definition avl_set:246
Const_iterator end() const
Get the end marker for the constant forward iterator.
Definition avl_set:350
Const_rev_iterator rend() const
Get the end marker for the constant backward iterator.
Definition avl_set:372
Const_iterator begin() const
Get the constant forward iterator for the first element in the set.
Definition avl_set:345
ALLOC< _Node > Node_allocator
Type for the node allocator.
Definition avl_set:211
Rev_iterator rend()
Get the end marker for the mutable backward iterator.
Definition avl_set:383
Iterator begin()
Get the mutable forward iterator for the first element of the set.
Definition avl_set:356
Avl_set_iter< _Node, Const_item_type, Rev > Const_rev_iterator
Constant backward iterator for the set.
Definition avl_set:237
GET_KEY Get_key
Key-getter type to derive the sort key of an internal node.
Definition avl_set:150
cxx::Pair< Iterator, int > insert(Item_type const &item)
Insert an item into the set.
Definition avl_set:412
Type_traits< Item_type >::Const_type Const_item_type
Type used for const items within the set.
Definition avl_set:154
int erase(Key_type const &item)
Erase the item with the given key.
Definition avl_set:313
Base_avl_set(Base_avl_set const &o)
Create a copy of an AVL-tree based set.
Definition avl_set:402
Avl_set_iter< _Node, Const_item_type, Fwd > Const_iterator
Constant forward iterator for the set.
Definition avl_set:231
Node lower_bound_node(Key_type const &key) const
Find the first node greater or equal to key.
Definition avl_set:335
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:292
Const_rev_iterator rend() const
Get the end marker for the constant backward iterator.
Definition bst.h:206
Const_rev_iterator rbegin() const
Get the constant backward iterator for the last element in the set.
Definition bst.h:201
Rev Rev_iter_ops
Helper for building reverse iterators for different wrapper classes.
Definition bst.h:73
Node * find_node(Key_param_type key) const
find the node with the given key.
Definition bst.h:276
Const_iterator find(Key_param_type key) const
find the node with the given key.
Definition bst.h:312
void remove_all(FUNC &&callback)
Clear the tree.
Definition bst.h:258
Const_iterator end() const
Get the end marker for the constant forward iterator.
Definition bst.h:184
Fwd Fwd_iter_ops
Helper for building forward iterators for different wrapper classes.
Definition bst.h:71
Const_iterator begin() const
Get the constant forward iterator for the first element in the set.
Definition bst.h:179
Standard allocator based on operator new () .
Definition std_alloc:68
Helper type to distinguish the oeprator new version that does not throw exceptions.
Definition std_alloc:30
Our C++ library.
Definition arith:22
Internal, key-getter for Avl_set nodes.
Definition avl_set:108
Pair of two values.
Definition pair:37
Second second
Second value.
Definition pair:46
First first
First value.
Definition pair:44