L4Re Operating System Framework
Interface and Usage Documentation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 * License: see LICENSE.spdx (in this directory or the directories above)
12 */
13
14#pragma once
15
16#include <l4/cxx/std_alloc>
17#include <l4/cxx/std_ops>
18#include <l4/cxx/type_traits>
19#include <l4/cxx/avl_tree>
20
21struct Avl_set_tester;
22
23namespace cxx {
24namespace Bits {
33template< typename Node, typename Key, typename Node_op >
34class Avl_set_iter : public __Bst_iter_b<Node, Node_op>
35{
36private:
38 typedef __Bst_iter_b<Node, Node_op> Base;
39
41 typedef typename Type_traits<Key>::Non_const_type Non_const_key;
42
44 typedef Avl_set_iter<Node, Non_const_key, Node_op> Non_const_iter;
45
46 using Base::_n;
47 using Base::_r;
48 using Base::inc;
49
50public:
52 Avl_set_iter() = default;
53
58 Avl_set_iter(Node const *t) : Base(t) {}
59
64 Avl_set_iter(Base const &o) : Base(o) {}
65
67 Avl_set_iter(Non_const_iter const &o)
68 : Base(o) {}
69
71 Avl_set_iter &operator = (Non_const_iter const &o)
72 { Base::operator = (o); return *this; }
73
78 Key &operator * () const { return const_cast<Node*>(_n)->item; }
83 Key *operator -> () const { return &const_cast<Node*>(_n)->item; }
87 Avl_set_iter &operator ++ () { inc(); return *this; }
91 Avl_set_iter operator ++ (int)
92 { Avl_set_iter tmp = *this; inc(); return tmp; }
93
94};
95
97template<typename KEY_TYPE>
99{
100 typedef KEY_TYPE Key_type;
101 template<typename NODE>
102 static Key_type const &key_of(NODE const *n)
103 { return n->item; }
104};
105
106
119template< typename ITEM_TYPE, class COMPARE,
120 template<typename A> class ALLOC,
121 typename GET_KEY>
123{
124 friend struct ::Avl_set_tester;
125
126public:
133 enum
134 {
136 E_exist = 17,
137 E_nomem = 12,
138 E_inval = 22
139 };
141 typedef ITEM_TYPE Item_type;
143 typedef GET_KEY Get_key;
145 typedef typename GET_KEY::Key_type Key_type;
147 typedef typename Type_traits<Item_type>::Const_type Const_item_type;
149 typedef COMPARE Item_compare;
150
151private:
153 class _Node : public Avl_tree_node
154 {
155 public:
157 Item_type item;
158
159 _Node() = default;
160
161 _Node(Item_type const &item) : Avl_tree_node(), item(item) {}
162
163 template<typename ...ARGS>
164 _Node(ARGS &&...args) : Avl_tree_node(), item(cxx::forward<ARGS>(args)...)
165 {}
166 };
167
168public:
172 class Node
173 {
174 private:
175 struct No_type;
176 friend class Base_avl_set<ITEM_TYPE, COMPARE, ALLOC, GET_KEY>;
177 _Node const *_n;
178 explicit Node(_Node const *n) : _n(n) {}
179
180 public:
182 Node() : _n(0) {}
183
189 Item_type const &operator * () { return _n->item; }
195 Item_type const *operator -> () { return &_n->item; }
196
201 bool valid() const { return _n; }
202
204 operator Item_type const * () { return _n ? &_n->item : 0; }
205 };
206
208 typedef ALLOC<_Node> Node_allocator;
209
210private:
212 Tree _tree;
214 Node_allocator _alloc;
215
216 Base_avl_set &operator = (Base_avl_set const &) = delete;
217
218 typedef typename Tree::Fwd_iter_ops Fwd;
219 typedef typename Tree::Rev_iter_ops Rev;
220
221public:
222 typedef typename Type_traits<Item_type>::Param_type Item_param_type;
223
225 typedef Avl_set_iter<_Node, Item_type, Fwd> Iterator;
226 typedef Iterator iterator;
228 typedef Avl_set_iter<_Node, Const_item_type, Fwd> Const_iterator;
229 typedef Const_iterator const_iterator;
231 typedef Avl_set_iter<_Node, Item_type, Rev> Rev_iterator;
232 typedef Rev_iterator reverse_iterator;
234 typedef Avl_set_iter<_Node, Const_item_type, Rev> Const_rev_iterator;
235 typedef Const_rev_iterator const_reverse_iterator;
236
243 explicit Base_avl_set(Node_allocator const &alloc = Node_allocator())
244 : _tree(), _alloc(alloc)
245 {}
246
248 {
249 _tree.remove_all([this](_Node *n)
250 {
251 n->~_Node();
252 _alloc.free(n);
253 });
254 }
255
263 inline Base_avl_set(Base_avl_set const &o);
264
283
284 template<typename... Args>
285 cxx::Pair<Iterator, int> emplace(Args&&... args);
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/* In-place insert new _Node. */
430template< typename Item, class Compare, template< typename A > class Alloc, typename KEY_TYPE>
431template<typename... Args>
434{
435 _Node *n = _alloc.alloc();
436 if (!n)
437 return cxx::pair(end(), -E_nomem);
438
439 new (n, Nothrow()) _Node(cxx::forward<Args>(args)...);
440 Pair<_Node *, bool> err = _tree.insert(n);
441 if (!err.second)
442 {
443 n->~_Node();
444 _alloc.free(n);
445 }
446
447 return cxx::pair(Iterator(typename Tree::Iterator(err.first, err.first)), err.second ? 0 : -E_exist);
448}
449
450} // namespace Bits
451
463template< typename ITEM_TYPE, class COMPARE = Lt_functor<ITEM_TYPE>,
464 template<typename A> class ALLOC = New_allocator>
465class Avl_set :
466 public Bits::Base_avl_set<ITEM_TYPE, COMPARE, ALLOC,
467 Bits::Avl_set_get_key<ITEM_TYPE> >
468{
469private:
470 typedef Bits::Base_avl_set<ITEM_TYPE, COMPARE, ALLOC,
472
473public:
474 typedef typename Base::Node_allocator Node_allocator;
475 Avl_set() = default;
476 Avl_set(Node_allocator const &alloc)
477 : Base(alloc)
478 {}
479};
480
481} // namespace cxx
AVL tree.
AVL set for simple comparable items.
Definition avl_set:468
Node of an AVL tree.
Definition avl_tree:30
Avl_tree_node()=default
Create an uninitialized node, this is what you should do.
A generic AVL tree.
Definition avl_tree:101
Pair< Node *, bool > insert(Node *new_node)
Insert a new node into this AVL tree.
Definition avl_tree:220
Node * remove(Key_param_type key)
Remove the node with key from the tree.
Definition avl_tree:282
A smart pointer to a tree item.
Definition avl_set:173
bool valid() const
Validity check.
Definition avl_set:201
Item_type const * operator->()
Dereferenced member access.
Definition avl_set:195
Node()
Default construction for NIL pointer.
Definition avl_set:182
Item_type const & operator*()
Dereference the pointer.
Definition avl_set:189
Internal: AVL set with internally managed nodes.
Definition avl_set:123
Avl_set_iter< _Node, Item_type, Fwd > Iterator
Forward iterator for the set.
Definition avl_set:225
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:231
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
GET_KEY::Key_type Key_type
Type of the sort key used for the items.
Definition avl_set:145
@ E_noent
Item does not exist.
Definition avl_set:135
@ E_nomem
Memory allocation failed.
Definition avl_set:137
@ E_exist
Item exists already.
Definition avl_set:136
@ E_inval
Internal error.
Definition avl_set:138
ITEM_TYPE Item_type
Type for the items store in the set.
Definition avl_set:141
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:149
Base_avl_set(Node_allocator const &alloc=Node_allocator())
Create a AVL-tree based set.
Definition avl_set:243
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:208
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:234
GET_KEY Get_key
Key-getter type to derive the sort key of an internal node.
Definition avl_set:143
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:147
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:228
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:285
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
Node * find_node(Key_param_type key) const
find the node with the given key.
Definition bst.h:269
Const_iterator find(Key_param_type key) const
find the node with the given key.
Definition bst.h:305
void remove_all(FUNC &&callback)
Clear the tree.
Definition bst.h:251
Const_iterator end() const
Get the end marker for the constant forward iterator.
Definition bst.h:177
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
Helper type to distinguish the operator new version that does not throw exceptions.
Definition std_alloc:19
Our C++ library.
Definition arith:11
Internal, key-getter for Avl_set nodes.
Definition avl_set:99
Pair of two values.
Definition pair:28
Second second
Second value.
Definition pair:37
First first
First value.
Definition pair:35