ReactOS 0.4.16-dev-2546-g56481e1
tif_hash_set.c File Reference
#include "tif_config.h"
#include "tif_hash_set.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for tif_hash_set.c:

Go to the source code of this file.

Classes

struct  _TIFFList
 
struct  _TIFFHashSet
 

Typedefs

typedef struct _TIFFList TIFFList
 

Functions

static unsigned long TIFFHashSetHashPointer (const void *elt)
 
static bool TIFFHashSetEqualPointer (const void *elt1, const void *elt2)
 
TIFFHashSetTIFFHashSetNew (TIFFHashSetHashFunc fnHashFunc, TIFFHashSetEqualFunc fnEqualFunc, TIFFHashSetFreeEltFunc fnFreeEltFunc)
 
int TIFFHashSetSize (const TIFFHashSet *set)
 
static TIFFListTIFFHashSetGetNewListElt (TIFFHashSet *set)
 
static void TIFFHashSetReturnListElt (TIFFHashSet *set, TIFFList *psList)
 
static void TIFFHashSetClearInternal (TIFFHashSet *set, bool bFinalize)
 
static void TIFFListDestroy (TIFFList *psList)
 
void TIFFHashSetDestroy (TIFFHashSet *set)
 
static bool TIFFHashSetRehash (TIFFHashSet *set)
 
static void ** TIFFHashSetFindPtr (TIFFHashSet *set, const void *elt)
 
bool TIFFHashSetInsert (TIFFHashSet *set, void *elt)
 
voidTIFFHashSetLookup (TIFFHashSet *set, const void *elt)
 
static bool TIFFHashSetRemoveInternal (TIFFHashSet *set, const void *elt, bool bDeferRehash)
 
bool TIFFHashSetRemove (TIFFHashSet *set, const void *elt)
 

Variables

static const int anPrimes []
 

Typedef Documentation

◆ TIFFList

List element structure.

Definition at line 40 of file tif_hash_set.c.

Function Documentation

◆ TIFFHashSetClearInternal()

static void TIFFHashSetClearInternal ( TIFFHashSet set,
bool  bFinalize 
)
static

Definition at line 226 of file tif_hash_set.c.

227{
228 assert(set != NULL);
229 for (int i = 0; i < set->nAllocatedSize; i++)
230 {
231 TIFFList *cur = set->tabList[i];
232 while (cur)
233 {
234 if (set->fnFreeEltFunc)
235 set->fnFreeEltFunc(cur->pData);
236 TIFFList *psNext = cur->psNext;
237 if (bFinalize)
238 free(cur);
239 else
241 cur = psNext;
242 }
243 set->tabList[i] = NULL;
244 }
245 set->bRehash = false;
246}
Definition: _set.h:50
#define free
Definition: debug_ros.c:5
#define NULL
Definition: types.h:112
#define assert(_expr)
Definition: assert.h:32
FxCollectionEntry * cur
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
static void TIFFHashSetReturnListElt(TIFFHashSet *set, TIFFList *psList)
Definition: tif_hash_set.c:208

Referenced by TIFFHashSetDestroy().

◆ TIFFHashSetDestroy()

void TIFFHashSetDestroy ( TIFFHashSet set)

Destroys an allocated hash set.

This function also frees the elements if a free function was provided at the creation of the hash set.

Parameters
setthe hash set

Definition at line 285 of file tif_hash_set.c.

286{
287 if (set)
288 {
290 free(set->tabList);
291 TIFFListDestroy(set->psRecyclingList);
292 free(set);
293 }
294}
static void TIFFListDestroy(TIFFList *psList)
Definition: tif_hash_set.c:260
static void TIFFHashSetClearInternal(TIFFHashSet *set, bool bFinalize)
Definition: tif_hash_set.c:226

Referenced by _TIFFCleanupIFDOffsetAndNumberMaps().

◆ TIFFHashSetEqualPointer()

static bool TIFFHashSetEqualPointer ( const void elt1,
const void elt2 
)
static

Equality function for arbitrary pointers

Parameters
elt1the first arbitrary pointer to compare
elt2the second arbitrary pointer to compare
Returns
true if the pointers are equal

Definition at line 108 of file tif_hash_set.c.

109{
110 return elt1 == elt2;
111}

Referenced by TIFFHashSetNew().

◆ TIFFHashSetFindPtr()

