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 * @defgroup HashList Hashlist Key Value list implementation;
32 * Hashlist is a simple implementation of key value pairs. It doesn't implement collision handling.
33 * the Hashingalgorythm is pluggeable on creation.
34 * items are added with a functionpointer destructs them; that way complex structures can be added.
35 * if no pointer is given, simply free is used. Use @ref reference_free_handler if you don't want us to free you rmemory.
39 * @defgroup HashListData Datastructures used for the internals of HashList
44 * @defgroup HashListDebug Hashlist debugging functions
49 * @defgroup HashListPrivate Hashlist internal functions
54 * @defgroup HashListSort Hashlist sorting functions
59 * @defgroup HashListAccess Hashlist functions to access / put / delete items in(to) the list
64 * @defgroup HashListAlgorithm functions to condense Key to an integer.
69 * @defgroup HashListMset MSet is sort of a derived hashlist, its special for treating Messagesets as Citadel uses them to store access rangesx
74 * @ingroup HashListData
75 * @brief Hash Payload storage Structure; filled in linear.
78 void *Data; /**< the Data belonging to this storage */
79 DeleteHashDataFunc Destructor; /**< if we want to destroy Data, do it with this function. */
84 * @ingroup HashListData
85 * @brief Hash key element; sorted by key
88 long Key; /**< Numeric Hashkey comperator for hash sorting */
89 long Position; /**< Pointer to a Payload struct in the Payload Aray */
90 char *HashKey; /**< the Plaintext Hashkey */
91 long HKLen; /**< length of the Plaintext Hashkey */
92 Payload *PL; /**< pointer to our payload for sorting */
96 * @ingroup HashListData
97 * @brief Hash structure; holds arrays of Hashkey and Payload.
100 Payload **Members; /**< Our Payload members. This fills up linear */
101 HashKey **LookupTable; /**< Hash Lookup table. Elements point to members, and are sorted by their hashvalue */
102 char **MyKeys; /**< this keeps the members for a call of GetHashKeys */
103 HashFunc Algorithm; /**< should we use an alternating algorithm to calc the hash values? */
104 long nMembersUsed; /**< how many pointers inside of the array are used? */
105 long nLookupTableItems; /**< how many items of the lookup table are used? */
106 long MemberSize; /**< how big is Members and LookupTable? */
107 long tainted; /**< if 0, we're hashed, else s.b. else sorted us in his own way. */
108 long uniq; /**< are the keys going to be uniq? */
112 * @ingroup HashListData
113 * @brief Anonymous Hash Iterator Object. used for traversing the whole array from outside
116 long Position; /**< Position inside of the hash */
117 int StepWidth; /**< small? big? forward? backward? */
122 * @ingroup HashListDebug
123 * @brief Iterate over the hash and call PrintEntry.
124 * @param Hash your Hashlist structure
125 * @param Trans is called so you could for example print 'A:' if the next entries are like that.
126 * Must be aware to receive NULL in both pointers.
127 * @param PrintEntry print entry one by one
128 * @return the number of items printed
130 int PrintHash(HashList *Hash, TransitionFunc Trans, PrintHashDataFunc PrintEntry)
140 for (i=0; i < Hash->nLookupTableItems; i++) {
145 if (Hash->LookupTable[i - 1] == NULL)
148 Previous = Hash->Members[Hash->LookupTable[i-1]->Position]->Data;
150 if (Hash->LookupTable[i] == NULL) {
155 Next = Hash->Members[Hash->LookupTable[i]->Position]->Data;
156 KeyStr = Hash->LookupTable[i]->HashKey;
159 Trans(Previous, Next, i % 2);
160 PrintEntry(KeyStr, Next, i % 2);
167 * @ingroup HashListDebug
168 * @brief verify the contents of a hash list; here for debugging purposes.
169 * @param Hash your Hashlist structure
170 * @param First Functionpointer to allow you to print your payload
171 * @param Second Functionpointer to allow you to print your payload
174 int dbg_PrintHash(HashList *Hash, PrintHashContent First, PrintHashContent Second)
179 const char *bla = "";
187 if (Hash->MyKeys != NULL)
190 Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nLookupTableItems);
192 printf("----------------------------------\n");
194 for (i=0; i < Hash->nLookupTableItems; i++) {
196 if (Hash->LookupTable[i] == NULL)
207 key = Hash->LookupTable[i]->Key;
208 foo = Hash->LookupTable[i]->HashKey;
214 First(Hash->Members[Hash->LookupTable[i]->Position]->Data);
224 Second(Hash->Members[Hash->LookupTable[i]->Position]->Data);
233 printf (" ---- Hashkey[%ld][%ld]: '%s' Value: '%s' ; %s\n", i, key, foo, bar, bla);
237 printf("----------------------------------\n");
243 int TestValidateHash(HashList *TestHash)
247 if (TestHash->nMembersUsed != TestHash->nLookupTableItems)
250 if (TestHash->nMembersUsed > TestHash->MemberSize)
253 for (i=0; i < TestHash->nMembersUsed; i++)
256 if (TestHash->LookupTable[i]->Position > TestHash->nMembersUsed)
259 if (TestHash->Members[TestHash->LookupTable[i]->Position] == NULL)
261 if (TestHash->Members[TestHash->LookupTable[i]->Position]->Data == NULL)
268 * @ingroup HashListAccess
269 * @brief instanciate a new hashlist
270 * @return the newly allocated list.
272 HashList *NewHash(int Uniq, HashFunc F)
275 NewList = malloc (sizeof(HashList));
276 memset(NewList, 0, sizeof(HashList));
278 NewList->Members = malloc(sizeof(Payload*) * 100);
279 memset(NewList->Members, 0, sizeof(Payload*) * 100);
281 NewList->LookupTable = malloc(sizeof(HashKey*) * 100);
282 memset(NewList->LookupTable, 0, sizeof(HashKey*) * 100);
284 NewList->MemberSize = 100;
285 NewList->tainted = 0;
286 NewList->uniq = Uniq;
287 NewList->Algorithm = F;
292 int GetCount(HashList *Hash)
294 if(Hash==NULL) return 0;
295 return Hash->nLookupTableItems;
300 * @ingroup HashListPrivate
301 * @brief private destructor for one hash element.
302 * Crashing? go one frame up and do 'print *FreeMe->LookupTable[i]'
303 * @param Data an element to free using the user provided destructor, or just plain free
305 static void DeleteHashPayload (Payload *Data)
307 /** do we have a destructor for our payload? */
308 if (Data->Destructor)
309 Data->Destructor(Data->Data);
315 * @ingroup HashListPrivate
316 * @brief Destructor for nested hashes
318 void HDeleteHash(void *vHash)
320 HashList *FreeMe = (HashList*)vHash;
325 * @ingroup HashListAccess
326 * @brief flush the members of a hashlist
327 * Crashing? do 'print *FreeMe->LookupTable[i]'
328 * @param Hash Hash to destroy. Is NULL'ed so you are shure its done.
330 void DeleteHashContent(HashList **Hash)
338 /* even if there are sparse members already deleted... */
339 for (i=0; i < FreeMe->nMembersUsed; i++)
341 /** get rid of our payload */
342 if (FreeMe->Members[i] != NULL)
344 DeleteHashPayload(FreeMe->Members[i]);
345 free(FreeMe->Members[i]);
347 /** delete our hashing data */
348 if (FreeMe->LookupTable[i] != NULL)
350 free(FreeMe->LookupTable[i]->HashKey);
351 free(FreeMe->LookupTable[i]);
354 FreeMe->nMembersUsed = 0;
356 FreeMe->nLookupTableItems = 0;
357 memset(FreeMe->Members, 0, sizeof(Payload*) * FreeMe->MemberSize);
358 memset(FreeMe->LookupTable, 0, sizeof(HashKey*) * FreeMe->MemberSize);
360 /** did s.b. want an array of our keys? free them. */
361 if (FreeMe->MyKeys != NULL)
362 free(FreeMe->MyKeys);
366 * @ingroup HashListAccess
367 * @brief destroy a hashlist and all of its members
368 * Crashing? do 'print *FreeMe->LookupTable[i]'
369 * @param Hash Hash to destroy. Is NULL'ed so you are shure its done.
371 void DeleteHash(HashList **Hash)
378 DeleteHashContent(Hash);
379 /** now, free our arrays... */
380 free(FreeMe->LookupTable);
381 free(FreeMe->Members);
383 /** buye bye cruel world. */
389 * @ingroup HashListPrivate
390 * @brief Private function to increase the hash size.
391 * @param Hash the Hasharray to increase
393 static void IncreaseHashSize(HashList *Hash)
395 /* Ok, Our space is used up. Double the available space. */
396 Payload **NewPayloadArea;
402 /** If we grew to much, this might be the place to rehash and shrink again.
403 if ((Hash->NMembersUsed > Hash->nLookupTableItems) &&
404 ((Hash->NMembersUsed - Hash->nLookupTableItems) >
405 (Hash->nLookupTableItems / 10)))
412 /** double our payload area */
413 NewPayloadArea = (Payload**) malloc(sizeof(Payload*) * Hash->MemberSize * 2);
414 memset(&NewPayloadArea[Hash->MemberSize], 0, sizeof(Payload*) * Hash->MemberSize);
415 memcpy(NewPayloadArea, Hash->Members, sizeof(Payload*) * Hash->MemberSize);
417 Hash->Members = NewPayloadArea;
419 /** double our hashtable area */
420 NewTable = malloc(sizeof(HashKey*) * Hash->MemberSize * 2);
421 memset(&NewTable[Hash->MemberSize], 0, sizeof(HashKey*) * Hash->MemberSize);
422 memcpy(NewTable, Hash->LookupTable, sizeof(HashKey*) * Hash->MemberSize);
423 free(Hash->LookupTable);
424 Hash->LookupTable = NewTable;
426 Hash->MemberSize *= 2;
431 * @ingroup HashListPrivate
432 * @brief private function to add a new item to / replace an existing in - the hashlist
433 * if the hash list is full, its re-alloced with double size.
434 * @param Hash our hashlist to manipulate
435 * @param HashPos where should we insert / replace?
436 * @param HashKeyStr the Hash-String
437 * @param HKLen length of HashKeyStr
438 * @param Data your Payload to add
439 * @param Destructor Functionpointer to free Data. if NULL, default free() is used.
441 static void InsertHashItem(HashList *Hash,
444 const char *HashKeyStr,
447 DeleteHashDataFunc Destructor)
449 Payload *NewPayloadItem;
455 if (Hash->nMembersUsed >= Hash->MemberSize)
456 IncreaseHashSize (Hash);
458 /** Arrange the payload */
459 NewPayloadItem = (Payload*) malloc (sizeof(Payload));
460 NewPayloadItem->Data = Data;
461 NewPayloadItem->Destructor = Destructor;
462 /** Arrange the hashkey */
463 NewHashKey = (HashKey*) malloc (sizeof(HashKey));
464 NewHashKey->HashKey = (char *) malloc (HKLen + 1);
465 NewHashKey->HKLen = HKLen;
466 memcpy (NewHashKey->HashKey, HashKeyStr, HKLen + 1);
467 NewHashKey->Key = HashBinKey;
468 NewHashKey->PL = NewPayloadItem;
469 /** our payload is queued at the end... */
470 NewHashKey->Position = Hash->nMembersUsed;
471 /** but if we should be sorted into a specific place... */
472 if ((Hash->nLookupTableItems != 0) &&
473 (HashPos != Hash->nLookupTableItems) ) {
476 ItemsAfter = Hash->nLookupTableItems - HashPos;
477 /** make space were we can fill us in */
480 memmove(&Hash->LookupTable[HashPos + 1],
481 &Hash->LookupTable[HashPos],
482 ItemsAfter * sizeof(HashKey*));
486 Hash->Members[Hash->nMembersUsed] = NewPayloadItem;
487 Hash->LookupTable[HashPos] = NewHashKey;
488 Hash->nMembersUsed++;
489 Hash->nLookupTableItems++;
493 * @ingroup HashListSort
494 * @brief if the user has tainted the hash, but wants to insert / search items by their key
495 * we need to search linear through the array. You have been warned that this will take more time!
496 * @param Hash Our Hash to manipulate
497 * @param HashBinKey the Hash-Number to lookup.
498 * @return the position (most closely) matching HashBinKey (-> Caller needs to compare! )
500 static long FindInTaintedHash(HashList *Hash, long HashBinKey)
507 for (SearchPos = 0; SearchPos < Hash->nLookupTableItems; SearchPos ++) {
508 if (Hash->LookupTable[SearchPos]->Key == HashBinKey){
516 * @ingroup HashListPrivate
517 * @brief Private function to lookup the Item / the closest position to put it in
518 * @param Hash Our Hash to manipulate
519 * @param HashBinKey the Hash-Number to lookup.
520 * @return the position (most closely) matching HashBinKey (-> Caller needs to compare! )
522 static long FindInHash(HashList *Hash, long HashBinKey)
531 return FindInTaintedHash(Hash, HashBinKey);
533 SearchPos = Hash->nLookupTableItems / 2;
534 StepWidth = SearchPos / 2;
535 while ((SearchPos > 0) &&
536 (SearchPos < Hash->nLookupTableItems))
538 /** Did we find it? */
539 if (Hash->LookupTable[SearchPos]->Key == HashBinKey){
542 /** are we Aproximating in big steps? */
544 if (Hash->LookupTable[SearchPos]->Key > HashBinKey)
545 SearchPos -= StepWidth;
547 SearchPos += StepWidth;
550 else { /** We are right next to our target, within 4 positions */
551 if (Hash->LookupTable[SearchPos]->Key > HashBinKey) {
552 if ((SearchPos > 0) &&
553 (Hash->LookupTable[SearchPos - 1]->Key < HashBinKey))
558 if ((SearchPos + 1 < Hash->nLookupTableItems) &&
559 (Hash->LookupTable[SearchPos + 1]->Key > HashBinKey))
571 * @ingroup HashListAlgorithm
572 * @brief another hashing algorithm; treat it as just a pointer to int.
573 * @param str Our pointer to the int value
574 * @param len the length of the data pointed to; needs to be sizeof int, else we won't use it!
575 * @return the calculated hash value
577 long Flathash(const char *str, long len)
579 if (len != sizeof (int))
581 else return *(int*)str;
585 * @ingroup HashListAlgorithm
586 * @brief another hashing algorithm; treat it as just a pointer to long.
587 * @param str Our pointer to the long value
588 * @param len the length of the data pointed to; needs to be sizeof long, else we won't use it!
589 * @return the calculated hash value
591 long lFlathash(const char *str, long len)
593 if (len != sizeof (long))
595 else return *(long*)str;
599 * @ingroup HashListPrivate
600 * @brief private abstract wrapper around the hashing algorithm
601 * @param HKey the hash string
602 * @param HKLen length of HKey
603 * @return the calculated hash value
605 inline static long CalcHashKey (HashList *Hash, const char *HKey, long HKLen)
610 if (Hash->Algorithm == NULL)
611 return hashlittle(HKey, HKLen, 9283457);
613 return Hash->Algorithm(HKey, HKLen);
618 * @ingroup HashListAccess
619 * @brief Add a new / Replace an existing item in the Hash
620 * @param Hash the list to manipulate
621 * @param HKey the hash-string to store Data under
622 * @param HKLen Length of HKey
623 * @param Data the payload you want to associate with HKey
624 * @param DeleteIt if not free() should be used to delete Data set to NULL, else DeleteIt is used.
626 void Put(HashList *Hash, const char *HKey, long HKLen, void *Data, DeleteHashDataFunc DeleteIt)
634 /** first, find out were we could fit in... */
635 HashBinKey = CalcHashKey(Hash, HKey, HKLen);
636 HashAt = FindInHash(Hash, HashBinKey);
638 if (HashAt >= Hash->MemberSize)
639 IncreaseHashSize (Hash);
641 /** oh, we're brand new... */
642 if (Hash->LookupTable[HashAt] == NULL) {
643 InsertHashItem(Hash, HashAt, HashBinKey, HKey, HKLen, Data, DeleteIt);
644 }/** Insert Before? */
645 else if (Hash->LookupTable[HashAt]->Key > HashBinKey) {
646 InsertHashItem(Hash, HashAt, HashBinKey, HKey, HKLen, Data, DeleteIt);
647 }/** Insert After? */
648 else if (Hash->LookupTable[HashAt]->Key < HashBinKey) {
649 InsertHashItem(Hash, HashAt + 1, HashBinKey, HKey, HKLen, Data, DeleteIt);
651 else { /** Ok, we have a colision. replace it. */
655 PayloadPos = Hash->LookupTable[HashAt]->Position;
656 DeleteHashPayload(Hash->Members[PayloadPos]);
657 Hash->Members[PayloadPos]->Data = Data;
658 Hash->Members[PayloadPos]->Destructor = DeleteIt;
661 InsertHashItem(Hash, HashAt + 1, HashBinKey, HKey, HKLen, Data, DeleteIt);
667 * @ingroup HashListAccess
668 * @brief Lookup the Data associated with HKey
669 * @param Hash the Hashlist to search in
670 * @param HKey the hashkey to look up
671 * @param HKLen length of HKey
672 * @param Data returns the Data associated with HKey
673 * @return 0 if not found, 1 if.
675 int GetHash(HashList *Hash, const char *HKey, long HKLen, void **Data)
687 /** first, find out were we could be... */
688 HashBinKey = CalcHashKey(Hash, HKey, HKLen);
689 HashAt = FindInHash(Hash, HashBinKey);
690 if ((HashAt < 0) || /**< Not found at the lower edge? */
691 (HashAt >= Hash->nLookupTableItems) || /**< Not found at the upper edge? */
692 (Hash->LookupTable[HashAt]->Key != HashBinKey)) { /**< somewhere inbetween but no match? */
696 else { /** GOTCHA! */
699 MemberPosition = Hash->LookupTable[HashAt]->Position;
700 *Data = Hash->Members[MemberPosition]->Data;
706 int GetKey(HashList *Hash, char *HKey, long HKLen, void **Payload)
712 * @ingroup HashListAccess
713 * @brief get the Keys present in this hash, simila to array_keys() in PHP
714 * Attention: List remains to Hash! don't modify or free it!
715 * @param Hash Your Hashlist to extract the keys from
716 * @param List returns the list of hashkeys stored in Hash
718 int GetHashKeys(HashList *Hash, char ***List)
723 if (Hash->MyKeys != NULL)
726 Hash->MyKeys = (char**) malloc(sizeof(char*) * Hash->nLookupTableItems);
727 for (i=0; i < Hash->nLookupTableItems; i++) {
729 Hash->MyKeys[i] = Hash->LookupTable[i]->HashKey;
731 *List = (char**)Hash->MyKeys;
732 return Hash->nLookupTableItems;
736 * @ingroup HashListAccess
737 * @brief creates a hash-linear iterator object
738 * @param Hash the list we reference
739 * @param StepWidth in which step width should we iterate?
740 * If negative, the last position matching the
741 * step-raster is provided.
742 * @return the hash iterator
744 HashPos *GetNewHashPos(HashList *Hash, int StepWidth)
748 Ret = (HashPos*)malloc(sizeof(HashPos));
750 Ret->StepWidth = StepWidth;
753 if (Ret->StepWidth < 0) {
754 Ret->Position = Hash->nLookupTableItems - 1;
763 * @ingroup HashListAccess
764 * @brief Set iterator object to point to key. If not found, don't change iterator
765 * @param Hash the list we reference
766 * @param HKey key to search for
767 * @param HKLen length of key
768 * @param At HashPos to update
769 * @return 0 if not found
771 int GetHashPosFromKey(HashList *Hash, const char *HKey, long HKLen, HashPos *At)
782 /** first, find out were we could be... */
783 HashBinKey = CalcHashKey(Hash, HKey, HKLen);
784 HashAt = FindInHash(Hash, HashBinKey);
785 if ((HashAt < 0) || /**< Not found at the lower edge? */
786 (HashAt >= Hash->nLookupTableItems) || /**< Not found at the upper edge? */
787 (Hash->LookupTable[HashAt]->Key != HashBinKey)) { /**< somewhere inbetween but no match? */
791 At->Position = HashAt;
796 * @ingroup HashListAccess
797 * @brief Delete from the Hash the entry at Position
798 * @param Hash the list we reference
799 * @param At the position within the Hash
800 * @return 0 if not found
802 int DeleteEntryFromHash(HashList *Hash, HashPos *At)
808 /* if lockable, lock here */
809 if ((Hash == NULL) ||
810 (At->Position >= Hash->nLookupTableItems) ||
811 (At->Position < 0) ||
812 (At->Position > Hash->nLookupTableItems))
818 FreeMe = Hash->Members[Hash->LookupTable[At->Position]->Position];
819 Hash->Members[Hash->LookupTable[At->Position]->Position] = NULL;
822 /** delete our hashing data */
823 if (Hash->LookupTable[At->Position] != NULL)
825 free(Hash->LookupTable[At->Position]->HashKey);
826 free(Hash->LookupTable[At->Position]);
827 if (At->Position < Hash->nLookupTableItems)
829 memmove(&Hash->LookupTable[At->Position],
830 &Hash->LookupTable[At->Position + 1],
831 (Hash->nLookupTableItems - At->Position - 1) *
834 Hash->LookupTable[Hash->nLookupTableItems - 1] = NULL;
837 Hash->LookupTable[At->Position] = NULL;
838 Hash->nLookupTableItems--;
843 /** get rid of our payload */
846 DeleteHashPayload(FreeMe);
853 * @ingroup HashListAccess
854 * @brief retrieve the counter from the itteratoor
856 * @param At the Iterator to analyze
857 * @return the n'th hashposition we point at
859 int GetHashPosCounter(HashList *Hash, HashPos *At)
861 if ((Hash == NULL) ||
862 (At->Position >= Hash->nLookupTableItems) ||
863 (At->Position < 0) ||
864 (At->Position > Hash->nLookupTableItems))
870 * @ingroup HashListAccess
871 * @brief frees a linear hash iterator
873 void DeleteHashPos(HashPos **DelMe)
884 * @ingroup HashListAccess
885 * @brief Get the data located where HashPos Iterator points at, and Move HashPos one forward
886 * @param Hash your Hashlist to follow
887 * @param At the position to retrieve the Item from and move forward afterwards
888 * @param HKLen returns Length of Hashkey Returned
889 * @param HashKey returns the Hashkey corrosponding to HashPos
890 * @param Data returns the Data found at HashPos
891 * @return whether the item was found or not.
893 int GetNextHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data)
897 if ((Hash == NULL) ||
898 (At->Position >= Hash->nLookupTableItems) ||
899 (At->Position < 0) ||
900 (At->Position > Hash->nLookupTableItems))
902 *HKLen = Hash->LookupTable[At->Position]->HKLen;
903 *HashKey = Hash->LookupTable[At->Position]->HashKey;
904 PayloadPos = Hash->LookupTable[At->Position]->Position;
905 *Data = Hash->Members[PayloadPos]->Data;
907 /* Position is NULL-Based, while Stepwidth is not... */
908 if ((At->Position % abs(At->StepWidth)) == 0)
909 At->Position += At->StepWidth;
911 At->Position += ((At->Position) % abs(At->StepWidth)) *
912 (At->StepWidth / abs(At->StepWidth));
917 * @ingroup HashListAccess
918 * @brief Get the data located where HashPos Iterator points at
919 * @param Hash your Hashlist to follow
920 * @param At the position retrieve the data from
921 * @param HKLen returns Length of Hashkey Returned
922 * @param HashKey returns the Hashkey corrosponding to HashPos
923 * @param Data returns the Data found at HashPos
924 * @return whether the item was found or not.
926 int GetHashPos(HashList *Hash, HashPos *At, long *HKLen, const char **HashKey, void **Data)
930 if ((Hash == NULL) ||
931 (At->Position >= Hash->nLookupTableItems) ||
932 (At->Position < 0) ||
933 (At->Position > Hash->nLookupTableItems))
935 *HKLen = Hash->LookupTable[At->Position]->HKLen;
936 *HashKey = Hash->LookupTable[At->Position]->HashKey;
937 PayloadPos = Hash->LookupTable[At->Position]->Position;
938 *Data = Hash->Members[PayloadPos]->Data;
944 * @ingroup HashListAccess
945 * @brief Move HashPos one forward
946 * @param Hash your Hashlist to follow
947 * @param At the position to move forward
948 * @return whether there is a next item or not.
950 int NextHashPos(HashList *Hash, HashPos *At)
952 if ((Hash == NULL) ||
953 (At->Position >= Hash->nLookupTableItems) ||
954 (At->Position < 0) ||
955 (At->Position > Hash->nLookupTableItems))
958 /* Position is NULL-Based, while Stepwidth is not... */
959 if ((At->Position % abs(At->StepWidth)) == 0)
960 At->Position += At->StepWidth;
962 At->Position += ((At->Position) % abs(At->StepWidth)) *
963 (At->StepWidth / abs(At->StepWidth));
964 return !((At->Position >= Hash->nLookupTableItems) ||
965 (At->Position < 0) ||
966 (At->Position > Hash->nLookupTableItems));
970 * @ingroup HashListAccess
971 * @brief Get the data located where At points to
972 * note: you should prefer iterator operations instead of using me.
973 * @param Hash your Hashlist peek from
974 * @param At get the item in the position At.
975 * @param HKLen returns Length of Hashkey Returned
976 * @param HashKey returns the Hashkey corrosponding to HashPos
977 * @param Data returns the Data found at HashPos
978 * @return whether the item was found or not.
980 int GetHashAt(HashList *Hash,long At, long *HKLen, const char **HashKey, void **Data)
984 if ((Hash == NULL) ||
986 (At >= Hash->nLookupTableItems))
988 *HKLen = Hash->LookupTable[At]->HKLen;
989 *HashKey = Hash->LookupTable[At]->HashKey;
990 PayloadPos = Hash->LookupTable[At]->Position;
991 *Data = Hash->Members[PayloadPos]->Data;
996 * @ingroup HashListSort
997 * @brief Get the data located where At points to
998 * note: you should prefer iterator operations instead of using me.
999 * @param Hash your Hashlist peek from
1000 * @param HKLen returns Length of Hashkey Returned
1001 * @param HashKey returns the Hashkey corrosponding to HashPos
1002 * @param Data returns the Data found at HashPos
1003 * @return whether the item was found or not.
1006 long GetHashIDAt(HashList *Hash,long At)
1008 if ((Hash == NULL) ||
1010 (At > Hash->nLookupTableItems))
1013 return Hash->LookupTable[At]->Key;
1019 * @ingroup HashListSort
1020 * @brief sorting function for sorting the Hash alphabeticaly by their strings
1021 * @param Key1 first item
1022 * @param Key2 second item
1024 static int SortByKeys(const void *Key1, const void* Key2)
1026 HashKey *HKey1, *HKey2;
1027 HKey1 = *(HashKey**) Key1;
1028 HKey2 = *(HashKey**) Key2;
1030 return strcasecmp(HKey1->HashKey, HKey2->HashKey);
1034 * @ingroup HashListSort
1035 * @brief sorting function for sorting the Hash alphabeticaly reverse by their strings
1036 * @param Key1 first item
1037 * @param Key2 second item
1039 static int SortByKeysRev(const void *Key1, const void* Key2)
1041 HashKey *HKey1, *HKey2;
1042 HKey1 = *(HashKey**) Key1;
1043 HKey2 = *(HashKey**) Key2;
1045 return strcasecmp(HKey2->HashKey, HKey1->HashKey);
1049 * @ingroup HashListSort
1050 * @brief sorting function to regain hash-sequence and revert tainted status
1051 * @param Key1 first item
1052 * @param Key2 second item
1054 static int SortByHashKeys(const void *Key1, const void* Key2)
1056 HashKey *HKey1, *HKey2;
1057 HKey1 = *(HashKey**) Key1;
1058 HKey2 = *(HashKey**) Key2;
1060 return HKey1->Key > HKey2->Key;
1065 * @ingroup HashListSort
1066 * @brief sort the hash alphabeticaly by their keys.
1067 * Caution: This taints the hashlist, so accessing it later
1068 * will be significantly slower! You can un-taint it by SortByHashKeyStr
1069 * @param Hash the list to sort
1070 * @param Order 0/1 Forward/Backward
1072 void SortByHashKey(HashList *Hash, int Order)
1074 if (Hash->nLookupTableItems < 2)
1076 qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*),
1077 (Order)?SortByKeys:SortByKeysRev);
1082 * @ingroup HashListSort
1083 * @brief sort the hash by their keys (so it regains untainted state).
1084 * this will result in the sequence the hashing allgorithm produces it by default.
1085 * @param Hash the list to sort
1087 void SortByHashKeyStr(HashList *Hash)
1090 if (Hash->nLookupTableItems < 2)
1092 qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*), SortByHashKeys);
1097 * @ingroup HashListSort
1098 * @brief gives user sort routines access to the hash payload
1099 * @param HashVoid to retrieve Data to
1100 * @return Data belonging to HashVoid
1102 const void *GetSearchPayload(const void *HashVoid)
1104 return (*(HashKey**)HashVoid)->PL->Data;
1108 * @ingroup HashListSort
1109 * @brief sort the hash by your sort function. see the following sample.
1110 * this will result in the sequence the hashing allgorithm produces it by default.
1111 * @param Hash the list to sort
1112 * @param SortBy Sortfunction; see below how to implement this
1114 void SortByPayload(HashList *Hash, CompareFunc SortBy)
1116 if (Hash->nLookupTableItems < 2)
1118 qsort(Hash->LookupTable, Hash->nLookupTableItems, sizeof(HashKey*), SortBy);
1126 * given you've put char * into your hash as a payload, a sort function might
1128 * int SortByChar(const void* First, const void* Second)
1131 * a = (char*) GetSearchPayload(First);
1132 * b = (char*) GetSearchPayload(Second);
1133 * return strcmp (a, b);
1139 * @ingroup HashListAccess
1140 * @brief Generic function to free a reference.
1141 * since a reference actualy isn't needed to be freed, do nothing.
1143 void reference_free_handler(void *ptr)
1150 * @ingroup HashListAlgorithm
1151 * This exposes the hashlittle() function to consumers.
1153 int HashLittle(const void *key, size_t length) {
1154 return (int)hashlittle(key, length, 1);
1159 * @ingroup HashListMset
1160 * @brief parses an MSet string into a list for later use
1161 * @param MSetList List to be read from MSetStr
1162 * @param MSetStr String containing the list
1164 int ParseMSet(MSet **MSetList, StrBuf *MSetStr)
1166 const char *POS = NULL, *SetPOS = NULL;
1169 long StartSet, EndSet;
1173 if ((MSetStr == NULL) || (StrLength(MSetStr) == 0))
1176 OneSet = NewStrBufPlain(NULL, StrLength(MSetStr));
1178 ThisMSet = NewHash(0, lFlathash);
1180 *MSetList = (MSet*) ThisMSet;
1182 /* an MSet is a coma separated value list. */
1183 StrBufExtract_NextToken(OneSet, MSetStr, &POS, ',');
1187 /* One set may consist of two Numbers: Start + optional End */
1188 StartSet = StrBufExtractNext_long(OneSet, &SetPOS, ':');
1189 EndSet = 0; /* no range is our default. */
1190 /* do we have an end (aka range?) */
1191 if ((SetPOS != NULL) && (SetPOS != StrBufNOTNULL))
1193 if (*(SetPOS) == '*')
1194 EndSet = LONG_MAX; /* ranges with '*' go until infinity */
1196 /* in other cases, get the EndPoint */
1197 EndSet = StrBufExtractNext_long(OneSet, &SetPOS, ':');
1200 pEndSet = (long*) malloc (sizeof(long));
1203 Put(ThisMSet, LKEY(StartSet), pEndSet, NULL);
1204 /* if we don't have another, we're done. */
1205 if (POS == StrBufNOTNULL)
1207 StrBufExtract_NextToken(OneSet, MSetStr, &POS, ',');
1209 FreeStrBuf(&OneSet);
1215 * @ingroup HashListMset
1216 * @brief checks whether a message is inside a mset
1217 * @param MSetList List to search for MsgNo
1218 * @param MsgNo number to search in mset
1220 int IsInMSetList(MSet *MSetList, long MsgNo)
1222 /* basicaly we are a ... */
1223 long MemberPosition;
1224 HashList *Hash = (HashList*) MSetList;
1231 if (Hash->MemberSize == 0)
1233 /** first, find out were we could fit in... */
1234 HashAt = FindInHash(Hash, MsgNo);
1236 /* we're below the first entry, so not found. */
1239 /* upper edge? move to last item */
1240 if (HashAt >= Hash->nMembersUsed)
1241 HashAt = Hash->nMembersUsed - 1;
1242 /* Match? then we got it. */
1243 else if (Hash->LookupTable[HashAt]->Key == MsgNo)
1245 /* One above possible range start? we need to move to the lower one. */
1246 else if ((HashAt > 0) &&
1247 (Hash->LookupTable[HashAt]->Key > MsgNo))
1250 /* Fetch the actual data */
1251 StartAt = Hash->LookupTable[HashAt]->Key;
1252 MemberPosition = Hash->LookupTable[HashAt]->Position;
1253 EndAt = *(long*) Hash->Members[MemberPosition]->Data;
1254 if ((MsgNo >= StartAt) && (EndAt == LONG_MAX))
1259 /* inside of range? */
1260 if ((StartAt <= MsgNo) && (EndAt >= MsgNo))
1267 * @ingroup HashListMset
1268 * @brief frees a mset [redirects to @ref DeleteHash
1269 * @param FreeMe to be free'd
1271 void DeleteMSet(MSet **FreeMe)
1273 DeleteHash((HashList**) FreeMe);