From 6317f6941770e6b9b21d5fb1e0053cb1d3fb1a46 Mon Sep 17 00:00:00 2001 From: Dave West Date: Thu, 3 Dec 2009 20:00:03 +0000 Subject: [PATCH] For peer review. Added two new hash manipulation functions. int SetHashPosFromKey(HashList *Hash, const char *HKey, long HKLen, HashPos *At); int DeleteEntryFromHash(HashList *Hash, HashPos *At); First one searches for the Key and updates the HashPos to point at it. Second one deletes an entry from the hash, entry is pointed to by HashPos --- libcitadel/lib/hash.c | 65 +++++++++++++++++++++++++++++++++++++ libcitadel/lib/libcitadel.h | 2 ++ 2 files changed, 67 insertions(+) diff --git a/libcitadel/lib/hash.c b/libcitadel/lib/hash.c index be29055a1..33e3cfade 100644 --- a/libcitadel/lib/hash.c +++ b/libcitadel/lib/hash.c @@ -584,6 +584,71 @@ HashPos *GetNewHashPos(HashList *Hash, int StepWidth) return Ret; } +/** + * @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 + * \returns 0 if not found + */ +int SetHashPosFromKey(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->nMembersUsed) || /**< Not found at the upper edge? */ + (Hash->LookupTable[HashAt]->Key != HashBinKey)) { /**< somewhere inbetween but no match? */ + return 0; + } + /** GOTCHA! */ + At->Position = Hash->LookupTable[HashAt]->Position; + return 1; +} + +/** + * @brief Delete from the Hash the entry at Position + * @param Hash the list we reference + * @param At the position within the Hash + * \returns 0 if not found + */ +int DeleteEntryFromHash(HashList *Hash, HashPos *At) +{ + if (Hash == NULL) + return 0; + + if ((Hash == NULL) || + (At->Position >= Hash->nMembersUsed) || + (At->Position < 0) || + (At->Position > Hash->nMembersUsed)) + return 0; + /** get rid of our payload */ + if (Hash->Members[At->Position] != NULL) + { + DeleteHashPayload(Hash->Members[At->Position]); + free(Hash->Members[At->Position]); + Hash->Members[At->Position] = NULL; + } + /** delete our hashing data */ + if (Hash->LookupTable[At->Position] != NULL) + { + free(Hash->LookupTable[At->Position]->HashKey); + free(Hash->LookupTable[At->Position]); + Hash->LookupTable[At->Position] = NULL; + } + return 1; +} + /** * @brief retrieve the counter from the itteratoor * @param the Iterator to analyze diff --git a/libcitadel/lib/libcitadel.h b/libcitadel/lib/libcitadel.h index 258d76045..f76fd9e75 100644 --- a/libcitadel/lib/libcitadel.h +++ b/libcitadel/lib/libcitadel.h @@ -447,6 +447,8 @@ int GetHashKeys(HashList *Hash, char ***List); int dbg_PrintHash(HashList *Hash, PrintHashContent first, PrintHashContent Second); int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry); HashPos *GetNewHashPos(HashList *Hash, int StepWidth); +int SetHashPosFromKey(HashList *Hash, const char *HKey, long HKLen, HashPos *At); +int DeleteEntryFromHash(HashList *Hash, HashPos *At); int GetHashPosCounter(HashList *Hash, HashPos *At); void DeleteHashPos(HashPos **DelMe); int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data); -- 2.30.2