31#pragma GCC visibility push(default)
35template <
class KeyType,
class ItemType>
38 const ItemType &item) :
45template <
class KeyType,
class ItemType>
46std::ostream &
operator<<(std::ostream &out,
const AbaHashItem<KeyType, ItemType> &rhs)
48 return out <<
'(' << rhs.key_ <<
',' << rhs.item_ <<
')';
52template <
class KeyType,
class ItemType>
53inline AbaHashItem<KeyType, ItemType> * AbaHashItem<KeyType,
60template <
class KeyType,
class ItemType>
61AbaHash<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++)
71template <
class KeyType,
class ItemType>
72AbaHash<KeyType, ItemType>::~AbaHash()
74 AbaHashItem<KeyType, ItemType> *h1;
75 AbaHashItem<KeyType, ItemType> *h2;
78 for (i = 0; i < size_; i++) {
90template <
class KeyType,
class ItemType>
91std::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++) {
111template <
class KeyType,
class ItemType>
112void 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];
125template <
class KeyType,
class ItemType>
126void 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];
154template <
class KeyType,
class ItemType>
155const ItemType * AbaHash<KeyType, ItemType>::find(
const KeyType &key)
const
157 const AbaHashItem<KeyType, ItemType> *slot;
159 slot = table_[hf(key)];
162 if (key == slot->key_)
return &(slot->item_);
168template <
class KeyType,
class ItemType>
169ItemType * AbaHash<KeyType, ItemType>::find(
const KeyType &key)
171 AbaHashItem<KeyType, ItemType> *slot;
173 slot = table_[hf(key)];
176 if (key == slot->key_)
return &(slot->item_);
182template <
class KeyType,
class ItemType>
183bool AbaHash<KeyType, ItemType>::find (
const KeyType &key,
const ItemType &item)
const
185 const AbaHashItem<KeyType, ItemType> *slot;
187 slot = table_[hf(key)];
190 if (key == slot->key_ && slot->item_ == item)
198template <
class KeyType,
class ItemType>
199ItemType *AbaHash<KeyType, ItemType>::initializeIteration(
const KeyType &key)
201 iter_ = table_[hf(key)];
203 if (key == iter_->key_)
return &(iter_->item_);
204 iter_ = iter_->next_;
209template <
class KeyType,
class ItemType>
210const 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_;
220template <
class KeyType,
class ItemType>
221ItemType *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();
233template <
class KeyType,
class ItemType>
234const 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();
246template <
class KeyType,
class ItemType>
247int AbaHash<KeyType, ItemType>::remove(
const KeyType &key)
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_;
278template <
class KeyType,
class ItemType>
279int AbaHash<KeyType, ItemType>::remove(
const KeyType &key,
const ItemType &item)
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_;
310template <
class KeyType,
class ItemType>
311inline int AbaHash<KeyType, ItemType>::size()
const
317template <
class KeyType,
class ItemType>
318inline int AbaHash<KeyType, ItemType>::nCollisions()
const
324template <
class KeyType,
class ItemType>
325inline 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)));
335template <
class KeyType,
class ItemType>
336inline int AbaHash<KeyType, ItemType>::hf(
unsigned key)
const
338 double x = key*0.6180339887;
340 return (
int) (
size()*fmod(x, 1.0));
344template <
class KeyType,
class ItemType>
345int 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];
362template <
class KeyType,
class ItemType>
363void 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
AbaHashItem(const KeyType &key, const ItemType &item)
The constructor.
#define OGDF_THROW_PARAM(CLASS, PARAM)
Replacement for throw.
std::ostream & operator<<(std::ostream &os, const ogdf::Array< E, INDEX > &a)
Prints array a to output stream os.