Changeset 793 for cpp/frams/util/hashtable.h
- Timestamp:
- 05/29/18 16:51:14 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/util/hashtable.h
r286 r793 11 11 { 12 12 public: 13 int hash;14 SString key;15 void *value;16 HashEntry *next;13 int hash; 14 SString key; 15 void *value; 16 HashEntry *next; 17 17 18 HashEntry(int h,const SString& k,void *v):hash(h),key(k),value(v),next(0){}18 HashEntry(int h, const SString& k, void *v) :hash(h), key(k), value(v), next(0){} 19 19 }; 20 20 … … 23 23 class HashTable 24 24 { 25 friend class HashEntryIterator;26 HashEntry **tab;27 int size;28 int count;29 int threshold;30 float load;31 int sync;25 friend class HashEntryIterator; 26 HashEntry **tab; 27 int size; 28 int count; 29 int threshold; 30 float load; 31 int sync; 32 32 33 int hash(const SString &s);34 void rehash(int newsize);33 int hash(const SString &s); 34 void rehash(int newsize); 35 35 public: 36 36 37 HashTable(int initsize,float lo) {init(initsize,lo);}38 HashTable(int initsize) {init(initsize,0.75);}39 HashTable() {init();}40 ~HashTable();37 HashTable(int initsize, float lo) { init(initsize, lo); } 38 HashTable(int initsize) { init(initsize, 0.75); } 39 HashTable() { init(); } 40 ~HashTable(); 41 41 42 /** always use init() after clear() ! */43 void clear();44 void init(int initsize=11,float lo=0.75);42 /** always use init() after clear() ! */ 43 void clear(); 44 void init(int initsize = 11, float lo = 0.75); 45 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 52 53 54 void* remove(HashEntryIterator& it);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 55 56 void debugprint();57 void getstats(float *);56 void debugprint(); 57 void getstats(float *); 58 58 }; 59 59 60 60 /** for(HashEntryIterator it(hashtable);it;it++) 61 62 63 64 65 */61 { 62 ... it->value 63 ... it->key 64 } 65 */ 66 66 class HashEntryIterator 67 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 {if (!entry) findNext();} 76 HashEntryIterator() {} 77 void operator++(); 78 void operator++(int) {operator++();} 79 HashEntry* operator->() {return entry;} 80 bool isValid() {return (entry&&(sync==ht->sync))?1:0;} 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; } 81 83 }; 82 84
Note: See TracChangeset
for help on using the changeset viewer.