static void ** TIFFHashSetFindPtr ( TIFFHashSet set,
const void elt 
)
static

Definition at line 409 of file tif_hash_set.c.

410{
411 const unsigned long nHashVal = set->fnHashFunc(elt) % set->nAllocatedSize;
412 TIFFList *cur = set->tabList[nHashVal];
413 while (cur)
414 {
415 if (set->fnEqualFunc(cur->pData, elt))
416 return &cur->pData;
417 cur = cur->psNext;
418 }
419 return NULL;
420}

Referenced by TIFFHashSetInsert(), and TIFFHashSetLookup().

◆ TIFFHashSetGetNewListElt()

static TIFFList * TIFFHashSetGetNewListElt ( TIFFHashSet set)
static

Definition at line 190 of file tif_hash_set.c.

191{
192 if (set->psRecyclingList)
193 {
194 TIFFList *psRet = set->psRecyclingList;
195 psRet->pData = NULL;
196 set->nRecyclingListSize--;
197 set->psRecyclingList = psRet->psNext;
198 return psRet;
199 }
200
201 return (TIFFList *)malloc(sizeof(TIFFList));
202}
#define malloc
Definition: debug_ros.c:4
struct _TIFFList * psNext
Definition: tif_hash_set.c:52
void * pData
Definition: tif_hash_set.c:48

Referenced by TIFFHashSetInsert().

◆ TIFFHashSetHashPointer()

static unsigned long TIFFHashSetHashPointer ( const void elt)
static

Hash function for an arbitrary pointer

Parameters
eltthe arbitrary pointer to hash
Returns
the hash value of the pointer

Definition at line 90 of file tif_hash_set.c.

91{
92 return (unsigned long)(uintptr_t)((void *)(elt));
93}
unsigned int uintptr_t
Definition: corecrt.h:185

Referenced by TIFFHashSetNew().

◆ TIFFHashSetInsert()

bool TIFFHashSetInsert ( TIFFHashSet set,
void elt 
)

Inserts an element into a hash set.

If the element was already inserted in the hash set, the previous element is replaced by the new element. If a free function was provided, it is used to free the previously inserted element

Parameters
setthe hash set
eltthe new element to insert in the hash set
Returns
true if success. If false is returned, elt has not been inserted, but TIFFHashSetInsert() will have run the free function if provided.

Definition at line 440 of file tif_hash_set.c.

441{
442 assert(set != NULL);
443 void **pElt = TIFFHashSetFindPtr(set, elt);
444 if (pElt)
445 {
446 if (set->fnFreeEltFunc)
447 set->fnFreeEltFunc(*pElt);
448
449 *pElt = elt;
450 return true;
451 }
452
453 if (set->nSize >= 2 * set->nAllocatedSize / 3 ||
454 (set->bRehash && set->nIndiceAllocatedSize > 0 &&
455 set->nSize <= set->nAllocatedSize / 2))
456 {
457 set->nIndiceAllocatedSize++;
459 {
460 set->nIndiceAllocatedSize--;
461 if (set->fnFreeEltFunc)
462 set->fnFreeEltFunc(elt);
463 return false;
464 }
465 }
466
467 const unsigned long nHashVal = set->fnHashFunc(elt) % set->nAllocatedSize;
468#ifdef HASH_DEBUG
469 if (set->tabList[nHashVal])
470 set->nCollisions++;
471#endif
472
474 if (new_elt == NULL)
475 {
476 if (set->fnFreeEltFunc)
477 set->fnFreeEltFunc(elt);
478 return false;
479 }
480 new_elt->pData = elt;
481 new_elt->psNext = set->tabList[nHashVal];
482 set->tabList[nHashVal] = new_elt;
483 set->nSize++;
484
485 return true;
486}
static TIFFList * TIFFHashSetGetNewListElt(TIFFHashSet *set)
Definition: tif_hash_set.c:190
static void ** TIFFHashSetFindPtr(TIFFHashSet *set, const void *elt)
Definition: tif_hash_set.c:409
static bool TIFFHashSetRehash(TIFFHashSet *set)
Definition: tif_hash_set.c:366

Referenced by _TIFFCheckDirNumberAndOffset().

◆ TIFFHashSetLookup()

void * TIFFHashSetLookup ( TIFFHashSet set,
const void elt 
)

Returns the element found in the hash set corresponding to the element to look up The element must not be modified.

