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 * 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 {
25
34template<typename Node, typename Key, typename Node_op>
35class Avl_set_iter : public __Bst_iter_b<Node, Node_op>
36{
37private:
39 typedef __Bst_iter_b<Node, Node_op> Base;
40
42 typedef typename Type_traits<Key>::Non_const_type Non_const_key;
43
45 typedef Avl_set_iter<Node, Non_const_key, Node_op> Non_const_iter;
46
47 using Base::_n;
48 using Base::_r;
49 using Base::inc;
50
51public:
53 Avl_set_iter() = default;
54
59 Avl_set_iter(Node const *t) : Base(t) {}
60
65 Avl_set_iter(Base const &o) : Base(o) {}
66
68 Avl_set_iter(Non_const_iter const &o)
69 : Base(o) {}
70
72 Avl_set_iter &operator = (Non_const_iter const &o)
73 { Base::operator = (o); return *this; }
74
80 Key &operator * () const { return const_cast<Node*>(_n)->item; }
81
87 Key *operator -> () const { return &const_cast<Node*>(_n)->item; }
88
92 Avl_set_iter &operator ++ () { inc(); return *this; }
93
98 { Avl_set_iter tmp = *this; inc(); return tmp; }
99};
100
102template<typename KEY_TYPE>
104{
105 typedef KEY_TYPE Key_type;
106 template<typename NODE>
107 static Key_type const &key_of(NODE const *n)
108 { return n->item; }
109};
110
124template<typename ITEM_TYPE, class COMPARE,
125 template<typename A> class ALLOC,
126 typename GET_KEY>
128{
129 friend struct ::Avl_set_tester;
130
131public:
138 enum
139 {
141 E_exist = 17,
142 E_nomem = 12,
144 };
145
147 typedef ITEM_TYPE Item_type;
148
150 typedef GET_KEY Get_key;
151
153 typedef typename GET_KEY::Key_type Key_type;
154
156 typedef typename Type_traits<Item_type>::Const_type Const_item_type;
157
159 typedef COMPARE Item_compare;
160
161private:
163 class _Node : public Avl_tree_node
164 {
165 public:
167 Item_type item;
168
169 _Node() = default;
170
171 _Node(Item_type const &item) : Avl_tree_node(), item(item) {}
172
173 template<typename ...ARGS>
174 _Node(ARGS &&...args) : Avl_tree_node(), item(cxx::forward<ARGS>(args)...)
175 {}
176 };
177
178public:
182 class Node
183 {
184 private:
185 friend class Base_avl_set<ITEM_TYPE, COMPARE, ALLOC, GET_KEY>;
186
187 _Node const *_n;
188
189 explicit Node(_Node const *n) : _n(n) {}
190
191 public:
193 Node() : _n(0) {}
194
200 Item_type const &operator * () { return _n->item; }
201
207 Item_type const *operator -> () { return &_n->item; }
208
214 bool valid() const { return _n; }
215
217 operator Item_type const * () { return _n ? &_n->item : 0; }
218 };
219
221 typedef ALLOC<_Node> Node_allocator;
222
223private:
225
226 Tree _tree;
227
229 Node_allocator _alloc;
230
231 typedef typename Tree::Fwd_iter_ops Fwd;
232 typedef typename Tree::Rev_iter_ops Rev;
233
239 void deep_copy(Base_avl_set const &o)
240 {
241 for (Const_iterator i = o.begin(); i != o.end(); ++i)
242 insert(*i);
243 }
244
245public:
246 typedef typename Type_traits<Item_type>::Param_type Item_param_type;
247
250 typedef Iterator iterator;
253 typedef Const_iterator const_iterator;
256 typedef Rev_iterator reverse_iterator;
259 typedef Const_rev_iterator const_reverse_iterator;
260
268 explicit Base_avl_set(Node_allocator const &alloc = Node_allocator())
269 : _tree(), _alloc(alloc)
270 {}
271
273 { clear(); }
274
284 Node_allocator const &alloc = Node_allocator())
285 : _tree(), _alloc(alloc)
286 { deep_copy(o); }
287
296 {
297 if (this != &o)
298 {
299 clear();
300 deep_copy(o);
301 }
302
303 return *this;
304 }
305
324
343 template<typename... Args>
345
349 void clear() noexcept
350 {
351 _tree.remove_all([this](_Node *n)
352 {
353 n->~_Node();
354 _alloc.free(n);
355 });
356 }
357
366 int remove(Key_type const &item)
367 {
368 _Node *n = _tree.remove(item);
369
370 if (n)
371 {
372 n->~_Node();
373 _alloc.free(n);
374 return 0;
375 }
376
377 return -E_noent;
378 }
379
384 int erase(Key_type const &item)
385 { return remove(item); }
386
395 Node find_node(Key_type const &item) const
396 { return Node(_tree.find_node(item)); }
397
406 Node lower_bound_node(Key_type const &key) const
407 { return Node(_tree.lower_bound_node(key)); }
408
409 Node lower_bound_node(Key_type &&key) const
410 { return Node(_tree.lower_bound_node(key)); }
411
417 Const_iterator begin() const { return _tree.begin(); }
418
424 Const_iterator end() const { return _tree.end(); }
425
431 Iterator begin() { return _tree.begin(); }
432
438 Iterator end() { return _tree.end(); }
439
445 Const_rev_iterator rbegin() const { return _tree.rbegin(); }
446
452 Const_rev_iterator rend() const { return _tree.rend(); }
453
459 Rev_iterator rbegin() { return _tree.rbegin(); }
460
466 Rev_iterator rend() { return _tree.rend(); }
467
468 Const_iterator find(Key_type const &item) const
469 { return _tree.find(item); }
470
471#ifdef __DEBUG_L4_AVL
472 bool rec_dump(bool print)
473 {
474 return _tree.rec_dump(print);
475 }
476#endif
477};
478
479//----------------------------------------------------------------------------
480/* Implementation of AVL Tree */
481
482/* Insert new _Node. */
483template<typename Item, class Compare, template<typename A> class Alloc, typename KEY_TYPE>
484Pair<typename Base_avl_set<Item, Compare, Alloc, KEY_TYPE>::Iterator, int>
486{
487 _Node *n = _alloc.alloc();
488 if (!n)
489 return cxx::pair(end(), -E_nomem);
490
491 new (n, Nothrow()) _Node(item);
492 Pair<_Node *, bool> err = _tree.insert(n);
493 if (!err.second)
494 {
495 n->~_Node();
496 _alloc.free(n);
497 }
498
499 return cxx::pair(Iterator(typename Tree::Iterator(err.first, err.first)), err.second ? 0 : -E_exist);
500}
501
502/* In-place insert new _Node. */
503template<typename Item, class Compare, template<typename A> class Alloc, typename KEY_TYPE>
504template<typename... Args>
507{
508 _Node *n = _alloc.alloc();
509 if (!n)
510 return cxx::pair(end(), -E_nomem);
511
512 new (n, Nothrow()) _Node(cxx::forward<Args>(args)...);
513 Pair<_Node *, bool> err = _tree.insert(n);
514 if (!err.second)
515 {
516 n->~_Node();
517 _alloc.free(n);
518 }
519
520 return cxx::pair(Iterator(typename Tree::Iterator(err.first, err.first)), err.second ? 0 : -E_exist);
521}
522
523} // namespace Bits
524
536template<typename ITEM_TYPE, template<typename T> class COMPARE = Lt_functor,
537 template<typename A> class ALLOC = New_allocator>
538class Avl_set :
539 public Bits::Base_avl_set<ITEM_TYPE, COMPARE<ITEM_TYPE>, ALLOC,
540 Bits::Avl_set_get_key<ITEM_TYPE>>
541{
542private:
545
546public:
547 typedef typename Base::Node_allocator Node_allocator;
548 Avl_set() = default;
549 Avl_set(Node_allocator const &alloc)
550 : Base(alloc)
551 {}
552};
553
554} // namespace cxx
AVL tree.
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
Generic iterator for the AVL-tree based set.
Definition avl_set:36
Avl_set_iter()=default
Create an invalid iterator (end marker).
Avl_set_iter & operator=(Non_const_iter const &o)
Allow assignment of non-const iterator to const iterator versions.
Definition avl_set:72
Key * operator->() const
Member access to the item the iterator points to.
Definition avl_set:87
Avl_set_iter(Non_const_iter const &o)
Allow copy of non-const iterator to const iterator versions.
Definition avl_set:68
Avl_set_iter(Node const *t)
Create an iterator for the given tree.
Definition avl_set:59
Avl_set_iter & operator++()
Set the iterator to the next element (pre increment).
Definition avl_set:92
Avl_set_iter(Base const &o)
Create an iterator from a BST iterator.
Definition avl_set:65
Key & operator*() const
Dereference the iterator and get the item out of the tree.
Definition avl_set:80
bool valid() const
Validity check.
Definition avl_set:214
Item_type const * operator->()
Dereferenced member access.
Definition avl_set:207
Node()
Default construction for NIL pointer.
Definition avl_set:193
Item_type const & operator*()
Dereference the pointer.
Definition avl_set:200
AVL set with internally managed nodes.
Definition avl_set:128
Node find_node(Key_type const &item) const
Lookup a node equal to item.
Definition avl_set:395
Const_rev_iterator rbegin() const
Get the constant backward iterator for the last element in the set.
Definition avl_set:445
Iterator end()
Get the end marker for the mutable forward iterator.
Definition avl_set:438
Rev_iterator rbegin()
Get the mutable backward iterator for the last element of the set.
Definition avl_set:459
Base_avl_set & operator=(Base_avl_set const &o)
Copy assignment operator of an AVL-tree based set.
Definition avl_set:295
int remove(Key_type const &item)
Remove an item from the set.
Definition avl_set:366
Base_avl_set(Node_allocator const &alloc=Node_allocator())
Create a AVL-tree based set.
Definition avl_set:268
Const_iterator end() const
Get the end marker for the constant forward iterator.
Definition avl_set:424
Const_rev_iterator rend() const
Get the end marker for the constant backward iterator.
Definition avl_set:452
Const_iterator begin() const
Get the constant forward iterator for the first element in the set.
Definition avl_set:417
Rev_iterator rend()
Get the end marker for the mutable backward iterator.
Definition avl_set:466
Iterator begin()
Get the mutable forward iterator for the first element of the set.
Definition avl_set:431
cxx::Pair< Iterator, int > insert(Item_type const &item)
Insert an item into the set.
Definition avl_set:485
int erase(Key_type const &item)
Erase the item with the given key.
Definition avl_set:384
void clear() noexcept
Remove all items from the set.
Definition avl_set:349
cxx::Pair< Iterator, int > emplace(Args &&... args)
Emplace an item into the set.
Base_avl_set(Base_avl_set const &o, Node_allocator const &alloc=Node_allocator())
Create a copy of an AVL-tree based set.
Definition avl_set:283
Node lower_bound_node(Key_type const &key) const
Find the first node greater or equal to key.
Definition avl_set:406
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
Rev Rev_iter_ops
Helper for building reverse iterators for different wrapper classes.
Definition bst.h:67
Const_iterator find(Key_param_type key) const
find the node with the given key.
Definition bst.h:306
Fwd Fwd_iter_ops
Helper for building forward iterators for different wrapper classes.
Definition bst.h:65
Standard allocator based on operator new () .
Definition std_alloc:57
Helper type to distinguish the operator new version that does not throw exceptions.
Definition std_alloc:19
Internal helpers for the cxx package.
Definition avl_map:21
Our C++ library.
Definition arith:11
Internal, key-getter for Avl_set nodes.
Definition avl_set:104
Generic comparator class that defaults to the less-than operator.
Definition std_ops:19
Pair of two values.
Definition pair:28
Second second
Second value.
Definition pair:37
First first
First value.
Definition pair:35