// @ZBS { // *MODULE_OWNER_NAME zhashtable // } #ifndef ZHASHTABLE_H #define ZHASHTABLE_H // The HashTable class is a generic associative array // that associate keys which are nul-terminate strings with // generic buffers of arbitrary length. // // HashTables will automatically rebuild themselves when // they get to dense which can be an expensive operation // therefore it is best if you create them with an // initial size roughly double what you expect will be used. class ZHashTable { protected: int collisions; // Incremented each time the set() function collides. // Useful for profiling the hash function int initialSize; // How big the initial array is int hasAnyChanged; // Has anything in here changed since the last time this was cleared int hashTableSize; // Number of pointers in the hash table int numRecords; // Actual active records in the table char **hashTable; // Array of key pointers, which are of the form: // 1 byte flags: #define aaDELETED (0x01) #define aaCHANGED (0x02) // followed by 4 bytes val length // followed by nul-terminated key string // followed by data of arbitrary length unsigned int hash( char *key ); // Hashing function. currently using the one from http://burtleburtle.net/bob/hash/doobs.html int internalSet( char **_hashTable, int _hashTableSize, char *key, char *_recPtr ); // Internal method to functionalize rebuilds of tables // Returns 1 if the key is new, 0 if it is replacing an existing public: ZHashTable( int _initialSize=64 ); // For optimal performance make _initialSize twice as big as expected size ~ZHashTable(); void clear(); // kill everything, start fresh int size() { return hashTableSize; } // The size of the hashtable including empty records int activeCount() { return numRecords; } // The actual number of active records void copyFrom( ZHashTable &table ); // Copies all the items from table. // Does *NOT* clear first, if you want to // start off fresh, call clear() first int lookup( char *key ); // Returns the index of key, or -1 if it wasn't found // These the the i_th key.val pair in the table. // They are useful when traversing the entire contents // For example: // for( int i=0; i