Changeset 560 for trunk/psLib/src/sysUtils/psHash.c
- Timestamp:
- May 1, 2004, 1:04:35 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/sysUtils/psHash.c (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sysUtils/psHash.c
r494 r560 13 13 #include "psTrace.h" 14 14 15 // A bucket that holds an item of data 16 typedef struct HashBucket 17 { 18 char *key; // key for this item of data 19 void *data; // the data itself 20 struct HashBucket *next; // list of other possible keys 21 } 22 HashBucket; 23 24 // An entire hash table 25 struct HashTable 26 { 27 int nbucket; // number of buckets 28 HashBucket **buckets; // the buckets themselves 29 }; 30 31 /****************************************************************************** 32 Con/Destruct buckets 33 *****************************************************************************/ 34 static HashBucket *hashBucketAlloc(const char *key, 35 void *data, 36 HashBucket *next) 37 { 38 HashBucket *bucket = psAlloc(sizeof(HashBucket)); 15 /****************************************************************************** 16 Construct buckets 17 *****************************************************************************/ 18 static psHashBucket *hashBucketAlloc(const char *key, 19 void *data, 20 psHashBucket *next) 21 { 22 psHashBucket *bucket = psAlloc(sizeof(psHashBucket)); 39 23 40 24 bucket->key = psStringCopy(key); … … 46 30 47 31 /****************************************************************************** 48 49 *****************************************************************************/ 50 static void hashBucketFree( 51 HashBucket *bucket, // bucket to free 52 void (*itemFree)(void *item)) // how to free hashed data; 53 // or NULL 32 Destruct buckets 33 *****************************************************************************/ 34 static void hashBucketFree(psHashBucket *bucket, // bucket to free 35 void (*itemFree)(void *item)) // how to free data; 54 36 { 55 37 if (bucket == NULL) … … 75 57 { 76 58 psHash *table = psAlloc(sizeof(psHash)); 77 table->buckets = psAlloc(nbucket*sizeof( HashBucket *));59 table->buckets = psAlloc(nbucket*sizeof(psHashBucket *)); 78 60 table->nbucket = nbucket; 79 61 … … 104 86 { 105 87 if (table->buckets[i] != NULL) { 106 HashBucket *ptr = table->buckets[i];88 psHashBucket *ptr = table->buckets[i]; 107 89 while (ptr != NULL) { 108 HashBucket *tmp = ptr->next;90 psHashBucket *tmp = ptr->next; 109 91 hashBucketFree(ptr, itemFree); 110 92 ptr = tmp; … … 124 106 N.b. this is NOT a good hash function! See Knuth for some better ones 125 107 *****************************************************************************/ 126 static void *doHashWork( 127 psHash *table, // table to insert in 128 const char *key, // key to use 129 void *data, // data to insert, 130 // or (if NULL) retrieve/remove 131 int remove 132 , // remove the item from the list? 133 void (*itemFree)(void *item)) // how to free hashed data 134 // or NULL 108 static void *doHashWork(psHash *table, // table to insert in 109 const char *key, // key to use 110 void *data, // data to insert, 111 // or (if NULL) retrieve/remove 112 int remove 113 , // remove the item from the list? 114 void (*itemFree)(void *item)) 115 // how to free hashed data 135 116 { 136 117 long int hash = 1; … … 142 123 hash &= (table->nbucket - 1); 143 124 144 HashBucket *ptr = table->buckets[hash]; 145 /* 146 * We've found the correct hash bucket, now we need to know what to do 147 */ 125 psHashBucket *ptr = table->buckets[hash]; 126 127 // We've found the correct hash bucket, now we need to know what to do 148 128 if (data == NULL) { // retrieve/remove 149 129 if (remove 150 130 ) { 151 HashBucket *optr = ptr;131 psHashBucket *optr = ptr; 152 132 while (ptr != NULL) { 153 133 if (strcmp(key, ptr->key) == 0) { // found it! … … 196 176 } 197 177 } 198 /* 199 * Not found, so insert at the front of the list 200 */ 178 179 // Not found, so insert at the front of the list 201 180 table->buckets[hash] = hashBucketAlloc(key, data, table->buckets[hash]); 202 181 … … 208 187 Insert a value into a hash table 209 188 *****************************************************************************/ 210 void *psHashInsert(psHash *table, // table to insert in189 void *psHashInsert(psHash *table, // table to insert in 211 190 const char *key, // key to use 212 void *data, // data to insert191 void *data, // data to insert 213 192 void (*itemFree)(void *item)) // how to free hashed data; 214 // or NULL215 193 { 216 194 return doHashWork(table, key, data, 0, itemFree); … … 220 198 Lookup a value in a hash table 221 199 *****************************************************************************/ 222 void *psHashLookup(psHash *table, // table to lookup key in200 void *psHashLookup(psHash *table, // table to lookup key in 223 201 const char *key) // key to lookup 224 202 { … … 229 207 Remove and return a value from a hash table 230 208 *****************************************************************************/ 231 void *psHashRemove(psHash *table, // table to lookup key in209 void *psHashRemove(psHash *table, // table to lookup key in 232 210 const char *key) // key to lookup 233 211 {
Note:
See TracChangeset
for help on using the changeset viewer.
