Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/libtiff-src/tif_hash_set.c

raw
1 /**********************************************************************
2 *
3 * Name: tif_hash_set.c
4 * Purpose: Hash set functions.
5 * Author: Even Rouault, <even dot rouault at spatialys.com>
6 *
7 **********************************************************************
8 * Copyright (c) 2008-2009, Even Rouault <even dot rouault at spatialys.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 ****************************************************************************/
28
29 #include "tif_config.h"
30
31 #include "tif_hash_set.h"
32
33 #include <assert.h>
34 #include <stdbool.h>
35 #include <stdint.h>
36 #include <sys/types.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39
40 /** List element structure. */
41 typedef struct _TIFFList TIFFList;
42
43 /** List element structure. */
44 struct _TIFFList
45 {
46 /*! Pointer to the data object. Should be allocated and freed by the
47 * caller.
48 * */
49 void *pData;
50 /*! Pointer to the next element in list. NULL, if current element is the
51 * last one.
52 */
53 struct _TIFFList *psNext;
54 };
55
56 struct _TIFFHashSet
57 {
58 TIFFHashSetHashFunc fnHashFunc;
59 TIFFHashSetEqualFunc fnEqualFunc;
60 TIFFHashSetFreeEltFunc fnFreeEltFunc;
61 TIFFList **tabList;
62 int nSize;
63 int nIndiceAllocatedSize;
64 int nAllocatedSize;
65 TIFFList *psRecyclingList;
66 int nRecyclingListSize;
67 bool bRehash;
68 #ifdef HASH_DEBUG
69 int nCollisions;
70 #endif
71 };
72
73 static const int anPrimes[] = {
74 53, 97, 193, 389, 769, 1543, 3079,
75 6151, 12289, 24593, 49157, 98317, 196613, 393241,
76 786433, 1572869, 3145739, 6291469, 12582917, 25165843, 50331653,
77 100663319, 201326611, 402653189, 805306457, 1610612741};
78
79 /************************************************************************/
80 /* TIFFHashSetHashPointer() */
81 /************************************************************************/
82
83 /**
84 * Hash function for an arbitrary pointer
85 *
86 * @param elt the arbitrary pointer to hash
87 *
88 * @return the hash value of the pointer
89 */
90
91 static unsigned long TIFFHashSetHashPointer(const void *elt)
92 {
93 return (unsigned long)(uintptr_t)((void *)(elt));
94 }
95
96 /************************************************************************/
97 /* TIFFHashSetEqualPointer() */
98 /************************************************************************/
99
100 /**
101 * Equality function for arbitrary pointers
102 *
103 * @param elt1 the first arbitrary pointer to compare
104 * @param elt2 the second arbitrary pointer to compare
105 *
106 * @return true if the pointers are equal
107 */
108
109 static bool TIFFHashSetEqualPointer(const void *elt1, const void *elt2)
110 {
111 return elt1 == elt2;
112 }
113
114 /************************************************************************/
115 /* TIFFHashSetNew() */
116 /************************************************************************/
117
118 /**
119 * Creates a new hash set
120 *
121 * The hash function must return a hash value for the elements to insert.
122 * If fnHashFunc is NULL, TIFFHashSetHashPointer will be used.
123 *
124 * The equal function must return if two elements are equal.
125 * If fnEqualFunc is NULL, TIFFHashSetEqualPointer will be used.
126 *
127 * The free function is used to free elements inserted in the hash set,
128 * when the hash set is destroyed, when elements are removed or replaced.
129 * If fnFreeEltFunc is NULL, elements inserted into the hash set will not be
130 * freed.
131 *
132 * @param fnHashFunc hash function. May be NULL.
133 * @param fnEqualFunc equal function. May be NULL.
134 * @param fnFreeEltFunc element free function. May be NULL.
135 *
136 * @return a new hash set
137 */
138
139 TIFFHashSet *TIFFHashSetNew(TIFFHashSetHashFunc fnHashFunc,
140 TIFFHashSetEqualFunc fnEqualFunc,
141 TIFFHashSetFreeEltFunc fnFreeEltFunc)
142 {
143 TIFFHashSet *set = (TIFFHashSet *)malloc(sizeof(TIFFHashSet));
144 if (set == NULL)
145 return NULL;
146 set->fnHashFunc = fnHashFunc ? fnHashFunc : TIFFHashSetHashPointer;
147 set->fnEqualFunc = fnEqualFunc ? fnEqualFunc : TIFFHashSetEqualPointer;
148 set->fnFreeEltFunc = fnFreeEltFunc;
149 set->nSize = 0;
150 set->tabList = (TIFFList **)(calloc(53, sizeof(TIFFList *)));
151 if (set->tabList == NULL)
152 {
153 free(set);
154 return NULL;
155 }
156 set->nIndiceAllocatedSize = 0;
157 set->nAllocatedSize = 53;
158 set->psRecyclingList = NULL;
159 set->nRecyclingListSize = 0;
160 set->bRehash = false;
161 #ifdef HASH_DEBUG
162 set->nCollisions = 0;
163 #endif
164 return set;
165 }
166
167 /************************************************************************/
168 /* TIFFHashSetSize() */
169 /************************************************************************/
170
171 /**
172 * Returns the number of elements inserted in the hash set
173 *
174 * Note: this is not the internal size of the hash set
175 *
176 * @param set the hash set
177 *
178 * @return the number of elements in the hash set
179 */
180
181 int TIFFHashSetSize(const TIFFHashSet *set)
182 {
183 assert(set != NULL);
184 return set->nSize;
185 }
186
187 /************************************************************************/
188 /* TIFFHashSetGetNewListElt() */
189 /************************************************************************/
190
191 static TIFFList *TIFFHashSetGetNewListElt(TIFFHashSet *set)
192 {
193 if (set->psRecyclingList)
194 {
195 TIFFList *psRet = set->psRecyclingList;
196 psRet->pData = NULL;
197 set->nRecyclingListSize--;
198 set->psRecyclingList = psRet->psNext;
199 return psRet;
200 }
201
202 return (TIFFList *)malloc(sizeof(TIFFList));
203 }
204
205 /************************************************************************/
206 /* TIFFHashSetReturnListElt() */
207 /************************************************************************/
208
209 static void TIFFHashSetReturnListElt(TIFFHashSet *set, TIFFList *psList)
210 {
211 if (set->nRecyclingListSize < 128)
212 {
213 psList->psNext = set->psRecyclingList;
214 set->psRecyclingList = psList;
215 set->nRecyclingListSize++;
216 }
217 else
218 {
219 free(psList);
220 }
221 }
222
223 /************************************************************************/
224 /* TIFFHashSetClearInternal() */
225 /************************************************************************/
226
227 static void TIFFHashSetClearInternal(TIFFHashSet *set, bool bFinalize)
228 {
229 assert(set != NULL);
230 for (int i = 0; i < set->nAllocatedSize; i++)
231 {
232 TIFFList *cur = set->tabList[i];
233 while (cur)
234 {
235 if (set->fnFreeEltFunc)
236 set->fnFreeEltFunc(cur->pData);
237 TIFFList *psNext = cur->psNext;
238 if (bFinalize)
239 free(cur);
240 else
241 TIFFHashSetReturnListElt(set, cur);
242 cur = psNext;
243 }
244 set->tabList[i] = NULL;
245 }
246 set->bRehash = false;
247 }
248
249 /************************************************************************/
250 /* TIFFListDestroy() */
251 /************************************************************************/
252
253 /**
254 * Destroy a list. Caller responsible for freeing data objects contained in
255 * list elements.
256 *
257 * @param psList pointer to list head.
258 *
259 */
260
261 static void TIFFListDestroy(TIFFList *psList)
262 {
263 TIFFList *psCurrent = psList;
264
265 while (psCurrent)
266 {
267 TIFFList *const psNext = psCurrent->psNext;
268 free(psCurrent);
269 psCurrent = psNext;
270 }
271 }
272
273 /************************************************************************/
274 /* TIFFHashSetDestroy() */
275 /************************************************************************/
276
277 /**
278 * Destroys an allocated hash set.
279 *
280 * This function also frees the elements if a free function was
281 * provided at the creation of the hash set.
282 *
283 * @param set the hash set
284 */
285
286 void TIFFHashSetDestroy(TIFFHashSet *set)
287 {
288 if (set)
289 {
290 TIFFHashSetClearInternal(set, true);
291 free(set->tabList);
292 TIFFListDestroy(set->psRecyclingList);
293 free(set);
294 }
295 }
296
297 #ifdef notused
298 /************************************************************************/
299 /* TIFFHashSetClear() */
300 /************************************************************************/
301
302 /**
303 * Clear all elements from a hash set.
304 *
305 * This function also frees the elements if a free function was
306 * provided at the creation of the hash set.
307 *
308 * @param set the hash set
309 */
310
311 void TIFFHashSetClear(TIFFHashSet *set)
312 {
313 TIFFHashSetClearInternal(set, false);
314 set->nIndiceAllocatedSize = 0;
315 set->nAllocatedSize = 53;
316 #ifdef HASH_DEBUG
317 set->nCollisions = 0;
318 #endif
319 set->nSize = 0;
320 }
321
322 /************************************************************************/
323 /* TIFFHashSetForeach() */
324 /************************************************************************/
325
326 /**
327 * Walk through the hash set and runs the provided function on all the
328 * elements
329 *
330 * This function is provided the user_data argument of TIFFHashSetForeach.
331 * It must return true to go on the walk through the hash set, or FALSE to
332 * make it stop.
333 *
334 * Note : the structure of the hash set must *NOT* be modified during the
335 * walk.
336 *
337 * @param set the hash set.
338 * @param fnIterFunc the function called on each element.
339 * @param user_data the user data provided to the function.
340 */
341
342 void TIFFHashSetForeach(TIFFHashSet *set, TIFFHashSetIterEltFunc fnIterFunc,
343 void *user_data)
344 {
345 assert(set != NULL);
346 if (!fnIterFunc)
347 return;
348
349 for (int i = 0; i < set->nAllocatedSize; i++)
350 {
351 TIFFList *cur = set->tabList[i];
352 while (cur)
353 {
354 if (!fnIterFunc(cur->pData, user_data))
355 return;
356
357 cur = cur->psNext;
358 }
359 }
360 }
361 #endif
362
363 /************************************************************************/
364 /* TIFFHashSetRehash() */
365 /************************************************************************/
366
367 static bool TIFFHashSetRehash(TIFFHashSet *set)
368 {
369 int nNewAllocatedSize = anPrimes[set->nIndiceAllocatedSize];
370 TIFFList **newTabList =
371 (TIFFList **)(calloc(nNewAllocatedSize, sizeof(TIFFList *)));
372 if (newTabList == NULL)
373 return false;
374 #ifdef HASH_DEBUG
375 TIFFDebug("TIFFHASH",
376 "hashSet=%p, nSize=%d, nCollisions=%d, "
377 "fCollisionRate=%.02f",
378 set, set->nSize, set->nCollisions,
379 set->nCollisions * 100.0 / set->nSize);
380 set->nCollisions = 0;
381 #endif
382 for (int i = 0; i < set->nAllocatedSize; i++)
383 {
384 TIFFList *cur = set->tabList[i];
385 while (cur)
386 {
387 const unsigned long nNewHashVal =
388 set->fnHashFunc(cur->pData) % nNewAllocatedSize;
389 #ifdef HASH_DEBUG
390 if (newTabList[nNewHashVal])
391 set->nCollisions++;
392 #endif
393 TIFFList *psNext = cur->psNext;
394 cur->psNext = newTabList[nNewHashVal];
395 newTabList[nNewHashVal] = cur;
396 cur = psNext;
397 }
398 }
399 free(set->tabList);
400 set->tabList = newTabList;
401 set->nAllocatedSize = nNewAllocatedSize;
402 set->bRehash = false;
403 return true;
404 }
405
406 /************************************************************************/
407 /* TIFFHashSetFindPtr() */
408 /************************************************************************/
409
410 static void **TIFFHashSetFindPtr(TIFFHashSet *set, const void *elt)
411 {
412 const unsigned long nHashVal = set->fnHashFunc(elt) % set->nAllocatedSize;
413 TIFFList *cur = set->tabList[nHashVal];
414 while (cur)
415 {
416 if (set->fnEqualFunc(cur->pData, elt))
417 return &cur->pData;
418 cur = cur->psNext;
419 }
420 return NULL;
421 }
422
423 /************************************************************************/
424 /* TIFFHashSetInsert() */
425 /************************************************************************/
426
427 /**
428 * Inserts an element into a hash set.
429 *
430 * If the element was already inserted in the hash set, the previous
431 * element is replaced by the new element. If a free function was provided,
432 * it is used to free the previously inserted element
433 *
434 * @param set the hash set
435 * @param elt the new element to insert in the hash set
436 *
437 * @return true if success. If false is returned, elt has not been inserted,
438 * but TIFFHashSetInsert() will have run the free function if provided.
439 */
440
441 bool TIFFHashSetInsert(TIFFHashSet *set, void *elt)
442 {
443 assert(set != NULL);
444 void **pElt = TIFFHashSetFindPtr(set, elt);
445 if (pElt)
446 {
447 if (set->fnFreeEltFunc)
448 set->fnFreeEltFunc(*pElt);
449
450 *pElt = elt;
451 return true;
452 }
453
454 if (set->nSize >= 2 * set->nAllocatedSize / 3 ||
455 (set->bRehash && set->nIndiceAllocatedSize > 0 &&
456 set->nSize <= set->nAllocatedSize / 2))
457 {
458 set->nIndiceAllocatedSize++;
459 if (!TIFFHashSetRehash(set))
460 {
461 set->nIndiceAllocatedSize--;
462 if (set->fnFreeEltFunc)
463 set->fnFreeEltFunc(elt);
464 return false;
465 }
466 }
467
468 const unsigned long nHashVal = set->fnHashFunc(elt) % set->nAllocatedSize;
469 #ifdef HASH_DEBUG
470 if (set->tabList[nHashVal])
471 set->nCollisions++;
472 #endif
473
474 TIFFList *new_elt = TIFFHashSetGetNewListElt(set);
475 if (new_elt == NULL)
476 {
477 if (set->fnFreeEltFunc)
478 set->fnFreeEltFunc(elt);
479 return false;
480 }
481 new_elt->pData = elt;
482 new_elt->psNext = set->tabList[nHashVal];
483 set->tabList[nHashVal] = new_elt;
484 set->nSize++;
485
486 return true;
487 }
488
489 /************************************************************************/
490 /* TIFFHashSetLookup() */
491 /************************************************************************/
492
493 /**
494 * Returns the element found in the hash set corresponding to the element to
495 * look up The element must not be modified.
496 *
497 * @param set the hash set
498 * @param elt the element to look up in the hash set
499 *
500 * @return the element found in the hash set or NULL
501 */
502
503 void *TIFFHashSetLookup(TIFFHashSet *set, const void *elt)
504 {
505 assert(set != NULL);
506 void **pElt = TIFFHashSetFindPtr(set, elt);
507 if (pElt)
508 return *pElt;
509
510 return NULL;
511 }
512
513 /************************************************************************/
514 /* TIFFHashSetRemoveInternal() */
515 /************************************************************************/
516
517 static bool TIFFHashSetRemoveInternal(TIFFHashSet *set, const void *elt,
518 bool bDeferRehash)
519 {
520 assert(set != NULL);
521 if (set->nIndiceAllocatedSize > 0 && set->nSize <= set->nAllocatedSize / 2)
522 {
523 set->nIndiceAllocatedSize--;
524 if (bDeferRehash)
525 set->bRehash = true;
526 else
527 {
528 if (!TIFFHashSetRehash(set))
529 {
530 set->nIndiceAllocatedSize++;
531 return false;
532 }
533 }
534 }
535
536 int nHashVal = (int)(set->fnHashFunc(elt) % set->nAllocatedSize);
537 TIFFList *cur = set->tabList[nHashVal];
538 TIFFList *prev = NULL;
539 while (cur)
540 {
541 if (set->fnEqualFunc(cur->pData, elt))
542 {
543 if (prev)
544 prev->psNext = cur->psNext;
545 else
546 set->tabList[nHashVal] = cur->psNext;
547
548 if (set->fnFreeEltFunc)
549 set->fnFreeEltFunc(cur->pData);
550
551 TIFFHashSetReturnListElt(set, cur);
552 #ifdef HASH_DEBUG
553 if (set->tabList[nHashVal])
554 set->nCollisions--;
555 #endif
556 set->nSize--;
557 return true;
558 }
559 prev = cur;
560 cur = cur->psNext;
561 }
562 return false;
563 }
564
565 /************************************************************************/
566 /* TIFFHashSetRemove() */
567 /************************************************************************/
568
569 /**
570 * Removes an element from a hash set
571 *
572 * @param set the hash set
573 * @param elt the new element to remove from the hash set
574 *
575 * @return true if the element was in the hash set
576 */
577
578 bool TIFFHashSetRemove(TIFFHashSet *set, const void *elt)
579 {
580 return TIFFHashSetRemoveInternal(set, elt, false);
581 }
582
583 #ifdef notused
584 /************************************************************************/
585 /* TIFFHashSetRemoveDeferRehash() */
586 /************************************************************************/
587
588 /**
589 * Removes an element from a hash set.
590 *
591 * This will defer potential rehashing of the set to later calls to
592 * TIFFHashSetInsert() or TIFFHashSetRemove().
593 *
594 * @param set the hash set
595 * @param elt the new element to remove from the hash set
596 *
597 * @return true if the element was in the hash set
598 */
599
600 bool TIFFHashSetRemoveDeferRehash(TIFFHashSet *set, const void *elt)
601 {
602 return TIFFHashSetRemoveInternal(set, elt, true);
603 }
604 #endif