16#include <l4/cxx/std_alloc>
17#include <l4/cxx/std_ops>
18#include <l4/cxx/type_traits>
34template<
typename Node,
typename Key,
typename Node_op>
39 typedef __Bst_iter_b<Node, Node_op> Base;
42 typedef typename Type_traits<Key>::Non_const_type Non_const_key;
73 { Base::operator = (o);
return *
this; }
80 Key &
operator * ()
const {
return const_cast<Node*
>(_n)->item; }
87 Key *
operator -> ()
const {
return &
const_cast<Node*
>(_n)->item; }
102template<
typename KEY_TYPE>
105 typedef KEY_TYPE Key_type;
106 template<
typename NODE>
107 static Key_type
const &key_of(NODE
const *n)
124template<
typename ITEM_TYPE,
class COMPARE,
125 template<
typename A>
class ALLOC,
129 friend struct ::Avl_set_tester;
173 template<
typename ...ARGS>
174 _Node(ARGS &&...args) :
Avl_tree_node(), item(cxx::forward<ARGS>(args)...)
185 friend class Base_avl_set<ITEM_TYPE, COMPARE, ALLOC, GET_KEY>;
189 explicit Node(_Node
const *n) : _n(n) {}
217 operator Item_type const * () {
return _n ? &_n->item : 0; }
246 typedef typename Type_traits<Item_type>::Param_type Item_param_type;
269 : _tree(), _alloc(alloc)
285 : _tree(), _alloc(alloc)
343 template<
typename... Args>
351 _tree.remove_all([
this](_Node *n)
368 _Node *n = _tree.remove(item);
396 {
return Node(_tree.find_node(item)); }
407 {
return Node(_tree.lower_bound_node(key)); }
469 {
return _tree.
find(item); }
472 bool rec_dump(
bool print)
474 return _tree.rec_dump(print);
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>
487 _Node *n = _alloc.alloc();
491 new (n,
Nothrow()) _Node(item);
503template<
typename Item,
class Compare,
template<
typename A>
class Alloc,
typename KEY_TYPE>
504template<
typename... Args>
508 _Node *n = _alloc.alloc();
510 return cxx::pair(end(), -E_nomem);
512 new (n,
Nothrow()) _Node(cxx::forward<Args>(args)...);
520 return cxx::pair(Iterator(
typename Tree::Iterator(err.
first, err.
first)), err.
second ? 0 : -E_exist);
536template<
typename ITEM_TYPE,
template<
typename T>
class COMPARE =
Lt_functor,
540 Bits::Avl_set_get_key<ITEM_TYPE>>
549 Avl_set(Node_allocator
const &alloc)
Avl_tree_node()=default
Create an uninitialized node, this is what you should do.
Pair< Node *, bool > insert(Node *new_node)
Insert a new node into this AVL tree.
Generic iterator for the AVL-tree based set.
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.
Key * operator->() const
Member access to the item the iterator points to.
Avl_set_iter(Non_const_iter const &o)
Allow copy of non-const iterator to const iterator versions.
Avl_set_iter(Node const *t)
Create an iterator for the given tree.
Avl_set_iter & operator++()
Set the iterator to the next element (pre increment).
Avl_set_iter(Base const &o)
Create an iterator from a BST iterator.
Key & operator*() const
Dereference the iterator and get the item out of the tree.
bool valid() const
Validity check.
Item_type const * operator->()
Dereferenced member access.
Node()
Default construction for NIL pointer.
Item_type const & operator*()
Dereference the pointer.
AVL set with internally managed nodes.
Avl_set_iter< _Node, Item_type, Fwd > Iterator
Node find_node(Key_type const &item) const
Lookup a node equal to item.
Avl_set_iter< _Node, Item_type, Rev > Rev_iterator
Const_rev_iterator rbegin() const
Get the constant backward iterator for the last element in the set.
Iterator end()
Get the end marker for the mutable forward iterator.
Rev_iterator rbegin()
Get the mutable backward iterator for the last element of the set.
Base_avl_set & operator=(Base_avl_set const &o)
Copy assignment operator of an AVL-tree based set.
Bits::Avl_map_get_key< KEY_TYPE >::Key_type Key_type
Local_item_type Item_type
int remove(Key_type const &item)
Remove an item from the set.
Base_avl_set(Node_allocator const &alloc=Node_allocator())
Create a AVL-tree based set.
Const_iterator end() const
Get the end marker for the constant forward iterator.
Const_rev_iterator rend() const
Get the end marker for the constant backward iterator.
Const_iterator begin() const
Get the constant forward iterator for the first element in the set.
ALLOC< _Node > Node_allocator
Rev_iterator rend()
Get the end marker for the mutable backward iterator.
Iterator begin()
Get the mutable forward iterator for the first element of the set.
Avl_set_iter< _Node, Const_item_type, Rev > Const_rev_iterator
Bits::Avl_map_get_key< KEY_TYPE > Get_key
cxx::Pair< Iterator, int > insert(Item_type const &item)
Insert an item into the set.
Type_traits< Item_type >::Const_type Const_item_type
int erase(Key_type const &item)
Erase the item with the given key.
void clear() noexcept
Remove all items from the set.
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.
Avl_set_iter< _Node, Const_item_type, Fwd > Const_iterator
Node lower_bound_node(Key_type const &key) const
Find the first node greater or equal to key.
Node * lower_bound_node(Key_param_type key) const
Find the first node with a key not less than the given key.
Rev Rev_iter_ops
Helper for building reverse iterators for different wrapper classes.
Const_iterator find(Key_param_type key) const
find the node with the given key.
Fwd Fwd_iter_ops
Helper for building forward iterators for different wrapper classes.
Standard allocator based on operator new () .
Helper type to distinguish the operator new version that does not throw exceptions.
Internal helpers for the cxx package.
Internal, key-getter for Avl_set nodes.
Generic comparator class that defaults to the less-than operator.
Second second
Second value.