X-Git-Url: https://code.citadel.org/?p=citadel.git;a=blobdiff_plain;f=libcitadel%2Flib%2Fhash.c;h=ab7eec1896206b12797b442b4952114414c64285;hp=e3be284023c196ddce920a4966c49adef566a9e4;hb=6d24a8dc9cf6653ec01ebc85c74939a3c9d7031c;hpb=25adc7cc329866f761a239e7a1500198419a1ff9 diff --git a/libcitadel/lib/hash.c b/libcitadel/lib/hash.c index e3be28402..ab7eec189 100644 --- a/libcitadel/lib/hash.c +++ b/libcitadel/lib/hash.c @@ -1,6 +1,25 @@ +/* + * Copyright (c) 1987-2011 by the citadel.org team + * + * This program is open source software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + #include #include #include +#include //dbg #include #include "libcitadel.h" @@ -8,18 +27,64 @@ typedef struct Payload Payload; +/** + * @defgroup HashList Hashlist Key Value list implementation; + * Hashlist is a simple implementation of key value pairs. It doesn't implement collision handling. + * the Hashingalgorythm is pluggeable on creation. + * items are added with a functionpointer destructs them; that way complex structures can be added. + * if no pointer is given, simply free is used. Use @ref reference_free_handler if you don't want us to free you rmemory. + */ + +/** + * @defgroup HashListData Datastructures used for the internals of HashList + * @ingroup HashList + */ + +/** + * @defgroup HashListDebug Hashlist debugging functions + * @ingroup HashList + */ + +/** + * @defgroup HashListPrivate Hashlist internal functions + * @ingroup HashList + */ + +/** + * @defgroup HashListSort Hashlist sorting functions + * @ingroup HashList + */ + +/** + * @defgroup HashListAccess Hashlist functions to access / put / delete items in(to) the list + * @ingroup HashList + */ + +/** + * @defgroup HashListAlgorithm functions to condense Key to an integer. + * @ingroup HashList + */ + +/** + * @defgroup HashListMset MSet is sort of a derived hashlist, its special for treating Messagesets as Citadel uses them to store access rangesx + * @ingroup HashList + */ + +/** + * @ingroup HashListData + * @brief Hash Payload storage Structure; filled in linear. + */ struct Payload { - /** - * \brief Hash Payload storage Structure; filled in linear. - */ void *Data; /**< the Data belonging to this storage */ DeleteHashDataFunc Destructor; /**< if we want to destroy Data, do it with this function. */ }; + +/** + * @ingroup HashListData + * @brief Hash key element; sorted by key + */ struct HashKey { - /** - * \brief Hash key element; sorted by key - */ long Key; /**< Numeric Hashkey comperator for hash sorting */ long Position; /**< Pointer to a Payload struct in the Payload Aray */ char *HashKey; /**< the Plaintext Hashkey */ @@ -27,36 +92,40 @@ struct HashKey { Payload *PL; /**< pointer to our payload for sorting */ }; +/** + * @ingroup HashListData + * @brief Hash structure; holds arrays of Hashkey and Payload. + */ struct HashList { - /** - * \brief Hash structure; holds arrays of Hashkey and Payload. - */ Payload **Members; /**< Our Payload members. This fills up linear */ HashKey **LookupTable; /**< Hash Lookup table. Elements point to members, and are sorted by their hashvalue */ char **MyKeys; /**< this keeps the members for a call of GetHashKeys */ HashFunc Algorithm; /**< should we use an alternating algorithm to calc the hash values? */ long nMembersUsed; /**< how many pointers inside of the array are used? */ + long nLookupTableItems; /**< how many items of the lookup table are used? */ long MemberSize; /**< how big is Members and LookupTable? */ long tainted; /**< if 0, we're hashed, else s.b. else sorted us in his own way. */ long uniq; /**< are the keys going to be uniq? */ }; +/** + * @ingroup HashListData + * @brief Anonymous Hash Iterator Object. used for traversing the whole array from outside + */ struct HashPos { - /** - * \brief Anonymous Hash Iterator Object. used for traversing the whole array from outside - */ - long Position; - int StepWidth; + long Position; /**< Position inside of the hash */ + int StepWidth; /**< small? big? forward? backward? */ }; /** - * \brief Iterate over the hash and call PrintEntry. - * \param Hash your Hashlist structure - * \param Trans is called so you could for example print 'A:' if the next entries are like that. + * @ingroup HashListDebug + * @brief Iterate over the hash and call PrintEntry. + * @param Hash your Hashlist structure + * @param Trans is called so you could for example print 'A:' if the next entries are like that. * Must be aware to receive NULL in both pointers. - * \param PrintEntry print entry one by one - * \returns the number of items printed + * @param PrintEntry print entry one by one + * @return the number of items printed */ int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry) { @@ -68,7 +137,7 @@ int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry if (Hash == NULL) return 0; - for (i=0; i < Hash->nMembersUsed; i++) { + for (i=0; i < Hash->nLookupTableItems; i++) { if (i==0) { Previous = NULL; } @@ -95,18 +164,21 @@ int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry /** - * \brief verify the contents of a hash list; here for debugging purposes. - * \param Hash your Hashlist structure - * \param First Functionpointer to allow you to print your payload - * \param Second Functionpointer to allow you to print your payload - * \returns 0 + * @ingroup HashListDebug + * @brief verify the contents of a hash list; here for debugging purposes. + * @param Hash your Hashlist structure + * @param First Functionpointer to allow you to print your payload + * @param Second Functionpointer to allow you to print your payload + * @return 0 */ int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Second) { +#ifdef DEBUG const char *foo; const char *bar; const char *bla = ""; long key; +#endif long i; if (Hash == NULL) @@ -115,33 +187,55 @@ int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Secon if (Hash->MyKeys != NULL) free (Hash->MyKeys); - Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nMembersUsed); + Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nLookupTableItems); #ifdef DEBUG printf("----------------------------------\n"); #endif - for (i=0; i < Hash->nMembersUsed; i++) { + for (i=0; i < Hash->nLookupTableItems; i++) { if (Hash->LookupTable[i] == NULL) { +#ifdef DEBUG foo = ""; bar = ""; key = 0; +#endif } else { +#ifdef DEBUG key = Hash->LookupTable[i]->Key; foo = Hash->LookupTable[i]->HashKey; +#endif if (First != NULL) - bar = First(Hash->Members[Hash->LookupTable[i]->Position]->Data); +#ifdef DEBUG + bar = +#endif + First(Hash->Members[Hash->LookupTable[i]->Position]->Data); +#ifdef DEBUG else bar = ""; +#endif + if (Second != NULL) - bla = Second(Hash->Members[Hash->LookupTable[i]->Position]->Data); +#ifdef DEBUG + bla = +#endif + Second(Hash->Members[Hash->LookupTable[i]->Position]->Data); +#ifdef DEBUG + else bla = ""; +#endif + } #ifdef DEBUG - printf (" ---- Hashkey[%ld][%ld]: '%s' Value: '%s' ; %s\n", i, key, foo, bar, bla); + if ((Hash->Algorithm == lFlathash) || (Hash->Algorithm == Flathash)) { + printf (" ---- Hashkey[%ld][%ld]: %ld '%s' Value: '%s' ; %s\n", i, key, *(long*) foo, foo, bar, bla); + } + else { + printf (" ---- Hashkey[%ld][%ld]: '%s' Value: '%s' ; %s\n", i, key, foo, bar, bla); + } #endif } #ifdef DEBUG @@ -151,20 +245,58 @@ int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Secon } +int TestValidateHash(HashList *TestHash) +{ + long i; + + if (TestHash->nMembersUsed != TestHash->nLookupTableItems) + return 1; + + if (TestHash->nMembersUsed > TestHash->MemberSize) + return 2; + + for (i=0; i < TestHash->nMembersUsed; i++) + { + + if (TestHash->LookupTable[i]->Position > TestHash->nMembersUsed) + return 3; + + if (TestHash->Members[TestHash->LookupTable[i]->Position] == NULL) + return 4; + if (TestHash->Members[TestHash->LookupTable[i]->Position]->Data == NULL) + return 5; + } + return 0; +} + /** - * \brief instanciate a new hashlist - * \returns the newly allocated list. + * @ingroup HashListAccess + * @brief instanciate a new hashlist + * @return the newly allocated list. */ HashList *NewHash(int Uniq, HashFunc F) { HashList *NewList; NewList = malloc (sizeof(HashList)); + if (NewList == NULL) + return NULL; memset(NewList, 0, sizeof(HashList)); NewList->Members = malloc(sizeof(Payload*) * 100); + if (NewList->Members == NULL) + { + free(NewList); + return NULL; + } memset(NewList->Members, 0, sizeof(Payload*) * 100); NewList->LookupTable = malloc(sizeof(HashKey*) * 100); + if (NewList->LookupTable == NULL) + { + free(NewList->Members); + free(NewList); + return NULL; + } memset(NewList->LookupTable, 0, sizeof(HashKey*) * 100); NewList->MemberSize = 100; @@ -178,14 +310,15 @@ HashList *NewHash(int Uniq, HashFunc F) int GetCount(HashList *Hash) { if(Hash==NULL) return 0; - return Hash->nMembersUsed; + return Hash->nLookupTableItems; } /** - * \brief private destructor for one hash element. + * @ingroup HashListPrivate + * @brief private destructor for one hash element. * Crashing? go one frame up and do 'print *FreeMe->LookupTable[i]' - * \param Data an element to free using the user provided destructor, or just plain free + * @param Data an element to free using the user provided destructor, or just plain free */ static void DeleteHashPayload (Payload *Data) { @@ -197,7 +330,8 @@ static void DeleteHashPayload (Payload *Data) } /** - * \brief Destructor for nested hashes + * @ingroup HashListPrivate + * @brief Destructor for nested hashes */ void HDeleteHash(void *vHash) { @@ -206,11 +340,12 @@ void HDeleteHash(void *vHash) } /** - * \brief destroy a hashlist and all of its members + * @ingroup HashListAccess + * @brief flush the members of a hashlist * Crashing? do 'print *FreeMe->LookupTable[i]' - * \param Hash Hash to destroy. Is NULL'ed so you are shure its done. + * @param Hash Hash to destroy. Is NULL'ed so you are shure its done. */ -void DeleteHash(HashList **Hash) +void DeleteHashContent(HashList **Hash) { int i; HashList *FreeMe; @@ -218,6 +353,7 @@ void DeleteHash(HashList **Hash) FreeMe = *Hash; if (FreeMe == NULL) return; + /* even if there are sparse members already deleted... */ for (i=0; i < FreeMe->nMembersUsed; i++) { /** get rid of our payload */ @@ -233,94 +369,156 @@ void DeleteHash(HashList **Hash) free(FreeMe->LookupTable[i]); } } - /** now, free our arrays... */ - free(FreeMe->LookupTable); - free(FreeMe->Members); + FreeMe->nMembersUsed = 0; + FreeMe->tainted = 0; + FreeMe->nLookupTableItems = 0; + memset(FreeMe->Members, 0, sizeof(Payload*) * FreeMe->MemberSize); + memset(FreeMe->LookupTable, 0, sizeof(HashKey*) * FreeMe->MemberSize); + /** did s.b. want an array of our keys? free them. */ if (FreeMe->MyKeys != NULL) free(FreeMe->MyKeys); +} + +/** + * @ingroup HashListAccess + * @brief destroy a hashlist and all of its members + * Crashing? do 'print *FreeMe->LookupTable[i]' + * @param Hash Hash to destroy. Is NULL'ed so you are shure its done. + */ +void DeleteHash(HashList **Hash) +{ + HashList *FreeMe; + + FreeMe = *Hash; + if (FreeMe == NULL) + return; + DeleteHashContent(Hash); + /** now, free our arrays... */ + free(FreeMe->LookupTable); + free(FreeMe->Members); + /** buye bye cruel world. */ free (FreeMe); *Hash = NULL; } /** - * \brief Private function to increase the hash size. - * \param Hash the Hasharray to increase + * @ingroup HashListPrivate + * @brief Private function to increase the hash size. + * @param Hash the Hasharray to increase */ -static void IncreaseHashSize(HashList *Hash) +static int IncreaseHashSize(HashList *Hash) { /* Ok, Our space is used up. Double the available space. */ Payload **NewPayloadArea; HashKey **NewTable; if (Hash == NULL) - return ; + return 0; + + /** If we grew to much, this might be the place to rehash and shrink again. + if ((Hash->NMembersUsed > Hash->nLookupTableItems) && + ((Hash->NMembersUsed - Hash->nLookupTableItems) > + (Hash->nLookupTableItems / 10))) + { + + + } + */ - /** double our payload area */ NewPayloadArea = (Payload**) malloc(sizeof(Payload*) * Hash->MemberSize * 2); + if (NewPayloadArea == NULL) + return 0; + NewTable = malloc(sizeof(HashKey*) * Hash->MemberSize * 2); + if (NewTable == NULL) + { + free(NewPayloadArea); + return 0; + } + + /** double our payload area */ memset(&NewPayloadArea[Hash->MemberSize], 0, sizeof(Payload*) * Hash->MemberSize); memcpy(NewPayloadArea, Hash->Members, sizeof(Payload*) * Hash->MemberSize); free(Hash->Members); Hash->Members = NewPayloadArea; /** double our hashtable area */ - NewTable = malloc(sizeof(HashKey*) * Hash->MemberSize * 2); memset(&NewTable[Hash->MemberSize], 0, sizeof(HashKey*) * Hash->MemberSize); memcpy(NewTable, Hash->LookupTable, sizeof(HashKey*) * Hash->MemberSize); free(Hash->LookupTable); Hash->LookupTable = NewTable; Hash->MemberSize *= 2; + return 1; } /** - * \brief private function to add a new item to / replace an existing in - the hashlist + * @ingroup HashListPrivate + * @brief private function to add a new item to / replace an existing in - the hashlist * if the hash list is full, its re-alloced with double size. - * \parame Hash our hashlist to manipulate - * \param HashPos where should we insert / replace? - * \param HashKeyStr the Hash-String - * \param HKLen length of HashKeyStr - * \param Data your Payload to add - * \param Destructor Functionpointer to free Data. if NULL, default free() is used. - */ -static void InsertHashItem(HashList *Hash, - long HashPos, - long HashBinKey, - const char *HashKeyStr, - long HKLen, - void *Data, - DeleteHashDataFunc Destructor) + * @param Hash our hashlist to manipulate + * @param HashPos where should we insert / replace? + * @param HashKeyStr the Hash-String + * @param HKLen length of HashKeyStr + * @param Data your Payload to add + * @param Destructor Functionpointer to free Data. if NULL, default free() is used. + */ +static int InsertHashItem(HashList *Hash, + long HashPos, + long HashBinKey, + const char *HashKeyStr, + long HKLen, + void *Data, + DeleteHashDataFunc Destructor) { Payload *NewPayloadItem; HashKey *NewHashKey; + char *HashKeyOrgVal; if (Hash == NULL) - return; + return 0; - if (Hash->nMembersUsed >= Hash->MemberSize) - IncreaseHashSize (Hash); + if ((Hash->nMembersUsed >= Hash->MemberSize) && + (!IncreaseHashSize (Hash))) + return 0; - /** Arrange the payload */ NewPayloadItem = (Payload*) malloc (sizeof(Payload)); + if (NewPayloadItem == NULL) + return 0; + NewHashKey = (HashKey*) malloc (sizeof(HashKey)); + if (NewHashKey == NULL) + { + free(NewPayloadItem); + return 0; + } + HashKeyOrgVal = (char *) malloc (HKLen + 1); + if (HashKeyOrgVal == NULL) + { + free(NewHashKey); + free(NewPayloadItem); + return 0; + } + + + /** Arrange the payload */ NewPayloadItem->Data = Data; NewPayloadItem->Destructor = Destructor; /** Arrange the hashkey */ - NewHashKey = (HashKey*) malloc (sizeof(HashKey)); - NewHashKey->HashKey = (char *) malloc (HKLen + 1); NewHashKey->HKLen = HKLen; + NewHashKey->HashKey = HashKeyOrgVal; memcpy (NewHashKey->HashKey, HashKeyStr, HKLen + 1); NewHashKey->Key = HashBinKey; NewHashKey->PL = NewPayloadItem; /** our payload is queued at the end... */ NewHashKey->Position = Hash->nMembersUsed; /** but if we should be sorted into a specific place... */ - if ((Hash->nMembersUsed != 0) && - (HashPos != Hash->nMembersUsed) ) { + if ((Hash->nLookupTableItems != 0) && + (HashPos != Hash->nLookupTableItems) ) { long ItemsAfter; - ItemsAfter = Hash->nMembersUsed - HashPos; + ItemsAfter = Hash->nLookupTableItems - HashPos; /** make space were we can fill us in */ if (ItemsAfter > 0) { @@ -333,14 +531,17 @@ static void InsertHashItem(HashList *Hash, Hash->Members[Hash->nMembersUsed] = NewPayloadItem; Hash->LookupTable[HashPos] = NewHashKey; Hash->nMembersUsed++; + Hash->nLookupTableItems++; + return 1; } /** - * \brief if the user has tainted the hash, but wants to insert / search items by their key + * @ingroup HashListSort + * @brief if the user has tainted the hash, but wants to insert / search items by their key * we need to search linear through the array. You have been warned that this will take more time! - * \param Hash Our Hash to manipulate - * \param HashBinKey the Hash-Number to lookup. - * \returns the position (most closely) matching HashBinKey (-> Caller needs to compare! ) + * @param Hash Our Hash to manipulate + * @param HashBinKey the Hash-Number to lookup. + * @return the position (most closely) matching HashBinKey (-> Caller needs to compare! ) */ static long FindInTaintedHash(HashList *Hash, long HashBinKey) { @@ -349,7 +550,7 @@ static long FindInTaintedHash(HashList *Hash, long HashBinKey) if (Hash == NULL) return 0; - for (SearchPos = 0; SearchPos < Hash->nMembersUsed; SearchPos ++) { + for (SearchPos = 0; SearchPos < Hash->nLookupTableItems; SearchPos ++) { if (Hash->LookupTable[SearchPos]->Key == HashBinKey){ return SearchPos; } @@ -358,10 +559,11 @@ static long FindInTaintedHash(HashList *Hash, long HashBinKey) } /** - * \brief Private function to lookup the Item / the closest position to put it in - * \param Hash Our Hash to manipulate - * \param HashBinKey the Hash-Number to lookup. - * \returns the position (most closely) matching HashBinKey (-> Caller needs to compare! ) + * @ingroup HashListPrivate + * @brief Private function to lookup the Item / the closest position to put it in + * @param Hash Our Hash to manipulate + * @param HashBinKey the Hash-Number to lookup. + * @return the position (most closely) matching HashBinKey (-> Caller needs to compare! ) */ static long FindInHash(HashList *Hash, long HashBinKey) { @@ -374,10 +576,10 @@ static long FindInHash(HashList *Hash, long HashBinKey) if (Hash->tainted) return FindInTaintedHash(Hash, HashBinKey); - SearchPos = Hash->nMembersUsed / 2; + SearchPos = Hash->nLookupTableItems / 2; StepWidth = SearchPos / 2; while ((SearchPos > 0) && - (SearchPos < Hash->nMembersUsed)) + (SearchPos < Hash->nLookupTableItems)) { /** Did we find it? */ if (Hash->LookupTable[SearchPos]->Key == HashBinKey){ @@ -399,7 +601,7 @@ static long FindInHash(HashList *Hash, long HashBinKey) SearchPos --; } else { - if ((SearchPos + 1 < Hash->nMembersUsed) && + if ((SearchPos + 1 < Hash->nLookupTableItems) && (Hash->LookupTable[SearchPos + 1]->Key > HashBinKey)) return SearchPos; SearchPos ++; @@ -412,23 +614,51 @@ static long FindInHash(HashList *Hash, long HashBinKey) /** - * \brief another hashing algorithm; treat it as just a pointer to long. - * \param str Our pointer to the long value - * \param len the length of the data pointed to; needs to be sizeof int, else we won't use it! - * \returns the calculated hash value + * @ingroup HashListAlgorithm + * @brief another hashing algorithm; treat it as just a pointer to int. + * @param str Our pointer to the int value + * @param len the length of the data pointed to; needs to be sizeof int, else we won't use it! + * @return the calculated hash value */ -int Flathash(const char *str, long len) +long Flathash(const char *str, long len) { if (len != sizeof (int)) + { +#ifdef DEBUG + int *crash = NULL; + *crash = 1; +#endif return 0; + } else return *(int*)str; } /** - * \brief private abstract wrapper around the hashing algorithm - * \param HKey the hash string - * \param HKLen length of HKey - * \returns the calculated hash value + * @ingroup HashListAlgorithm + * @brief another hashing algorithm; treat it as just a pointer to long. + * @param str Our pointer to the long value + * @param len the length of the data pointed to; needs to be sizeof long, else we won't use it! + * @return the calculated hash value + */ +long lFlathash(const char *str, long len) +{ + if (len != sizeof (long)) + { +#ifdef DEBUG + int *crash = NULL; + *crash = 1; +#endif + return 0; + } + else return *(long*)str; +} + +/** + * @ingroup HashListPrivate + * @brief private abstract wrapper around the hashing algorithm + * @param HKey the hash string + * @param HKLen length of HKey + * @return the calculated hash value */ inline static long CalcHashKey (HashList *Hash, const char *HKey, long HKLen) { @@ -443,12 +673,13 @@ inline static long CalcHashKey (HashList *Hash, const char *HKey, long HKLen) /** - * \brief Add a new / Replace an existing item in the Hash - * \param HashList the list to manipulate - * \param HKey the hash-string to store Data under - * \param HKeyLen Length of HKey - * \param Data the payload you want to associate with HKey - * \param DeleteIt if not free() should be used to delete Data set to NULL, else DeleteIt is used. + * @ingroup HashListAccess + * @brief Add a new / Replace an existing item in the Hash + * @param Hash the list to manipulate + * @param HKey the hash-string to store Data under + * @param HKLen Length of HKey + * @param Data the payload you want to associate with HKey + * @param DeleteIt if not free() should be used to delete Data set to NULL, else DeleteIt is used. */ void Put(HashList *Hash, const char *HKey, long HKLen, void *Data, DeleteHashDataFunc DeleteIt) { @@ -462,16 +693,17 @@ void Put(HashList *Hash, const char *HKey, long HKLen, void *Data, DeleteHashDat HashBinKey = CalcHashKey(Hash, HKey, HKLen); HashAt = FindInHash(Hash, HashBinKey); - if (HashAt >= Hash->MemberSize) - IncreaseHashSize (Hash); + if ((HashAt >= Hash->MemberSize) && + (!IncreaseHashSize (Hash))) + return; /** oh, we're brand new... */ if (Hash->LookupTable[HashAt] == NULL) { InsertHashItem(Hash, HashAt, HashBinKey, HKey, HKLen, Data, DeleteIt); - }/** Insert After? */ + }/** Insert Before? */ else if (Hash->LookupTable[HashAt]->Key > HashBinKey) { InsertHashItem(Hash, HashAt, HashBinKey, HKey, HKLen, Data, DeleteIt); - }/** Insert before? */ + }/** Insert After? */ else if (Hash->LookupTable[HashAt]->Key < HashBinKey) { InsertHashItem(Hash, HashAt + 1, HashBinKey, HKey, HKLen, Data, DeleteIt); } @@ -491,12 +723,13 @@ void Put(HashList *Hash, const char *HKey, long HKLen, void *Data, DeleteHashDat } /** - * \brief Lookup the Data associated with HKey - * \param Hash the Hashlist to search in - * \param HKey the hashkey to look up - * \param HKLen length of HKey - * \param Data returns the Data associated with HKey - * \returns 0 if not found, 1 if. + * @ingroup HashListAccess + * @brief Lookup the Data associated with HKey + * @param Hash the Hashlist to search in + * @param HKey the hashkey to look up + * @param HKLen length of HKey + * @param Data returns the Data associated with HKey + * @return 0 if not found, 1 if. */ int GetHash(HashList *Hash, const char *HKey, long HKLen, void **Data) { @@ -514,7 +747,7 @@ int GetHash(HashList *Hash, const char *HKey, long HKLen, void **Data) HashBinKey = CalcHashKey(Hash, HKey, HKLen); HashAt = FindInHash(Hash, HashBinKey); if ((HashAt < 0) || /**< Not found at the lower edge? */ - (HashAt >= Hash->nMembersUsed) || /**< Not found at the upper edge? */ + (HashAt >= Hash->nLookupTableItems) || /**< Not found at the upper edge? */ (Hash->LookupTable[HashAt]->Key != HashBinKey)) { /**< somewhere inbetween but no match? */ *Data = NULL; return 0; @@ -535,47 +768,57 @@ int GetKey(HashList *Hash, char *HKey, long HKLen, void **Payload) } /** - * \brief get the Keys present in this hash, simila to array_keys() in PHP + * @ingroup HashListAccess + * @brief get the Keys present in this hash, similar to array_keys() in PHP * Attention: List remains to Hash! don't modify or free it! - * \param Hash Your Hashlist to extract the keys from - * \param List returns the list of hashkeys stored in Hash + * @param Hash Your Hashlist to extract the keys from + * @param List returns the list of hashkeys stored in Hash */ int GetHashKeys(HashList *Hash, char ***List) { long i; + + *List = NULL; if (Hash == NULL) return 0; if (Hash->MyKeys != NULL) free (Hash->MyKeys); - Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nMembersUsed); - for (i=0; i < Hash->nMembersUsed; i++) { - + Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nLookupTableItems); + if (Hash->MyKeys == NULL) + return 0; + + for (i=0; i < Hash->nLookupTableItems; i++) + { Hash->MyKeys[i] = Hash->LookupTable[i]->HashKey; } *List = (char**)Hash->MyKeys; - return Hash->nMembersUsed; + return Hash->nLookupTableItems; } /** - * \brief creates a hash-linear iterator object - * \param Hash the list we reference - * \param in which step width should we iterate? + * @ingroup HashListAccess + * @brief creates a hash-linear iterator object + * @param Hash the list we reference + * @param StepWidth in which step width should we iterate? * If negative, the last position matching the * step-raster is provided. - * \returns the hash iterator + * @return the hash iterator */ -HashPos *GetNewHashPos(HashList *Hash, int StepWidth) +HashPos *GetNewHashPos(const HashList *Hash, int StepWidth) { HashPos *Ret; Ret = (HashPos*)malloc(sizeof(HashPos)); + if (Ret == NULL) + return NULL; + if (StepWidth != 0) Ret->StepWidth = StepWidth; else Ret->StepWidth = 1; if (Ret->StepWidth < 0) { - Ret->Position = Hash->nMembersUsed - 1; + Ret->Position = Hash->nLookupTableItems - 1; } else { Ret->Position = 0; @@ -584,22 +827,139 @@ HashPos *GetNewHashPos(HashList *Hash, int StepWidth) } /** - * \brief retrieve the counter from the itteratoor - * \param the Iterator to analyze - * \returns the n'th hashposition we point at + * @ingroup HashListAccess + * @brief resets a hash-linear iterator object + * @param Hash the list we reference + * @param StepWidth in which step width should we iterate? + * @param it the iterator object to manipulate + * If negative, the last position matching the + * step-raster is provided. + * @return the hash iterator + */ +void RewindHashPos(const HashList *Hash, HashPos *it, int StepWidth) +{ + if (StepWidth != 0) + it->StepWidth = StepWidth; + else + it->StepWidth = 1; + if (it->StepWidth < 0) { + it->Position = Hash->nLookupTableItems - 1; + } + else { + it->Position = 0; + } +} + +/** + * @ingroup HashListAccess + * @brief Set iterator object to point to key. If not found, don't change iterator + * @param Hash the list we reference + * @param HKey key to search for + * @param HKLen length of key + * @param At HashPos to update + * @return 0 if not found + */ +int GetHashPosFromKey(HashList *Hash, const char *HKey, long HKLen, HashPos *At) +{ + long HashBinKey; + long HashAt; + + if (Hash == NULL) + return 0; + + if (HKLen <= 0) { + return 0; + } + /** first, find out were we could be... */ + HashBinKey = CalcHashKey(Hash, HKey, HKLen); + HashAt = FindInHash(Hash, HashBinKey); + if ((HashAt < 0) || /**< Not found at the lower edge? */ + (HashAt >= Hash->nLookupTableItems) || /**< Not found at the upper edge? */ + (Hash->LookupTable[HashAt]->Key != HashBinKey)) { /**< somewhere inbetween but no match? */ + return 0; + } + /** GOTCHA! */ + At->Position = HashAt; + return 1; +} + +/** + * @ingroup HashListAccess + * @brief Delete from the Hash the entry at Position + * @param Hash the list we reference + * @param At the position within the Hash + * @return 0 if not found + */ +int DeleteEntryFromHash(HashList *Hash, HashPos *At) +{ + Payload *FreeMe; + if (Hash == NULL) + return 0; + + /* if lockable, lock here */ + if ((Hash == NULL) || + (At->Position >= Hash->nLookupTableItems) || + (At->Position < 0) || + (At->Position > Hash->nLookupTableItems)) + { + /* unlock... */ + return 0; + } + + FreeMe = Hash->Members[Hash->LookupTable[At->Position]->Position]; + Hash->Members[Hash->LookupTable[At->Position]->Position] = NULL; + + + /** delete our hashing data */ + if (Hash->LookupTable[At->Position] != NULL) + { + free(Hash->LookupTable[At->Position]->HashKey); + free(Hash->LookupTable[At->Position]); + if (At->Position < Hash->nLookupTableItems) + { + memmove(&Hash->LookupTable[At->Position], + &Hash->LookupTable[At->Position + 1], + (Hash->nLookupTableItems - At->Position - 1) * + sizeof(HashKey*)); + + Hash->LookupTable[Hash->nLookupTableItems - 1] = NULL; + } + else + Hash->LookupTable[At->Position] = NULL; + Hash->nLookupTableItems--; + } + /* unlock... */ + + + /** get rid of our payload */ + if (FreeMe != NULL) + { + DeleteHashPayload(FreeMe); + free(FreeMe); + } + return 1; +} + +/** + * @ingroup HashListAccess + * @brief retrieve the counter from the itteratoor + * @param Hash which + * @param At the Iterator to analyze + * @return the n'th hashposition we point at */ int GetHashPosCounter(HashList *Hash, HashPos *At) { if ((Hash == NULL) || - (At->Position >= Hash->nMembersUsed) || + (At->Position >= Hash->nLookupTableItems) || (At->Position < 0) || - (At->Position > Hash->nMembersUsed)) + (At->Position > Hash->nLookupTableItems)) return 0; return At->Position; } /** - * \brief frees a linear hash iterator + * @ingroup HashListAccess + * @brief frees a linear hash iterator */ void DeleteHashPos(HashPos **DelMe) { @@ -612,30 +972,30 @@ void DeleteHashPos(HashPos **DelMe) /** - * \brief Get the data located where HashPos Iterator points at, and Move HashPos one forward - * \param Hash your Hashlist to follow - * \param HKLen returns Length of Hashkey Returned - * \param HashKey returns the Hashkey corrosponding to HashPos - * \param Data returns the Data found at HashPos - * \returns whether the item was found or not. + * @ingroup HashListAccess + * @brief Get the data located where HashPos Iterator points at, and Move HashPos one forward + * @param Hash your Hashlist to follow + * @param At the position to retrieve the Item from and move forward afterwards + * @param HKLen returns Length of Hashkey Returned + * @param HashKey returns the Hashkey corrosponding to HashPos + * @param Data returns the Data found at HashPos + * @return whether the item was found or not. */ -int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data) +int GetNextHashPos(const HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data) { long PayloadPos; - long offset = 0; if ((Hash == NULL) || - (At->Position >= Hash->nMembersUsed) || + (At->Position >= Hash->nLookupTableItems) || (At->Position < 0) || - (At->Position > Hash->nMembersUsed)) + (At->Position > Hash->nLookupTableItems)) return 0; *HKLen = Hash->LookupTable[At->Position]->HKLen; *HashKey = Hash->LookupTable[At->Position]->HashKey; PayloadPos = Hash->LookupTable[At->Position]->Position; *Data = Hash->Members[PayloadPos]->Data; + /* Position is NULL-Based, while Stepwidth is not... */ - if (At->StepWidth < 0) - offset = 1; if ((At->Position % abs(At->StepWidth)) == 0) At->Position += At->StepWidth; else @@ -645,13 +1005,68 @@ int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKe } /** - * \brief Get the data located where At points to + * @ingroup HashListAccess + * @brief Get the data located where HashPos Iterator points at + * @param Hash your Hashlist to follow + * @param At the position retrieve the data from + * @param HKLen returns Length of Hashkey Returned + * @param HashKey returns the Hashkey corrosponding to HashPos + * @param Data returns the Data found at HashPos + * @return whether the item was found or not. + */ +int GetHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data) +{ + long PayloadPos; + + if ((Hash == NULL) || + (At->Position >= Hash->nLookupTableItems) || + (At->Position < 0) || + (At->Position > Hash->nLookupTableItems)) + return 0; + *HKLen = Hash->LookupTable[At->Position]->HKLen; + *HashKey = Hash->LookupTable[At->Position]->HashKey; + PayloadPos = Hash->LookupTable[At->Position]->Position; + *Data = Hash->Members[PayloadPos]->Data; + + return 1; +} + +/** + * @ingroup HashListAccess + * @brief Move HashPos one forward + * @param Hash your Hashlist to follow + * @param At the position to move forward + * @return whether there is a next item or not. + */ +int NextHashPos(HashList *Hash, HashPos *At) +{ + if ((Hash == NULL) || + (At->Position >= Hash->nLookupTableItems) || + (At->Position < 0) || + (At->Position > Hash->nLookupTableItems)) + return 0; + + /* Position is NULL-Based, while Stepwidth is not... */ + if ((At->Position % abs(At->StepWidth)) == 0) + At->Position += At->StepWidth; + else + At->Position += ((At->Position) % abs(At->StepWidth)) * + (At->StepWidth / abs(At->StepWidth)); + return !((At->Position >= Hash->nLookupTableItems) || + (At->Position < 0) || + (At->Position > Hash->nLookupTableItems)); +} + +/** + * @ingroup HashListAccess + * @brief Get the data located where At points to * note: you should prefer iterator operations instead of using me. - * \param Hash your Hashlist peek from - * \param HKLen returns Length of Hashkey Returned - * \param HashKey returns the Hashkey corrosponding to HashPos - * \param Data returns the Data found at HashPos - * \returns whether the item was found or not. + * @param Hash your Hashlist peek from + * @param At get the item in the position At. + * @param HKLen returns Length of Hashkey Returned + * @param HashKey returns the Hashkey corrosponding to HashPos + * @param Data returns the Data found at HashPos + * @return whether the item was found or not. */ int GetHashAt(HashList *Hash,long At, long *HKLen, const char **HashKey, void **Data) { @@ -659,7 +1074,7 @@ int GetHashAt(HashList *Hash,long At, long *HKLen, const char **HashKey, void ** if ((Hash == NULL) || (At < 0) || - (At > Hash->nMembersUsed)) + (At >= Hash->nLookupTableItems)) return 0; *HKLen = Hash->LookupTable[At]->HKLen; *HashKey = Hash->LookupTable[At]->HashKey; @@ -669,9 +1084,33 @@ int GetHashAt(HashList *Hash,long At, long *HKLen, const char **HashKey, void ** } /** - * \brief sorting function for sorting the Hash alphabeticaly by their strings - * \param Key1 first item - * \param Key2 second item + * @ingroup HashListSort + * @brief Get the data located where At points to + * note: you should prefer iterator operations instead of using me. + * @param Hash your Hashlist peek from + * @param HKLen returns Length of Hashkey Returned + * @param HashKey returns the Hashkey corrosponding to HashPos + * @param Data returns the Data found at HashPos + * @return whether the item was found or not. + */ +/* +long GetHashIDAt(HashList *Hash,long At) +{ + if ((Hash == NULL) || + (At < 0) || + (At > Hash->nLookupTableItems)) + return 0; + + return Hash->LookupTable[At]->Key; +} +*/ + + +/** + * @ingroup HashListSort + * @brief sorting function for sorting the Hash alphabeticaly by their strings + * @param Key1 first item + * @param Key2 second item */ static int SortByKeys(const void *Key1, const void* Key2) { @@ -683,9 +1122,10 @@ static int SortByKeys(const void *Key1, const void* Key2) } /** - * \brief sorting function for sorting the Hash alphabeticaly reverse by their strings - * \param Key1 first item - * \param Key2 second item + * @ingroup HashListSort + * @brief sorting function for sorting the Hash alphabeticaly reverse by their strings + * @param Key1 first item + * @param Key2 second item */ static int SortByKeysRev(const void *Key1, const void* Key2) { @@ -697,9 +1137,10 @@ static int SortByKeysRev(const void *Key1, const void* Key2) } /** - * \brief sorting function to regain hash-sequence and revert tainted status - * \param Key1 first item - * \param Key2 second item + * @ingroup HashListSort + * @brief sorting function to regain hash-sequence and revert tainted status + * @param Key1 first item + * @param Key2 second item */ static int SortByHashKeys(const void *Key1, const void* Key2) { @@ -712,39 +1153,42 @@ static int SortByHashKeys(const void *Key1, const void* Key2) /** - * \brief sort the hash alphabeticaly by their keys. + * @ingroup HashListSort + * @brief sort the hash alphabeticaly by their keys. * Caution: This taints the hashlist, so accessing it later * will be significantly slower! You can un-taint it by SortByHashKeyStr - * \param Hash the list to sort - * \param Order 0/1 Forward/Backward + * @param Hash the list to sort + * @param Order 0/1 Forward/Backward */ void SortByHashKey(HashList *Hash, int Order) { - if (Hash->nMembersUsed < 2) + if (Hash->nLookupTableItems < 2) return; - qsort(Hash->LookupTable, Hash->nMembersUsed, sizeof(HashKey*), + qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*), (Order)?SortByKeys:SortByKeysRev); Hash->tainted = 1; } /** - * \brief sort the hash by their keys (so it regains untainted state). + * @ingroup HashListSort + * @brief sort the hash by their keys (so it regains untainted state). * this will result in the sequence the hashing allgorithm produces it by default. - * \param Hash the list to sort + * @param Hash the list to sort */ void SortByHashKeyStr(HashList *Hash) { Hash->tainted = 0; - if (Hash->nMembersUsed < 2) + if (Hash->nLookupTableItems < 2) return; - qsort(Hash->LookupTable, Hash->nMembersUsed, sizeof(HashKey*), SortByHashKeys); + qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*), SortByHashKeys); } /** - * \brief gives user sort routines access to the hash payload - * \param Searchentry to retrieve Data to - * \returns Data belonging to HashVoid + * @ingroup HashListSort + * @brief gives user sort routines access to the hash payload + * @param HashVoid to retrieve Data to + * @return Data belonging to HashVoid */ const void *GetSearchPayload(const void *HashVoid) { @@ -752,16 +1196,17 @@ const void *GetSearchPayload(const void *HashVoid) } /** - * \brief sort the hash by your sort function. see the following sample. + * @ingroup HashListSort + * @brief sort the hash by your sort function. see the following sample. * this will result in the sequence the hashing allgorithm produces it by default. - * \param Hash the list to sort - * \param SortBy Sortfunction; see below how to implement this + * @param Hash the list to sort + * @param SortBy Sortfunction; see below how to implement this */ void SortByPayload(HashList *Hash, CompareFunc SortBy) { - if (Hash->nMembersUsed < 2) + if (Hash->nLookupTableItems < 2) return; - qsort(Hash->LookupTable, Hash->nMembersUsed, sizeof(HashKey*), SortBy); + qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*), SortBy); Hash->tainted = 1; } @@ -781,17 +1226,9 @@ void SortByPayload(HashList *Hash, CompareFunc SortBy) */ -/* - * Generic function to free a pointer. This can be used as a callback with the - * hash table, even on systems where free() is defined as a macro or has had other - * horrible things done to it. - */ -void generic_free_handler(void *ptr) { - free(ptr); -} - -/* - * Generic function to free a reference. +/** + * @ingroup HashListAccess + * @brief Generic function to free a reference. * since a reference actualy isn't needed to be freed, do nothing. */ void reference_free_handler(void *ptr) @@ -800,10 +1237,142 @@ void reference_free_handler(void *ptr) } -/* +/** + * @ingroup HashListAlgorithm * This exposes the hashlittle() function to consumers. */ int HashLittle(const void *key, size_t length) { return (int)hashlittle(key, length, 1); } + +/** + * @ingroup HashListMset + * @brief parses an MSet string into a list for later use + * @param MSetList List to be read from MSetStr + * @param MSetStr String containing the list + */ +int ParseMSet(MSet **MSetList, StrBuf *MSetStr) +{ + const char *POS = NULL, *SetPOS = NULL; + StrBuf *OneSet; + HashList *ThisMSet; + long StartSet, EndSet; + long *pEndSet; + + *MSetList = NULL; + if ((MSetStr == NULL) || (StrLength(MSetStr) == 0)) + return 0; + + OneSet = NewStrBufPlain(NULL, StrLength(MSetStr)); + if (OneSet == NULL) + return 0; + + ThisMSet = NewHash(0, lFlathash); + if (ThisMSet == NULL) + { + FreeStrBuf(&OneSet); + return 0; + } + + *MSetList = (MSet*) ThisMSet; + + /* an MSet is a coma separated value list. */ + StrBufExtract_NextToken(OneSet, MSetStr, &POS, ','); + do { + SetPOS = NULL; + + /* One set may consist of two Numbers: Start + optional End */ + StartSet = StrBufExtractNext_long(OneSet, &SetPOS, ':'); + EndSet = 0; /* no range is our default. */ + /* do we have an end (aka range?) */ + if ((SetPOS != NULL) && (SetPOS != StrBufNOTNULL)) + { + if (*(SetPOS) == '*') + EndSet = LONG_MAX; /* ranges with '*' go until infinity */ + else + /* in other cases, get the EndPoint */ + EndSet = StrBufExtractNext_long(OneSet, &SetPOS, ':'); + } + + pEndSet = (long*) malloc (sizeof(long)); + if (pEndSet == NULL) + { + FreeStrBuf(&OneSet); + DeleteHash(&ThisMSet); + return 0; + } + *pEndSet = EndSet; + + Put(ThisMSet, LKEY(StartSet), pEndSet, NULL); + /* if we don't have another, we're done. */ + if (POS == StrBufNOTNULL) + break; + StrBufExtract_NextToken(OneSet, MSetStr, &POS, ','); + } while (1); + FreeStrBuf(&OneSet); + + return 1; +} + +/** + * @ingroup HashListMset + * @brief checks whether a message is inside a mset + * @param MSetList List to search for MsgNo + * @param MsgNo number to search in mset + */ +int IsInMSetList(MSet *MSetList, long MsgNo) +{ + /* basicaly we are a ... */ + long MemberPosition; + HashList *Hash = (HashList*) MSetList; + long HashAt; + long EndAt; + long StartAt; + + if (Hash == NULL) + return 0; + if (Hash->MemberSize == 0) + return 0; + /** first, find out were we could fit in... */ + HashAt = FindInHash(Hash, MsgNo); + + /* we're below the first entry, so not found. */ + if (HashAt < 0) + return 0; + /* upper edge? move to last item */ + if (HashAt >= Hash->nMembersUsed) + HashAt = Hash->nMembersUsed - 1; + /* Match? then we got it. */ + else if (Hash->LookupTable[HashAt]->Key == MsgNo) + return 1; + /* One above possible range start? we need to move to the lower one. */ + else if ((HashAt > 0) && + (Hash->LookupTable[HashAt]->Key > MsgNo)) + HashAt -=1; + + /* Fetch the actual data */ + StartAt = Hash->LookupTable[HashAt]->Key; + MemberPosition = Hash->LookupTable[HashAt]->Position; + EndAt = *(long*) Hash->Members[MemberPosition]->Data; + if ((MsgNo >= StartAt) && (EndAt == LONG_MAX)) + return 1; + /* no range? */ + if (EndAt == 0) + return 0; + /* inside of range? */ + if ((StartAt <= MsgNo) && (EndAt >= MsgNo)) + return 1; + return 0; +} + + +/** + * @ingroup HashListMset + * @brief frees a mset [redirects to @ref DeleteHash + * @param FreeMe to be free'd + */ +void DeleteMSet(MSet **FreeMe) +{ + DeleteHash((HashList**) FreeMe); +}