22 #ifndef ELM_GENSTRUCT_HASHTABLE_H
23 #define ELM_GENSTRUCT_HASHTABLE_H
25 #include <elm/util/Pair.h>
26 #include <elm/PreIterator.h>
27 #include <elm/util/HashKey.h>
28 #include <elm/type_info.h>
30 namespace elm {
namespace genstruct {
33 template <
class K,
class T,
class H = HashKey<K> >
36 typedef struct node_t {
45 node_t *find(
const K& key) {
46 int i = H::hash(key) % size;
47 for(node_t *node = tab[i], *prev = 0; node; prev = node, node = node->next)
48 if(H::equals(node->key, key)) {
49 if(prev) { prev->next = node->next; node->next = tab[i]; tab[i] = node; }
55 node_t *make(
const K& key,
const T&
value)
56 {
int i = H::hash(key) % size; node_t *node =
new node_t; node->next = tab[i];
57 tab[i] = node; node->key = key; node->value =
value;
return node; }
60 class InternIterator {
62 inline InternIterator(
const HashTable<K, T, H>& _htab): htab(&_htab) { i = 0; step(); }
63 inline InternIterator(
const InternIterator& it): node(it.node), htab(it.htab), i(it.i) { }
64 inline InternIterator& operator=(
const InternIterator& it) { htab = it.htab; i = it.i; node = it.node; }
65 inline bool ended(
void)
const {
return i >= htab->size; }
66 inline void next(
void) { node = node->next;
if(!node) { i++; step(); } }
70 inline void step(
void) {
for(; i < htab->size; i++)
if(htab->tab[i]) { node = htab->tab[i];
break; } }
78 inline operator const K&(void)
const {
return get(); }
79 inline const T& operator*(
void)
const {
return get(); };
80 inline T& operator=(
const T& v)
const {
81 node_t *node = t.find(k);
87 inline const T&
get(void)
const { node_t *node = t.find(k); ASSERTP(node,
"key " << k <<
" not in hashtab");
return node->value; }
93 HashTable(
int _size = 211): size(_size), tab(new node_t *[_size])
94 {
for(
int i = 0; i < size; i++) tab[i] = 0; }
96 {
clear();
delete [] tab; }
99 {
for(
int i = 0; i <size; i++)
if(tab[i])
return false;
return true; }
101 {
int cnt = 0;
for(
int i = 0; i < size; i++)
for(node_t *cur = tab[i]; cur; cur = cur->next) cnt++;
return cnt; }
105 inline const T
get(
const K& key,
const T& def_value)
106 { node_t *node = find(key);
return node ?
type_info<T>::get(node->value) : def_value; }
108 { node_t *node = find(key);
return node != 0; }
113 void put(
const K& key,
const T& value)
114 { node_t *node = find(key);
if(node) node->value =
value;
else add(key, value); }
115 void add(
const K& key,
const T& value) { make(key, value); }
119 void remove(
const K& key) {
120 int i = H::hash(key) % size;
121 for(node_t *node = tab[i], *prev = 0; node; prev = node, node = node->next)
124 prev->next = node->next;
133 for(
int i = 0; i < size; i++) {
134 for(node_t *cur = tab[i], *next; cur; cur = next) { next = cur->next;
delete cur; }
178 for (node = htab.tab[i]; node && (node->key != key); node = node->next)
180 ASSERT(node != NULL); }
181 inline bool ended(
void)
const {
return node == 0; }
184 inline const T&
item(
void)
const {
return this->node->value; }
191 #endif // ELM_GENSTRUCT_HASHTABLE_H
const K & item(void) const
Definition: HashTable.h:145
const K & key(void) const
Definition: HashTable.h:156
Pair< K, T > item(void) const
Definition: HashTable.h:165
Definition: PreIterator.h:29
KeyIterator & operator=(const KeyIterator &it)
Definition: HashTable.h:144
PairIterator(const HashTable< K, T, H > &htab)
Definition: HashTable.h:162
int count(void) const
Definition: HashTable.h:100
Pair< T1, T2 > pair(const T1 &v1, const T2 &v2)
Definition: Pair.h:41
static const T & get(const T &v)
Definition: type_info.h:49
bool hasKey(const K &key)
Definition: HashTable.h:107
const T & useItem(void) const
Definition: HashTable.h:155
void putAll(const HashTable< K, T, H > &htab)
Definition: HashTable.h:116
Ref operator[](const K &key)
Definition: HashTable.h:111
HashTable(int _size=211)
Definition: HashTable.h:93
static void put(T &l, const T &v)
Definition: type_info.h:48
KeyIterator(const KeyIterator &it)
Definition: HashTable.h:143
bool exists(const K &key)
Definition: HashTable.h:109
bool isEmpty(void) const
Definition: HashTable.h:98
Definition: type_info.h:70
const T & item(void) const
Definition: HashTable.h:154
value_t value(CString name, int value)
Definition: rtti.h:40
Iterator(const HashTable< K, T, H > &htab)
Definition: HashTable.h:151
Iterator & operator=(const Iterator &it)
Definition: HashTable.h:153
const T & item(void) const
Definition: HashTable.h:184
void put(const K &key, const T &value)
Definition: HashTable.h:113
KeyIterator(const HashTable< K, T, H > &htab)
Definition: HashTable.h:142
~HashTable(void)
Definition: HashTable.h:95
SameKeyIterator(const HashTable< K, T, H > &_htab, const K &_key)
Definition: HashTable.h:175
Definition: HashTable.h:169
void clear(void)
Definition: HashTable.h:132
Iterator(const Iterator &it)
Definition: HashTable.h:152
void next(void)
Definition: HashTable.h:182
void add(const K &key, const T &value)
Definition: HashTable.h:115
bool ended(void) const
Definition: HashTable.h:181
Definition: HashTable.h:149
PairIterator(const PairIterator &it)
Definition: HashTable.h:163
PairIterator & operator=(const PairIterator &it)
Definition: HashTable.h:164
Definition: HashTable.h:160
Definition: HashTable.h:140
Definition: HashTable.h:34