TutHashCreatePtr


Name

TutHashCreatePtr — create a hash table with pointer keys

Synopsis

T_HASH_TABLE TutHashCreatePtr(init_size) 
T_INT4 init_size; 

Arguments

init_size — the initial size of the hash table

Return Values

Returns a new hash table suitable for pointer keys.

Diagnostics

None

Description

TutHashCreatePtr creates a new hash table with pointers for keys. Dynamically allocated pointers, such as those returned by TutMalloc, have the property of being suitably aligned to store any data object (such as integers, floats or doubles). These pointers, when cast to long integers, are multiples of 16 on most system architectures. Therefore the hash function can simply divide the pointer key by 16 to produce a nice tight spread of hash codes.

Caution

Providing an init_size that is too small causes the hash table to be resized often as new entries are inserted.

See Also

TutHashCreate, TutHashCreateInt, TutHashCreateStr, TutHashCreateStri

Examples

This example uses a hash table to look up file names from open files:

T_HASH_TABLE hash_table; 
FILE *file1; 
FILE *file2; 
 
hash_table = TutHashCreatePtr(4); 
/* map file pointers to file name strings */ 
file1 = TutFOpen("foo.tmp", "w"); 
if (file1 != NULL) { 
  TutHashInsert(hash_table, file1, "foo.tmp"); 
}  
file2 = TutFOpen("bar.tmp", "w"); 
if (file2 != NULL) { 
  TutHashInsert(hash_table, file2, "bar.tmp"); 
}  
 
TutOut("The value for file %x is %s.\n", 
        file1, 
        TutHashLookup(hash_table, file1)); 

This example is the hash function and test function used by TutHashCreatePtr:

/* =========================================================== */ 
/*..utHashCalcPtr -- hash function for (malloced) pointers */ 
static T_INT4 utHashCalcPtr(key) 
T_PTR key; 
{ 
  return (T_UINT4)key >> 4; /* divide by 16 */ 
} /* utHashCalcPtr */ 
 
/* =========================================================== */ 
/*..utHashTestPtr -- test function for (malloced) pointers */ 
static T_BOOL utHashTestPtr(key1, key2) 
T_PTR key1; 
T_PTR key2; 
{ 
   return key1 == key2; 
} /* utHashTestPtr */ 

This example creates the hash table:

/* ============================================================== */ 
/*..TutHashCreatePtr -- convenience func to create hash table with ptr keys */ 
T_HASH_TABLE TutHashCreatePtr(init_size) 
T_INT4 init_size; /* initial size of hash table */ 
{ 
  return TutHashCreate(init_size, utHashCalcPtr, utHashTestPtr); 
} /* TutHashCreatePtr */ 

TIBCO SmartSockets™ Utilities
Software Release 6.8, July 2006
Copyright © TIBCO Software Inc. All rights reserved
www.tibco.com