* Eliminate generic_free_handler() since it is not needed
[citadel.git] / libcitadel / lib / hash.c
index 616429ca87904aabbe16f21abef5999a28585136..684dc466de08889b505ed8b7fa7f0dce82d6e486 100644 (file)
@@ -8,18 +8,19 @@
 
 typedef struct Payload Payload;
 
+/**
+ * @brief Hash Payload storage Structure; filled in linear.
+ */
 struct Payload {
-       /**
-        * \brief Hash Payload storage Structure; filled in linear.
-        */
        void *Data; /**< the Data belonging to this storage */
        DeleteHashDataFunc Destructor; /**< if we want to destroy Data, do it with this function. */
 };
 
+
+/**
+ * @brief Hash key element; sorted by key
+ */
 struct HashKey {
-        /**
-        * \brief Hash key element; sorted by key
-        */
        long Key;         /**< Numeric Hashkey comperator for hash sorting */
        long Position;    /**< Pointer to a Payload struct in the Payload Aray */
        char *HashKey;    /**< the Plaintext Hashkey */
@@ -27,35 +28,36 @@ struct HashKey {
        Payload *PL;      /**< pointer to our payload for sorting */
 };
 
+/**
+ * @brief Hash structure; holds arrays of Hashkey and Payload. 
+ */
 struct HashList {
-       /**
-        * \brief Hash structure; holds arrays of Hashkey and Payload. 
-        */
        Payload **Members;     /**< Our Payload members. This fills up linear */
        HashKey **LookupTable; /**< Hash Lookup table. Elements point to members, and are sorted by their hashvalue */
        char **MyKeys;         /**< this keeps the members for a call of GetHashKeys */
        HashFunc Algorithm;    /**< should we use an alternating algorithm to calc the hash values? */
        long nMembersUsed;     /**< how many pointers inside of the array are used? */
+       long nLookupTableItems; /**< how many items of the lookup table are used? */
        long MemberSize;       /**< how big is Members and LookupTable? */
        long tainted;          /**< if 0, we're hashed, else s.b. else sorted us in his own way. */
        long uniq;             /**< are the keys going to be uniq? */
 };
 
+/**
+ * @brief Anonymous Hash Iterator Object. used for traversing the whole array from outside 
+ */
 struct HashPos {
-       /**
-        * \brief Anonymous Hash Iterator Object. used for traversing the whole array from outside 
-        */
-       long Position;
-       int StepWidth;
+       long Position;        /**< Position inside of the hash */
+       int StepWidth;        /**< small? big? forward? backward? */
 };
 
 
 /**
- * \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.
+ * @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
+ * @param PrintEntry print entry one by one
  * \returns the number of items printed
  */
 int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry)
@@ -68,7 +70,7 @@ int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry
        if (Hash == NULL)
                return 0;
 
-       for (i=0; i < Hash->nMembersUsed; i++) {
+       for (i=0; i < Hash->nLookupTableItems; i++) {
                if (i==0) {
                        Previous = NULL;
                }
@@ -95,10 +97,10 @@ int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry
 
 
 /**
- * \brief verify the contents of a hash list; here for debugging purposes.
- * \param Hash your Hashlist structure
- * \param First Functionpointer to allow you to print your payload
- * \param Second Functionpointer to allow you to print your payload
+ * @brief verify the contents of a hash list; here for debugging purposes.
+ * @param Hash your Hashlist structure
+ * @param First Functionpointer to allow you to print your payload
+ * @param Second Functionpointer to allow you to print your payload
  * \returns 0
  */
 int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Second)
@@ -115,11 +117,11 @@ int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Secon
        if (Hash->MyKeys != NULL)
                free (Hash->MyKeys);
 
-       Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nMembersUsed);
+       Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nLookupTableItems);
 #ifdef DEBUG
        printf("----------------------------------\n");
 #endif
