Main Page | Modules | Data Structures | File List | Data Fields | Related Pages

hashmap.c

00001 #include "hashmap.h"
00002 #include "dbug_mem.h"
00003 
00004 
00005 int
00006 chtbl_init(CHTbl *htbl, int buckets, int (*h)(const void *key), int
00007    (*match)(const void *obj1, const void *obj2), void (*destroy)(void *obj))
00008 {
00009     int i;
00010 
00011     //MSG_DEBUG("chtbl_init() called");
00012     
00013     T_RDWR_INIT(&(htbl->rwlock), NULL);
00014 
00015     /* Allocate space for the hash table. */
00016     if ((htbl->table = (List *)malloc(buckets * sizeof(List))) == NULL)
00017         return -1;
00018 
00019     /* Initialize the buckets. */
00020     htbl->buckets = buckets;
00021 
00022     for (i = 0; i < htbl->buckets; i++) {
00023         list_init(&htbl->table[i], match, destroy);
00024     }
00025 
00026     /* Encapsulate the functions. */
00027     htbl->h = h;
00028 
00029     return 0;
00030 }
00031 
00032 
00033 void
00034 chtbl_destroy(CHTbl *htbl)
00035 {
00036     int i;
00037 
00038     //MSG_DEBUG("chtbl_destroy() called");
00039     
00040     T_RDWR_WLOCK(&(htbl->rwlock));
00041 
00042     /* Destroy each bucket. */
00043     for (i = 0; i < htbl->buckets; i++) {
00044         list_destroy(&htbl->table[i]);
00045     }
00046     
00047     /* Free the storage allocated for the hash table. */
00048     free(htbl->table);
00049     
00050     
00051     /* No operations are allowed now, but clear the structure as a precaution. */
00052     memset(htbl, 0, sizeof(CHTbl));
00053     
00054     T_RDWR_WUNLOCK(&(htbl->rwlock));
00055 
00056     return;
00057 }
00058 
00059 
00060 int
00061 chtbl_size(CHTbl *htbl)
00062 {
00063     int cnt = 0;
00064     int i;
00065     
00066     //MSG_DEBUG("chtbl_size() called");
00067     
00068     T_RDWR_RLOCK(&(htbl->rwlock));
00069     
00070     for (i = 0; i < htbl->buckets; i++) {
00071         cnt += list_getsize(&htbl->table[i]);
00072     }
00073     
00074     T_RDWR_RUNLOCK(&(htbl->rwlock));
00075     
00076     return cnt;
00077 }
00078 
00079 
00080 int
00081 chtbl_insert(CHTbl *htbl, const void *data)
00082 {
00083     void *temp;
00084     int bucket, retval;
00085      
00086 
00087     //MSG_DEBUG("chtbl_insert() called");
00088     
00089     /* Do nothing if the data is already in the table. */
00090     temp = (void *)data;
00091     
00092     T_RDWR_RLOCK(&(htbl->rwlock));
00093 
00094     /* Hash the key. */
00095     bucket = htbl->h(data) % htbl->buckets;
00096     
00097     /* Insert the data into the bucket. */
00098     retval = list_insert(&htbl->table[bucket], (void *)data);
00099     
00100     T_RDWR_RUNLOCK(&(htbl->rwlock));
00101 
00102     return retval;
00103 }
00104 
00105 
00106 int
00107 chtbl_remove(CHTbl *htbl, void **data)
00108 {
00109     int bucket;
00110     int retval = -1;
00111  
00112     //MSG_DEBUG("chtbl_remove() called");
00113     
00114     T_RDWR_RLOCK(&(htbl->rwlock));
00115 
00116     /* Hash the key. */
00117     bucket = htbl->h(*data) % htbl->buckets;
00118     
00119     /* Remove the data from the bucket. */
00120     retval = list_remove(&htbl->table[bucket], data);
00121 
00122     T_RDWR_RUNLOCK(&(htbl->rwlock));
00123 
00124     /* Return that the data was not found. */
00125     return retval;
00126 }
00127 
00128 
00129 int
00130 chtbl_lookup(CHTbl *htbl, void **data)
00131 {
00132     int bucket = 0;
00133     int retval = 0;
00134     
00135     //MSG_DEBUG("chtbl_lookup() called");
00136     
00137     T_RDWR_RLOCK(&(htbl->rwlock));
00138 
00139     /* Hash the key. */
00140     bucket = htbl->h(*data) % htbl->buckets;
00141     
00142     retval = list_lookup(&(htbl->table[bucket]), data);
00143     
00144     T_RDWR_RUNLOCK(&(htbl->rwlock));
00145 
00146     return retval;
00147 }

Generated on Thu May 27 23:27:28 2004 for Mntd by doxygen 1.3.5