3 * Hash table implemenation
9 #define Bucket JOIN(HASHTYPE,Buket)
10 #define Bucket_s JOIN(HASHTYPE,Buket_s)
12 typedef struct Bucket_s * Bucket;
17 Bucket next; /*!< pointer to next item in bucket */
18 HTKEYTYPE key; /*!< hash key */
20 int dataCount; /*!< data entries */
21 HTDATATYPE data[1]; /*!< data - grows by resizing whole bucket */
28 int numBuckets; /*!< number of hash buckets */
29 Bucket * buckets; /*!< hash bucket array */
30 hashFunctionType fn; /*!< generate hash value for key */
31 hashEqualityType eq; /*!< compare hash keys for equality */
33 int bucketCount; /*!< number of used buckets */
34 int keyCount; /*!< number of keys */
36 int dataCount; /*!< number of data entries */
37 hashFreeData freeData;
42 * Find entry in hash table.
43 * @param ht pointer to hash table
44 * @param key pointer to key value
45 * @param keyHash key hash
46 * @return pointer to hash bucket of key (or NULL)
49 Bucket HASHPREFIX(findEntry)(HASHTYPE ht, HTKEYTYPE key, unsigned int keyHash)
51 unsigned int hash = keyHash % ht->numBuckets;
52 Bucket b = ht->buckets[hash];
54 while (b && ht->eq(b->key, key))
60 HASHTYPE HASHPREFIX(Create)(int numBuckets,
61 hashFunctionType fn, hashEqualityType eq,
64 , hashFreeData freeData
70 ht = xmalloc(sizeof(*ht));
71 ht->numBuckets = numBuckets;
72 ht->buckets = xcalloc(numBuckets, sizeof(*ht->buckets));
73 ht->freeKey = freeKey;
75 ht->freeData = freeData;
80 ht->bucketCount = ht->keyCount = 0;
84 static void HASHPREFIX(Resize)(HASHTYPE ht, int numBuckets) {
85 Bucket * buckets = xcalloc(numBuckets, sizeof(*ht->buckets));
87 for (int i=0; i<ht->numBuckets; i++) {
88 Bucket b = ht->buckets[i];
91 unsigned int hash = ht->fn(b->key) % numBuckets;
93 b->next = buckets[hash];
99 ht->buckets = buckets;
100 ht->numBuckets = numBuckets;
103 unsigned int HASHPREFIX(KeyHash)(HASHTYPE ht, HTKEYTYPE key)
108 void HASHPREFIX(AddHEntry)(HASHTYPE ht, HTKEYTYPE key, unsigned int keyHash
114 unsigned int hash = keyHash % ht->numBuckets;
115 Bucket b = ht->buckets[hash];
117 Bucket * b_addr = ht->buckets + hash;
121 ht->bucketCount += 1;
124 while (b && ht->eq(b->key, key)) {
133 b = xmalloc(sizeof(*b));
139 b->next = ht->buckets[hash];
140 ht->buckets[hash] = b;
144 // resizing bucket TODO: increase exponentially
145 // Bucket_s already contains space for one dataset
146 b = *b_addr = xrealloc(
147 b, sizeof(*b) + sizeof(b->data[0]) * (b->dataCount));
148 // though increasing dataCount after the resize
149 b->data[b->dataCount++] = data;
153 if (ht->keyCount > ht->numBuckets) {
154 HASHPREFIX(Resize)(ht, ht->numBuckets * 2);
158 void HASHPREFIX(AddEntry)(HASHTYPE ht, HTKEYTYPE key
165 HASHPREFIX(AddHEntry)(ht, key, ht->fn(key), data);
167 HASHPREFIX(AddHEntry)(ht, key, ht->fn(key));
171 void HASHPREFIX(Empty)( HASHTYPE ht)
176 if (ht->bucketCount == 0) return;
178 for (i = 0; i < ht->numBuckets; i++) {
182 ht->buckets[i] = NULL;
187 b->key = ht->freeKey(b->key);
191 for (j=0; j < b->dataCount; j++ ) {
192 b->data[j] = ht->freeData(b->data[j]);
197 } while ((b = n) != NULL);
206 HASHTYPE HASHPREFIX(Free)(HASHTYPE ht)
210 HASHPREFIX(Empty)(ht);
211 ht->buckets = _free(ht->buckets);
217 int HASHPREFIX(HasHEntry)(HASHTYPE ht, HTKEYTYPE key, unsigned int keyHash)
221 if (!(b = HASHPREFIX(findEntry)(ht, key, keyHash))) return 0; else return 1;
224 int HASHPREFIX(HasEntry)(HASHTYPE ht, HTKEYTYPE key)
226 return HASHPREFIX(HasHEntry)(ht, key, ht->fn(key));
229 int HASHPREFIX(GetHEntry)(HASHTYPE ht, HTKEYTYPE key, unsigned int keyHash,
231 HTDATATYPE** data, int * dataCount,
236 int rc = ((b = HASHPREFIX(findEntry)(ht, key, keyHash)) != NULL);
240 *data = rc ? b->data : NULL;
242 *dataCount = rc ? b->dataCount : 0;
250 int HASHPREFIX(GetEntry)(HASHTYPE ht, HTKEYTYPE key,
252 HTDATATYPE** data, int * dataCount,
256 return HASHPREFIX(GetHEntry)(ht, key, ht->fn(key),
263 unsigned int HASHPREFIX(NumBuckets)(HASHTYPE ht) {
264 return ht->numBuckets;
267 unsigned int HASHPREFIX(UsedBuckets)(HASHTYPE ht) {
268 return ht->bucketCount;
271 unsigned int HASHPREFIX(NumKeys)(HASHTYPE ht) {
276 unsigned int HASHPREFIX(NumData)(HASHTYPE ht) {
277 return ht->dataCount;
282 void HASHPREFIX(PrintStats)(HASHTYPE ht) {
286 int hashcnt=0, bucketcnt=0, datacnt=0;
289 for (i=0; i<ht->numBuckets; i++) {
291 for (bucket=ht->buckets[i]; bucket; bucket=bucket->next){
294 datacnt += bucket->dataCount;
297 if (maxbuckets < buckets) maxbuckets = buckets;
298 if (buckets) hashcnt++;
299 bucketcnt += buckets;
301 fprintf(stderr, "Hashsize: %i\n", ht->numBuckets);
302 fprintf(stderr, "Hashbuckets: %i\n", hashcnt);
303 fprintf(stderr, "Keys: %i\n", bucketcnt);
304 fprintf(stderr, "Values: %i\n", datacnt);
305 fprintf(stderr, "Max Keys/Bucket: %i\n", maxbuckets);