| 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
|---|
| 2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
|---|
| 3 | // See LICENSE.txt for details. |
|---|
| 4 | |
|---|
| 5 | #ifndef _HASHTABLE_H_ |
|---|
| 6 | #define _HASHTABLE_H_ |
|---|
| 7 | |
|---|
| 8 | #include "sstring.h" |
|---|
| 9 | |
|---|
| 10 | class HashEntry |
|---|
| 11 | { |
|---|
| 12 | public: |
|---|
| 13 | int hash; |
|---|
| 14 | SString key; |
|---|
| 15 | void *value; |
|---|
| 16 | HashEntry *next; |
|---|
| 17 | |
|---|
| 18 | HashEntry(int h, const SString& k, void *v) :hash(h), key(k), value(v), next(0){} |
|---|
| 19 | }; |
|---|
| 20 | |
|---|
| 21 | class HashEntryIterator; |
|---|
| 22 | |
|---|
| 23 | class HashTable |
|---|
| 24 | { |
|---|
| 25 | friend class HashEntryIterator; |
|---|
| 26 | HashEntry **tab; |
|---|
| 27 | int size; |
|---|
| 28 | int count; |
|---|
| 29 | int threshold; |
|---|
| 30 | float load; |
|---|
| 31 | int sync; |
|---|
| 32 | |
|---|
| 33 | int hash(const SString &s); |
|---|
| 34 | void rehash(int newsize); |
|---|
| 35 | public: |
|---|
| 36 | |
|---|
| 37 | HashTable(int initsize, float lo) { init(initsize, lo); } |
|---|
| 38 | HashTable(int initsize) { init(initsize, 0.75); } |
|---|
| 39 | HashTable() { init(); } |
|---|
| 40 | ~HashTable(); |
|---|
| 41 | |
|---|
| 42 | /** always use init() after clear() ! */ |
|---|
| 43 | void clear(); |
|---|
| 44 | void init(int initsize = 11, float lo = 0.75); |
|---|
| 45 | |
|---|
| 46 | int getSize() { return count; } |
|---|
| 47 | void* put(const SString& key, void *value); |
|---|
| 48 | void* get(const SString& key, int *reallygot = 0); |
|---|
| 49 | void* remove(const SString& key); |
|---|
| 50 | /** can be used inside iteration loop: |
|---|
| 51 | for(HashEntryIterator it(hashtable);it;) hashtable.remove(it); |
|---|
| 52 | \note iterator is "iterated" to the next entry when the current one is removed (no "it++"!) |
|---|
| 53 | */ |
|---|
| 54 | void* remove(HashEntryIterator& it); |
|---|
| 55 | |
|---|
| 56 | void debugprint(); |
|---|
| 57 | void getstats(float *); |
|---|
| 58 | }; |
|---|
| 59 | |
|---|
| 60 | /** for(HashEntryIterator it(hashtable);it;it++) |
|---|
| 61 | { |
|---|
| 62 | ... it->value |
|---|
| 63 | ... it->key |
|---|
| 64 | } |
|---|
| 65 | */ |
|---|
| 66 | class HashEntryIterator |
|---|
| 67 | { |
|---|
| 68 | void findNext(); |
|---|
| 69 | public: |
|---|
| 70 | const HashTable *ht; |
|---|
| 71 | int hashindex; |
|---|
| 72 | HashEntry *entry; |
|---|
| 73 | int sync; |
|---|
| 74 | HashEntryIterator(const HashTable&t) :ht(&t), hashindex(0), entry(t.tab[0]), sync(ht->sync) |
|---|
| 75 | { |
|---|
| 76 | if (!entry) findNext(); |
|---|
| 77 | } |
|---|
| 78 | HashEntryIterator() {} |
|---|
| 79 | void operator++(); |
|---|
| 80 | void operator++(int) { operator++(); } |
|---|
| 81 | HashEntry* operator->() { return entry; } |
|---|
| 82 | bool isValid() { return (entry && (sync == ht->sync)) ? 1 : 0; } |
|---|
| 83 | }; |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | #endif |
|---|