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 
49  using Base::_n;
50  using Base::_r;
51  using Base::inc;
52 
53 public:
55  Avl_set_iter() {}
56 
62  Avl_set_iter(Node const *t) : Base(t) {}
63 
64  Avl_set_iter(Base const &o) : Base(o) {}
65 
66  Avl_set_iter(Avl_set_iter<Node, typename Type_traits<Key>::Non_const_type, Node_op> const &o)
67  : Base(o) {}
68 
73  Key &operator * () const { return const_cast<Node*>(_n)->item; }
78  Key *operator -> () const { return &const_cast<Node*>(_n)->item; }
82  Avl_set_iter &operator ++ () { inc(); return *this; }
86  Avl_set_iter &operator ++ (int)
87  { Avl_set_iter tmp = *this; inc(); return tmp; }
88 
89 };
90 
92 template<typename KEY_TYPE>
94 {
95  typedef KEY_TYPE Key_type;
96  template<typename NODE>
97  static Key_type const &key_of(NODE const *n)
98  { return n->item; }
99 };
100 
101 
114 template< typename ITEM_TYPE, class COMPARE,
115  template<typename A> class ALLOC,
116  typename GET_KEY>
118 {
119 public:
126  enum
127  {
128  E_noent = 2,
129  E_exist = 17,
130  E_nomem = 12,
131  E_inval = 22
132  };
134  typedef ITEM_TYPE Item_type;
136  typedef GET_KEY Get_key;
138  typedef typename GET_KEY::Key_type Key_type;
140  typedef typename Type_traits<Item_type>::Const_type Const_item_type;
142  typedef COMPARE Item_compare;
143 
144 private:
146  class _Node : public Avl_tree_node
147  {
148  public:
150  Item_type item;
151 
152  _Node() : Avl_tree_node(), item() {}
153 
154  _Node(Item_type const &item) : Avl_tree_node(), item(item) {}
155  };
156 
157 public:
161  class Node
162  {
163  private:
164  struct No_type;
165  friend class Base_avl_set<ITEM_TYPE, COMPARE, ALLOC, GET_KEY>;
166  _Node const *_n;
167  explicit Node(_Node const *n) : _n(n) {}
168 
169  public:
171  Node() : _n(0) {}
172 
174  Node &operator = (Node const &o) { _n = o._n; return *this; }
175 
181  Item_type const &operator * () { return _n->item; }
187  Item_type const *operator -> () { return &_n->item; }
188 
193  bool valid() const { return _n; }
194 
196  operator Item_type const * () { if (_n) return &_n->item; else return 0; }
197  };
198 
200  typedef ALLOC<_Node> Node_allocator;
201 
202 private:
204  Tree _tree;
206  Node_allocator _alloc;
207 
208  Base_avl_set &operator = (Base_avl_set const &) = delete;
209 
210  typedef typename Tree::Fwd_iter_ops Fwd;
211  typedef typename Tree::Rev_iter_ops Rev;
212 
213 public:
214  typedef typename Type_traits<Item_type>::Param_type Item_param_type;
215 
217  typedef Avl_set_iter<_Node, Item_type, Fwd> Iterator;
218  typedef Iterator iterator;
220  typedef Avl_set_iter<_Node, Const_item_type, Fwd> Const_iterator;
221  typedef Const_iterator const_iterator;
223  typedef Avl_set_iter<_Node, Item_type, Rev> Rev_iterator;
224  typedef Rev_iterator reverse_iterator;
226  typedef Avl_set_iter<_Node, Const_item_type, Rev> Const_rev_iterator;
227  typedef Const_rev_iterator const_reverse_iterator;
228 
235  explicit Base_avl_set(Node_allocator const &alloc = Node_allocator())
236  : _tree(), _alloc(alloc)
237  {}
238 
239  ~Base_avl_set()
240  {
241  _tree.remove_all([this](_Node *n)
242  {
243  n->~_Node();
244  _alloc.free(n);
245  });
246  }
247 
255  inline Base_avl_set(Base_avl_set const &o);
256 
274  cxx::Pair<Iterator, int> insert(Item_type const &item);
275 
285  int remove(Key_type const &item)
286  {
287  _Node *n = _tree.remove(item);
288  if ((long)n == -E_inval)
289  return -E_inval;
290 
291  if (n)
292  {
293  n->~_Node();
294  _alloc.free(n);
295  return 0;
296  }
297 
298  return -E_noent;
299  }
300 
305  int erase(Key_type const &item)
306  { return remove(item); }
307 
316  Node find_node(Key_type const &item) const
317  { return Node(_tree.find_node(item)); }
318 
327  Node lower_bound_node(Key_type const &key) const
328  { return Node(_tree.lower_bound_node(key)); }
329 
330  Node lower_bound_node(Key_type &&key) const
331  { return Node(_tree.lower_bound_node(key)); }
332 
337  Const_iterator begin() const { return _tree.begin(); }
342  Const_iterator end() const { return _tree.end(); }
343 
348  Iterator begin() { return _tree.begin(); }
353  Iterator end() { return _tree.end(); }
354 
359  Const_rev_iterator rbegin() const { return _tree.rbegin(); }
364  Const_rev_iterator rend() const { return _tree.rend(); }
365 
370  Rev_iterator rbegin() { return _tree.rbegin(); }
375  Rev_iterator rend() { return _tree.rend(); }
376 
377  Const_iterator find(Key_type const &item) const
378  { return _tree.find(item); }
379 };
380 
381 
382 //----------------------------------------------------------------------------
383 /* Implementation of AVL Tree */
384 
385 /* Create a copy */
386 template< typename Item, class Compare, template<typename A> class Alloc, typename KEY_TYPE>
388  : _tree(), _alloc(o._alloc)
389 {
390  for (Const_iterator i = o.begin(); i != o.end(); ++i)
391  insert(*i);
392 }
393 
394 /* Insert new _Node. */
395 template< typename Item, class Compare, template< typename A > class Alloc, typename KEY_TYPE>
398 {
399  _Node *n = _alloc.alloc();
400  if (!n)
401  return cxx::pair(end(), -E_nomem);
402 
403  new (n, Nothrow()) _Node(item);
404  Pair<_Node *, bool> err = _tree.insert(n);
405  if (!err.second)
406  {
407  n->~_Node();
408  _alloc.free(n);
409  }
410 
411  return cxx::pair(Iterator(typename Tree::Iterator(err.first, err.first)), err.second ? 0 : -E_exist);
412 }
413 
414 } // namespace Bits
415 
427 template< typename ITEM_TYPE, class COMPARE = Lt_functor<ITEM_TYPE>,
428  template<typename A> class ALLOC = New_allocator>
429 class Avl_set :
430  public Bits::Base_avl_set<ITEM_TYPE, COMPARE, ALLOC,
431  Bits::Avl_set_get_key<ITEM_TYPE> >
432 {
433 private:
434  typedef Bits::Base_avl_set<ITEM_TYPE, COMPARE, ALLOC,
436 
437 public:
438  typedef typename Base::Node_allocator Node_allocator;
439  Avl_set() = default;
440  Avl_set(Node_allocator const &alloc)
441  : Base(alloc)
442  {}
443 };
444 
445 } // namespace cxx
Rev_iterator rbegin()
Get the mutable backward iterator for the last element of the set.
Definition: avl_set:370
ITEM_TYPE Item_type
Type for the items store in the set.
Definition: avl_set:134
Our C++ library.
Definition: arith:22
Const_rev_iterator rbegin() const
Get the constant backward iterator for the last element in the set.
Definition: bst.h:201
Node find_node(Key_type const &item) const
Lookup a node equal to item.
Definition: avl_set:316
Node * remove(Key_param_type key)
Remove the node with key from the tree.
Definition: avl_tree:291
Node()
Default construction for NIL pointer.
Definition: avl_set:171
Pair< Node *, bool > insert(Node *new_node)
Insert a new node into this AVL tree.
Definition: avl_tree:229
Helper type to distinguish the oeprator new version that does not throw exceptions.
Definition: std_alloc:30
bool valid() const
Validity check.
Definition: avl_set:193
Rev Rev_iter_ops
Helper for building reverse iterators for different wrapper classes.
Definition: bst.h:73
Const_iterator end() const
Get the end marker for the constant forward iterator.
Definition: avl_set:342
AVL set for simple compareable items.
Definition: avl_set:429
Const_rev_iterator rend() const
Get the end marker for the constant backward iterator.
Definition: bst.h:206
AVL tree.
Second second
Second value.
Definition: pair:46
Avl_set_iter< _Node, Item_type, Fwd > Iterator
Forward iterator for the set.
Definition: avl_set:217
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_iterator find(Key_param_type key) const
find the node with the given key.
Definition: bst.h:312
Node * find_node(Key_param_type key) const
find the node with the given key.
Definition: bst.h:276
ALLOC< _Node > Node_allocator
Type for the node allocator.
Definition: avl_set:200
Iterator end()
Get the end marker for the mutable forward iterator.
Definition: avl_set:353
COMPARE Item_compare
Type for the comparison functor.
Definition: avl_set:142
A smart pointer to a tree item.
Definition: avl_set:161
void remove_all(FUNC &&callback)
Clear the tree.
Definition: bst.h:258
First first
First value.
Definition: pair:44
Const_rev_iterator rbegin() const
Get the constant backward iterator for the last element in the set.
Definition: avl_set:359
Avl_set_iter< _Node, Item_type, Rev > Rev_iterator
Backward iterator for the set.
Definition: avl_set:223
Const_iterator begin() const
Get the constant forward iterator for the first element in the set.
Definition: avl_set:337
Rev_iterator rend()
Get the end marker for the mutable backward iterator.
Definition: avl_set:375
Internal: AVL set with internally managed nodes.
Definition: avl_set:117
Const_iterator begin() const
Get the constant forward iterator for the first element in the set.
Definition: bst.h:179
Avl_set_iter< _Node, Const_item_type, Rev > Const_rev_iterator
Constant backward iterator for the set.
Definition: avl_set:226
int erase(Key_type const &item)
Erase the item with the given key.
Definition: avl_set:305
Iterator begin()
Get the mutable forward iterator for the first element of the set.
Definition: avl_set:348
GET_KEY::Key_type Key_type
Type of the sort key used for the items.
Definition: avl_set:138
Const_iterator end() const
Get the end marker for the constant forward iterator.
Definition: bst.h:184
GET_KEY Get_key
Key-getter type to derive the sort key of an internal node.
Definition: avl_set:136
Avl_set_iter< _Node, Const_item_type, Fwd > Const_iterator
Constant forward iterator for the set.
Definition: avl_set:220
Node lower_bound_node(Key_type const &key) const
Find the first node greater or equal to key.
Definition: avl_set:327
Const_rev_iterator rend() const
Get the end marker for the constant backward iterator.
Definition: avl_set:364
Node of an AVL tree.
Definition: avl_tree:38
Type_traits< Item_type >::Const_type Const_item_type
Type used for const items within the set.
Definition: avl_set:140
Fwd Fwd_iter_ops
Helper for building forward iterators for different wrapper classes.
Definition: bst.h:71
Base_avl_set(Node_allocator const &alloc=Node_allocator())
Create a AVL-tree based set.
Definition: avl_set:235
Internal, key-getter for Avl_set nodes.
Definition: avl_set:93
Pair of two values.
Definition: pair:36
Standard allocator based on operator new () .
Definition: std_alloc:60