2 * Copyright (c) 1987-2011 by the citadel.org team
4 * This program is open source software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "libcitadel.h"
28 typedef struct Payload Payload;
31 * @brief Hash Payload storage Structure; filled in linear.
34 void *Data; /**< the Data belonging to this storage */
35 DeleteHashDataFunc Destructor; /**< if we want to destroy Data, do it with this function. */
40 * @brief Hash key element; sorted by key
43 long Key; /**< Numeric Hashkey comperator for hash sorting */
44 long Position; /**< Pointer to a Payload struct in the Payload Aray */
45 char *HashKey; /**< the Plaintext Hashkey */
46 long HKLen; /**< length of the Plaintext Hashkey */
47 Payload *PL; /**< pointer to our payload for sorting */
51 * @brief Hash structure; holds arrays of Hashkey and Payload.
54 Payload **Members; /**< Our Payload members. This fills up linear */
55 HashKey **LookupTable; /**< Hash Lookup table. Elements point to members, and are sorted by their hashvalue */
56 char **MyKeys; /**< this keeps the members for a call of GetHashKeys */
57 HashFunc Algorithm; /**< should we use an alternating algorithm to calc the hash values? */
58 long nMembersUsed; /**< how many pointers inside of the array are used? */
59 long nLookupTableItems; /**< how many items of the lookup table are used? */
60 long MemberSize; /**< how big is Members and LookupTable? */
61 long tainted; /**< if 0, we're hashed, else s.b. else sorted us in his own way. */
62 long uniq; /**< are the keys going to be uniq? */
66 * @brief Anonymous Hash Iterator Object. used for traversing the whole array from outside
69 long Position; /**< Position inside of the hash */
70 int StepWidth; /**< small? big? forward? backward? */
75 * @brief Iterate over the hash and call PrintEntry.
76 * @param Hash your Hashlist structure
77 * @param Trans is called so you could for example print 'A:' if the next entries are like that.
78 * Must be aware to receive NULL in both pointers.
79 * @param PrintEntry print entry one by one
80 * \returns the number of items printed
82 int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry)
92 for (i=0; i < Hash->nLookupTableItems; i++) {
97 if (Hash->LookupTable[i - 1] == NULL)
100 Previous = Hash->Members[Hash->LookupTable[i-1]->Position]->Data;
102 if (Hash->LookupTable[i] == NULL) {
107 Next = Hash->Members[Hash->LookupTable[i]->Position]->Data;
108 KeyStr = Hash->LookupTable[i]->HashKey;
111 Trans(Previous, Next, i % 2);
112 PrintEntry(KeyStr, Next, i % 2);
119 * @brief verify the contents of a hash list; here for debugging purposes.
120 * @param Hash your Hashlist structure
121 * @param First Functionpointer to allow you to print your payload
122 * @param Second Functionpointer to allow you to print your payload
125 int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Second)
129 const char *bla = "";
136 if (Hash->MyKeys != NULL)
139 Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nLookupTableItems);
141 printf("----------------------------------\n");
143 for (i=0; i < Hash->nLookupTableItems; i++) {
145 if (Hash->LookupTable[i] == NULL)
153 key = Hash->LookupTable[i]->Key;
154 foo = Hash->LookupTable[i]->HashKey;
156 bar = First(Hash->Members[Hash->LookupTable[i]->Position]->Data);
160 bla = Second(Hash->Members[Hash->LookupTable[i]->Position]->Data);
165 printf (" ---- Hashkey[%ld][%ld]: '%s' Value: '%s' ; %s\n", i, key, foo, bar, bla);
169 printf("----------------------------------\n");
176 * @brief instanciate a new hashlist
177 * \returns the newly allocated list.
179 HashList *NewHash(int Uniq, HashFunc F)
182 NewList = malloc (sizeof(HashList));
183 memset(NewList, 0, sizeof(HashList));
185 NewList->Members = malloc(sizeof(Payload*) * 100);
186 memset(NewList->Members, 0, sizeof(Payload*) * 100);
188 NewList->LookupTable = malloc(sizeof(HashKey*) * 100);
189 memset(NewList->LookupTable, 0, sizeof(HashKey*) * 100);
191 NewList->MemberSize = 100;
192 NewList->tainted = 0;
193 NewList->uniq = Uniq;
194 NewList->Algorithm = F;
199 int GetCount(HashList *Hash)
201 if(Hash==NULL) return 0;
202 return Hash->nLookupTableItems;
207 * @brief private destructor for one hash element.
208 * Crashing? go one frame up and do 'print *FreeMe->LookupTable[i]'
209 * @param Data an element to free using the user provided destructor, or just plain free
211 static void DeleteHashPayload (Payload *Data)
213 /** do we have a destructor for our payload? */
214 if (Data->Destructor)
215 Data->Destructor(Data->Data);
221 * @brief Destructor for nested hashes
223 void HDeleteHash(void *vHash)
225 HashList *FreeMe = (HashList*)vHash;
230 * @brief destroy a hashlist and all of its members
231 * Crashing? do 'print *FreeMe->LookupTable[i]'
232 * @param Hash Hash to destroy. Is NULL'ed so you are shure its done.
234 void DeleteHash(HashList **Hash)
242 /* even if there are sparse members already deleted... */
243 for (i=0; i < FreeMe->nMembersUsed; i++)
245 /** get rid of our payload */
246 if (FreeMe->Members[i] != NULL)
248 DeleteHashPayload(FreeMe->Members[i]);
249 free(FreeMe->Members[i]);
251 /** delete our hashing data */
252 if (FreeMe->LookupTable[i] != NULL)
254 free(FreeMe->LookupTable[i]->HashKey);
255 free(FreeMe->LookupTable[i]);
258 /** now, free our arrays... */
259 free(FreeMe->LookupTable);
260 free(FreeMe->Members);
261 /** did s.b. want an array of our keys? free them. */
262 if (FreeMe->MyKeys != NULL)
263 free(FreeMe->MyKeys);
264 /** buye bye cruel world. */
270 * @brief Private function to increase the hash size.
271 * @param Hash the Hasharray to increase
273 static void IncreaseHashSize(HashList *Hash)
275 /* Ok, Our space is used up. Double the available space. */
276 Payload **NewPayloadArea;
282 /** If we grew to much, this might be the place to rehash and shrink again.
283 if ((Hash->NMembersUsed > Hash->nLookupTableItems) &&
284 ((Hash->NMembersUsed - Hash->nLookupTableItems) >
285 (Hash->nLookupTableItems / 10)))
292 /** double our payload area */
293 NewPayloadArea = (Payload**) malloc(sizeof(Payload*) * Hash->MemberSize * 2);
294 memset(&NewPayloadArea[Hash->MemberSize], 0, sizeof(Payload*) * Hash->MemberSize);
295 memcpy(NewPayloadArea, Hash->Members, sizeof(Payload*) * Hash->MemberSize);
297 Hash->Members = NewPayloadArea;
299 /** double our hashtable area */
300 NewTable = malloc(sizeof(HashKey*) * Hash->MemberSize * 2);
301 memset(&NewTable[Hash->MemberSize], 0, sizeof(HashKey*) * Hash->MemberSize);
302 memcpy(NewTable, Hash->LookupTable, sizeof(HashKey*) * Hash->MemberSize);
303 free(Hash->LookupTable);
304 Hash->LookupTable = NewTable;
306 Hash->MemberSize *= 2;
311 * @brief private function to add a new item to / replace an existing in - the hashlist
312 * if the hash list is full, its re-alloced with double size.
313 * \parame Hash our hashlist to manipulate
314 * @param HashPos where should we insert / replace?
315 * @param HashKeyStr the Hash-String
316 * @param HKLen length of HashKeyStr
317 * @param Data your Payload to add
318 * @param Destructor Functionpointer to free Data. if NULL, default free() is used.
320 static void InsertHashItem(HashList *Hash,
323 const char *HashKeyStr,
326 DeleteHashDataFunc Destructor)
328 Payload *NewPayloadItem;
334 if (Hash->nMembersUsed >= Hash->MemberSize)
335 IncreaseHashSize (Hash);
337 /** Arrange the payload */
338 NewPayloadItem = (Payload*) malloc (sizeof(Payload));
339 NewPayloadItem->Data = Data;
340 NewPayloadItem->Destructor = Destructor;
341 /** Arrange the hashkey */
342 NewHashKey = (HashKey*) malloc (sizeof(HashKey));
343 NewHashKey->HashKey = (char *) malloc (HKLen + 1);
344 NewHashKey->HKLen = HKLen;
345 memcpy (NewHashKey->HashKey, HashKeyStr, HKLen + 1);
346 NewHashKey->Key = HashBinKey;
347 NewHashKey->PL = NewPayloadItem;
348 /** our payload is queued at the end... */
349 NewHashKey->Position = Hash->nMembersUsed;
350 /** but if we should be sorted into a specific place... */
351 if ((Hash->nLookupTableItems != 0) &&
352 (HashPos != Hash->nLookupTableItems) ) {
355 ItemsAfter = Hash->nLookupTableItems - HashPos;
356 /** make space were we can fill us in */
359 memmove(&Hash->LookupTable[HashPos + 1],
360 &Hash->LookupTable[HashPos],
361 ItemsAfter * sizeof(HashKey*));
365 Hash->Members[Hash->nMembersUsed] = NewPayloadItem;
366 Hash->LookupTable[HashPos] = NewHashKey;
367 Hash->nMembersUsed++;
368 Hash->nLookupTableItems++;
372 * @brief if the user has tainted the hash, but wants to insert / search items by their key
373 * we need to search linear through the array. You have been warned that this will take more time!
374 * @param Hash Our Hash to manipulate
375 * @param HashBinKey the Hash-Number to lookup.
376 * \returns the position (most closely) matching HashBinKey (-> Caller needs to compare! )
378 static long FindInTaintedHash(HashList *Hash, long HashBinKey)
385 for (SearchPos = 0; SearchPos < Hash->nLookupTableItems; SearchPos ++) {
386 if (Hash->LookupTable[SearchPos]->Key == HashBinKey){
394 * @brief Private function to lookup the Item / the closest position to put it in
395 * @param Hash Our Hash to manipulate
396 * @param HashBinKey the Hash-Number to lookup.
397 * \returns the position (most closely) matching HashBinKey (-> Caller needs to compare! )
399 static long FindInHash(HashList *Hash, long HashBinKey)
408 return FindInTaintedHash(Hash, HashBinKey);
410 SearchPos = Hash->nLookupTableItems / 2;
411 StepWidth = SearchPos / 2;
412 while ((SearchPos > 0) &&
413 (SearchPos < Hash->nLookupTableItems))
415 /** Did we find it? */
416 if (Hash->LookupTable[SearchPos]->Key == HashBinKey){
419 /** are we Aproximating in big steps? */
421 if (Hash->LookupTable[SearchPos]->Key > HashBinKey)
422 SearchPos -= StepWidth;
424 SearchPos += StepWidth;
427 else { /** We are right next to our target, within 4 positions */
428 if (Hash->LookupTable[SearchPos]->Key > HashBinKey) {
429 if ((SearchPos > 0) &&
430 (Hash->LookupTable[SearchPos - 1]->Key < HashBinKey))
435 if ((SearchPos + 1 < Hash->nLookupTableItems) &&
436 (Hash->LookupTable[SearchPos + 1]->Key > HashBinKey))
448 * @brief another hashing algorithm; treat it as just a pointer to int.
449 * @param str Our pointer to the int value
450 * @param len the length of the data pointed to; needs to be sizeof int, else we won't use it!
451 * \returns the calculated hash value
453 long Flathash(const char *str, long len)
455 if (len != sizeof (int))
457 else return *(int*)str;
461 * @brief another hashing algorithm; treat it as just a pointer to long.
462 * @param str Our pointer to the long value
463 * @param len the length of the data pointed to; needs to be sizeof long, else we won't use it!
464 * \returns the calculated hash value
466 long lFlathash(const char *str, long len)
468 if (len != sizeof (long))
470 else return *(long*)str;
474 * @brief private abstract wrapper around the hashing algorithm
475 * @param HKey the hash string
476 * @param HKLen length of HKey
477 * \returns the calculated hash value
479 inline static long CalcHashKey (HashList *Hash, const char *HKey, long HKLen)
484 if (Hash->Algorithm == NULL)
485 return hashlittle(HKey, HKLen, 9283457);
487 return Hash->Algorithm(HKey, HKLen);
492 * @brief Add a new / Replace an existing item in the Hash
493 * @param HashList the list to manipulate
494 * @param HKey the hash-string to store Data under
495 * @param HKeyLen Length of HKey
496 * @param Data the payload you want to associate with HKey
497 * @param DeleteIt if not free() should be used to delete Data set to NULL, else DeleteIt is used.
499 void Put(HashList *Hash, const char *HKey, long HKLen, void *Data, DeleteHashDataFunc DeleteIt)
507 /** first, find out were we could fit in... */
508 HashBinKey = CalcHashKey(Hash, HKey, HKLen);
509 HashAt = FindInHash(Hash, HashBinKey);
511 if (HashAt >= Hash->MemberSize)
512 IncreaseHashSize (Hash);
514 /** oh, we're brand new... */
515 if (Hash->LookupTable[HashAt] == NULL) {
516 InsertHashItem(Hash, HashAt, HashBinKey, HKey, HKLen, Data, DeleteIt);
517 }/** Insert After? */
518 else if (Hash->LookupTable[HashAt]->Key > HashBinKey) {
519 InsertHashItem(Hash, HashAt, HashBinKey, HKey, HKLen, Data, DeleteIt);
520 }/** Insert before? */
521 else if (Hash->LookupTable[HashAt]->Key < HashBinKey) {
522 InsertHashItem(Hash, HashAt + 1, HashBinKey, HKey, HKLen, Data, DeleteIt);
524 else { /** Ok, we have a colision. replace it. */
528 PayloadPos = Hash->LookupTable[HashAt]->Position;
529 DeleteHashPayload(Hash->Members[PayloadPos]);
530 Hash->Members[PayloadPos]->Data = Data;
531 Hash->Members[PayloadPos]->Destructor = DeleteIt;
534 InsertHashItem(Hash, HashAt + 1, HashBinKey, HKey, HKLen, Data, DeleteIt);
540 * @brief Lookup the Data associated with HKey
541 * @param Hash the Hashlist to search in
542 * @param HKey the hashkey to look up
543 * @param HKLen length of HKey
544 * @param Data returns the Data associated with HKey
545 * \returns 0 if not found, 1 if.
547 int GetHash(HashList *Hash, const char *HKey, long HKLen, void **Data)
559 /** first, find out were we could be... */
560 HashBinKey = CalcHashKey(Hash, HKey, HKLen);
561 HashAt = FindInHash(Hash, HashBinKey);
562 if ((HashAt < 0) || /**< Not found at the lower edge? */
563 (HashAt >= Hash->nLookupTableItems) || /**< Not found at the upper edge? */
564 (Hash->LookupTable[HashAt]->Key != HashBinKey)) { /**< somewhere inbetween but no match? */
568 else { /** GOTCHA! */
571 MemberPosition = Hash->LookupTable[HashAt]->Position;
572 *Data = Hash->Members[MemberPosition]->Data;
578 int GetKey(HashList *Hash, char *HKey, long HKLen, void **Payload)
584 * @brief get the Keys present in this hash, simila to array_keys() in PHP
585 * Attention: List remains to Hash! don't modify or free it!
586 * @param Hash Your Hashlist to extract the keys from
587 * @param List returns the list of hashkeys stored in Hash
589 int GetHashKeys(HashList *Hash, char ***List)
594 if (Hash->MyKeys != NULL)
597 Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nLookupTableItems);
598 for (i=0; i < Hash->nLookupTableItems; i++) {
600 Hash->MyKeys[i] = Hash->LookupTable[i]->HashKey;
602 *List = (char**)Hash->MyKeys;
603 return Hash->nLookupTableItems;
607 * @brief creates a hash-linear iterator object
608 * @param Hash the list we reference
609 * @param in which step width should we iterate?
610 * If negative, the last position matching the
611 * step-raster is provided.
612 * \returns the hash iterator
614 HashPos *GetNewHashPos(HashList *Hash, int StepWidth)
618 Ret = (HashPos*)malloc(sizeof(HashPos));
620 Ret->StepWidth = StepWidth;
623 if (Ret->StepWidth < 0) {
624 Ret->Position = Hash->nLookupTableItems - 1;
633 * @brief Set iterator object to point to key. If not found, don't change iterator
634 * @param Hash the list we reference
635 * @param HKey key to search for
636 * @param HKLen length of key
637 * @param At HashPos to update
638 * \returns 0 if not found
640 int GetHashPosFromKey(HashList *Hash, const char *HKey, long HKLen, HashPos *At)
651 /** first, find out were we could be... */
652 HashBinKey = CalcHashKey(Hash, HKey, HKLen);
653 HashAt = FindInHash(Hash, HashBinKey);
654 if ((HashAt < 0) || /**< Not found at the lower edge? */
655 (HashAt >= Hash->nLookupTableItems) || /**< Not found at the upper edge? */
656 (Hash->LookupTable[HashAt]->Key != HashBinKey)) { /**< somewhere inbetween but no match? */
660 At->Position = Hash->LookupTable[HashAt]->Position;
665 * @brief Delete from the Hash the entry at Position
666 * @param Hash the list we reference
667 * @param At the position within the Hash
668 * \returns 0 if not found
670 int DeleteEntryFromHash(HashList *Hash, HashPos *At)
676 /* if lockable, lock here */
677 if ((Hash == NULL) ||
678 (At->Position >= Hash->nLookupTableItems) ||
679 (At->Position < 0) ||
680 (At->Position > Hash->nLookupTableItems))
686 FreeMe = Hash->Members[Hash->LookupTable[At->Position]->Position];
687 Hash->Members[Hash->LookupTable[At->Position]->Position] = NULL;
690 /** delete our hashing data */
691 if (Hash->LookupTable[At->Position] != NULL)
693 free(Hash->LookupTable[At->Position]->HashKey);
694 free(Hash->LookupTable[At->Position]);
695 if (At->Position < Hash->nLookupTableItems)
697 memmove(&Hash->LookupTable[At->Position],
698 &Hash->LookupTable[At->Position + 1],
699 (Hash->nLookupTableItems - At->Position - 1) *
702 Hash->LookupTable[Hash->nLookupTableItems - 1] = NULL;
705 Hash->LookupTable[At->Position] = NULL;
706 Hash->nLookupTableItems--;
711 /** get rid of our payload */
714 DeleteHashPayload(FreeMe);
721 * @brief retrieve the counter from the itteratoor
722 * @param the Iterator to analyze
723 * \returns the n'th hashposition we point at
725 int GetHashPosCounter(HashList *Hash, HashPos *At)
727 if ((Hash == NULL) ||
728 (At->Position >= Hash->nLookupTableItems) ||
729 (At->Position < 0) ||
730 (At->Position > Hash->nLookupTableItems))
736 * @brief frees a linear hash iterator
738 void DeleteHashPos(HashPos **DelMe)
749 * @brief Get the data located where HashPos Iterator points at, and Move HashPos one forward
750 * @param Hash your Hashlist to follow
751 * @param At the position to retrieve the Item from and move forward afterwards
752 * @param HKLen returns Length of Hashkey Returned
753 * @param HashKey returns the Hashkey corrosponding to HashPos
754 * @param Data returns the Data found at HashPos
755 * \returns whether the item was found or not.
757 int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data)
761 if ((Hash == NULL) ||
762 (At->Position >= Hash->nLookupTableItems) ||
763 (At->Position < 0) ||
764 (At->Position > Hash->nLookupTableItems))
766 *HKLen = Hash->LookupTable[At->Position]->HKLen;
767 *HashKey = Hash->LookupTable[At->Position]->HashKey;
768 PayloadPos = Hash->LookupTable[At->Position]->Position;
769 *Data = Hash->Members[PayloadPos]->Data;
771 /* Position is NULL-Based, while Stepwidth is not... */
772 if ((At->Position % abs(At->StepWidth)) == 0)
773 At->Position += At->StepWidth;
775 At->Position += ((At->Position) % abs(At->StepWidth)) *
776 (At->StepWidth / abs(At->StepWidth));
781 * @brief Get the data located where HashPos Iterator points at
782 * @param Hash your Hashlist to follow
783 * @param At the position retrieve the data from
784 * @param HKLen returns Length of Hashkey Returned
785 * @param HashKey returns the Hashkey corrosponding to HashPos
786 * @param Data returns the Data found at HashPos
787 * \returns whether the item was found or not.
789 int GetHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data)
793 if ((Hash == NULL) ||
794 (At->Position >= Hash->nLookupTableItems) ||
795 (At->Position < 0) ||
796 (At->Position > Hash->nLookupTableItems))
798 *HKLen = Hash->LookupTable[At->Position]->HKLen;
799 *HashKey = Hash->LookupTable[At->Position]->HashKey;
800 PayloadPos = Hash->LookupTable[At->Position]->Position;
801 *Data = Hash->Members[PayloadPos]->Data;
807 * @brief Move HashPos one forward
808 * @param Hash your Hashlist to follow
809 * @param At the position to move forward
810 * \returns whether there is a next item or not.
812 int NextHashPos(HashList *Hash, HashPos *At)
814 if ((Hash == NULL) ||
815 (At->Position >= Hash->nLookupTableItems) ||
816 (At->Position < 0) ||
817 (At->Position > Hash->nLookupTableItems))
820 /* Position is NULL-Based, while Stepwidth is not... */
821 if ((At->Position % abs(At->StepWidth)) == 0)
822 At->Position += At->StepWidth;
824 At->Position += ((At->Position) % abs(At->StepWidth)) *
825 (At->StepWidth / abs(At->StepWidth));
826 return !((At->Position >= Hash->nLookupTableItems) ||
827 (At->Position < 0) ||
828 (At->Position > Hash->nLookupTableItems));
832 * @brief Get the data located where At points to
833 * note: you should prefer iterator operations instead of using me.
834 * @param Hash your Hashlist peek from
835 * @param HKLen returns Length of Hashkey Returned
836 * @param HashKey returns the Hashkey corrosponding to HashPos
837 * @param Data returns the Data found at HashPos
838 * \returns whether the item was found or not.
840 int GetHashAt(HashList *Hash,long At, long *HKLen, const char **HashKey, void **Data)
844 if ((Hash == NULL) ||
846 (At >= Hash->nLookupTableItems))
848 *HKLen = Hash->LookupTable[At]->HKLen;
849 *HashKey = Hash->LookupTable[At]->HashKey;
850 PayloadPos = Hash->LookupTable[At]->Position;
851 *Data = Hash->Members[PayloadPos]->Data;
856 * @brief Get the data located where At points to
857 * note: you should prefer iterator operations instead of using me.
858 * @param Hash your Hashlist peek from
859 * @param HKLen returns Length of Hashkey Returned
860 * @param HashKey returns the Hashkey corrosponding to HashPos
861 * @param Data returns the Data found at HashPos
862 * \returns whether the item was found or not.
865 long GetHashIDAt(HashList *Hash,long At)
867 if ((Hash == NULL) ||
869 (At > Hash->nLookupTableItems))
872 return Hash->LookupTable[At]->Key;
878 * @brief sorting function for sorting the Hash alphabeticaly by their strings
879 * @param Key1 first item
880 * @param Key2 second item
882 static int SortByKeys(const void *Key1, const void* Key2)
884 HashKey *HKey1, *HKey2;
885 HKey1 = *(HashKey**) Key1;
886 HKey2 = *(HashKey**) Key2;
888 return strcasecmp(HKey1->HashKey, HKey2->HashKey);
892 * @brief sorting function for sorting the Hash alphabeticaly reverse by their strings
893 * @param Key1 first item
894 * @param Key2 second item
896 static int SortByKeysRev(const void *Key1, const void* Key2)
898 HashKey *HKey1, *HKey2;
899 HKey1 = *(HashKey**) Key1;
900 HKey2 = *(HashKey**) Key2;
902 return strcasecmp(HKey2->HashKey, HKey1->HashKey);
906 * @brief sorting function to regain hash-sequence and revert tainted status
907 * @param Key1 first item
908 * @param Key2 second item
910 static int SortByHashKeys(const void *Key1, const void* Key2)
912 HashKey *HKey1, *HKey2;
913 HKey1 = *(HashKey**) Key1;
914 HKey2 = *(HashKey**) Key2;
916 return HKey1->Key > HKey2->Key;
921 * @brief sort the hash alphabeticaly by their keys.
922 * Caution: This taints the hashlist, so accessing it later
923 * will be significantly slower! You can un-taint it by SortByHashKeyStr
924 * @param Hash the list to sort
925 * @param Order 0/1 Forward/Backward
927 void SortByHashKey(HashList *Hash, int Order)
929 if (Hash->nLookupTableItems < 2)
931 qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*),
932 (Order)?SortByKeys:SortByKeysRev);
937 * @brief sort the hash by their keys (so it regains untainted state).
938 * this will result in the sequence the hashing allgorithm produces it by default.
939 * @param Hash the list to sort
941 void SortByHashKeyStr(HashList *Hash)
944 if (Hash->nLookupTableItems < 2)
946 qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*), SortByHashKeys);
951 * @brief gives user sort routines access to the hash payload
952 * @param Searchentry to retrieve Data to
953 * \returns Data belonging to HashVoid
955 const void *GetSearchPayload(const void *HashVoid)
957 return (*(HashKey**)HashVoid)->PL->Data;
961 * @brief sort the hash by your sort function. see the following sample.
962 * this will result in the sequence the hashing allgorithm produces it by default.
963 * @param Hash the list to sort
964 * @param SortBy Sortfunction; see below how to implement this
966 void SortByPayload(HashList *Hash, CompareFunc SortBy)
968 if (Hash->nLookupTableItems < 2)
970 qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*), SortBy);
978 * given you've put char * into your hash as a payload, a sort function might
980 * int SortByChar(const void* First, const void* Second)
983 * a = (char*) GetSearchPayload(First);
984 * b = (char*) GetSearchPayload(Second);
985 * return strcmp (a, b);
991 * Generic function to free a reference.
992 * since a reference actualy isn't needed to be freed, do nothing.
994 void reference_free_handler(void *ptr)
1001 * This exposes the hashlittle() function to consumers.
1003 int HashLittle(const void *key, size_t length) {
1004 return (int)hashlittle(key, length, 1);
1009 * \brief parses an MSet string into a list for later use
1010 * \param MSetList List to be read from MSetStr
1011 * \param MSetStr String containing the list
1013 int ParseMSet(MSet **MSetList, StrBuf *MSetStr)
1015 const char *POS = NULL, *SetPOS = NULL;
1018 long StartSet, EndSet;
1022 if ((MSetStr == NULL) || (StrLength(MSetStr) == 0))
1025 OneSet = NewStrBufPlain(NULL, StrLength(MSetStr));
1027 ThisMSet = NewHash(0, lFlathash);
1029 *MSetList = (MSet*) ThisMSet;
1031 /* an MSet is a coma separated value list. */
1032 StrBufExtract_NextToken(OneSet, MSetStr, &POS, ',');
1036 /* One set may consist of two Numbers: Start + optional End */
1037 StartSet = StrBufExtractNext_long(OneSet, &SetPOS, ':');
1038 EndSet = 0; /* no range is our default. */
1039 /* do we have an end (aka range?) */
1040 if ((SetPOS != NULL) && (SetPOS != StrBufNOTNULL))
1042 if (*(SetPOS) == '*')
1043 EndSet = LONG_MAX; /* ranges with '*' go until infinity */
1045 /* in other cases, get the EndPoint */
1046 EndSet = StrBufExtractNext_long(OneSet, &SetPOS, ':');
1049 pEndSet = (long*) malloc (sizeof(long));
1052 Put(ThisMSet, LKEY(StartSet), pEndSet, NULL);
1053 /* if we don't have another, we're done. */
1054 if (POS == StrBufNOTNULL)
1056 StrBufExtract_NextToken(OneSet, MSetStr, &POS, ',');
1058 FreeStrBuf(&OneSet);
1064 * \brief checks whether a message is inside a mset
1065 * \param MSetList List to search for MsgNo
1066 * \param MsgNo number to search in mset
1068 int IsInMSetList(MSet *MSetList, long MsgNo)
1070 /* basicaly we are a ... */
1071 long MemberPosition;
1072 HashList *Hash = (HashList*) MSetList;
1079 if (Hash->MemberSize == 0)
1081 /** first, find out were we could fit in... */
1082 HashAt = FindInHash(Hash, MsgNo);
1084 /* we're below the first entry, so not found. */
1087 /* upper edge? move to last item */
1088 if (HashAt >= Hash->nMembersUsed)
1089 HashAt = Hash->nMembersUsed - 1;
1090 /* Match? then we got it. */
1091 else if (Hash->LookupTable[HashAt]->Key == MsgNo)
1093 /* One above possible range start? we need to move to the lower one. */
1094 else if ((HashAt > 0) &&
1095 (Hash->LookupTable[HashAt]->Key > MsgNo))
1098 /* Fetch the actual data */
1099 StartAt = Hash->LookupTable[HashAt]->Key;
1100 MemberPosition = Hash->LookupTable[HashAt]->Position;
1101 EndAt = *(long*) Hash->Members[MemberPosition]->Data;
1102 if ((MsgNo >= StartAt) && (EndAt == LONG_MAX))
1107 /* inside of range? */
1108 if ((StartAt <= MsgNo) && (EndAt >= MsgNo))
1115 * \brief frees a mset [redirects to @ref DeleteHash
1116 * \param FreeMe to be free'd
1118 void DeleteMSet(MSet **FreeMe)
1120 DeleteHash((HashList**) FreeMe);