-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFreqHash.c
406 lines (337 loc) · 9.94 KB
/
FreqHash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
* FreqHash.c
*
* A hash table specially designed for counting letter frequency.
*
* In order to use this hash table you must include stdio, stdlib and string.
*/
#define DEFAULT_CAPACITY 10
#define RESIZE_MIN 16
typedef struct {
char *key;
double value;
} Pair;
typedef struct {
Pair *pairs;
size_t length; /* length of pairs array */
} Bucket;
typedef struct {
Bucket *buckets;
size_t length; /* length of buckets array */
size_t count; /* number of items in buckets array */
} Hash;
/*
* Allocates (hash) with the default capacity.
*/
int hash_init(Hash *hash);
/*
* Allocates (hash) with the given capacity.
*/
int hash_init_capacity(Hash *hash, size_t capacity);
/*
* Clears (hash), deleting its contents and clearing all allocated memory.
*/
int hash_clear(Hash *hash);
/*
* Determines whether (key) exists in (hash). If so, returns nonzero; if not, returns
* zero.
*/
int hash_exists(Hash hash, char *key);
/*
* Finds (key) in (hash) and returns its corresponding value. A result of -1 indicates
* that the key was not found.
*/
long hash_get(Hash hash, char *key);
/*
* This is a function that is particularly useful when counting letter frequency. It
* finds (key) in (hash) and increases it by (value). If (key) is not found, it is created
* and its value is set to (value).
*/
int hash_inc(Hash *hash, const char *key, double value);
/*
* Prints (hash) to the output stream.
*/
int hash_print(Hash hash);
/*
* Puts (key) and (value) as a pair into (hash).
*/
long hash_put(Hash *hash, const char *key, double value);
/* Takes a function and calls that function for each key-value pair in Hash.
* If f returns a nonzero value, this function exits and returns that value.
*/
int hash_foreach(Hash hash, int (*f)(const char *key, double value));
/*
* Sorts (hash) by value and puts the resulting array in (res).
*
* res: A pointer to an array, for the sorted hash to be placed in. This function will
* allocate res, so do not pass in an already-allocated pointer.
* length: The length of res. The function will set this value.
*/
int hash_sort(Pair **res, size_t *length, Hash hash);
int pair_comparator(const void *x, const void *y);
int hash_test();
size_t hash_function(const char *key);
void * hash_malloc(size_t size);
void * hash_realloc(void *ptr, size_t size);
int hash_resize(Hash *hash);
size_t next_power_of_2(size_t x);
size_t next_size(size_t x);
int do_resize(size_t x);
int hash_init(Hash *hash)
{
return hash_init_capacity(hash, DEFAULT_CAPACITY);
}
int hash_init_capacity(Hash *hash, size_t capacity)
{
hash->buckets = hash_malloc(sizeof(Bucket) * next_size(capacity));
hash->length = next_size(capacity);
hash->count = 0;
return 0;
}
int hash_clear(Hash *hash)
{
size_t i, j;
for (i = 0; i < hash->length; ++i)
if (hash->buckets[i].pairs) {
for (j = 0; j < hash->buckets[i].length; ++j)
free(hash->buckets[i].pairs[j].key);
free(hash->buckets[i].pairs);
}
free(hash->buckets);
hash->length = 0;
return 0;
}
int hash_exists(Hash hash, char *key)
{
size_t j, i = hash_function(key) % hash.length;
if (hash.buckets[i].pairs) {
// Find the key in the bucket.
for (j = 0; j < hash.buckets[i].length; ++j)
if (strcmp(hash.buckets[i].pairs[j].key, key) == 0)
return 1;
}
// The key does not exist in the bucket.
return 0;
}
long hash_get(Hash hash, char *key)
{
size_t j, i = hash_function(key) % hash.length;
if (hash.buckets[i].pairs) {
// Find the key in the bucket.
for (j = 0; j < hash.buckets[i].length; ++j)
if (strcmp(hash.buckets[i].pairs[j].key, key) == 0)
return hash.buckets[i].pairs[j].value;
}
// The key does not exist in the bucket.
return -1;
}
int hash_inc(Hash *hash, const char *key, double value)
{
size_t j, i = hash_function(key) % hash->length;
if (hash->buckets[i].pairs) {
// Find the pair in the bucket.
for (j = 0; j < hash->buckets[i].length; ++j)
if (strcmp(hash->buckets[i].pairs[j].key, key) == 0) {
hash->buckets[i].pairs[j].value += value;
return 0;
}
// The pair does not exist in the bucket.
// Resize the bucket if necessary.
if (resize_p(hash->buckets[i].length)) {
hash->buckets[i].pairs = hash_realloc(hash->buckets[i].pairs,
sizeof(Pair) * next_size(hash->buckets[i].length + 1));
for (j = hash->buckets[i].length; j < next_size(hash->buckets[i].length + 1); ++j)
hash->buckets[i].pairs[j].key = NULL;
}
// Put the new pair at the end of the bucket.
hash->buckets[i].pairs[j].key = hash_malloc(strlen(key) + 1);
strcpy(hash->buckets[i].pairs[j].key, key);
hash->buckets[i].pairs[j].value = value;
++hash->buckets[i].length;
} else {
// The bucket does not exist. Create a new bucket and put the pair in it.
hash->buckets[i].pairs = hash_malloc(sizeof(Pair) * next_size(1));
hash->buckets[i].length = 1;
hash->buckets[i].pairs[0].key = hash_malloc(strlen(key) + 1);
strcpy(hash->buckets[i].pairs[0].key, key);
hash->buckets[i].pairs[0].value = value;
}
++hash->count;
if (hash->count * 100 > hash->length * 75) {
hash_resize(hash);
}
return 0;
}
int hash_merge(Hash *dest, Hash src)
{
size_t i, j;
for (i = 0; i < src.length; ++i)
for (j = 0; j < src.buckets[i].length; ++j)
hash_inc(dest, src.buckets[i].pairs[j].key, src.buckets[i].pairs[j].value);
return 0;
}
int hash_print(Hash hash)
{
size_t i, j;
for (i = 0; i < hash.length; ++i)
for (j = 0; j < hash.buckets[i].length; ++j)
if (hash.buckets[i].pairs[j].key)
printf("%s => %.8f, ", hash.buckets[i].pairs[j].key, hash.buckets[i].pairs[j].value);
printf("\n");
return 0;
}
long hash_put(Hash *hash, const char *key, double value)
{
size_t j, i = hash_function(key) % hash->length;
if (hash->buckets[i].pairs) {
// Find the pair in the bucket.
for (j = 0; j < hash->buckets[i].length; ++j)
if (strcmp(hash->buckets[i].pairs[j].key, key) == 0) {
hash->buckets[i].pairs[j].value = value;
return 0;
}
// The pair does not exist in the bucket.
// Resize the bucket if necessary.
if (resize_p(hash->buckets[i].length)) {
hash->buckets[i].pairs = hash_realloc(hash->buckets[i].pairs,
sizeof(Pair) * next_size(hash->buckets[i].length + 1));
for (j = hash->buckets[i].length; j < next_size(hash->buckets[i].length + 1); ++j)
hash->buckets[i].pairs[j].key = NULL;
}
// Put the new pair at the end of the bucket.
hash->buckets[i].pairs[j].key = hash_malloc(strlen(key) + 1);
strcpy(hash->buckets[i].pairs[j].key, key);
hash->buckets[i].pairs[j].value = value;
++hash->buckets[i].length;
} else {
// The bucket does not exist. Create a new bucket and put the pair in it.
hash->buckets[i].pairs = hash_malloc(sizeof(Pair) * next_size(1));
hash->buckets[i].length = 1;
hash->buckets[i].pairs[0].key = hash_malloc(strlen(key) + 1);
strcpy(hash->buckets[i].pairs[0].key, key);
hash->buckets[i].pairs[0].value = value;
}
++hash->count;
if (hash->count * 100 > hash->length * 75) {
hash_resize(hash);
}
return 0;
}
int hash_foreach(Hash hash, int (*f)(const char *key, double value))
{
int i, j, ret = 0;
for (i = 0; i < hash.length; ++i)
for (j = 0; j < hash.buckets[i].length; ++j)
if (hash.buckets[i].pairs[j].key) {
ret = (*f)(hash.buckets[i].pairs[j].key,
hash.buckets[i].pairs[j].value);
if (ret != 0)
return ret;
}
return 0;
}
int hash_sort(Pair **res, size_t *length, Hash hash)
{
*length = hash.count;
*res = malloc(sizeof(Bucket) * hash.count);
size_t i, j, k = 0;
for (i = 0; i < hash.length; ++i) {
for (j = 0; j < hash.buckets[i].length; ++j) {
(*res)[k] = hash.buckets[i].pairs[j];
++k;
}
}
qsort(*res, k, sizeof(Pair), &pair_comparator);
*length = k;
return 0;
}
int pair_comparator(const void *x, const void *y)
{
const Pair *xp = (const Pair *) x;
const Pair *yp = (const Pair *) y;
if (xp->value > yp->value) return -1;
else if (xp->value < yp->value) return 1;
else return 0;
}
int hash_test()
{
Hash hash;
hash_init(&hash);
hash_put(&hash, "hello", 1);
hash_put(&hash, "hello", 3);
hash_inc(&hash, "world", 5);
hash_inc(&hash, "world", 5);
hash_print(hash);
hash_clear(&hash);
return 0;
}
size_t hash_function(const char *key)
{
size_t x = 5381;
--key;
while (*(++key))
x = 33*x + *key;
return x;
}
void * hash_malloc(size_t size)
{
void *mem = malloc(size);
if (mem == NULL)
fprintf(stderr, "Error: Memory allocation failed.\n");
else memset(mem, 0, size);
return mem;
}
void * hash_realloc(void *ptr, size_t size)
{
void *mem = realloc(ptr, size);
if (mem == NULL)
fprintf(stderr, "Error: Memory reallocation failed.\n");
return mem;
}
int hash_resize(Hash *hash)
{
Hash res;
hash_init_capacity(&res, sizeof(Bucket) * next_size(hash->length));
size_t i, j;
for (i = 0; i < hash->length; ++i) {
for (j = 0; j < hash->buckets[i].length; ++j)
if (hash->buckets[i].pairs[j].key)
hash_put(&res, hash->buckets[i].pairs[j].key, hash->buckets[i].pairs[j].value);
}
hash_clear(hash);
*hash = res;
return 0;
}
/*
* Returns the next power of 2. In the case in which x is already a power of 2,
* it will return x << 1. 0 returns 0.
*
* Source: http://graphics.stanford.edu/~seander/bithacks.html
*/
size_t next_power_of_2(size_t x)
{
// This ORs x with every possible 1 bit up to its length, so every bit will
// be filled. Then it adds 1 which for instance turns (e.g.) 111 into 1000.
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
return x + 1;
}
/*
* next_size() and resize_p() are used to determine whether an array of a given size
* needs to be resized. The length of the array is always a power of 2. When the array
* needs to be resized, it is reallocated to be twice as large.
*/
size_t next_size(size_t x)
{
return x < RESIZE_MIN ? RESIZE_MIN : next_power_of_2(x);
}
int resize_p(size_t x)
{
// Iff x is a power of 2, then x and (x-1) will share no digits. For instance,
// 1000 & 111 = 0. Do not resize unless x is greater than a certain minimum.
return x >= RESIZE_MIN && !(x & (x - 1));
}