seanhalle@35: /* seanhalle@35: * Copyright 2009 OpenSourceResearchInstitute.org seanhalle@35: * Licensed under GNU General Public License version 2 seanhalle@35: * seanhalle@35: * Author: seanhalle@yahoo.com seanhalle@35: */ seanhalle@35: seanhalle@35: #ifndef _PRHASH_H seanhalle@35: #define _PRHASH_H seanhalle@35: seanhalle@35: #include seanhalle@35: #include seanhalle@35: #include seanhalle@35: #include seanhalle@35: seanhalle@35: #include seanhalle@35: seanhalle@35: //===================== defines ===================== seanhalle@35: #define TRUE 1 seanhalle@35: #define FALSE 0 seanhalle@35: seanhalle@35: #define DEFAULT_HASH_TABLE_SIZE 1 << 10 seanhalle@35: #define DEFAULT_POWER_OF_2_TABLE_SIZE 10 seanhalle@35: seanhalle@35: seanhalle@35: //===================== structs ===================== seanhalle@35: union hashkey_t{ seanhalle@35: char hashable[8]; seanhalle@35: int32 parts[2]; seanhalle@35: }; seanhalle@35: seanhalle@35: typedef union hashkey_t hashkey_t; seanhalle@35: seanhalle@35: typedef struct _HashEntry HashEntry; seanhalle@35: seanhalle@35: struct _HashEntry seanhalle@35: { seanhalle@35: char *key; seanhalle@35: void *content; seanhalle@35: HashEntry *next; seanhalle@35: }; seanhalle@35: seanhalle@35: typedef void (*FreeEntryContentFnPtr) ( void * ); seanhalle@35: seanhalle@35: typedef struct seanhalle@35: { int32 tableSz; seanhalle@35: int32 numEntries; seanhalle@35: HashEntry* *entries; seanhalle@35: int32 hashMask; seanhalle@35: int32 prevHash; seanhalle@35: FreeEntryContentFnPtr freeEntryContentFn; seanhalle@35: } seanhalle@35: HashTable; seanhalle@35: seanhalle@35: seanhalle@35: //=========================================================================== seanhalle@35: // Public functions seanhalle@35: HashTable *makeHashTable( int numHashSlots, FreeEntryContentFnPtr freeFn ); seanhalle@35: seanhalle@35: int32 putEntryIntoTable( HashEntry *entry, HashTable *table); seanhalle@35: int32 addValueIntoTable( char* key, void *value, HashTable *table); seanhalle@35: HashEntry *getEntryFromTable( char *key, HashTable *table ); seanhalle@35: void *getValueFromTable( char *key, HashTable *table ); seanhalle@35: seanhalle@35: bool8 deleteEntryFromTable( char *key, HashTable *table ); seanhalle@35: bool8 deleteThisEntryFromTable( HashEntry *entry, HashTable *table ); seanhalle@35: bool8 deleteEntrysValueInTable( char *key, HashTable *table ); seanhalle@35: bool8 deleteEntryFromTableAndFreeValue( char *key, HashTable *table ); seanhalle@35: void freeHashTable( HashTable *table ); seanhalle@35: //char *paramBagToString( ParamBag * bag ) seanhalle@35: seanhalle@35: //================= Same Fns, but for 32b array key hash fn ================ seanhalle@35: HashTable *makeHashTable32(int32 powerOf2OfSz, FreeEntryContentFnPtr freeFn); seanhalle@35: HashTable *makeDefaultSizeHashTable32( FreeEntryContentFnPtr freeFn ); seanhalle@35: seanhalle@35: int32 putEntryIntoTable32( HashEntry *entry, HashTable *table); seanhalle@35: HashEntry *addValueIntoTable32( uint32 key[], void *value, HashTable *table); seanhalle@35: HashEntry *getEntryFromTable32( uint32 key[], HashTable *table ); seanhalle@35: void *getValueFromTable32( uint32 key[], HashTable *table ); seanhalle@35: seanhalle@35: bool32 deleteEntryFromTable32( uint32 key[], HashTable *table ); seanhalle@35: seanhalle@35: //=========================================================================== seanhalle@35: // Internal functions seanhalle@35: void freeHashEntryUsing( HashEntry *entry, HashTable *table ); seanhalle@35: unsigned int hashThisKey( char *s, int hashSz ); seanhalle@35: void nullOutTablesArray( HashTable *table ); seanhalle@35: void doubleTableSize( HashTable *table ); seanhalle@35: void freeHashEntryButNotContent( HashEntry *entry ); seanhalle@35: seanhalle@35: uint32 seanhalle@35: jenkHash32( const uint32 *key, /* array of uint32 values */ seanhalle@35: int32 length); /* num uint32 in the key */ seanhalle@35: seanhalle@35: #endif /* _PRIVATE_HASH_H */ seanhalle@35: