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:
120  enum
121  {
122  E_noent = 2,
123  E_exist = 17,
124  E_nomem = 12,
125  E_inval = 22
126  };
128  typedef ITEM_TYPE Item_type;
130  typedef GET_KEY Get_key;
132  typedef typename GET_KEY::Key_type Key_type;
134  typedef typename Type_traits<Item_type>::Const_type Const_item_type;
136  typedef COMPARE Item_compare;
137 
138 private:
140  class _Node : public Avl_tree_node
141  {
142  public:
144  Item_type item;
145 
146  _Node() : Avl_tree_node(), item() {}
147 
148  _Node(Item_type const &item) : Avl_tree_node(), item(item) {}
149  };
150 
151 public:
155  class Node
156  {
157  private:
158  struct No_type;
159  friend class Base_avl_set<ITEM_TYPE, COMPARE, ALLOC, GET_KEY>;
160  _Node const *_n;
161  explicit Node(_Node const *n) : _n(n) {}
162 
163  public:
165  Node() : _n(0) {}
166 
168  Node &operator = (Node const &o) { _n = o._n; return *this; }
169 
175  Item_type const &operator * () { return _n->item; }
181  Item_type const *operator -> () { return &_n->item; }
182 
187  bool valid() const { return _n; }
188 
190  operator Item_type const * () { if (_n) return &_n->item; else return 0; }
191  };
192 
194  typedef ALLOC<_Node> Node_allocator;
195 
196 private:
198  Tree _tree;
200  Node_allocator _alloc;
201 
202  Base_avl_set &operator = (Base_avl_set const &) = delete;
203 
204  typedef typename Tree::Fwd_iter_ops Fwd;
205  typedef typename Tree::Rev_iter_ops Rev;
206 
207 public:
208  typedef typename Type_traits<Item_type>::Param_type Item_param_type;
209 
211  typedef Avl_set_iter<_Node, Item_type, Fwd> Iterator;
212  typedef Iterator iterator;
214  typedef Avl_set_iter<_Node, Const_item_type, Fwd> Const_iterator;
215  typedef Const_iterator const_iterator;
217  typedef Avl_set_iter<_Node, Item_type, Rev> Rev_iterator;
218  typedef Rev_iterator reverse_iterator;
220  typedef Avl_set_iter<_Node, Const_item_type, Rev> Const_rev_iterator;
221  typedef Const_rev_iterator const_reverse_iterator;
222 
229  explicit Base_avl_set(Node_allocator const &alloc = Node_allocator())
230  : _tree(), _alloc(alloc)
231  {}
232 
233  ~Base_avl_set()
234  {
235  _tree.remove_all([this](_Node *n)
236  {
237  n->~_Node();
238  _alloc.free(n);
239  });
240  }
241 
249  inline Base_avl_set(Base_avl_set const &o);
250 
267  cxx::Pair<Iterator, int> insert(Item_type const &item);
268 
278  int remove(Key_type const &item)
279  {
280  _Node *n = _tree.remove(item);
281  if ((long)n == -E_inval)
282  return -E_inval;
283 
284  if (n)
285  {
286  n->~_Node();
287  _alloc.free(n);
288  return 0;
289  }
290 
291  return -E_noent;
292  }
293 
298  int erase(Key_type const &item)
299  { return remove(item); }
300 
309  Node find_node(Key_type const &item) const
310  { return Node(_tree.find_node(item)); }
311 
320  Node lower_bound_node(Key_type const &key) const
321  { return Node(_tree.lower_bound_node(key)); }
322 
323  Node lower_bound_node(Key_type &&key) const
324  { return Node(_tree.lower_bound_node(key)); }
325 
330  Const_iterator begin() const { return _tree.begin(); }
335  Const_iterator end() const { return _tree.end(); }
336 
341  Iterator begin() { return _tree.begin(); }
346  Iterator end() { return _tree.end(); }
347 
352  Const_rev_iterator rbegin() const { return _tree.rbegin(); }
357  Const_rev_iterator rend() const { return _tree.rend(); }
358 
363  Rev_iterator rbegin() { return _tree.rbegin(); }
368  Rev_iterator rend() { return _tree.rend(); }
369 
370  Const_iterator find(Key_type const &item) const
371  { return _tree.find(item); }
372 };
373 
374 
375 //----------------------------------------------------------------------------
376 /* Implementation of AVL Tree */
377 
378 /* Create a copy */
379 template< typename Item, class Compare, template<typename A> class Alloc, typename KEY_TYPE>
381  : _tree(), _alloc(o._alloc)
382 {
383  for (Const_iterator i = o.begin(); i != o.end(); ++i)
384  insert(*i);
385 }
386 
387 /* Insert new _Node. */
388 template< typename Item, class Compare, template< typename A > class Alloc, typename KEY_TYPE>
391 {
392  _Node *n = _alloc.alloc();
393  if (!n)
394  return cxx::pair(end(), -E_nomem);
395 
396  new (n, Nothrow()) _Node(item);
397  Pair<_Node *, bool> err = _tree.insert(n);
398  if (!err.second)
399  _alloc.free(n);
400 
401  return cxx::pair(Iterator(typename Tree::Iterator(err.first, err.first)), err.second ? 0 : -E_exist);
402 }
403 
404 } // namespace Bits
405 
417 template< typename ITEM_TYPE, class COMPARE = Lt_functor<ITEM_TYPE>,
418  template<typename A> class ALLOC = New_allocator>
419 class Avl_set :
420  public Bits::Base_avl_set<ITEM_TYPE, COMPARE, ALLOC,
421  Bits::Avl_set_get_key<ITEM_TYPE> >
422 {
423 private:
424  typedef Bits::Base_avl_set<ITEM_TYPE, COMPARE, ALLOC,
426 
427 public:
428  typedef typename Base::Node_allocator Node_allocator;
429  Avl_set() = default;
430  Avl_set(Node_allocator const &alloc)
431  : Base(alloc)
432  {}
433 };
434 
435 } // namespace cxx
Rev_iterator rbegin()
Get the mutable backward iterator for the last element of the set.
Definition: avl_set:363
ITEM_TYPE Item_type
Type for the items store in the set.
Definition: avl_set:128
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:309
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:165
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:187
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:335
AVL set for simple compareable items.
Definition: avl_set:419
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:211
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:194
Iterator end()
Get the end marker for the mutable forward iterator.
Definition: avl_set:346
COMPARE Item_compare
Type for the comparison functor.
Definition: avl_set:136
A smart pointer to a tree item.
Definition: avl_set:155
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:352
Avl_set_iter< _Node, Item_type, Rev > Rev_iterator
Backward iterator for the set.
Definition: avl_set:217
Const_iterator begin() const
Get the constant forward iterator for the first element in the set.
Definition: avl_set:330
Rev_iterator rend()
Get the end marker for the mutable backward iterator.
Definition: avl_set:368
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:220
int erase(Key_type const &item)
Erase the item with the given key.
Definition: avl_set:298
Iterator begin()
Get the mutable forward iterator for the first element of the set.
Definition: avl_set:341
GET_KEY::Key_type Key_type
Type of the sort key used for the items.
Definition: avl_set:132
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:130
Avl_set_iter< _Node, Const_item_type, Fwd > Const_iterator
Constant forward iterator for the set.
Definition: avl_set:214
Node lower_bound_node(Key_type const &key) const
Find the first node greater or equal to key.
Definition: avl_set:320
Const_rev_iterator rend() const
Get the end marker for the constant backward iterator.
Definition: avl_set:357
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:134
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:229
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