rpmhash: Accessor functions for the size and usage
[platform/upstream/rpm.git] / lib / rpmhash.C
1 /**
2  * \file lib/rpmhash.c
3  * Hash table implemenation
4  */
5
6 #include "system.h"
7 #include "debug.h"
8
9 #define Bucket JOIN(HASHTYPE,Buket)
10 #define Bucket_s JOIN(HASHTYPE,Buket_s)
11
12 typedef struct  Bucket_s * Bucket;
13
14 /**
15  */
16 struct  Bucket_s {
17     Bucket next;        /*!< pointer to next item in bucket */
18     HTKEYTYPE key;      /*!< hash key */
19 #ifdef HTDATATYPE
20     int dataCount;      /*!< data entries */
21     HTDATATYPE data[1]; /*!< data - grows by resizing whole bucket */
22 #endif
23 };
24
25 /**
26  */
27 struct HASHSTRUCT {
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 */
32     hashFreeKey freeKey;
33     int bucketCount;                    /*!< number of used buckets */
34     int keyCount;                       /*!< number of keys */
35 #ifdef HTDATATYPE
36     int dataCount;                      /*!< number of data entries */
37     hashFreeData freeData;
38 #endif
39 };
40
41 /**
42  * Find entry in hash table.
43  * @param ht            pointer to hash table
44  * @param key           pointer to key value
45  * @return pointer to hash bucket of key (or NULL)
46  */
47 static
48 Bucket HASHPREFIX(findEntry)(HASHTYPE ht, HTKEYTYPE key)
49 {
50     unsigned int hash;
51     Bucket b;
52
53     hash = ht->fn(key) % ht->numBuckets;
54     b = ht->buckets[hash];
55
56     while (b && ht->eq(b->key, key))
57         b = b->next;
58
59     return b;
60 }
61
62 HASHTYPE HASHPREFIX(Create)(int numBuckets,
63                             hashFunctionType fn, hashEqualityType eq,
64                             hashFreeKey freeKey
65 #ifdef HTDATATYPE
66 , hashFreeData freeData
67 #endif
68 )
69 {
70     HASHTYPE ht;
71
72     ht = xmalloc(sizeof(*ht));
73     ht->numBuckets = numBuckets;
74     ht->buckets = xcalloc(numBuckets, sizeof(*ht->buckets));
75     ht->freeKey = freeKey;
76 #ifdef HTDATATYPE
77     ht->freeData = freeData;
78     ht->dataCount = 0;
79 #endif
80     ht->fn = fn;
81     ht->eq = eq;
82     ht->bucketCount = ht->keyCount = 0;
83     return ht;
84 }
85
86 static HASHPREFIX(Resize)(HASHTYPE ht, int numBuckets) {
87     Bucket * buckets = xcalloc(numBuckets, sizeof(*ht->buckets));
88
89     for (int i=0; i<ht->numBuckets; i++) {
90         Bucket b = ht->buckets[i];
91         Bucket nextB;
92         while (b != NULL) {
93             unsigned int hash = ht->fn(b->key) % numBuckets;
94             nextB = b->next;
95             b->next = buckets[hash];
96             buckets[hash] = b;
97             b = nextB;
98         }
99     }
100     free(ht->buckets);
101     ht->buckets = buckets;
102     ht->numBuckets = numBuckets;
103 }
104
105 void HASHPREFIX(AddEntry)(HASHTYPE ht, HTKEYTYPE key
106 #ifdef HTDATATYPE
107 , HTDATATYPE data
108 #endif
109 )
110 {
111     unsigned int hash;
112     Bucket b;
113     Bucket * b_addr;
114
115     hash = ht->fn(key) % ht->numBuckets;
116     b = ht->buckets[hash];
117     b_addr = ht->buckets + hash;
118
119     if (b == NULL) {
120         ht->bucketCount += 1;
121     }
122
123     while (b && ht->eq(b->key, key)) {
124         b_addr = &(b->next);
125         b = b->next;
126     }
127
128     if (b == NULL) {
129         ht->keyCount += 1;
130         b = xmalloc(sizeof(*b));
131         b->key = key;
132 #ifdef HTDATATYPE
133         b->dataCount = 1;
134         b->data[0] = data;
135 #endif
136         b->next = ht->buckets[hash];
137         ht->buckets[hash] = b;
138     }
139 #ifdef HTDATATYPE
140     else {
141         // resizing bucket TODO: increase exponentially
142         // Bucket_s already contains space for one dataset
143         b = *b_addr = xrealloc(
144             b, sizeof(*b) + sizeof(b->data[0]) * (b->dataCount));
145         // though increasing dataCount after the resize
146         b->data[b->dataCount++] = data;
147     }
148     ht->dataCount += 1;
149 #endif
150     if (ht->keyCount > ht->numBuckets) {
151         HASHPREFIX(Resize)(ht, ht->numBuckets * 2);
152     }
153 }
154
155 HASHTYPE HASHPREFIX(Free)(HASHTYPE ht)
156 {
157     Bucket b, n;
158     int i;
159     if (ht==NULL)
160         return ht;
161     for (i = 0; i < ht->numBuckets; i++) {
162         b = ht->buckets[i];
163         if (b == NULL)
164             continue;
165         ht->buckets[i] = NULL;
166
167         do {
168             n = b->next;
169             if (ht->freeKey)
170                 b->key = ht->freeKey(b->key);
171 #ifdef HTDATATYPE
172             if (ht->freeData) {
173                 int j;
174                 for (j=0; j < b->dataCount; j++ ) {
175                     b->data[j] = ht->freeData(b->data[j]);
176                 }
177             }
178 #endif
179             b = _free(b);
180         } while ((b = n) != NULL);
181     }
182
183     ht->buckets = _free(ht->buckets);
184     ht = _free(ht);
185
186     return NULL;
187 }
188
189 int HASHPREFIX(HasEntry)(HASHTYPE ht, HTKEYTYPE key)
190 {
191     Bucket b;
192
193     if (!(b = HASHPREFIX(findEntry)(ht, key))) return 0; else return 1;
194 }
195
196 #ifdef HTDATATYPE
197
198 int HASHPREFIX(GetEntry)(HASHTYPE ht, HTKEYTYPE key, HTDATATYPE** data,
199                int * dataCount, HTKEYTYPE* tableKey)
200 {
201     Bucket b;
202     int rc = ((b = HASHPREFIX(findEntry)(ht, key)) != NULL);
203
204     if (data)
205         *data = rc ? b->data : NULL;
206     if (dataCount)
207         *dataCount = rc ? b->dataCount : 0;
208     if (tableKey && rc)
209         *tableKey = b->key;
210
211     return rc;
212 }
213
214 #endif
215
216
217 unsigned int HASHPREFIX(NumBuckets)(HASHTYPE ht) {
218     return ht->numBuckets;
219 }
220
221 unsigned int HASHPREFIX(UsedBuckets)(HASHTYPE ht) {
222     return ht->bucketCount;
223 }
224
225 unsigned int HASHPREFIX(NumKeys)(HASHTYPE ht) {
226     return ht->keyCount;
227 }
228
229 #ifdef HTDATATYPE
230 unsigned int HASHPREFIX(NumData)(HASHTYPE ht) {
231     return ht->dataCount;
232 }
233 #endif
234
235
236 void HASHPREFIX(PrintStats)(HASHTYPE ht) {
237     int i;
238     Bucket bucket;
239
240     int hashcnt=0, bucketcnt=0, datacnt=0;
241     int maxbuckets=0;
242
243     for (i=0; i<ht->numBuckets; i++) {
244         int buckets = 0;
245         for (bucket=ht->buckets[i]; bucket; bucket=bucket->next){
246             buckets++;
247 #ifdef HTDATATYPE
248             datacnt += bucket->dataCount;
249 #endif
250         }
251         if (maxbuckets < buckets) maxbuckets = buckets;
252         if (buckets) hashcnt++;
253         bucketcnt += buckets;
254     }
255     fprintf(stderr, "Hashsize: %i\n", ht->numBuckets);
256     fprintf(stderr, "Hashbuckets: %i\n", hashcnt);
257     fprintf(stderr, "Keys: %i\n", bucketcnt);
258     fprintf(stderr, "Values: %i\n", datacnt);
259     fprintf(stderr, "Max Keys/Bucket: %i\n", maxbuckets);
260 }