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