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