Parameters
setthe hash set
eltthe element to look up in the hash set
Returns
the element found in the hash set or NULL

Definition at line 502 of file tif_hash_set.c.

503{
504 assert(set != NULL);
505 void **pElt = TIFFHashSetFindPtr(set, elt);
506 if (pElt)
507 return *pElt;
508
509 return NULL;
510}

Referenced by _TIFFCheckDirNumberAndOffset(), _TIFFGetDirNumberFromOffset(), _TIFFGetOffsetFromDirNumber(), and _TIFFRemoveEntryFromDirectoryListByOffset().

◆ TIFFHashSetNew()

TIFFHashSet * TIFFHashSetNew ( TIFFHashSetHashFunc  fnHashFunc,
TIFFHashSetEqualFunc  fnEqualFunc,
TIFFHashSetFreeEltFunc  fnFreeEltFunc 
)

Creates a new hash set

The hash function must return a hash value for the elements to insert. If fnHashFunc is NULL, TIFFHashSetHashPointer will be used.

The equal function must return if two elements are equal. If fnEqualFunc is NULL, TIFFHashSetEqualPointer will be used.

The free function is used to free elements inserted in the hash set, when the hash set is destroyed, when elements are removed or replaced. If fnFreeEltFunc is NULL, elements inserted into the hash set will not be freed.

Parameters
fnHashFunchash function. May be NULL.
fnEqualFuncequal function. May be NULL.
fnFreeEltFuncelement free function. May be NULL.
Returns
a new hash set

Definition at line 138 of file tif_hash_set.c.

141{
143 if (set == NULL)
144 return NULL;
145 set->fnHashFunc = fnHashFunc ? fnHashFunc : TIFFHashSetHashPointer;
146 set->fnEqualFunc = fnEqualFunc ? fnEqualFunc : TIFFHashSetEqualPointer;
147 set->fnFreeEltFunc = fnFreeEltFunc;
148 set->nSize = 0;
149 set->tabList = (TIFFList **)(calloc(53, sizeof(TIFFList *)));
150 if (set->tabList == NULL)
151 {
152 free(set);
153 return NULL;
154 }
155 set->nIndiceAllocatedSize = 0;
156 set->nAllocatedSize = 53;
157 set->psRecyclingList = NULL;
158 set->nRecyclingListSize = 0;
159 set->bRehash = false;
160#ifdef HASH_DEBUG
161 set->nCollisions = 0;
162#endif
163 return set;
164}
#define calloc
Definition: rosglue.h:14
static unsigned long TIFFHashSetHashPointer(const void *elt)
Definition: tif_hash_set.c:90
static bool TIFFHashSetEqualPointer(const void *elt1, const void *elt2)
Definition: tif_hash_set.c:108

Referenced by _TIFFCheckDirNumberAndOffset().

◆ TIFFHashSetRehash()

static bool TIFFHashSetRehash ( TIFFHashSet set)
static

Definition at line 366 of file tif_hash_set.c.

367{
368 int nNewAllocatedSize = anPrimes[set->nIndiceAllocatedSize];
369 TIFFList **newTabList =
370 (TIFFList **)(calloc(nNewAllocatedSize, sizeof(TIFFList *)));
371 if (newTabList == NULL)
372 return false;
373#ifdef HASH_DEBUG
374 TIFFDebug("TIFFHASH",
375 "hashSet=%p, nSize=%d, nCollisions=%d, "
376 "fCollisionRate=%.02f",
377 set, set->nSize, set->nCollisions,
378 set->nCollisions * 100.0 / set->nSize);
379 set->nCollisions = 0;
380#endif
381 for (int i = 0; i < set->nAllocatedSize; i++)
382 {
383 TIFFList *cur = set->tabList[i];
384 while (cur)
385 {
386 const unsigned long nNewHashVal =
387 set->fnHashFunc(cur->pData) % nNewAllocatedSize;
388#ifdef HASH_DEBUG
389 if (newTabList[nNewHashVal])
390 set->nCollisions++;
391#endif
392 TIFFList *psNext = cur->psNext;
393 cur->psNext = newTabList[nNewHashVal];
394 newTabList[nNewHashVal] = cur;
395 cur = psNext;
396 }
397 }
398 free(set->tabList);
399 set->tabList = newTabList;
400 set->nAllocatedSize = nNewAllocatedSize;
401 set->bRehash = false;
402 return true;
403}
static const int anPrimes[]
Definition: tif_hash_set.c:72

