-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_example.c
63 lines (53 loc) · 1.79 KB
/
hash_example.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
/* Licensed under the MIT License by Bart Grantham, 2010. See ./LICENSE or
* http://www.opensource.org/licenses/mit-license.php
*/
#include "hash.h"
#include <fcntl.h>
#define KEYS_IN_FLIGHT 1000
int main(int argc, char * argv[])
{
hash_entry * myhash = NULL;
FILE * words;
int i=0, j=0, k=0, l=0, m=0;
char buf[1024];
char * dictionary = "/usr/share/dict/words";
char * value_sentinel = "value_sentinel";
char * testwords[KEYS_IN_FLIGHT];
void * max_ptr;
printf("Churning through %s, %d keys \"in flight\" at any given moment\n", dictionary, KEYS_IN_FLIGHT);
bzero(testwords, KEYS_IN_FLIGHT * sizeof(char*));
words = fopen(dictionary, "r");
// rotate words in and out of the hash tables...
while ( fgets(buf, sizeof(buf)-1, words) )
{
j = strlen(buf);
buf[j-1] = '\0';
if ( testwords[i%KEYS_IN_FLIGHT] != NULL )
{
hash_clear(myhash, testwords[i%KEYS_IN_FLIGHT]);
free(testwords[i%KEYS_IN_FLIGHT]);
}
testwords[i%KEYS_IN_FLIGHT] = malloc(j);
strncpy(testwords[i%KEYS_IN_FLIGHT], buf, j);
hash_set(myhash, testwords[i%KEYS_IN_FLIGHT], value_sentinel);
i++;
}
/*
// clear the remaining words, commented so that hash_stats(), etc. below
// have something to operate on
for(i=0; i<KEYS_IN_FLIGHT; i++)
{
hash_clear(myhash, testwords[i%KEYS_IN_FLIGHT]);
free(testwords[i%KEYS_IN_FLIGHT]);
}
*/
hash_dump(myhash);
i = j = k = 0;
max_ptr = NULL;
hash_stats(myhash, &i, &j, &k, &max_ptr);
printf("depth:\t: %d\n", hash_depth(myhash));
printf("tables\t: %d\nentries\t: %d\nnulls\t: %d\n", i, j, k);
printf("sparseness\t:%f\n", hash_sparseness(myhash));
printf("max pointer\t:%p\n", max_ptr);
return 0;
}