Changeset 1073 for trunk/psLib/src/sysUtils/psHash.c
- Timestamp:
- Jun 23, 2004, 1:00:17 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/sysUtils/psHash.c (modified) (21 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sysUtils/psHash.c
r964 r1073 10 10 * @author George Gusciora, MHPCC 11 11 * 12 * @version $Revision: 1. 8$ $Name: not supported by cvs2svn $13 * @date $Date: 2004-06- 10 00:09:55 $12 * @version $Revision: 1.9 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2004-06-23 23:00:15 $ 14 14 * 15 15 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 18 18 #include <stdio.h> 19 19 #include <string.h> 20 #include <stdbool.h> 20 21 #include "psHash.h" 21 22 #include "psMemory.h" … … 24 25 #include "psAbort.h" 25 26 27 static psHashBucket *hashBucketAlloc(const char *key,void *data,psHashBucket *next); 28 static void hashBucketFree(psHashBucket *bucket); 29 static void *doHashWork(psHash* table, const char* key, void* data, bool remove 30 ); 31 static void hashFree(psHash *table); 26 32 27 33 /****************************************************************************** … … 87 93 // Allocate memory for the new hash bucket. 88 94 psHashBucket *bucket = psAlloc(sizeof(psHashBucket)); 95 p_psMemSetDeallocator(bucket,(psFreeFcn)hashBucketFree); 89 96 90 97 // Initialize the bucket. … … 105 112 106 113 /****************************************************************************** 107 hashBucketFree(bucket, itemFree): This procedure deallocates the specified 108 hash bucket. If "itemFree" is NULL, then we simply free the data with the 109 standard psFree() function. If "itemFree" is not NULL, then it must be a 110 function pointer which takes the hash bucket as a parameter, and frees the 111 data in that hash bucket. 114 hashBucketFree(bucket): This procedure deallocates the specified 115 hash bucket. 112 116 Inputs: 113 117 bucket: the hash bucket to be freed. 114 itemFree: a function pointer, possibly NULL.115 118 Return: 116 119 NONE 117 120 *****************************************************************************/ 118 static void hashBucketFree(psHashBucket *bucket, // bucket to free 119 void (*itemFree)(void *item)) // how to free data; 120 { 121 if (bucket == NULL) 122 { 121 static void hashBucketFree(psHashBucket *bucket) 122 { 123 if (bucket == NULL) { 123 124 return; 124 125 } … … 126 127 // A bucket is actually a linked list of buckets. We recursively step 127 128 // through that linked list, free each bucket. 128 if (bucket->next != NULL) 129 { 130 hashBucketFree(bucket, itemFree); 131 } 129 psFree(bucket->next); 132 130 133 131 psFree(bucket->key); 134 psMemDecrRefCounter(bucket->data); 135 136 if (itemFree != NULL) 137 { 138 itemFree(bucket->data); 139 } else 140 { 141 psFree(bucket->data); 142 } 143 144 psFree(bucket); 132 133 psFree(bucket->data); 145 134 } 146 135 … … 159 148 // Create the new hash table. 160 149 psHash *table = psAlloc(sizeof(psHash)); 150 p_psMemSetDeallocator(table,(psFreeFcn)hashFree); 161 151 162 152 // Allocate memory for the buckets. … … 185 175 Inputs: 186 176 table: a hash table 187 itemFree: a function pointer, possibly NULL.188 177 Return: 189 178 NONE 190 179 *****************************************************************************/ 191 void psHashFree(psHash *table, // hash table to be freed 192 void (*itemFree)(void *item)) // how to free hashed data; or NULL 180 static void hashFree(psHash *table) 193 181 { 194 182 psHashBucket *tmp = NULL; // Used to step through linked list. … … 196 184 int i = 0; // Loop index variable. 197 185 198 if (table == NULL) 199 { 186 if (table == NULL) { 200 187 return; 201 188 } … … 204 191 // NULL, then free the bucket via a function call to hashBucketFree(); 205 192 206 for (i = 0; i < table->nbucket; i++) 207 { 193 for (i = 0; i < table->nbucket; i++) { 208 194 // A bucket is composed of a linked list of buckets. We use the 209 195 // "tmp" and "ptr" pointers to step through that list and free each … … 214 200 while (ptr != NULL) { 215 201 tmp = ptr->next; 216 hashBucketFree(ptr, itemFree);202 psFree(ptr); 217 203 ptr = tmp; 218 204 } … … 222 208 // Free the bucket structure, then the hash table. 223 209 psFree(table->buckets); 224 psFree(table); 225 } 226 227 /****************************************************************************** 228 doHashWork(table, key, data, remove, itemFree): This is an internal 210 } 211 212 /****************************************************************************** 213 doHashWork(table, key, data, remove): This is an internal 229 214 procedure which does the bulk of the work in using the hash table. Depending 230 215 upon the input parameters, it will either insert a new key/data into the hash 231 216 table, retrieve the data for a specified key, or remove a key/data item. If 232 we try to insert a key that already exists in the hash table, then we call 233 the user-supplied function itemfree (or psFree if that is NULL), to free the 234 existing data/key item. 217 we try to insert a key that already exists in the hash table, then we deallocate 218 the existing data/key item. 235 219 Inputs: 236 220 table: a hash table … … 238 222 data: the data to insert, if not NULL 239 223 remove: set to non-zero if the key/data should be removed from the table. 240 itemFree: function pointer241 224 Return: 242 225 NONE … … 246 229 there is little common code between those functions. 247 230 *****************************************************************************/ 248 static void *doHashWork(psHash *table, // table to insert in 249 const char *key, // key to use 250 void *data, // data to insert, or (if NULL) retrieve/remove 251 int remove 252 , // remove the item from the list? 253 void (*itemFree)(void *item)) // how to free hashed data 231 static void *doHashWork(psHash *table, const char *key, void *data, bool remove 232 ) 254 233 { 255 234 long int hash = 1; // This will contain an integer value … … 313 292 } 314 293 315 psFree(ptr->key);316 294 psFree(ptr); 317 295 318 296 // By definition, the data associated with that key 319 297 // must be returned, not freed. 320 return psMemDecrRefCounter(data);298 return data; 321 299 } 322 300 optr = ptr; … … 354 332 // the new data was not inserted into the hash table. 355 333 356 if (itemFree == NULL) { 357 psFree(psMemDecrRefCounter(ptr->data)); 358 } else { 359 itemFree(psMemDecrRefCounter(ptr->data)); 360 } 334 psFree(ptr->data); 361 335 362 336 ptr->data = psMemIncrRefCounter(data); … … 385 359 NONE 386 360 *****************************************************************************/ 387 void *psHashInsert(psHash *table, // table to insert in 388 const char *key, // key to use 389 void *data, // data to insert 390 void (*itemFree)(void *item)) // how to free hashed data; 361 void *psHashInsert(psHash *table, const char *key, void *data) 391 362 { 392 363 if (table == NULL) { … … 400 371 } 401 372 402 return doHashWork(table, key, data, 0 , itemFree);373 return doHashWork(table, key, data, 0); 403 374 } 404 375 … … 425 396 426 397 427 return doHashWork(table, key, NULL, 0 , NULL);398 return doHashWork(table, key, NULL, 0); 428 399 } 429 400 … … 438 409 The data that was associated with that key. 439 410 *****************************************************************************/ 440 void *psHashRemove(psHash *table, // table to lookup key in 441 const char *key, // key to lookup 442 void (*itemFree)(void *item)) // how to free hashed data; 411 void *psHashRemove(psHash *table, const char *key) 443 412 { 444 413 if (table == NULL) { … … 449 418 } 450 419 451 return doHashWork(table, key, NULL, 1 , itemFree);452 } 420 return doHashWork(table, key, NULL, 1); 421 }
Note:
See TracChangeset
for help on using the changeset viewer.
