* Allow hashiterator to do bigger steps than 1 and to go reverse
authorWilfried Göesgens <willi@citadel.org>
Sun, 7 Dec 2008 11:29:43 +0000 (11:29 +0000)
committerWilfried Göesgens <willi@citadel.org>
Sun, 7 Dec 2008 11:29:43 +0000 (11:29 +0000)
 -> API-Change

libcitadel/lib/hash.c
libcitadel/lib/libcitadel.h

index e9796343f273832ca10926b37ae5f279f9fd6d8e..fd8916f1757988627045b9dca75f7088e5b6025f 100644 (file)
@@ -46,6 +46,7 @@ struct HashPos {
         * \brief Anonymous Hash Iterator Object. used for traversing the whole array from outside 
         */
        long Position;
+       int StepWidth;
 };
 
 
@@ -558,17 +559,41 @@ int GetHashKeys(HashList *Hash, char ***List)
 
 /**
  * \brief creates a hash-linear iterator object
+ * \param Hash the list we reference
+ * \param in which step width should we iterate?
+ *  If negative, the last position matching the 
+ *  step-raster is provided.
  * \returns the hash iterator
  */
-HashPos *GetNewHashPos(void)
+HashPos *GetNewHashPos(HashList *Hash, int StepWidth)
 {
        HashPos *Ret;
        
        Ret = (HashPos*)malloc(sizeof(HashPos));
-       Ret->Position = 0;
+       if (StepWidth != 0)
+               Ret->StepWidth = StepWidth;
+       else
+               Ret->StepWidth = 1;
+       if (Ret->StepWidth <  0) {
+               Ret->Position = (Hash->nMembersUsed % (-Ret->StepWidth)) 
+                       * (-Ret->StepWidth);
+       }
+       else {
+               Ret->Position = 0;
+       }
        return Ret;
 }
 
+/**
+ * \brief retrieve the counter from the itteratoor
+ * \param the Iterator to analyze
+ * \returns the n'th hashposition we point at
+ */
+int GetHashPosCounter(HashPos *At)
+{
+       return At->Position;
+}
+
 /**
  * \brief frees a linear hash iterator
  */
@@ -594,13 +619,18 @@ int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKe
 {
        long PayloadPos;
 
-       if ((Hash == NULL) || (Hash->nMembersUsed <= At->Position))
+       if ((Hash == NULL) || (At->Position >= Hash->nMembersUsed))
                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;
-       At->Position++;
+
+       At->Position += At->StepWidth;
+       if (At->Position > Hash->nMembersUsed) {
+               At->Position = Hash->nMembersUsed;
+               return 0;
+       }
        return 1;
 }
 
@@ -732,7 +762,7 @@ void generic_free_handler(void *ptr) {
  */
 void reference_free_handler(void *ptr) 
 {
-       1;
+       return;
 }
 
 
index 28358c89a4cdd80596c69ca62dd988a2c9824d5d..cff82af47d738c3d2a60061781a1f4369f3de898 100644 (file)
@@ -402,7 +402,8 @@ int GetKey(HashList *Hash, char *HKey, long HKLen, void **Data);
 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(void);
+HashPos *GetNewHashPos(HashList *Hash, int StepWidth);
+int GetHashPosCounter(HashPos *At);
 void DeleteHashPos(HashPos **DelMe);
 int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data);
 void SortByHashKey(HashList *Hash, int Order);