Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
TreeMap.h
1 /*
2  * TreeMap class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2004-07, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_DATA_TREEMAP_H
22 #define ELM_DATA_TREEMAP_H
23 
24 #include <elm/data/TreeBag.h>
25 #include <elm/data/util.h>
26 
27 namespace elm {
28 
29 // SortedBinMap class
30 template <class K, class T, class C = Comparator<K>, class E = Equiv<T>, class A = DefaultAlloc >
31 class TreeMap: public E {
32  typedef Pair<K, T> value_t;
34 public:
35  inline TreeMap() { }
36  inline TreeMap(const TreeMap<K, T, C>& map): tree(map.tree) { }
37  inline const Comparator<K>& comparator() const { return tree.comparator(); }
38  inline Comparator<K>& comparator() { return tree.comparator(); }
39  inline E& equivalence() { return *this; }
40  inline A& allocator() { return tree.allocator(); }
41 
42  // Collection concept
43  inline int count(void) const { return tree.count(); }
44  inline bool contains(const K &k) const { return tree.find(key(k)) != nullptr; }
45  inline bool isEmpty(void) const { return tree.isEmpty(); }
46  inline operator bool(void) const { return !isEmpty(); }
47 
48  class Iter: public PreIterator<Iter, const T&> {
49  friend class TreeMap;
50  public:
51  inline Iter(const TreeMap& map): iter(map.tree) { }
52  inline bool ended(void) const { return iter.ended(); }
53  inline void next(void) { iter.next(); }
54  inline const T &item(void) const { return iter.item().snd; }
55  inline bool equals(const Iter& ii) const { return iter.equals(ii.iter); }
56  private:
57  inline Iter(const typename tree_t::Iter& i): iter(i) { }
58  typename tree_t::Iter iter;
59  };
60  inline Iter begin() const { return Iter(tree.begin()); }
61  inline Iter end() const { return Iter(tree.end()); }
62 
63  // Map concept
64  inline const T& get(const K &key, const T &def) const {
65  const value_t *val = tree.find(pair(key, T()));
66  return val ? val->snd : def;
67  }
68  inline Option<T> get(const K &key) const {
69  const value_t *res = tree.find(pair(key, T()));
70  return res ? Option<T>(res->snd) : none;
71  }
72  inline bool hasKey(const K &k) const { return tree.contains(key(k)); }
73 
74  class KeyIter: public PreIterator<KeyIter, const K&> {
75  friend class TreeMap;
76  public:
77  inline KeyIter(const TreeMap& map): iter(map.tree) { }
78  inline bool ended(void) const { return iter.ended(); }
79  inline void next(void) { iter.next(); }
80  const K &item(void) const { return iter.item().fst; }
81  inline bool equals(const KeyIter& ii) const { return iter.equals(ii.iter); }
82  private:
83  inline KeyIter(const Iter& i): iter(i.iter) { }
84  typename tree_t::Iter iter;
85  };
86  inline Iterable<KeyIter> keys() const { return subiter(KeyIter(*this), KeyIter(end())); }
87 
88  class PairIter: public PreIterator<PairIter, const value_t&> {
89  friend class TreeMap;
90  public:
91  inline PairIter(const TreeMap& map): iter(map.tree) { }
92  inline bool ended(void) const { return iter.ended(); }
93  inline void next(void) { iter.next(); }
94  const value_t &item(void) const { return iter.item(); }
95  inline bool equals(const PairIter& ii) const { return iter.equals(ii.iter); }
96  private:
97  inline PairIter(const Iter& i): iter(i.iter) { }
98  typename tree_t::Iter iter;
99  };
100  inline Iterable<PairIter> pairs() const { return subiter(PairIter(*this), PairIter(end())); }
101 
102  // MutableMap concept
103  inline void put(const K& key, const T& value)
104  { tree.add(value_t(key, value)); }
105  inline void remove(const K& key)
106  { tree.remove(pair(key, T())); }
107  inline void remove(const Iter& iter)
108  { tree.remove(iter.iter); }
109 
110 private:
111  inline Pair<K, T> key(const K& k) const { return pair(k, T()); }
112  tree_t tree;
113 };
114 
115 } // elm
116 
117 #endif // ELM_DATA_TREEMAP_H
elm::TreeMap::get
const T & get(const K &key, const T &def) const
Definition: TreeMap.h:64
elm::Option
Definition: Option.h:35
elm::TreeMap::remove
void remove(const Iter &iter)
Definition: TreeMap.h:107
elm::TreeMap::Iter::next
void next(void)
Definition: TreeMap.h:53
elm::TreeBag
Definition: TreeBag.h:35
elm::TreeBag::comparator
const C & comparator() const
Definition: TreeBag.h:54
elm::iter
Definition: util_WAHVector.cpp:157
elm::TreeMap::allocator
A & allocator()
Definition: TreeMap.h:40
elm::iter
void iter(const C &c, const F &f)
Definition: util.h:95
elm::subiter
Iterable< I > subiter(const I &b, const I &e)
Definition: util.h:231
elm::iter::iter
iter(word_t *current, t::uint32 size)
Definition: util_WAHVector.cpp:159
elm::TreeMap::put
void put(const K &key, const T &value)
Definition: TreeMap.h:103
elm::TreeMap::PairIter::ended
bool ended(void) const
Definition: TreeMap.h:92
elm::TreeMap::count
int count(void) const
Definition: TreeMap.h:43
elm::TreeBag::add
void add(const T &x)
Definition: TreeBag.h:121
elm::pair
Pair< T1, T2 > pair(const T1 &v1, const T2 &v2)
Definition: Pair.h:63
elm::Pair::snd
T2 snd
Definition: Pair.h:36
elm::TreeBag::remove
void remove(const T &x)
Definition: TreeBag.h:150
elm::TreeBag::begin
Iter begin() const
Definition: TreeBag.h:89
elm::TreeBag::end
Iter end() const
Definition: TreeBag.h:90
elm::TreeMap::get
Option< T > get(const K &key) const
Definition: TreeMap.h:68
elm::Equiv::def
static Equiv< T > def
Definition: equiv.h:37
elm::Pair< K, T >
elm::iter::ended
bool ended(void) const
Definition: util_WAHVector.cpp:161
elm::TreeMap::PairIter::item
const value_t & item(void) const
Definition: TreeMap.h:94
value
elm::Iterable
Definition: util.h:220
bool
elm::TreeMap::pairs
Iterable< PairIter > pairs() const
Definition: TreeMap.h:100
elm::map
void map(const C &c, const F &f, D &d)
Definition: util.h:89
elm::TreeMap::Iter::ended
bool ended(void) const
Definition: TreeMap.h:52
elm::TreeMap::Iter::equals
bool equals(const Iter &ii) const
Definition: TreeMap.h:55
elm::TreeMap::Iter::Iter
Iter(const TreeMap &map)
Definition: TreeMap.h:51
elm::TreeMap::contains
bool contains(const K &k) const
Definition: TreeMap.h:44
elm::TreeMap::TreeMap
TreeMap(const TreeMap< K, T, C > &map)
Definition: TreeMap.h:36
elm::TreeMap::hasKey
bool hasKey(const K &k) const
Definition: TreeMap.h:72
elm::TreeMap::begin
Iter begin() const
Definition: TreeMap.h:60
elm::TreeBag::count
int count(void) const
Definition: TreeBag.h:59
elm::TreeMap::PairIter::next
void next(void)
Definition: TreeMap.h:93
elm::TreeMap::PairIter::equals
bool equals(const PairIter &ii) const
Definition: TreeMap.h:95
elm::none
const OptionalNone none
Definition: util_Option.cpp:154
elm
Definition: adapter.h:26
elm::TreeMap::KeyIter::next
void next(void)
Definition: TreeMap.h:79
elm::TreeBag::isEmpty
bool isEmpty(void) const
Definition: TreeBag.h:61
elm::TreeBag::contains
bool contains(const T &x) const
Definition: TreeBag.h:60
elm::TreeBag::allocator
A & allocator()
Definition: TreeBag.h:56
elm::TreeMap::KeyIter
Definition: TreeMap.h:74
elm::TreeMap::PairIter::PairIter
PairIter(const TreeMap &map)
Definition: TreeMap.h:91
elm::TreeMap::remove
void remove(const K &key)
Definition: TreeMap.h:105
elm::TreeMap
Definition: TreeMap.h:31
elm::TreeMap::comparator
Comparator< K > & comparator()
Definition: TreeMap.h:38
elm::TreeMap::comparator
const Comparator< K > & comparator() const
Definition: TreeMap.h:37
elm::TreeMap::equivalence
E & equivalence()
Definition: TreeMap.h:39
elm::iter::next
void next(void)
Definition: util_WAHVector.cpp:162
elm::Comparator< K >
elm::TreeMap::TreeMap
TreeMap()
Definition: TreeMap.h:35
elm::TreeMap::isEmpty
bool isEmpty(void) const
Definition: TreeMap.h:45
elm::TreeMap::PairIter
Definition: TreeMap.h:88
elm::TreeMap::keys
Iterable< KeyIter > keys() const
Definition: TreeMap.h:86
elm::TreeMap::KeyIter::ended
bool ended(void) const
Definition: TreeMap.h:78
elm::TreeMap::Iter::item
const T & item(void) const
Definition: TreeMap.h:54
elm::TreeMap::end
Iter end() const
Definition: TreeMap.h:61
elm::TreeMap::KeyIter::equals
bool equals(const KeyIter &ii) const
Definition: TreeMap.h:81
elm::TreeMap::KeyIter::item
const K & item(void) const
Definition: TreeMap.h:80
elm::TreeBag::find
const T * find(const T &x) const
Definition: TreeBag.h:186
elm::PreIterator
Definition: iter.h:28
elm::TreeBag::Iter
Definition: TreeBag.h:67
elm::TreeMap::Iter
Definition: TreeMap.h:48
elm::TreeMap::KeyIter::KeyIter
KeyIter(const TreeMap &map)
Definition: TreeMap.h:77