-       for (i=0; i < Hash->nMembersUsed; i++) {
+       for (i=0; i < Hash->nLookupTableItems; i++) {
                
                if (Hash->LookupTable[i] == NULL)
                {
@@ -152,7 +154,7 @@ int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Secon
 
 
 /**
- * \brief instanciate a new hashlist
+ * @brief instanciate a new hashlist
  * \returns the newly allocated list. 
  */
 HashList *NewHash(int Uniq, HashFunc F)
@@ -178,14 +180,14 @@ HashList *NewHash(int Uniq, HashFunc F)
 int GetCount(HashList *Hash)
 {
        if(Hash==NULL) return 0;
-       return Hash->nMembersUsed;
+       return Hash->nLookupTableItems;
 }
 
 
 /**
- * \brief private destructor for one hash element.
+ * @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
+ * @param Data an element to free using the user provided destructor, or just plain free
  */
 static void DeleteHashPayload (Payload *Data)
 {
@@ -197,7 +199,7 @@ static void DeleteHashPayload (Payload *Data)
 }
 
 /**
- * \brief Destructor for nested hashes
+ * @brief Destructor for nested hashes
  */
 void HDeleteHash(void *vHash)
 {
@@ -206,9 +208,9 @@ void HDeleteHash(void *vHash)
 }
 
 /**
- * \brief destroy a hashlist and all of its members
+ * @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.
+ * @param Hash Hash to destroy. Is NULL'ed so you are shure its done.
  */
 void DeleteHash(HashList **Hash)
 {
@@ -218,6 +220,7 @@ void DeleteHash(HashList **Hash)
        FreeMe = *Hash;
        if (FreeMe == NULL)
                return;
+       /* even if there are sparse members already deleted... */
        for (i=0; i < FreeMe->nMembersUsed; i++)
        {
                /** get rid of our payload */
@@ -245,8 +248,8 @@ void DeleteHash(HashList **Hash)
 }
 
 /**
- * \brief Private function to increase the hash size.
- * \param Hash the Hasharray to increase
+ * @brief Private function to increase the hash size.
+ * @param Hash the Hasharray to increase
  */
 static void IncreaseHashSize(HashList *Hash)
 {
@@ -257,6 +260,16 @@ static void IncreaseHashSize(HashList *Hash)
        if (Hash == NULL)
                return ;
 
+       /** If we grew to much, this might be the place to rehash and shrink again.
+       if ((Hash->NMembersUsed > Hash->nLookupTableItems) && 
+           ((Hash->NMembersUsed - Hash->nLookupTableItems) > 
+            (Hash->nLookupTableItems / 10)))
+       {
+
+
+       }
+       */
+
        /** double our payload area */
        NewPayloadArea = (Payload**) malloc(sizeof(Payload*) * Hash->MemberSize * 2);
        memset(&NewPayloadArea[Hash->MemberSize], 0, sizeof(Payload*) * Hash->MemberSize);
@@ -276,14 +289,14 @@ static void IncreaseHashSize(HashList *Hash)
 
 
 /**
- * \brief private function to add a new item to / replace an existing in -  the hashlist
+ * @brief private function to add a new item to / replace an existing in -  the hashlist
  * if the hash list is full, its re-alloced with double size.
  * \parame Hash our hashlist to manipulate
- * \param HashPos where should we insert / replace?
- * \param HashKeyStr the Hash-String
- * \param HKLen length of HashKeyStr
- * \param Data your Payload to add
- * \param Destructor Functionpointer to free Data. if NULL, default free() is used.
+ * @param HashPos where should we insert / replace?
+ * @param HashKeyStr the Hash-String
+ * @param HKLen length of HashKeyStr
+ * @param Data your Payload to add
+ * @param Destructor Functionpointer to free Data. if NULL, default free() is used.
  */
 static void InsertHashItem(HashList *Hash, 
                           long HashPos, 
@@ -316,11 +329,11 @@ static void InsertHashItem(HashList *Hash,
        /** our payload is queued at the end... */
        NewHashKey->Position = Hash->nMembersUsed;
        /** but if we should be sorted into a specific place... */
-       if ((Hash->nMembersUsed != 0) && 
-           (HashPos != Hash->nMembersUsed) ) {
+       if ((Hash->nLookupTableItems != 0) && 
+           (HashPos != Hash->nLookupTableItems) ) {
                long ItemsAfter;
 
-               ItemsAfter = Hash->nMembersUsed - HashPos;
+               ItemsAfter = Hash->nLookupTableItems - HashPos;
                /** make space were we can fill us in */
                if (ItemsAfter > 0)
                {
@@ -333,13 +346,14 @@ static void InsertHashItem(HashList *Hash,
        Hash->Members[Hash->nMembersUsed] = NewPayloadItem;
        Hash->LookupTable[HashPos] = NewHashKey;
        Hash->nMembersUsed++;
+       Hash->nLookupTableItems++;
 }
 
 /**
- * \brief if the user has tainted the hash, but wants to insert / search items by their key
+ * @brief if the user has tainted the hash, but wants to insert / search items by their key
  *  we need to search linear through the array. You have been warned that this will take more time!
- * \param Hash Our Hash to manipulate
- * \param HashBinKey the Hash-Number to lookup. 
+ * @param Hash Our Hash to manipulate
+ * @param HashBinKey the Hash-Number to lookup. 
  * \returns the position (most closely) matching HashBinKey (-> Caller needs to compare! )
  */
 static long FindInTaintedHash(HashList *Hash, long HashBinKey)
@@ -349,7 +363,7 @@ static long FindInTaintedHash(HashList *Hash, long HashBinKey)
        if (Hash == NULL)
                return 0;
 
-       for (SearchPos = 0; SearchPos < Hash->nMembersUsed; SearchPos ++) {
+       for (SearchPos = 0; SearchPos < Hash->nLookupTableItems; SearchPos ++) {
                if (Hash->LookupTable[SearchPos]->Key == HashBinKey){
                        return SearchPos;
                }
@@ -358,9 +372,9 @@ static long FindInTaintedHash(HashList *Hash, long HashBinKey)
 }
 
 /**
- * \brief Private function to lookup the Item / the closest position to put it in
- * \param Hash Our Hash to manipulate
- * \param HashBinKey the Hash-Number to lookup. 
+ * @brief Private function to lookup the Item / the closest position to put it in
+ * @param Hash Our Hash to manipulate
+ * @param HashBinKey the Hash-Number to lookup. 
  * \returns the position (most closely) matching HashBinKey (-> Caller needs to compare! )
  */
 static long FindInHash(HashList *Hash, long HashBinKey)
@@ -374,10 +388,10 @@ static long FindInHash(HashList *Hash, long HashBinKey)
        if (Hash->tainted)
                return FindInTaintedHash(Hash, HashBinKey);
 
-       SearchPos = Hash->nMembersUsed / 2;
+       SearchPos = Hash->nLookupTableItems / 2;
        StepWidth = SearchPos / 2;
        while ((SearchPos > 0) && 
-              (SearchPos < Hash->nMembersUsed)) 
+              (SearchPos < Hash->nLookupTableItems)) 
        {
                /** Did we find it? */
                if (Hash->LookupTable[SearchPos]->Key == HashBinKey){
@@ -399,7 +413,7 @@ static long FindInHash(HashList *Hash, long HashBinKey)
                                SearchPos --;
                        }
                        else {
-                               if ((SearchPos + 1 < Hash->nMembersUsed) && 
+                               if ((SearchPos + 1 < Hash->nLookupTableItems) && 
                                    (Hash->LookupTable[SearchPos + 1]->Key > HashBinKey))
                                        return SearchPos;
                                SearchPos ++;
@@ -412,9 +426,9 @@ static long FindInHash(HashList *Hash, long HashBinKey)
 
 
 /**
- * \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!
+ * @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)
@@ -425,9 +439,9 @@ int Flathash(const char *str, long len)
 }
 
 /**
- * \brief private abstract wrapper around the hashing algorithm
- * \param HKey the hash string
- * \param HKLen length of HKey
+ * @brief private abstract wrapper around the hashing algorithm
+ * @param HKey the hash string
+ * @param HKLen length of HKey
  * \returns the calculated hash value
  */
 inline static long CalcHashKey (HashList *Hash, const char *HKey, long HKLen)
@@ -443,12 +457,12 @@ inline static long CalcHashKey (HashList *Hash, const char *HKey, long HKLen)
 
 
 /**
- * \brief Add a new / Replace an existing item in the Hash
- * \param HashList the list to manipulate
- * \param HKey the hash-string to store Data under
- * \param HKeyLen Length of HKey
- * \param Data the payload you want to associate with HKey
- * \param DeleteIt if not free() should be used to delete Data set to NULL, else DeleteIt is used.
+ * @brief Add a new / Replace an existing item in the Hash
+ * @param HashList the list to manipulate
+ * @param HKey the hash-string to store Data under
+ * @param HKeyLen Length of HKey
+ * @param Data the payload you want to associate with HKey
+ * @param DeleteIt if not free() should be used to delete Data set to NULL, else DeleteIt is used.
  */
 void Put(HashList *Hash, const char *HKey, long HKLen, void *Data, DeleteHashDataFunc DeleteIt)
 {
@@ -491,11 +505,11 @@ void Put(HashList *Hash, const char *HKey, long HKLen, void *Data, DeleteHashDat
 }
 
 /**
- * \brief Lookup the Data associated with HKey
- * \param Hash the Hashlist to search in
- * \param HKey the hashkey to look up
- * \param HKLen length of HKey
- * \param Data returns the Data associated with HKey
+ * @brief Lookup the Data associated with HKey
+ * @param Hash the Hashlist to search in
+ * @param HKey the hashkey to look up
+ * @param HKLen length of HKey
+ * @param Data returns the Data associated with HKey
  * \returns 0 if not found, 1 if.
  */
 int GetHash(HashList *Hash, const char *HKey, long HKLen, void **Data)
@@ -514,7 +528,7 @@ int GetHash(HashList *Hash, const char *HKey, long HKLen, void **Data)
        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? */
+           (HashAt >= Hash->nLookupTableItems) || /**< Not found at the upper edge? */
            (Hash->LookupTable[HashAt]->Key != HashBinKey)) { /**< somewhere inbetween but no match? */
                *Data = NULL;
                return 0;
@@ -535,10 +549,10 @@ int GetKey(HashList *Hash, char *HKey, long HKLen, void **Payload)
 }
 
 /**
- * \brief get the Keys present in this hash, simila to array_keys() in PHP
+ * @brief get the Keys present in this hash, simila to array_keys() in PHP
  *  Attention: List remains to Hash! don't modify or free it!
- * \param Hash Your Hashlist to extract the keys from
- * \param List returns the list of hashkeys stored in Hash
+ * @param Hash Your Hashlist to extract the keys from
+ * @param List returns the list of hashkeys stored in Hash
  */
 int GetHashKeys(HashList *Hash, char ***List)
 {
@@ -548,19 +562,19 @@ int GetHashKeys(HashList *Hash, char ***List)
        if (Hash->MyKeys != NULL)
                free (Hash->MyKeys);
 
-       Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nMembersUsed);
-       for (i=0; i < Hash->nMembersUsed; i++) {
+       Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nLookupTableItems);
+       for (i=0; i < Hash->nLookupTableItems; i++) {
        
                Hash->MyKeys[i] = Hash->LookupTable[i]->HashKey;
        }
        *List = (char**)Hash->MyKeys;
-       return Hash->nMembersUsed;
+       return Hash->nLookupTableItems;
 }
 
 /**
- * \brief creates a hash-linear iterator object
- * \param Hash the list we reference
- * \param in which step width should we iterate?
+ * @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
@@ -575,7 +589,7 @@ HashPos *GetNewHashPos(HashList *Hash, int StepWidth)
        else
                Ret->StepWidth = 1;
        if (Ret->StepWidth <  0) {
-               Ret->Position = Hash->nMembersUsed - 1;
+               Ret->Position = Hash->nLookupTableItems - 1;
        }
        else {
                Ret->Position = 0;
@@ -584,17 +598,110 @@ HashPos *GetNewHashPos(HashList *Hash, int StepWidth)
 }
 
 /**
- * \brief retrieve the counter from the itteratoor
- * \param the Iterator to analyze
+ * @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 GetHashPosFromKey(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->nLookupTableItems) || /**< 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)
+{
+       Payload *FreeMe;
+       if (Hash == NULL)
+               return 0;
+
+       /* if lockable, lock here */
+       if ((Hash == NULL) || 
+           (At->Position >= Hash->nLookupTableItems) || 
+           (At->Position < 0) ||
+           (At->Position > Hash->nLookupTableItems))
+       {
+               /* unlock... */
+               return 0;
+       }
+
+       FreeMe = Hash->Members[Hash->LookupTable[At->Position]->Position];
+       Hash->Members[Hash->LookupTable[At->Position]->Position] = NULL;
+
+
+       /** delete our hashing data */
+       if (Hash->LookupTable[At->Position] != NULL)
+       {
+               free(Hash->LookupTable[At->Position]->HashKey);
+               free(Hash->LookupTable[At->Position]);
+               if (At->Position < Hash->nLookupTableItems)
+               {
+                       memmove(&Hash->LookupTable[At->Position],
+                               &Hash->LookupTable[At->Position + 1],
+                               (Hash->nLookupTableItems - At->Position - 1) * 
+                               sizeof(HashKey*));
+
+                       Hash->LookupTable[Hash->nLookupTableItems - 1] = NULL;
+               }
+               else 
+                       Hash->LookupTable[At->Position] = NULL;
+               Hash->nLookupTableItems--;
+       }
+       /* unlock... */
+
+
+       /** get rid of our payload */
+       if (FreeMe != NULL)
+       {
+               DeleteHashPayload(FreeMe);
+               free(FreeMe);
+       }
+       return 1;
+}
+
+/**
+ * @brief retrieve the counter from the itteratoor
+ * @param the Iterator to analyze
  * \returns the n'th hashposition we point at
  */
-int GetHashPosCounter(HashPos *At)
+int GetHashPosCounter(HashList *Hash, HashPos *At)
 {
+       if ((Hash == NULL) || 
+           (At->Position >= Hash->nLookupTableItems) || 
+           (At->Position < 0) ||
+           (At->Position > Hash->nLookupTableItems))
+               return 0;
        return At->Position;
 }
 
 /**
- * \brief frees a linear hash iterator
+ * @brief frees a linear hash iterator
  */
 void DeleteHashPos(HashPos **DelMe)
 {
@@ -607,54 +714,104 @@ void DeleteHashPos(HashPos **DelMe)
 
 
 /**
- * \brief Get the data located where HashPos Iterator points at, and Move HashPos one forward
- * \param Hash your Hashlist to follow
- * \param HKLen returns Length of Hashkey Returned
- * \param HashKey returns the Hashkey corrosponding to HashPos
- * \param Data returns the Data found at HashPos
+ * @brief Get the data located where HashPos Iterator points at, and Move HashPos one forward
+ * @param Hash your Hashlist to follow
+ * @param At the position to retrieve the Item from and move forward afterwards
+ * @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 GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data)
 {
        long PayloadPos;
 
-       if ((Hash == NULL) || (At->Position >= Hash->nMembersUsed) || (At->Position < 0))
+       if ((Hash == NULL) || 
+           (At->Position >= Hash->nLookupTableItems) || 
+           (At->Position < 0) ||
+           (At->Position > Hash->nLookupTableItems))
                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;
 
-       if (At->Position % abs(At->StepWidth) == 0)
+       /* Position is NULL-Based, while Stepwidth is not... */
+       if ((At->Position % abs(At->StepWidth)) == 0)
                At->Position += At->StepWidth;
        else 
-               At->Position += (At->Position % abs(At->StepWidth)) * 
+               At->Position += ((At->Position) % abs(At->StepWidth)) * 
                        (At->StepWidth / abs(At->StepWidth));
+       return 1;
+}
 
-       if (At->Position > Hash->nMembersUsed) {
-               At->Position = Hash->nMembersUsed;
-               return 0;
-       } else if (At->Position <= 0) {
-               At->Position = 0;
+/**
+ * @brief Get the data located where HashPos Iterator points at
+ * @param Hash your Hashlist to follow
+ * @param At the position retrieve the data 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 GetHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data)
+{
+       long PayloadPos;
+
+       if ((Hash == NULL) || 
+           (At->Position >= Hash->nLookupTableItems) || 
+           (At->Position < 0) ||
+           (At->Position > Hash->nLookupTableItems))
                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;
+
        return 1;
 }
 
 /**
- * \brief Get the data located where At points to
+ * @brief Move HashPos one forward
+ * @param Hash your Hashlist to follow
+ * @param At the position to move forward
+ * \returns whether there is a next item or not.
+ */
+int NextHashPos(HashList *Hash, HashPos *At)
+{
+       if ((Hash == NULL) || 
+           (At->Position >= Hash->nLookupTableItems) || 
+           (At->Position < 0) ||
+           (At->Position > Hash->nLookupTableItems))
+               return 0;
+
+       /* Position is NULL-Based, while Stepwidth is not... */
+       if ((At->Position % abs(At->StepWidth)) == 0)
+               At->Position += At->StepWidth;
+       else 
+               At->Position += ((At->Position) % abs(At->StepWidth)) * 
+                       (At->StepWidth / abs(At->StepWidth));
+       return !((At->Position >= Hash->nLookupTableItems) || 
+                (At->Position < 0) ||
+                (At->Position > Hash->nLookupTableItems));
+}
+
+/**
+ * @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
+ * @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))
+       if ((Hash == NULL) || 
+           (At < 0) || 
+           (At > Hash->nLookupTableItems))
                return 0;
        *HKLen = Hash->LookupTable[At]->HKLen;
        *HashKey = Hash->LookupTable[At]->HashKey;
@@ -664,9 +821,31 @@ int GetHashAt(HashList *Hash,long At, long *HKLen, const char **HashKey, void **
 }
 
 /**
- * \brief sorting function for sorting the Hash alphabeticaly by their strings
- * \param Key1 first item
- * \param Key2 second item
+ * @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.
+ */
+/*
+long GetHashIDAt(HashList *Hash,long At)
+{
+       if ((Hash == NULL) || 
+           (At < 0) || 
+           (At > Hash->nLookupTableItems))
+               return 0;
+
+       return Hash->LookupTable[At]->Key;
+}
+*/
+
+
+/**
+ * @brief sorting function for sorting the Hash alphabeticaly by their strings
+ * @param Key1 first item
+ * @param Key2 second item
  */
 static int SortByKeys(const void *Key1, const void* Key2)
 {
@@ -678,9 +857,9 @@ static int SortByKeys(const void *Key1, const void* Key2)
 }
 
 /**
- * \brief sorting function for sorting the Hash alphabeticaly reverse by their strings
- * \param Key1 first item
- * \param Key2 second item
+ * @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)
 {
@@ -692,9 +871,9 @@ static int SortByKeysRev(const void *Key1, const void* Key2)
 }
 
 /**
- * \brief sorting function to regain hash-sequence and revert tainted status
- * \param Key1 first item
- * \param Key2 second item
+ * @brief sorting function to regain hash-sequence and revert tainted status
+ * @param Key1 first item
+ * @param Key2 second item
  */
 static int SortByHashKeys(const void *Key1, const void* Key2)
 {
@@ -707,38 +886,38 @@ static int SortByHashKeys(const void *Key1, const void* Key2)
 
 
 /**
- * \brief sort the hash alphabeticaly by their keys.
+ * @brief sort the hash alphabeticaly by their keys.
  * 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
+ * @param Hash the list to sort
+ * @param Order 0/1 Forward/Backward
  */
 void SortByHashKey(HashList *Hash, int Order)
 {
-       if (Hash->nMembersUsed < 2)
+       if (Hash->nLookupTableItems < 2)
                return;
-       qsort(Hash->LookupTable, Hash->nMembersUsed, sizeof(HashKey*), 
+       qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*), 
              (Order)?SortByKeys:SortByKeysRev);
        Hash->tainted = 1;
 }
 
 /**
- * \brief sort the hash by their keys (so it regains untainted state).
+ * @brief sort the hash by their keys (so it regains untainted state).
  * this will result in the sequence the hashing allgorithm produces it by default.
- * \param Hash the list to sort
+ * @param Hash the list to sort
  */
 void SortByHashKeyStr(HashList *Hash)
 {
        Hash->tainted = 0;
-       if (Hash->nMembersUsed < 2)
+       if (Hash->nLookupTableItems < 2)
                return;
-       qsort(Hash->LookupTable, Hash->nMembersUsed, sizeof(HashKey*), SortByHashKeys);
+       qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*), SortByHashKeys);
 }
 
 
 /**
- * \brief gives user sort routines access to the hash payload
- * \param Searchentry to retrieve Data to
+ * @brief gives user sort routines access to the hash payload
+ * @param Searchentry to retrieve Data to
  * \returns Data belonging to HashVoid
  */
 const void *GetSearchPayload(const void *HashVoid)
@@ -747,16 +926,16 @@ const void *GetSearchPayload(const void *HashVoid)
 }
 
 /**
- * \brief sort the hash by your sort function. see the following sample.
+ * @brief sort the hash by your sort function. see the following sample.
  * this will result in the sequence the hashing allgorithm produces it by default.
- * \param Hash the list to sort
- * \param SortBy Sortfunction; see below how to implement this
+ * @param Hash the list to sort
+ * @param SortBy Sortfunction; see below how to implement this
  */
 void SortByPayload(HashList *Hash, CompareFunc SortBy)
 {
-       if (Hash->nMembersUsed < 2)
+       if (Hash->nLookupTableItems < 2)
                return;
-       qsort(Hash->LookupTable, Hash->nMembersUsed, sizeof(HashKey*), SortBy);
+       qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*), SortBy);
        Hash->tainted = 1;
 }
 
@@ -776,15 +955,6 @@ void SortByPayload(HashList *Hash, CompareFunc SortBy)
  */
 
 
-/*
- * 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.
@@ -795,4 +965,10 @@ void reference_free_handler(void *ptr)
 }
 
 
+/*
+ * This exposes the hashlittle() function to consumers.
+ */
+int HashLittle(const void *key, size_t length) {
+       return (int)hashlittle(key, length, 1);
+}