31 #pragma GCC visibility push(default)
35 template <
class KeyType,
class ItemType>
38 const ItemType &item) :
45 template <
class KeyType,
class ItemType>
46 std::ostream &
operator<<(std::ostream &out,
const AbaHashItem<KeyType, ItemType> &rhs)
48 return out <<
'(' << rhs.key_ <<
',' << rhs.item_ <<
')';
52 template <
class KeyType,
class ItemType>
53 inline AbaHashItem<KeyType, ItemType> * AbaHashItem<KeyType,
60 template <
class KeyType,
class ItemType>
61 AbaHash<KeyType, ItemType>::AbaHash(
int size)
62 : size_(
size), nCollisions_(0), iter_(nullptr)
64 table_ =
new AbaHashItem<KeyType, ItemType>* [
size];
66 for (
int i = 0; i <
size; i++)
71 template <
class KeyType,
class ItemType>
72 AbaHash<KeyType, ItemType>::~AbaHash()
74 AbaHashItem<KeyType, ItemType> *h1;
75 AbaHashItem<KeyType, ItemType> *h2;
78 for (i = 0; i < size_; i++) {
90 template <
class KeyType,
class ItemType>
91 std::ostream &
operator<<(std::ostream &out,
const AbaHash<KeyType, ItemType> &
hash)
93 AbaHashItem<KeyType, ItemType> *h;
94 const int s =
hash.size();
96 for (
int i = 0; i < s; i++) {
111 template <
class KeyType,
class ItemType>
112 void AbaHash<KeyType, ItemType>::insert(
114 const ItemType &item)
116 AbaHashItem<KeyType, ItemType> *h =
new AbaHashItem<KeyType, ItemType>(key, item);
117 int slotNum = hf(key);
119 if (table_[slotNum]) ++nCollisions_;
120 h->next_ = table_[slotNum];
125 template <
class KeyType,
class ItemType>
126 void AbaHash<KeyType, ItemType>::overWrite(
128 const ItemType &item)
131 int slotNum = hf(key);
132 if (table_[slotNum]) ++nCollisions_;
134 AbaHashItem<KeyType, ItemType> *h = table_[slotNum];
140 if (h->key_ == key) {
148 h =
new AbaHashItem<KeyType, ItemType>(key, item);
149 h->next_ = table_[slotNum];
154 template <
class KeyType,
class ItemType>
157 const AbaHashItem<KeyType, ItemType> *slot;
159 slot = table_[hf(key)];
162 if (key == slot->key_)
return &(slot->item_);
168 template <
class KeyType,
class ItemType>
171 AbaHashItem<KeyType, ItemType> *slot;
173 slot = table_[hf(key)];
176 if (key == slot->key_)
return &(slot->item_);
182 template <
class KeyType,
class ItemType>
185 const AbaHashItem<KeyType, ItemType> *slot;
187 slot = table_[hf(key)];
190 if (key == slot->key_ && slot->item_ == item)
198 template <
class KeyType,
class ItemType>
199 ItemType *AbaHash<KeyType, ItemType>::initializeIteration(
const KeyType &key)
201 iter_ = table_[hf(key)];
203 if (key == iter_->key_)
return &(iter_->item_);
204 iter_ = iter_->next_;
209 template <
class KeyType,
class ItemType>
210 const ItemType *AbaHash<KeyType, ItemType>::initializeIteration(
const KeyType &key)
const
212 iter_ = table_[hf(key)];
214 if (key == iter_->key_)
return &(iter_->item_);
215 iter_ = iter_->next_;
220 template <
class KeyType,
class ItemType>
221 ItemType *AbaHash<KeyType, ItemType>::next(
const KeyType &key)
223 if (iter_ ==
nullptr)
return nullptr;
224 iter_ = iter_->next_;
227 if (key == iter_->key_)
return &(iter_->item_);
228 iter_ = iter_->next();
233 template <
class KeyType,
class ItemType>
234 const ItemType *AbaHash<KeyType, ItemType>::next(
const KeyType &key)
const
236 if (iter_ ==
nullptr)
return nullptr;
237 iter_ = iter_->next_;
240 if (key == iter_->key_)
return &(iter_->item_);
241 iter_ = iter_->next();
246 template <
class KeyType,
class ItemType>
250 AbaHashItem<KeyType, ItemType> *h1;
251 AbaHashItem<KeyType, ItemType> *h2;
252 int slotNum = hf(key);
254 h1 = table_[slotNum];
259 if (h1->key_ == key) {
260 table_[slotNum] = h1->next_;
266 while ((h2 = h1->next_)) {
267 if (h2->key_ == key) {
268 h1->next_ = h2->next_;
278 template <
class KeyType,
class ItemType>
282 AbaHashItem<KeyType, ItemType> *h1;
283 AbaHashItem<KeyType, ItemType> *h2;
284 int slotNum = hf(key);
286 h1 = table_[slotNum];
291 if (h1->key_ == key && h1->item_ == item) {
292 table_[slotNum] = h1->next_;
298 while ((h2 = h1->next_)) {
299 if (h2->key_ == key && h2->item_ == item) {
300 h1->next_ = h2->next_;
310 template <
class KeyType,
class ItemType>
311 inline int AbaHash<KeyType, ItemType>::size()
const
317 template <
class KeyType,
class ItemType>
318 inline int AbaHash<KeyType, ItemType>::nCollisions()
const
324 template <
class KeyType,
class ItemType>
325 inline int AbaHash<KeyType, ItemType>::hf(
int key)
const
327 if (key < 0) key = -key;
329 double x = key*0.6180339887;
331 return (
int) (
size()*(x-floor(x)));
335 template <
class KeyType,
class ItemType>
336 inline int AbaHash<KeyType, ItemType>::hf(
unsigned key)
const
338 double x = key*0.6180339887;
340 return (
int) (
size()*fmod(x, 1.0));
344 template <
class KeyType,
class ItemType>
345 int AbaHash<KeyType, ItemType>::hf(
const string &str)
const
347 const int prime = 516595003;
348 const int mult = 314159;
350 string::size_type s = str.size();
352 for (string::size_type i = 0; i < s; i++) {
353 h += (h ^ (h >> 1)) + mult * (
unsigned char) str[i];
362 template <
class KeyType,
class ItemType>
363 void AbaHash<KeyType, ItemType>::resize(
int newSize)
367 Logger::ifout() <<
"AbaHash::resize(): new size of hash table must be positive.\n";
375 AbaHashItem<KeyType, ItemType> **newTable;
377 newTable =
new AbaHashItem<KeyType, ItemType>* [newSize];
379 for (
int i = 0; i < newSize; i++)
380 newTable[i] =
nullptr;
393 for (
int i = 0; i < oldSize; i++) {
396 AbaHashItem<KeyType, ItemType> *current = table_[i];
397 AbaHashItem<KeyType, ItemType> *next;
400 int slotNum = hf(current->key_);
401 next = current->next_;
403 current->next_ = newTable[slotNum];
404 newTable[slotNum] = current;
417 #pragma GCC visibility pop