For peer review.
authorDave West <davew@uncensored.citadel.org>
Thu, 3 Dec 2009 20:00:03 +0000 (20:00 +0000)
committerDave West <davew@uncensored.citadel.org>
Thu, 3 Dec 2009 20:00:03 +0000 (20:00 +0000)
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
libcitadel/lib/libcitadel.h

index be29055a195e19b9dc2a3dda5b630497cb1efd29..33e3cfadea18e4c245d8c09e3c10f61563b9cbd9 100644 (file)
@@ -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
index 258d76045432e8d7c67d3574c73172ae7ae70865..f76fd9e758fe8b6f4bc5fe91e033b715173f54ba 100644 (file)
@@ -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);