]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/hash.c
* HashLittle() function
[citadel.git] / libcitadel / lib / hash.c
index 937aa32e0922af063e998e9b08c345b6fc4c12ef..67ea9332d2ad8de73d406eb810092aadf0c281e2 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;
 };
 
 
@@ -132,8 +133,12 @@ int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Secon
                        foo = Hash->LookupTable[i]->HashKey;
                        if (First != NULL)
                                bar = First(Hash->Members[Hash->LookupTable[i]->Position]->Data);
+                       else 
+                               bar = "";
                        if (Second != NULL)
                                bla = Second(Hash->Members[Hash->LookupTable[i]->Position]->Data);
+                       else
+                               bla = "";
                }
 #ifdef DEBUG
                printf (" ---- Hashkey[%ld][%ld]: '%s' Value: '%s' ; %s\n", i, key, foo, bar, bla);
@@ -179,6 +184,7 @@ int GetCount(HashList *Hash)
 
 /**
  * \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
  */
 static void DeleteHashPayload (Payload *Data)
@@ -190,8 +196,18 @@ static void DeleteHashPayload (Payload *Data)
                free(Data->Data);
 }
 
+/**
+ * \brief Destructor for nested hashes
+ */
+void HDeleteHash(void *vHash)
+{
+       HashList *FreeMe = (HashList*)vHash;
+       DeleteHash(&FreeMe);
+}
+
 /**
  * \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)
@@ -394,6 +410,20 @@ static long FindInHash(HashList *Hash, long HashBinKey)
        return SearchPos;
 }
 
+
+/**
+ * \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
+ */
+int Flathash(const char *str, long len)
+{
+       if (len != sizeof (int))
+               return 0;
+       else return *(int*)str;
+}
+
 /**
  * \brief private abstract wrapper around the hashing algorithm
  * \param HKey the hash string
@@ -529,17 +559,40 @@ 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 - 1;
+       }
+       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
  */
@@ -561,17 +614,55 @@ void DeleteHashPos(HashPos **DelMe)
  * \param Data returns the Data found at HashPos
  * \returns whether the item was found or not.
  */
-int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, char **HashKey, void **Data)
+int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data)
 {
        long PayloadPos;
+       long offset = 0;
 
-       if ((Hash == NULL) || (Hash->nMembersUsed <= At->Position))
+       if ((Hash == NULL) || (At->Position >= Hash->nMembersUsed) || (At->Position < 0))
                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++;
+       /* 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 
+               At->Position += ((At->Position) % abs(At->StepWidth)) * 
+                       (At->StepWidth / abs(At->StepWidth));
+
+       if (At->Position > Hash->nMembersUsed) {
+               At->Position = Hash->nMembersUsed - 1;
+               return 0;
+       } else if (At->Position <= 0) {
+               At->Position = 0;
+               return 0;
+       }
+       return 1;
+}
+
+/**
+ * \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.
+ */
+int GetHashAt(HashList *Hash,long At, long *HKLen, const char **HashKey, void **Data)
+{
+       long PayloadPos;
+
+       if ((Hash == NULL) || (At >= Hash->nMembersUsed))
+               return 0;
+       *HKLen = Hash->LookupTable[At]->HKLen;
+       *HashKey = Hash->LookupTable[At]->HashKey;
+       PayloadPos = Hash->LookupTable[At]->Position;
+       *Data = Hash->Members[PayloadPos]->Data;
        return 1;
 }
 
@@ -686,3 +777,31 @@ void SortByPayload(HashList *Hash, CompareFunc SortBy)
  *      return strcmp (a, b);
  * }
  */
+
+
+/*
+ * 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.  
+ * since a reference actualy isn't needed to be freed, do nothing.
+ */
+void reference_free_handler(void *ptr) 
+{
+       return;
+}
+
+
+/*
+ * This exposes the hashlittle() function to consumers.
+ */
+int HashLittle(const void *key, size_t length) {
+       return (int)hashlittle(key, length, 1);
+}
+