Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

hashTable.c File Reference

#include "lclintMacros.nf"
#include "basic.h"
#include "256_random_numbers.nf"

Go to the source code of this file.

Defines

#define hbucket_undefined   0

Functions

void hashTable_free ( hashTable h)
hashTable hashTable_create (int size)
cstring hashTable_stats (hashTable h)
void hashTable_insert (hashTable h, cstring key, int value)
int hashTable_lookup (hashTable h, cstring key)
void hashTable_replaceKey (hashTable h, cstring oldkey, cstring newkey)
void hashTable_remove (hashTable h, cstring key)


Define Documentation

#define hbucket_undefined   0
 

Definition at line 40 of file hashTable.c.


Function Documentation

hashTable hashTable_create ( int size )
 

Definition at line 266 of file hashTable.c.

Referenced by fileTable_create().

00267 {
00268   int i;
00269   hashTable h = (hashTable) dmalloc (sizeof (*h));
00270   
00271   h->size = size;
00272   h->nentries = 0;
00273   h->buckets = (hbucket *) dmalloc (sizeof (*h->buckets) * size);
00274   
00275   /*@+loopexec@*/
00276   for (i = 0; i < size; i++)
00277     {
00278       h->buckets[i] = hbucket_undefined;
00279     }
00280   /*@-loopexec@*/
00281   return h;
00282 }

void hashTable_free ( hashTable h )
 

Definition at line 183 of file hashTable.c.

Referenced by fileTable_free().

00184 {
00185   int i;
00186 
00187   for (i = 0; i < h->size; i++)
00188     {
00189       hbucket_free (h->buckets[i]);
00190     }
00191 
00192   sfree (h->buckets);
00193   sfree (h);
00194 }

void hashTable_insert ( hashTable h,
cstring key,
int value )
 

Definition at line 338 of file hashTable.c.

00339 {
00340   unsigned int hindex;
00341   hbucket hb;
00342   hentry e;  
00343 
00344   
00345   h->nentries++;
00346 
00347   /*
00348   ** rehashing based (loosely) on code by Steve Harrison
00349   */
00350 
00351   if (h->nentries * 162 > h->size * 100) 
00352     {
00353       int i;
00354       int oldsize = h->size;
00355       int newsize = 1 + ((oldsize * 26244) / 10000); /* 26244 = 162^2 */
00356       hbucket *oldbuckets = h->buckets;
00357 
00358       
00359       h->size = newsize;  
00360       h->nentries = 0;
00361       h->buckets = (hbucket *) dmalloc (sizeof (*h->buckets) * newsize);
00362 
00363       /*@+loopexec@*/
00364       for (i = 0; i < newsize; i++)
00365         {
00366           h->buckets[i] = hbucket_undefined;
00367         }
00368       /*@=loopexec@*/
00369       
00370       for (i = 0; i < oldsize; i++)
00371         {
00372           hbucket bucket = oldbuckets[i];
00373           
00374           if (!hbucket_isNull (bucket))
00375             {
00376               int j;
00377 
00378               for (j = 0; j < bucket->size; j++)
00379                 {
00380                   hashTable_addEntry (h, bucket->entries[j]);
00381                 }
00382 
00383               sfree (bucket);
00384             }
00385         }
00386 
00387       sfree (oldbuckets);
00388     }
00389 
00390   
00391   e = hentry_create (key, value);
00392   hindex = hashTable_hashValue (h, key);
00393   hb = h->buckets[hindex];
00394   
00395   if (hbucket_isNull (hb))
00396     {
00397             h->buckets[hindex] = hbucket_single (e);
00398     }
00399   else
00400     {
00401             hbucket_add (hb, e);
00402     }
00403 
00404   }

int hashTable_lookup ( hashTable h,
cstring key )
 

Definition at line 407 of file hashTable.c.

00408 {
00409   hbucket hb = hashTable_hash (h, key);
00410 
00411   return (hbucket_lookup (hb, key));
00412 }

void hashTable_remove ( hashTable h,
cstring key )
 

Definition at line 443 of file hashTable.c.

00444 {
00445   hbucket hb = hashTable_hash (h, key);
00446 
00447   if (!hbucket_isNull (hb))
00448     {
00449       int i;
00450       
00451       for (i = 0; i < hb->size; i++)
00452         {
00453           if (cstring_equal (hb->entries[i].key, key))
00454             {
00455               if (i < hb->size - 1)
00456                 {
00457                   hb->entries[i] = hb->entries[hb->size - 1];
00458                 }
00459               
00460               hb->size--;
00461               return;
00462             }
00463         }
00464     }
00465 
00466   llbug (message ("hashTable_removeKey: %s not found", key));
00467 }

void hashTable_replaceKey ( hashTable h,
cstring oldkey,
cstring newkey )
 

Definition at line 419 of file hashTable.c.

00420 {
00421   hbucket hb = hashTable_hash (h, oldkey);
00422 
00423   llassert (cstring_equal (oldkey, newkey));
00424 
00425   if (!hbucket_isNull (hb))
00426     {
00427       int i;
00428       
00429       for (i = 0; i < hb->size; i++)
00430         {
00431           if (cstring_equal (hb->entries[i].key, oldkey))
00432             {
00433               hb->entries[i].key = newkey;
00434               return;
00435             }
00436         }
00437     }
00438 
00439   llbug (message ("hashTable_replaceKey: %s not found", oldkey));
00440 }

cstring hashTable_stats ( hashTable h )
 

Definition at line 308 of file hashTable.c.

00309 {
00310   return (message ("size: %d, collisions: %d, empty: %d\n", 
00311                         h->size, hashTable_countCollisions (h),
00312                         hashTable_countEmpty (h)));
00313 }


Generated at Fri Nov 3 18:57:59 2000 for LCLint by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000