L4Re - L4 Runtime Environment
avl_map
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  * economic rights: Technische Universität Dresden (Germany)
9  *
10  * This file is part of TUD:OS and distributed under the terms of the
11  * GNU General Public License 2.
12  * Please see the COPYING-GPL-2 file for details.
13  *
14  * As a special exception, you may use this file as part of a free software
15  * library without restriction. Specifically, if other files instantiate
16  * templates or use macros or inline functions from this file, or you compile
17  * this file and link it with other files to produce an executable, this
18  * file does not by itself cause the resulting executable to be covered by
19  * the GNU General Public License. This exception does not however
20  * invalidate any other reasons why the executable file might be covered by
21  * the GNU General Public License.
22  */
23 
24 #pragma once
25 
26 #include <l4/cxx/std_alloc>
27 #include <l4/cxx/std_ops>
28 #include <l4/cxx/pair>
29 #include <l4/cxx/avl_set>
30 
31 namespace cxx {
32 namespace Bits {
33 
35 template<typename KEY_TYPE>
37 {
38  typedef KEY_TYPE Key_type;
39  template<typename NODE>
40  static Key_type const &key_of(NODE const *n)
41  { return n->item.first; }
42 };
43 }
44 
53 template< typename KEY_TYPE, typename DATA_TYPE,
54  template<typename A> class COMPARE = Lt_functor,
55  template<typename B> class ALLOC = New_allocator >
56 class Avl_map :
57  public Bits::Base_avl_set<Pair<KEY_TYPE, DATA_TYPE>,
58  COMPARE<KEY_TYPE>, ALLOC,
59  Bits::Avl_map_get_key<KEY_TYPE> >
60 {
61 private:
65 
66 public:
68  typedef COMPARE<KEY_TYPE> Key_compare;
70  typedef KEY_TYPE Key_type;
72  typedef DATA_TYPE Data_type;
74  typedef typename Base_type::Node Node;
77 
78  typedef typename Base_type::Iterator Iterator;
79  typedef typename Base_type::Iterator iterator;
80  typedef typename Base_type::Const_iterator Const_iterator;
81  typedef typename Base_type::Const_iterator const_iterator;
82  typedef typename Base_type::Rev_iterator Rev_iterator;
83  typedef typename Base_type::Rev_iterator reverse_iterator;
84  typedef typename Base_type::Const_rev_iterator Const_rev_iterator;
85  typedef typename Base_type::Const_rev_iterator const_reverse_iterator;
86 
91  Avl_map(Node_allocator const &alloc = Node_allocator())
92  : Base_type(alloc)
93  {}
94 
110  cxx::Pair<Iterator, int> insert(Key_type const &key, Data_type const &data)
111  { return Base_type::insert(Pair<Key_type, Data_type>(key, data)); }
112 
118  Data_type const &operator [] (Key_type const &key) const
119  { return this->find_node(key)->second; }
120 
130  Data_type &operator [] (Key_type const &key)
131  {
132  Node n = this->find_node(key);
133  if (n)
134  return const_cast<Data_type&>(n->second);
135  else
136  return insert(key, Data_type()).first->second;
137  }
138 };
139 
140 }
141 
Our C++ library.
Definition: arith:22
Key-getter for Avl_map.
Definition: avl_map:36
cxx::Pair< Iterator, int > insert(Key_type const &key, Data_type const &data)
Insert a <key, data> pair into the map.
Definition: avl_map:110
Avl_set_iter< _Node, Item_type, Fwd > Iterator
Forward iterator for the set.
Definition: avl_set:217
Base_type::Node Node
Return type for find.
Definition: avl_map:74
ALLOC< _Node > Node_allocator
Type for the node allocator.
Definition: avl_set:200
AVL set.
COMPARE< KEY_TYPE > Key_compare
Type of the comparison functor.
Definition: avl_map:68
A smart pointer to a tree item.
Definition: avl_set:161
AVL tree based associative container.
Definition: avl_map:56
Avl_set_iter< _Node, Item_type, Rev > Rev_iterator
Backward iterator for the set.
Definition: avl_set:223
Internal: AVL set with internally managed nodes.
Definition: avl_set:117
Avl_map(Node_allocator const &alloc=Node_allocator())
Create an empty AVL-tree based map.
Definition: avl_map:91
Pair implementation.
Avl_set_iter< _Node, Const_item_type, Rev > Const_rev_iterator
Constant backward iterator for the set.
Definition: avl_set:226
Generic comparator class that defaults to the less-than operator.
Definition: std_ops:29
KEY_TYPE Key_type
Type of the key values.
Definition: avl_map:70
Base_type::Node_allocator Node_allocator
Type of the allocator.
Definition: avl_map:76
DATA_TYPE Data_type
Type of the data values.
Definition: avl_map:72
Avl_set_iter< _Node, Const_item_type, Fwd > Const_iterator
Constant forward iterator for the set.
Definition: avl_set:220
Pair of two values.
Definition: pair:36
Standard allocator based on operator new () .
Definition: std_alloc:60