]> code.citadel.org Git - citadel.git/blobdiff - libcitadel/lib/hash.c
* Allow hashiterator to do bigger steps than 1 and to go reverse
[citadel.git] / libcitadel / lib / hash.c
index 33048384031b0276d674cfc9ad4bdd2720f8b219..fd8916f1757988627045b9dca75f7088e5b6025f 100644 (file)
@@ -46,8 +46,54 @@ struct HashPos {
         * \brief Anonymous Hash Iterator Object. used for traversing the whole array from outside 
         */
        long Position;
+       int StepWidth;
 };
 
+
+/**
+ * \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
+ */
+int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry)
+{
+       int i;
+       void *Previous;
+       void *Next;
+       const char* KeyStr;
+
+       if (Hash == NULL)
+               return 0;
+
+       for (i=0; i < Hash->nMembersUsed; i++) {
+               if (i==0) {
+                       Previous = NULL;
+               }
+               else {
+                       if (Hash->LookupTable[i - 1] == NULL)
+                               Previous = NULL;
+                       else
+                               Previous = Hash->Members[Hash->LookupTable[i-1]->Position]->Data;
+               }
+               if (Hash->LookupTable[i] == NULL) {
+                       KeyStr = "";
+                       Next = NULL;
+               }
+               else {
+                       Next = Hash->Members[Hash->LookupTable[i]->Position]->Data;
+                       KeyStr = Hash->LookupTable[i]->HashKey;
+               }
+
+               Trans(Previous, Next, i % 2);
+               PrintEntry(KeyStr, Next, i % 2);
+       }
+       return i;
+}
+
+
 /**
  * \brief verify the contents of a hash list; here for debugging purposes.
  * \param Hash your Hashlist structure
@@ -55,13 +101,17 @@ struct HashPos {
  * \param Second Functionpointer to allow you to print your payload
  * \returns 0
  */
-int PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Second)
+int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Second)
 {
        const char *foo;
        const char *bar;
        const char *bla = "";
        long key;
        long i;
+
+       if (Hash == NULL)
+               return 0;
+
        if (Hash->MyKeys != NULL)
                free (Hash->MyKeys);
 
@@ -83,8 +133,12 @@ int PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Second)
                        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);
@@ -121,8 +175,16 @@ HashList *NewHash(int Uniq, HashFunc F)
        return NewList;
 }
 
+int GetCount(HashList *Hash)
+{
+       if(Hash==NULL) return 0;
+       return Hash->nMembersUsed;
+}
+
+
 /**
  * \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)
@@ -134,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)
@@ -182,6 +254,9 @@ static void IncreaseHashSize(HashList *Hash)
        Payload **NewPayloadArea;
        HashKey **NewTable;
        
+       if (Hash == NULL)
+               return ;
+
        /** double our payload area */
        NewPayloadArea = (Payload**) malloc(sizeof(Payload*) * Hash->MemberSize * 2);
        memset(&NewPayloadArea[Hash->MemberSize], 0, sizeof(Payload*) * Hash->MemberSize);
