34 template <
class KeyType,
class ItemType>
37 const ItemType &item) :
44 template <
class KeyType,
class ItemType>
45 std::ostream &
operator<<(std::ostream &out,
const AbaHashItem<KeyType, ItemType> &rhs)
47 return out <<
'(' << rhs.key_ <<
',' << rhs.item_ <<
')';
51 template <
class KeyType,
class ItemType>
52 inline AbaHashItem<KeyType, ItemType> * AbaHashItem<KeyType,
59 template <
class KeyType,
class ItemType>
60 AbaHash<KeyType, ItemType>::AbaHash(
int size)
61 : size_(
size), nCollisions_(0), iter_(nullptr)
63 table_ =
new AbaHashItem<KeyType, ItemType>* [
size];
65 for (
int i = 0; i <
size; i++)
70 template <
class KeyType,
class ItemType>
71 AbaHash<KeyType, ItemType>::~AbaHash()
73 AbaHashItem<KeyType, ItemType> *h1;
74 AbaHashItem<KeyType, ItemType> *h2;
77 for (i = 0; i < size_; i++) {
89 template <
class KeyType,
class ItemType>
90 std::ostream &
operator<<(std::ostream &out,
const AbaHash<KeyType, ItemType> &
hash)
92 AbaHashItem<KeyType, ItemType> *h;
93 const int s =
hash.size();
95 for (
int i = 0; i < s; i++) {
110 template <
class KeyType,
class ItemType>
111 void AbaHash<KeyType, ItemType>::insert(
113 const ItemType &item)
115 AbaHashItem<KeyType, ItemType> *h =
new AbaHashItem<KeyType, ItemType>(key, item);
116 int slotNum = hf(key);
118 if (table_[slotNum]) ++nCollisions_;
119 h->next_ = table_[slotNum];
124 template <
class KeyType,
class ItemType>
125 void AbaHash<KeyType, ItemType>::overWrite(
127 const ItemType &item)
130 int slotNum = hf(key);
131 if (table_[slotNum]) ++nCollisions_;
133 AbaHashItem<KeyType, ItemType> *h = table_[slotNum];
139 if (h->key_ == key) {
147 h =
new AbaHashItem<KeyType, ItemType>(key, item);
148 h->next_ = table_[slotNum];
153 template <
class KeyType,
class ItemType>
156 const AbaHashItem<KeyType, ItemType> *slot;
158 slot = table_[hf(key)];
161 if (key == slot->key_)
return &(slot->item_);
167 template <
class KeyType,
class ItemType>
170 AbaHashItem<KeyType, ItemType> *slot;
172 slot = table_[hf(key)];
175 if (key == slot->key_)
return &(slot->item_);
181 template <
class KeyType,
class ItemType>
184 const AbaHashItem<KeyType, ItemType> *slot;
186 slot = table_[hf(key)];
189 if (key == slot->key_ && slot->item_ == item)
197 template <
class KeyType,
class ItemType>
198 ItemType *AbaHash<KeyType, ItemType>::initializeIteration(
const KeyType &key)
200 iter_ = table_[hf(key)];
202 if (key == iter_->key_)
return &(iter_->item_);
203 iter_ = iter_->next_;
208 template <
class KeyType,
class ItemType>
209 const ItemType *AbaHash<KeyType, ItemType>::initializeIteration(
const KeyType &key)
const
211 iter_ = table_[hf(key)];
213 if (key == iter_->key_)
return &(iter_->item_);
214 iter_ = iter_->next_;
219 template <
class KeyType,
class ItemType>
220 ItemType *AbaHash<KeyType, ItemType>::next(
const KeyType &key)
222 if (iter_ ==
nullptr)
return nullptr;
223 iter_ = iter_->next_;
226 if (key == iter_->key_)
return &(iter_->item_);
227 iter_ = iter_->next();
232 template <
class KeyType,
class ItemType>
233 const ItemType *AbaHash<KeyType, ItemType>::next(
const KeyType &key)
const
235 if (iter_ ==
nullptr)
return nullptr;
236 iter_ = iter_->next_;
239 if (key == iter_->key_)
return &(iter_->item_);
240 iter_ = iter_->next();
245 template <
class KeyType,
class ItemType>
249 AbaHashItem<KeyType, ItemType> *h1;
250 AbaHashItem<KeyType, ItemType> *h2;
251 int slotNum = hf(key);
253 h1 = table_[slotNum];
258 if (h1->key_ == key) {
259 table_[slotNum] = h1->next_;
265 while ((h2 = h1->next_)) {
266 if (h2->key_ == key) {
267 h1->next_ = h2->next_;
277 template <
class KeyType,
class ItemType>
281 AbaHashItem<KeyType, ItemType> *h1;
282 AbaHashItem<KeyType, ItemType> *h2;
283 int slotNum = hf(key);
285 h1 = table_[slotNum];
290 if (h1->key_ == key && h1->item_ == item) {
291 table_[slotNum] = h1->next_;
297 while ((h2 = h1->next_)) {
298 if (h2->key_ == key && h2->item_ == item) {
299 h1->next_ = h2->next_;
309 template <
class KeyType,
class ItemType>
310 inline int AbaHash<KeyType, ItemType>::size()
const
316 template <
class KeyType,
class ItemType>
317 inline int AbaHash<KeyType, ItemType>::nCollisions()
const
323 template <
class KeyType,
class ItemType>
324 inline int AbaHash<KeyType, ItemType>::hf(
int key)
const
326 if (key < 0) key = -key;
328 double x = key*0.6180339887;
330 return (
int) (
size()*(x-floor(x)));
334 template <
class KeyType,
class ItemType>
335 inline int AbaHash<KeyType, ItemType>::hf(
unsigned key)
const
337 double x = key*0.6180339887;
339 return (
int) (
size()*fmod(x, 1.0));
343 template <
class KeyType,
class ItemType>
344 int AbaHash<KeyType, ItemType>::hf(
const string &str)
const
346 const int prime = 516595003;
347 const int mult = 314159;
349 string::size_type s = str.size();
351 for (string::size_type i = 0; i < s; i++) {
352 h += (h ^ (h >> 1)) + mult * (
unsigned char) str[i];
361 template <
class KeyType,
class ItemType>
362 void AbaHash<KeyType, ItemType>::resize(
int newSize)
366 Logger::ifout() <<
"AbaHash::resize(): new size of hash table must be positive.\n";
374 AbaHashItem<KeyType, ItemType> **newTable;
376 newTable =
new AbaHashItem<KeyType, ItemType>* [newSize];
378 for (
int i = 0; i < newSize; i++)
379 newTable[i] =
nullptr;
392 for (
int i = 0; i < oldSize; i++) {
395 AbaHashItem<KeyType, ItemType> *current = table_[i];
396 AbaHashItem<KeyType, ItemType> *next;
399 int slotNum = hf(current->key_);
400 next = current->next_;
402 current->next_ = newTable[slotNum];
403 newTable[slotNum] = current;