L4Re – L4 Runtime Environment
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 
32 namespace cxx {
33 namespace Bits {
42 template< typename Node, typename Key, typename Node_op >
43 class Avl_set_iter : public __Bst_iter_b<Node, Node_op>
44 {
45 private:
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 
59 public:
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 
106 template<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 
128 template< typename ITEM_TYPE, class COMPARE,
129  template<typename A> class ALLOC,
130  typename GET_KEY>
132 {
133 public:
140  enum
141  {
142  E_noent = 2,
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 
158 private:
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 
171 public:
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 * () { if (_n) return &_n->item; else return 0; }
208  };
209 
211  typedef ALLOC<_Node> Node_allocator;
212 
213 private:
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 
224 public:
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 
250  ~Base_avl_set()
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 
296  int remove(Key_type const &item)
297  {
298  _Node *n = _tree.remove(item);
299  if ((long)n == -E_inval)
300  return -E_inval;
301 
302  if (n)
303  {
304  n->~_Node();
305  _alloc.free(n);
306  return 0;
307  }
308 
309  return -E_noent;
310  }
311 
316  int erase(Key_type const &item)
317  { return remove(item); }
318 
327  Node find_node(Key_type const &item) const
328  { return Node(_tree.find_node(item)); }
329 
338  Node lower_bound_node(Key_type const &key) const
339  { return Node(_tree.lower_bound_node(key)); }
340 
341  Node lower_bound_node(Key_type &&key) const
342  { return Node(_tree.lower_bound_node(key)); }
343 
348  Const_iterator begin() const { return _tree.begin(); }
353  Const_iterator end() const { return _tree.end(); }
354 
359  Iterator begin() { return _tree.begin(); }
364  Iterator end() { return _tree.end(); }
365 
370  Const_rev_iterator rbegin() const { return _tree.rbegin(); }
375  Const_rev_iterator rend() const { return _tree.rend(); }
376 
381  Rev_iterator rbegin() { return _tree.rbegin(); }
386  Rev_iterator rend() { return _tree.rend(); }
387 
388  Const_iterator find(Key_type const &item) const
389  { return _tree.find(item); }
390 };
391 
392 
393 //----------------------------------------------------------------------------
394 /* Implementation of AVL Tree */
395 
396 /* Create a copy */
397 template< typename Item, class Compare, template<typename A> class Alloc, typename KEY_TYPE>
399  : _tree(), _alloc(o._alloc)
400 {
401  for (Const_iterator i = o.begin(); i != o.end(); ++i)
402  insert(*i);
403 }
404 
405 /* Insert new _Node. */
406 template< typename Item, class Compare, template< typename A > class Alloc, typename KEY_TYPE>
409 {
410  _Node *n = _alloc.alloc();
411  if (!n)
412  return cxx::pair(end(), -E_nomem);
413 
414  new (n, Nothrow()) _Node(item);
415  Pair<_Node *, bool> err = _tree.insert(n);
416  if (!err.second)
417  {
418  n->~_Node();
419  _alloc.free(n);
420  }
421 
422  return cxx::pair(Iterator(typename Tree::Iterator(err.first, err.first)), err.second ? 0 : -E_exist);
423 }
424 
425 } // namespace Bits
426 
438 template< typename ITEM_TYPE, class COMPARE = Lt_functor<ITEM_TYPE>,
439  template<typename A> class ALLOC = New_allocator>
440 class Avl_set :
441  public Bits::Base_avl_set<ITEM_TYPE, COMPARE, ALLOC,
442  Bits::Avl_set_get_key<ITEM_TYPE> >
443 {
444 private:
445  typedef Bits::Base_avl_set<ITEM_TYPE, COMPARE, ALLOC,
447 
448 public:
449  typedef typename Base::Node_allocator Node_allocator;
450  Avl_set() = default;
451  Avl_set(Node_allocator const &alloc)
452  : Base(alloc)
453  {}
454 };
455 
456 } // namespace cxx
AVL tree.
AVL set for simple compareable items.
Definition: avl_set:443
Node of an AVL tree.
Definition: avl_tree:39
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
Bst::Iterator Iterator
Forward iterator for the tree.
Definition: avl_tree:137
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:327
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:370
Iterator end()
Get the end marker for the mutable forward iterator.
Definition: avl_set:364
Rev_iterator rbegin()
Get the mutable backward iterator for the last element of the set.
Definition: avl_set:381
GET_KEY::Key_type Key_type
Type of the sort key used for the items.
Definition: avl_set:152
@ 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
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:296
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:353
Const_rev_iterator rend() const
Get the end marker for the constant backward iterator.
Definition: avl_set:375
Const_iterator begin() const
Get the constant forward iterator for the first element in the set.
Definition: avl_set:348
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:386
Iterator begin()
Get the mutable forward iterator for the first element of the set.
Definition: avl_set:359
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:408
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:316
Base_avl_set(Base_avl_set const &o)
Create a copy of an AVL-tree based set.
Definition: avl_set:398
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:338
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:61
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