Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
HashMap.h
1 /*
2  * HashMap class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2016, 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_HASHMAP_H_
22 #define ELM_DATA_HASHMAP_H_
23 #define ELM_STAT
24 
25 #include "HashTable.h"
26 #include "util.h"
27 #include <elm/delegate.h>
28 
29 namespace elm {
30 
31 template <class K, class T, class H = HashKey<K>, class A = DefaultAlloc, class E = Equiv<T> >
32 class HashMap: public E {
34 public:
35  typedef K key_t;
36  typedef T val_t;
38 
39  inline HashMap(int _size = 211): _tab(_size) { }
40  inline HashMap(const self_t& h): _tab(h._tab) { }
41  inline const H& hash() const { return _tab.hash().keyHash(); }
42  inline H& hash() { return _tab.hash().keyHash(); }
43  inline const A& allocator() const { return _tab.allocator(); }
44  inline A& allocator() { return _tab.allocator(); }
45  inline const E& equivalence() const { return *this; }
46  inline E& equivalence() { return *this; }
47 
48  inline void clear(void) { _tab.clear(); }
49  inline void add(const K& key, const T& val) { _tab.add(pair(key, val)); }
50 
51  inline T& fetch(const K& k)
52  { auto *n = _tab.get(key(k)); if(n != nullptr) return n->snd; return _tab.add(pair(k, T()))->snd; }
53 
54  // Map concept
55  inline Option<T> get(const K& k) const
56  { auto *r = _tab.get(key(k)); if(r) return some(r->snd); else return none; }
57  inline const T& get(const K& k, const T& def) const
58  { auto p = key(k); auto r = _tab.get(p); if(r) return r->snd; else return def; }
59  inline bool hasKey(const K& k) const { return _tab.hasKey(key(k)); }
60 
61  inline Option<T> get_const(const K& k) const
62  { auto *r = _tab.get_const(key(k)); if(r) return some(r->snd); else return none; }
63  inline const T& get_const(const K& k, const T& def) const
64  { auto r = _tab.get_const(key(k)); if(r) return r->snd; else return def; }
65  inline bool hasKey_const(const K& k) const { return _tab.hasKey_const(key(k)); }
66 
67  class KeyIter: public InplacePreIterator<KeyIter, K> {
68  public:
69  inline KeyIter(const self_t& htab): i(htab._tab) { };
70  inline KeyIter(const self_t& htab, bool end): i(htab._tab, end) { };
71  inline bool ended(void) const { return i.ended(); }
72  inline const K& item(void) const { return i.item().fst; }
73  inline void next(void) { i.next(); }
74  inline bool equals(const KeyIter& it) const { return i.equals(it.i); }
75  private:
76  typename tab_t::Iter i;
77  };
78  inline Iterable<KeyIter> keys() const { return subiter(KeyIter(*this), KeyIter(*this, true)); }
79 
80  class PairIter: public InplacePreIterator<PairIter, Pair<K, T> > {
81  public:
82  inline PairIter(const self_t& htab): i(htab._tab) { };
83  inline PairIter(const self_t& htab, bool end): i(htab._tab, end) { };
84  inline bool ended(void) const { return i.ended(); }
85  inline const Pair<K, T>& item(void) const { return i.item(); }
86  inline void next(void) { i.next(); }
87  inline bool equals(const PairIter& it) const { return i.equals(it.i); }
88  private:
89  typename tab_t::Iter i;
90  };
91  inline Iterable<PairIter> pairs() const { return subiter(PairIter(*this), PairIter(*this, true)); }
92 
93  // Collection concept
94  inline int count() const { return _tab.count(); }
95  inline bool isEmpty() const { return _tab.isEmpty(); }
96  inline operator bool() const { return !isEmpty(); }
97 
98  class Iter: public InplacePreIterator<Iter, T> {
99  friend class HashMap;
100  public:
101  inline Iter(const self_t& htab): i(htab._tab) { };
102  inline Iter(const self_t& htab, bool end): i(htab._tab, end) { };
103  inline bool ended(void) const { return i.ended(); }
104  inline const T& item(void) const { return i.item().snd; }
105  inline void next(void) { i.next(); }
106  inline const K& key(void) const { return i.item().fst; }
107  inline bool equals(const Iter& it) const { return i.equals(it.i); }
108  private:
109  typename tab_t::Iter i;
110  };
111  inline Iter begin(void) const { return Iter(*this); }
112  inline Iter end(void) const { return Iter(*this, true); }
113 
114  bool contains(const T& item) const
115  { for(const auto x: *this) if(x == item) return true; return false; }
116  template <class C> bool containsAll(const C& c) const
117  { for(typename C::Iter i(c); c; i++) if(!contains(*i)) return false; return true; }
118 
119  inline bool equals(const HashMap<K, T>& t) const
120  { return containsAll(t) && t.containsAll(*this); }
121  inline bool operator==(const HashMap<K, T>& t) const { return equals(t); }
122  inline bool operator!=(const HashMap<K, T>& t) const { return !equals(t); }
123 
124  inline bool includes(const HashMap<K, T>& t) const
125  { return containsAll(t); }
126  inline bool operator <=(const HashMap<K, T>& t) const { return t.contains(*this); }
127  inline bool operator >=(const HashMap<K, T>& t) const { return contains(t); }
128 
129  inline bool operator<(const HashMap<K, T>& t) const { return !equals(t) && t.contains(*this); }
130  inline bool operator>(const HashMap<K, T>& t) const { return !equals(t) && contains(t); }
131 
132  // MutableMap concept
133  inline void put(const K& key, const T& val) { _tab.put(pair(key, val)); }
134  inline void remove(const K& k) { _tab.remove(key(k)); }
135  inline void remove(const Iter& i) { _tab.remove(i.i); }
136 
137  inline const T& operator[](const K& k) const { auto *r = _tab.get(key(k)); ASSERT(r); return (*r).snd; }
138  inline StrictMapDelegate<self_t> operator[](const K& key) { return StrictMapDelegate<self_t>(*this, key); }
139  inline const T& operator[](const Iter& i) const { auto *r = _tab.get(key(i.key())); ASSERT(r); return (*r).snd; }
140  inline StrictMapDelegate<self_t> operator[](const Iter& i) { return StrictMapDelegate<self_t>(*this, i.key()); }
141 
142  template <class C> void putAll(const C& c)
143  { for(auto p: c.pairs()) put(p.fst, p.snd); }
144 
145 # ifdef ELM_STAT
146  int minEntry(void) const { return _tab.minEntry(); }
147  int maxEntry(void) const { return _tab.maxEntry(); }
148  int zeroEntry(void) const { return _tab.zeroEntry(); }
149  int size(void) const { return _tab.size(); }
150 # endif
151 
152  // deprecated
153  inline bool exists(const K& k) const { return hasKey(k); }
154  inline Iter operator*(void) const { return begin(); }
155 
156 private:
157  inline Pair<K, T> key(const K& k) const { return pair(k, T()); }
158  tab_t _tab;
159 };
160 
161 } // otawa
162 
163 #endif /* ELM_DATA_HASHTABLE_H_ */
elm::Option
Definition: Option.h:35
elm::HashMap::PairIter::equals
bool equals(const PairIter &it) const
Definition: HashMap.h:87
elm::HashMap::get
const T & get(const K &k, const T &def) const
Definition: HashMap.h:57
elm::HashMap::remove
void remove(const K &k)
Definition: HashMap.h:134
elm::HashMap::val_t
T val_t
Definition: HashMap.h:36
elm::HashMap::Iter::key
const K & key(void) const
Definition: HashMap.h:106
elm::HashMap::add
void add(const K &key, const T &val)
Definition: HashMap.h:49
elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::HashMap::putAll
void putAll(const C &c)
Definition: HashMap.h:142
elm::HashMap::count
int count() const
Definition: HashMap.h:94
elm::HashMap
Definition: HashMap.h:32
elm::HashTable::hasKey_const
bool hasKey_const(const T &key) const
Definition: HashTable.h:109
elm::subiter
Iterable< I > subiter(const I &b, const I &e)
Definition: util.h:231
elm::HashMap::Iter::next
void next(void)
Definition: HashMap.h:105
elm::HashMap::operator!=
bool operator!=(const HashMap< K, T > &t) const
Definition: HashMap.h:122
elm::HashMap::PairIter::item
const Pair< K, T > & item(void) const
Definition: HashMap.h:85
elm::HashMap::equals
bool equals(const HashMap< K, T > &t) const
Definition: HashMap.h:119
elm::HashMap::put
void put(const K &key, const T &val)
Definition: HashMap.h:133
elm::HashMap::Iter::item
const T & item(void) const
Definition: HashMap.h:104
elm::pair
Pair< T1, T2 > pair(const T1 &v1, const T2 &v2)
Definition: Pair.h:63
elm::HashMap::operator[]
const T & operator[](const Iter &i) const
Definition: HashMap.h:139
elm::HashMap::operator[]
StrictMapDelegate< self_t > operator[](const K &key)
Definition: HashMap.h:138
elm::HashMap::KeyIter::KeyIter
KeyIter(const self_t &htab, bool end)
Definition: HashMap.h:70
elm::HashMap::KeyIter::next
void next(void)
Definition: HashMap.h:73
elm::Equiv::def
static Equiv< T > def
Definition: equiv.h:37
elm::HashMap::PairIter::next
void next(void)
Definition: HashMap.h:86
elm::HashTable::hash
const H & hash() const
Definition: HashTable.h:98
elm::HashMap::containsAll
bool containsAll(const C &c) const
Definition: HashMap.h:116
elm::Pair< K, T >
elm::HashTable::Iter
Definition: HashTable.h:135
elm::HashMap::get
Option< T > get(const K &k) const
Definition: HashMap.h:55
elm::HashMap::equivalence
E & equivalence()
Definition: HashMap.h:46
elm::HashMap::PairIter::PairIter
PairIter(const self_t &htab, bool end)
Definition: HashMap.h:83
elm::HashTable::allocator
const A & allocator() const
Definition: HashTable.h:100
elm::some
Option< T > some(const T &val)
Definition: Option.h:81
void
elm::HashMap::KeyIter::item
const K & item(void) const
Definition: HashMap.h:72
elm::HashMap::zeroEntry
int zeroEntry(void) const
Definition: HashMap.h:148
elm::Iterable
Definition: util.h:220
bool
elm::AssocHashKey
Definition: hash.h:137
elm::HashTable::put
void put(const T &data)
Definition: HashTable.h:114
elm::HashMap::end
Iter end(void) const
Definition: HashMap.h:112
elm::HashMap::maxEntry
int maxEntry(void) const
Definition: HashMap.h:147
elm::HashMap::exists
bool exists(const K &k) const
Definition: HashMap.h:153
elm::HashTable::Iter::item
const T & item(void) const
Definition: HashTable.h:139
elm::HashTable::get
const T * get(const T &key) const
Definition: HashTable.h:103
elm::HashMap::KeyIter::equals
bool equals(const KeyIter &it) const
Definition: HashMap.h:74
elm::HashMap::operator*
Iter operator*(void) const
Definition: HashMap.h:154
elm::HashMap::KeyIter::KeyIter
KeyIter(const self_t &htab)
Definition: HashMap.h:69
elm::none
const OptionalNone none
Definition: util_Option.cpp:154
elm::HashMap::PairIter
Definition: HashMap.h:80
elm
Definition: adapter.h:26
elm::HashMap::pairs
Iterable< PairIter > pairs() const
Definition: HashMap.h:91
elm::HashMap::operator<
bool operator<(const HashMap< K, T > &t) const
Definition: HashMap.h:129
elm::HashTable::hasKey
bool hasKey(const T &key) const
Definition: HashTable.h:107
elm::HashMap::equivalence
const E & equivalence() const
Definition: HashMap.h:45
elm::HashMap::includes
bool includes(const HashMap< K, T > &t) const
Definition: HashMap.h:124
elm::HashMap::HashMap
HashMap(int _size=211)
Definition: HashMap.h:39
elm::HashMap::isEmpty
bool isEmpty() const
Definition: HashMap.h:95
elm::HashMap::Iter::ended
bool ended(void) const
Definition: HashMap.h:103
elm::HashMap::hasKey
bool hasKey(const K &k) const
Definition: HashMap.h:59
elm::HashMap::KeyIter
Definition: HashMap.h:67
elm::HashMap::minEntry
int minEntry(void) const
Definition: HashMap.h:146
elm::HashMap::allocator
const A & allocator() const
Definition: HashMap.h:43
elm::HashMap::allocator
A & allocator()
Definition: HashMap.h:44
elm::HashMap::get_const
Option< T > get_const(const K &k) const
Definition: HashMap.h:61
elm::HashMap::Iter::equals
bool equals(const Iter &it) const
Definition: HashMap.h:107
elm::HashMap::Iter
Definition: HashMap.h:98
elm::HashMap::Iter::Iter
Iter(const self_t &htab)
Definition: HashMap.h:101
elm::HashMap::operator==
bool operator==(const HashMap< K, T > &t) const
Definition: HashMap.h:121
elm::HashMap::fetch
T & fetch(const K &k)
Definition: HashMap.h:51
elm::HashMap::operator>=
bool operator>=(const HashMap< K, T > &t) const
Definition: HashMap.h:127
elm::HashMap::operator>
bool operator>(const HashMap< K, T > &t) const
Definition: HashMap.h:130
elm::HashMap::contains
bool contains(const T &item) const
Definition: HashMap.h:114
elm::HashTable::get_const
const T * get_const(const T &key) const
Definition: HashTable.h:105
elm::HashTable::count
int count(void) const
Definition: HashTable.h:124
elm::HashMap::get_const
const T & get_const(const K &k, const T &def) const
Definition: HashMap.h:63
elm::HashMap::size
int size(void) const
Definition: HashMap.h:149
elm::HashMap::operator<=
bool operator<=(const HashMap< K, T > &t) const
Definition: HashMap.h:126
elm::HashMap::remove
void remove(const Iter &i)
Definition: HashMap.h:135
elm::HashTable::clear
void clear(void)
Definition: HashTable.h:152
elm::HashTable::remove
void remove(const T &key)
Definition: HashTable.h:166
elm::HashTable
Definition: HashTable.h:32
elm::HashMap::hash
const H & hash() const
Definition: HashMap.h:41
elm::HashTable::add
T * add(const T &data)
Definition: HashTable.h:159
elm::HashMap::hasKey_const
bool hasKey_const(const K &k) const
Definition: HashMap.h:65
elm::StrictMapDelegate
Definition: delegate.h:95
elm::InplacePreIterator
Definition: iter.h:49
elm::HashMap::key_t
K key_t
Definition: HashMap.h:35
elm::HashMap::KeyIter::ended
bool ended(void) const
Definition: HashMap.h:71
elm::HashMap::begin
Iter begin(void) const
Definition: HashMap.h:111
elm::HashMap::HashMap
HashMap(const self_t &h)
Definition: HashMap.h:40
elm::HashMap::operator[]
const T & operator[](const K &k) const
Definition: HashMap.h:137
elm::HashMap::Iter::Iter
Iter(const self_t &htab, bool end)
Definition: HashMap.h:102
elm::HashMap::PairIter::ended
bool ended(void) const
Definition: HashMap.h:84
elm::HashMap::keys
Iterable< KeyIter > keys() const
Definition: HashMap.h:78
elm::HashMap::operator[]
StrictMapDelegate< self_t > operator[](const Iter &i)
Definition: HashMap.h:140
elm::HashMap::self_t
HashMap< K, T, H, A, E > self_t
Definition: HashMap.h:37
elm::HashMap::hash
H & hash()
Definition: HashMap.h:42
elm::HashMap::PairIter::PairIter
PairIter(const self_t &htab)
Definition: HashMap.h:82
elm::HashMap::clear
void clear(void)
Definition: HashMap.h:48
elm::HashTable::isEmpty
bool isEmpty(void) const
Definition: HashTable.h:121