Eliminate unused-but-set warning from gcc on hashes without datatype
[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 void 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 = ht->fn(key) % ht->numBuckets;
112     Bucket b = ht->buckets[hash];
113 #ifdef HTDATATYPE
114     Bucket * b_addr = ht->buckets + hash;
115 #endif
116
117     if (b == NULL) {
118         ht->bucketCount += 1;
119     }
120
121     while (b && ht->eq(b->key, key)) {
122 #ifdef HTDATATYPE
123         b_addr = &(b->next);
124 #endif
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 void HASHPREFIX(Empty)( HASHTYPE ht)
156 {
157     Bucket b, n;
158     int i;
159
160     if (ht->bucketCount == 0) return;
161
162     for (i = 0; i < ht->numBuckets; i++) {
163         b = ht->buckets[i];
164         if (b == NULL)
165             continue;
166         ht->buckets[i] = NULL;
167
168         do {
169             n = b->next;
170             if (ht->freeKey)
171                 b->key = ht->freeKey(b->key);
172 #ifdef HTDATATYPE
173             if (ht->freeData) {
174                 int j;
175                 for (j=0; j < b->dataCount; j++ ) {
176                     b->data[j] = ht->freeData(b->data[j]);
177                 }
178             }
179 #endif
180             b = _free(b);
181         } while ((b = n) != NULL);
182     }
183     ht->bucketCount = 0;
184     ht->keyCount = 0;
185 #ifdef HTDATATYPE
186     ht->dataCount = 0;
187 #endif
188 }
189
190 HASHTYPE HASHPREFIX(Free)(HASHTYPE ht)
191 {
192     if (ht==NULL)
193         return ht;
194     HASHPREFIX(Empty)(ht);
195     ht->buckets = _free(ht->buckets);
196     ht = _free(ht);
197
198     return NULL;
199 }
200
201 int HASHPREFIX(HasEntry)(HASHTYPE ht, HTKEYTYPE key)
202 {
203     Bucket b;
204
205     if (!(b = HASHPREFIX(findEntry)(ht, key))) return 0; else return 1;
206 }
207
208 int HASHPREFIX(GetEntry)(HASHTYPE ht, HTKEYTYPE key,
209 #ifdef HTDATATYPE
210                          HTDATATYPE** data, int * dataCount,
211 #endif
212                          HTKEYTYPE* tableKey)
213 {
214     Bucket b;
215     int rc = ((b = HASHPREFIX(findEntry)(ht, key)) != NULL);
216
217 #ifdef HTDATATYPE
218     if (data)
219         *data = rc ? b->data : NULL;
220     if (dataCount)
221         *dataCount = rc ? b->dataCount : 0;
222 #endif
223     if (tableKey && rc)
224         *tableKey = b->key;
225
226     return rc;
227 }
228
229 unsigned int HASHPREFIX(NumBuckets)(HASHTYPE ht) {
230     return ht->numBuckets;
231 }
232
233 unsigned int HASHPREFIX(UsedBuckets)(HASHTYPE ht) {
234     return ht->bucketCount;
235 }
236
237 unsigned int HASHPREFIX(NumKeys)(HASHTYPE ht) {
238     return ht->keyCount;
239 }
240
241 #ifdef HTDATATYPE
242 unsigned int HASHPREFIX(NumData)(HASHTYPE ht) {
243     return ht->dataCount;
244 }
245 #endif
246
247
248 void HASHPREFIX(PrintStats)(HASHTYPE ht) {
249     int i;
250     Bucket bucket;
251
252     int hashcnt=0, bucketcnt=0, datacnt=0;
253     int maxbuckets=0;
254
255     for (i=0; i<ht->numBuckets; i++) {
256         int buckets = 0;
257         for (bucket=ht->buckets[i]; bucket; bucket=bucket->next){
258             buckets++;
259 #ifdef HTDATATYPE
260             datacnt += bucket->dataCount;
261 #endif
262         }
263         if (maxbuckets < buckets) maxbuckets = buckets;
264         if (buckets) hashcnt++;
265         bucketcnt += buckets;
266     }
267     fprintf(stderr, "Hashsize: %i\n", ht->numBuckets);
268     fprintf(stderr, "Hashbuckets: %i\n", hashcnt);
269     fprintf(stderr, "Keys: %i\n", bucketcnt);
270     fprintf(stderr, "Values: %i\n", datacnt);
271     fprintf(stderr, "Max Keys/Bucket: %i\n", maxbuckets);
272 }