Referenced by TIFFHashSetInsert(), and TIFFHashSetRemoveInternal().

◆ TIFFHashSetRemove()

bool TIFFHashSetRemove ( TIFFHashSet set,
const void elt 
)

Removes an element from a hash set

Parameters
setthe hash set
eltthe new element to remove from the hash set
Returns
true if the element was in the hash set

Definition at line 577 of file tif_hash_set.c.

578{
579 return TIFFHashSetRemoveInternal(set, elt, false);
580}
static bool TIFFHashSetRemoveInternal(TIFFHashSet *set, const void *elt, bool bDeferRehash)
Definition: tif_hash_set.c:516

Referenced by _TIFFCheckDirNumberAndOffset(), and _TIFFRemoveEntryFromDirectoryListByOffset().

◆ TIFFHashSetRemoveInternal()

static bool TIFFHashSetRemoveInternal ( TIFFHashSet set,
const void elt,
bool  bDeferRehash 
)
static

Definition at line 516 of file tif_hash_set.c.

518{
519 assert(set != NULL);
520 if (set->nIndiceAllocatedSize > 0 && set->nSize <= set->nAllocatedSize / 2)
521 {
522 set->nIndiceAllocatedSize--;
523 if (bDeferRehash)
524 set->bRehash = true;
525 else
526 {
528 {
529 set->nIndiceAllocatedSize++;
530 return false;
531 }
532 }
533 }
534
535 int nHashVal = (int)(set->fnHashFunc(elt) % set->nAllocatedSize);
536 TIFFList *cur = set->tabList[nHashVal];
537 TIFFList *prev = NULL;
538 while (cur)
539 {
540 if (set->fnEqualFunc(cur->pData, elt))
541 {
542 if (prev)
543 prev->psNext = cur->psNext;
544 else
545 set->tabList[nHashVal] = cur->psNext;
546
547 if (set->fnFreeEltFunc)
548 set->fnFreeEltFunc(cur->pData);
549
551#ifdef HASH_DEBUG
552 if (set->tabList[nHashVal])
553 set->nCollisions--;
554#endif
555 set->nSize--;
556 return true;
557 }
558 prev = cur;
559 cur = cur->psNext;
560 }
561 return false;
562}
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31

Referenced by TIFFHashSetRemove().

◆ TIFFHashSetReturnListElt()

static void TIFFHashSetReturnListElt ( TIFFHashSet set,
TIFFList psList 
)
static

Definition at line 208 of file tif_hash_set.c.

209{
210 if (set->nRecyclingListSize < 128)
211 {
212 psList->psNext = set->psRecyclingList;
213 set->psRecyclingList = psList;
214 set->nRecyclingListSize++;
215 }
216 else
217 {
218 free(psList);
219 }
220}

Referenced by TIFFHashSetClearInternal(), and TIFFHashSetRemoveInternal().

◆ TIFFHashSetSize()

int TIFFHashSetSize ( const TIFFHashSet set)

Returns the number of elements inserted in the hash set

Note: this is not the internal size of the hash set

Parameters
setthe hash set
Returns
the number of elements in the hash set

Definition at line 180 of file tif_hash_set.c.

181{
182 assert(set != NULL);
183 return set->nSize;
184}

Referenced by _TIFFCheckDirNumberAndOffset().

◆ TIFFListDestroy()

static void TIFFListDestroy ( TIFFList psList)
static

Destroy a list. Caller responsible for freeing data objects contained in list elements.

Parameters
psListpointer to list head.

Definition at line 260 of file tif_hash_set.c.

261{
262 TIFFList *psCurrent = psList;
263
264 while (psCurrent)
265 {
266 TIFFList *const psNext = psCurrent->psNext;
267 free(psCurrent);
268 psCurrent = psNext;
269 }
270}

Referenced by TIFFHashSetDestroy().

Variable Documentation

◆ anPrimes

const int anPrimes[]
static
Initial value:
= {
53, 97, 193, 389, 769, 1543, 3079,
6151, 12289, 24593, 49157, 98317, 196613, 393241,
786433, 1572869, 3145739, 6291469, 12582917, 25165843, 50331653,
100663319, 201326611, 402653189, 805306457, 1610612741}

Definition at line 72 of file tif_hash_set.c.

Referenced by TIFFHashSetRehash().