| 1 |
#ifndef LCDF_HASHMAP_HH |
| 2 |
#define LCDF_HASHMAP_HH |
| 3 |
#include <assert.h> |
| 4 |
#include <lcdf/hashcode.hh> |
| 5 |
|
| 6 |
// K AND V REQUIREMENTS: |
| 7 |
// |
| 8 |
// K::K() |
| 9 |
// K::operator bool() const |
| 10 |
// Must have (bool)(K()) == false |
| 11 |
// and no k with (bool)k == false is stored. |
| 12 |
// K & K::operator=(const K &) |
| 13 |
// k1 == k2 |
| 14 |
// hashcode_t hashcode(const K &) |
| 15 |
// If hashcode(k1) != hashcode(k2), then k1 != k2. |
| 16 |
// Actually any unsigned integer type may be used. |
| 17 |
// |
| 18 |
// V::V() |
| 19 |
// V & V::operator=(const V &) |
| 20 |
|
| 21 |
template <class K, class V> class _HashMap_const_iterator; |
| 22 |
template <class K, class V> class _HashMap_iterator; |
| 23 |
|
| 24 |
template <class K, class V> |
| 25 |
class HashMap { public: |
| 26 |
|
| 27 |
HashMap(); |
| 28 |
explicit HashMap(const V &); |
| 29 |
HashMap(const HashMap<K, V> &); |
| 30 |
~HashMap() { delete[] _e; } |
| 31 |
|
| 32 |
int size() const { return _n; } |
| 33 |
bool empty() const { return _n == 0; } |
| 34 |
int capacity() const { return _capacity; } |
| 35 |
const V &default_value() const { return _default_value; } |
| 36 |
void set_default_value(const V &v) { _default_value = v; } |
| 37 |
|
| 38 |
typedef _HashMap_const_iterator<K, V> const_iterator; |
| 39 |
typedef _HashMap_iterator<K, V> iterator; |
| 40 |
|
| 41 |
inline const_iterator begin() const; |
| 42 |
inline iterator begin(); |
| 43 |
inline const_iterator end() const; |
| 44 |
inline iterator end(); |
| 45 |
|
| 46 |
inline const V &find(const K &) const; |
| 47 |
inline V *findp(const K &) const; |
| 48 |
inline const V &operator[](const K &k) const; |
| 49 |
V &find_force(const K &, const V &); |
| 50 |
inline V &find_force(const K &); |
| 51 |
|
| 52 |
bool insert(const K &, const V &); |
| 53 |
void clear(); |
| 54 |
|
| 55 |
HashMap<K, V> &operator=(const HashMap<K, V> &); |
| 56 |
void swap(HashMap<K, V> &); |
| 57 |
|
| 58 |
void resize(int size) { increase(size); } |
| 59 |
|
| 60 |
struct Pair { |
| 61 |
K key; |
| 62 |
V value; |
| 63 |
Pair() : key(), value() { } |
| 64 |
}; |
| 65 |
|
| 66 |
private: |
| 67 |
|
| 68 |
int _capacity; |
| 69 |
int _grow_limit; |
| 70 |
int _n; |
| 71 |
Pair *_e; |
| 72 |
V _default_value; |
| 73 |
|
| 74 |
void increase(int); |
| 75 |
inline void check_capacity(); |
| 76 |
inline int bucket(const K &) const; |
| 77 |
|
| 78 |
friend class _HashMap_const_iterator<K, V>; |
| 79 |
friend class _HashMap_iterator<K, V>; |
| 80 |
|
| 81 |
}; |
| 82 |
|
| 83 |
template <class K, class V> |
| 84 |
class _HashMap_const_iterator { public: |
| 85 |
typedef _HashMap_const_iterator const_iterator; |
| 86 |
typedef typename HashMap<K, V>::Pair Pair; |
| 87 |
|
| 88 |
operator bool() const { return _pos < _hm->_capacity; } |
| 89 |
bool operator!() const { return _pos >= _hm->_capacity; } |
| 90 |
|
| 91 |
void operator++(int); |
| 92 |
void operator++() { (*this)++; } |
| 93 |
|
| 94 |
const K &key() const { return _hm->_e[_pos].key; } |
| 95 |
const V &value() const { return _hm->_e[_pos].value; } |
| 96 |
const Pair &pair() const { return _hm->_e[_pos]; } |
| 97 |
|
| 98 |
inline bool operator==(const const_iterator &) const; |
| 99 |
inline bool operator!=(const const_iterator &) const; |
| 100 |
|
| 101 |
private: |
| 102 |
const HashMap<K, V> *_hm; |
| 103 |
int _pos; |
| 104 |
_HashMap_const_iterator(const HashMap<K, V> *, int); |
| 105 |
friend class HashMap<K, V>; |
| 106 |
friend class _HashMap_iterator<K, V>; |
| 107 |
}; |
| 108 |
|
| 109 |
template <class K, class V> |
| 110 |
class _HashMap_iterator : public _HashMap_const_iterator<K, V> { public: |
| 111 |
typedef _HashMap_iterator iterator; |
| 112 |
|
| 113 |
V &value() const { return this->_hm->_e[this->_pos].value; } |
| 114 |
|
| 115 |
private: |
| 116 |
_HashMap_iterator(const HashMap<K, V> *hm, int pos) : _HashMap_const_iterator<K, V>(hm, pos) { } |
| 117 |
friend class HashMap<K, V>; |
| 118 |
}; |
| 119 |
|
| 120 |
|
| 121 |
template <class K, class V> |
| 122 |
inline int |
| 123 |
HashMap<K, V>::bucket(const K &key) const |
| 124 |
{ |
| 125 |
assert(key); |
| 126 |
hashcode_t hc = hashcode(key); |
| 127 |
int i = hc & (_capacity - 1); |
| 128 |
int j = ((hc >> 6) & (_capacity - 1)) | 1; |
| 129 |
|
| 130 |
while (_e[i].key && !(_e[i].key == key)) |
| 131 |
i = (i + j) & (_capacity - 1); |
| 132 |
|
| 133 |
return i; |
| 134 |
} |
| 135 |
|
| 136 |
template <class K, class V> |
| 137 |
inline const V & |
| 138 |
HashMap<K, V>::find(const K &key) const |
| 139 |
{ |
| 140 |
int i = bucket(key); |
| 141 |
const V *v = (_e[i].key ? &_e[i].value : &_default_value); |
| 142 |
return *v; |
| 143 |
} |
| 144 |
|
| 145 |
template <class K, class V> |
| 146 |
inline const V & |
| 147 |
HashMap<K, V>::operator[](const K &key) const |
| 148 |
{ |
| 149 |
return find(key); |
| 150 |
} |
| 151 |
|
| 152 |
template <class K, class V> |
| 153 |
inline V * |
| 154 |
HashMap<K, V>::findp(const K &key) const |
| 155 |
{ |
| 156 |
int i = bucket(key); |
| 157 |
return _e[i].key ? &_e[i].value : 0; |
| 158 |
} |
| 159 |
|
| 160 |
template <class K, class V> |
| 161 |
inline V & |
| 162 |
HashMap<K, V>::find_force(const K &key) |
| 163 |
{ |
| 164 |
return find_force(key, _default_value); |
| 165 |
} |
| 166 |
|
| 167 |
template <class K, class V> |
| 168 |
inline _HashMap_const_iterator<K, V> |
| 169 |
HashMap<K, V>::begin() const |
| 170 |
{ |
| 171 |
return const_iterator(this, 0); |
| 172 |
} |
| 173 |
|
| 174 |
template <class K, class V> |
| 175 |
inline _HashMap_const_iterator<K, V> |
| 176 |
HashMap<K, V>::end() const |
| 177 |
{ |
| 178 |
return const_iterator(this, _capacity); |
| 179 |
} |
| 180 |
|
| 181 |
template <class K, class V> |
| 182 |
inline _HashMap_iterator<K, V> |
| 183 |
HashMap<K, V>::begin() |
| 184 |
{ |
| 185 |
return iterator(this, 0); |
| 186 |
} |
| 187 |
|
| 188 |
template <class K, class V> |
| 189 |
inline _HashMap_iterator<K, V> |
| 190 |
HashMap<K, V>::end() |
| 191 |
{ |
| 192 |
return iterator(this, _capacity); |
| 193 |
} |
| 194 |
|
| 195 |
template <class K, class V> |
| 196 |
inline bool |
| 197 |
_HashMap_const_iterator<K, V>::operator==(const const_iterator &i) const |
| 198 |
{ |
| 199 |
return _hm == i._hm && _pos == i._pos; |
| 200 |
} |
| 201 |
|
| 202 |
template <class K, class V> |
| 203 |
inline bool |
| 204 |
_HashMap_const_iterator<K, V>::operator!=(const const_iterator &i) const |
| 205 |
{ |
| 206 |
return _hm != i._hm || _pos != i._pos; |
| 207 |
} |
| 208 |
|
| 209 |
#include <lcdf/hashmap.cc> // necessary to support GCC 3.3 |
| 210 |
#endif |