@@ -221,6 +296,9 @@ static void InsertHashItem(HashList *Hash,
        Payload *NewPayloadItem;
        HashKey *NewHashKey;
 
+       if (Hash == NULL)
+               return;
+
        if (Hash->nMembersUsed >= Hash->MemberSize)
                IncreaseHashSize (Hash);
 
@@ -240,16 +318,14 @@ static void InsertHashItem(HashList *Hash,
        /** but if we should be sorted into a specific place... */
        if ((Hash->nMembersUsed != 0) && 
            (HashPos != Hash->nMembersUsed) ) {
-               long InsertAt;
                long ItemsAfter;
 
                ItemsAfter = Hash->nMembersUsed - HashPos;
-               InsertAt = HashPos;
                /** make space were we can fill us in */
                if (ItemsAfter > 0)
                {
-                       memmove(&Hash->LookupTable[InsertAt + 1],
-                               &Hash->LookupTable[InsertAt],
+                       memmove(&Hash->LookupTable[HashPos + 1],
+                               &Hash->LookupTable[HashPos],
                                ItemsAfter * sizeof(HashKey*));
                } 
        }
@@ -270,6 +346,9 @@ static long FindInTaintedHash(HashList *Hash, long HashBinKey)
 {
        long SearchPos;
 
+       if (Hash == NULL)
+               return 0;
+
        for (SearchPos = 0; SearchPos < Hash->nMembersUsed; SearchPos ++) {
                if (Hash->LookupTable[SearchPos]->Key == HashBinKey){
                        return SearchPos;
@@ -289,6 +368,9 @@ static long FindInHash(HashList *Hash, long HashBinKey)
        long SearchPos;
        long StepWidth;
 
+       if (Hash == NULL)
+               return 0;
+
        if (Hash->tainted)
                return FindInTaintedHash(Hash, HashBinKey);
 
@@ -328,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
@@ -336,6 +432,9 @@ static long FindInHash(HashList *Hash, long HashBinKey)
  */
 inline static long CalcHashKey (HashList *Hash, const char *HKey, long HKLen)
 {
+       if (Hash == NULL)
+               return 0;
+
        if (Hash->Algorithm == NULL)
                return hashlittle(HKey, HKLen, 9283457);
        else
@@ -356,6 +455,9 @@ void Put(HashList *Hash, const char *HKey, long HKLen, void *Data, DeleteHashDat
        long HashBinKey;
        long HashAt;
 
+       if (Hash == NULL)
+               return;
+
        /** first, find out were we could fit in... */
        HashBinKey = CalcHashKey(Hash, HKey, HKLen);
        HashAt = FindInHash(Hash, HashBinKey);
@@ -401,6 +503,9 @@ int GetHash(HashList *Hash, const char *HKey, long HKLen, void **Data)
        long HashBinKey;
        long HashAt;
 
+       if (Hash == NULL)
+               return 0;
+
        if (HKLen <= 0) {
                *Data = NULL;
                return  0;
@@ -438,6 +543,8 @@ int GetKey(HashList *Hash, char *HKey, long HKLen, void **Payload)
 int GetHashKeys(HashList *Hash, char ***List)
 {
        long i;
+       if (Hash == NULL)
+               return 0;
        if (Hash->MyKeys != NULL)
                free (Hash->MyKeys);
 
@@ -452,24 +559,51 @@ 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
  */
 void DeleteHashPos(HashPos **DelMe)
 {
-       free(*DelMe);
-       *DelMe = NULL;
+       if (*DelMe != NULL)
+       {
+               free(*DelMe);
+               *DelMe = NULL;
+       }
 }
 
 
@@ -481,17 +615,22 @@ 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;
 
-       if (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;
 }
 
@@ -509,6 +648,20 @@ static int SortByKeys(const void *Key1, const void* Key2)
        return strcasecmp(HKey1->HashKey, HKey2->HashKey);
 }
 
+/**
+ * \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)
+{
+       HashKey *HKey1, *HKey2;
+       HKey1 = *(HashKey**) Key1;
+       HKey2 = *(HashKey**) Key2;
+
+       return strcasecmp(HKey2->HashKey, HKey1->HashKey);
+}
+
 /**
  * \brief sorting function to regain hash-sequence and revert tainted status
  * \param Key1 first item
@@ -529,12 +682,14 @@ static int SortByHashKeys(const void *Key1, const void* Key2)
  * 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
  */
-void SortByHashKey(HashList *Hash)
+void SortByHashKey(HashList *Hash, int Order)
 {
        if (Hash->nMembersUsed < 2)
                return;
-       qsort(Hash->LookupTable, Hash->nMembersUsed, sizeof(HashKey*), SortByKeys);
+       qsort(Hash->LookupTable, Hash->nMembersUsed, sizeof(HashKey*), 
+             (Order)?SortByKeys:SortByKeysRev);
        Hash->tainted = 1;
 }
 
@@ -590,3 +745,25 @@ 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;
+}
+
+
+