1 #ifndef HASHTABLE_H 2 #define HASHTABLE_H 3 /* Hashtable - a general purpose hash table 4 ** 5 ** Copyright 2001 pinc Software. All Rights Reserved. 6 ** Released under the terms of the MIT license. 7 */ 8 9 10 #include "SupportDefs.h" 11 12 13 #define HASH_EMPTY_NONE 0 14 #define HASH_EMPTY_FREE 1 15 #define HASH_EMPTY_DELETE 2 16 17 class Hashtable 18 { 19 public: 20 Hashtable(int capacity = 100, float loadFactor = 0.75); 21 ~Hashtable(); 22 23 void SetHashFunction(uint32 (*func)(const void *)); 24 void SetCompareFunction(bool (*func)(const void *, const void *)); 25 26 bool IsEmpty() const; 27 bool ContainsKey(const void *key); 28 void *GetValue(const void *key); 29 30 bool Put(const void *key, void *value); 31 void *Remove(const void *key); 32 33 status_t GetNextEntry(void **value); 34 void Rewind(); 35 36 void MakeEmpty(int8 keyMode = HASH_EMPTY_NONE,int8 valueMode = HASH_EMPTY_NONE); 37 size_t Size() const; 38 39 protected: 40 class Entry 41 { 42 public: 43 Entry(Entry *_next, const void *_key, void *_value) 44 : next(_next), key(_key), value(_value) {} 45 46 Entry *next; 47 const void *key; 48 void *value; 49 }; 50 51 bool Rehash(); 52 Entry *GetHashEntry(const void *key); 53 54 int32 fCapacity,fCount,fThreshold,fModCount; 55 float fLoadFactor; 56 Entry **fTable; 57 uint32 (*fHashFunc)(const void *); 58 bool (*fCompareFunc)(const void *, const void *); 59 60 int32 fIteratorIndex; 61 Entry *fIteratorEntry; 62 }; 63 64 #endif // HASHTABLE_H 65