-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_hash.h
More file actions
247 lines (223 loc) · 6.16 KB
/
simple_hash.h
File metadata and controls
247 lines (223 loc) · 6.16 KB
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
/* The Computer Language Shootout
http://shootout.alioth.debian.org/
Contributed by Josh Goldfoot
to compile, use gcc -O3
This revision uses "simple_hash.h," available from
http://cvs.alioth.debian.org/cgi-bin/cvsweb.cgi/shootout/bench/Include/?cvsroot=shootout
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
enum { ht_num_primes = 28 };
static unsigned long ht_prime_list[ht_num_primes]
= {53ul, 97ul, 193ul, 389ul, 769ul, 1543ul, 3079ul,
6151ul, 12289ul, 24593ul, 49157ul, 98317ul, 196613ul, 393241ul,
786433ul, 1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul, 50331653ul,
100663319ul, 201326611ul, 402653189ul, 805306457ul, 1610612741ul, 3221225473ul, 4294967291ul};
struct ht_node {
char *key;
int val;
struct ht_node *next;
};
struct ht_ht {
int size;
struct ht_node **tbl;
int iter_index;
struct ht_node *iter_next;
int items;
#ifdef HT_DEBUG
int collisions;
#endif /* HT_DEBUG */
};
/*inline*/ int ht_val (struct ht_node *node) { return (node->val); }
/*inline*/ char *ht_key (struct ht_node *node) { return (node->key); }
/*inline*/ int ht_hashcode (struct ht_ht *ht, char *key) {
unsigned long val = 0;
for (; *key; ++key) val = 5 * val + *key;
return (val % ht->size);
}
extern char *strdup (const char *);
struct ht_node *ht_node_create (char *key) {
char *newkey;
struct ht_node *node;
if ((node = (struct ht_node *) malloc (sizeof (struct ht_node))) == 0) {
perror ("malloc ht_node");
exit (1);
}
if ((newkey = (char *) strdup (key)) == 0) {
perror ("strdup newkey");
exit (1);
}
node->key = newkey;
node->val = 0;
node->next = (struct ht_node *) NULL;
return (node);
}
struct ht_ht *ht_create (int size) {
int i = 0;
struct ht_ht *ht = (struct ht_ht *) malloc (sizeof (struct ht_ht));
while (ht_prime_list[i] < size) {
i++;
}
ht->size = ht_prime_list[i];
ht->tbl = (struct ht_node **) calloc (ht->size, sizeof (struct ht_node *));
ht->iter_index = 0;
ht->iter_next = 0;
ht->items = 0;
#ifdef HT_DEBUG
ht->collisions = 0;
#endif /* HT_DEBUG */
return (ht);
}
void ht_destroy (struct ht_ht *ht) {
struct ht_node *cur, *next;
int i;
#ifdef HT_DEBUG
int chain_len;
int max_chain_len = 0;
int density = 0;
fprintf (stderr, " HT: size %d\n", ht->size);
fprintf (stderr, " HT: items %d\n", ht->items);
fprintf (stderr, " HT: collisions %d\n", ht->collisions);
#endif /* HT_DEBUG */
for (i = 0; i < ht->size; i++) {
next = ht->tbl[i];
#ifdef HT_DEBUG
if (next) {
density++;
}
chain_len = 0;
#endif /* HT_DEBUG */
while (next) {
cur = next;
next = next->next;
free (cur->key);
free (cur);
#ifdef HT_DEBUG
chain_len++;
#endif /* HT_DEBUG */
}
#ifdef HT_DEBUG
if (chain_len > max_chain_len) max_chain_len = chain_len;
#endif /* HT_DEBUG */
}
free (ht->tbl);
free (ht);
#ifdef HT_DEBUG
fprintf (stderr, " HT: density %d\n", density);
fprintf (stderr, " HT: max chain len %d\n", max_chain_len);
#endif /* HT_DEBUG */
}
/*inline*/ struct ht_node *ht_find (struct ht_ht *ht, char *key) {
int hash_code = ht_hashcode (ht, key);
struct ht_node *node = ht->tbl[hash_code];
while (node) {
if (strcmp (key, node->key) == 0) return (node);
node = node->next;
}
return ((struct ht_node *) NULL);
}
/*inline*/ struct ht_node *ht_find_new (struct ht_ht *ht, char *key) {
int hash_code = ht_hashcode (ht, key);
struct ht_node *prev = 0, *node = ht->tbl[hash_code];
while (node) {
if (strcmp (key, node->key) == 0) return (node);
prev = node;
node = node->next;
#ifdef HT_DEBUG
ht->collisions++;
#endif /* HT_DEBUG */
}
ht->items++;
if (prev) {
return (prev->next = ht_node_create (key));
} else {
return (ht->tbl[hash_code] = ht_node_create (key));
}
}
/*
* Hash Table iterator data/functions
*/
/*inline*/ struct ht_node *ht_next (struct ht_ht *ht) {
unsigned long index;
struct ht_node *node = ht->iter_next;
if (node) {
ht->iter_next = node->next;
return (node);
} else {
while (ht->iter_index < ht->size) {
index = ht->iter_index++;
if (ht->tbl[index]) {
ht->iter_next = ht->tbl[index]->next;
return (ht->tbl[index]);
}
}
}
return ((struct ht_node *) NULL);
}
/*inline*/ struct ht_node *ht_first (struct ht_ht *ht) {
ht->iter_index = 0;
ht->iter_next = (struct ht_node *) NULL;
return (ht_next (ht));
}
/*inline*/ int ht_count (struct ht_ht *ht) { return (ht->items); }
long hash_table_size (int fl, long buflen) {
long maxsize1, maxsize2;
maxsize1 = buflen - fl;
maxsize2 = 4;
while (--fl > 0 && maxsize2 < maxsize1) maxsize2 = maxsize2 * 4;
if (maxsize1 < maxsize2) return maxsize1;
return maxsize2;
}
struct ht_ht *generate_frequencies (int fl, char *buffer, long buflen) {
struct ht_ht *ht;
char *reader;
long i;
char nulled;
if (fl > buflen) return NULL;
ht = ht_create (hash_table_size (fl, buflen));
for (i = 0; i < buflen - fl + 1; i++) {
reader = &(buffer[i]);
nulled = reader[fl];
reader[fl] = 0x00;
ht_find_new (ht, reader)->val++;
reader[fl] = nulled;
}
return ht;
}
typedef struct ssorter {
char *string;
int num;
} sorter;
void write_frequencies (int fl, char *buffer, long buflen) {
struct ht_ht *ht;
long total, i, j, size;
struct ht_node *nd;
sorter *s;
sorter tmp;
ht = generate_frequencies (fl, buffer, buflen);
total = 0;
size = 0;
for (nd = ht_first (ht); nd != NULL; nd = ht_next (ht)) {
total = total + nd->val;
size++;
}
s = calloc (size, sizeof (sorter));
i = 0;
for (nd = ht_first (ht); nd != NULL; nd = ht_next (ht)) {
s[i].string = nd->key;
s[i++].num = nd->val;
}
for (i = 0; i < size - 1; i++)
for (j = i + 1; j < size; j++)
if (s[i].num < s[j].num) {
memcpy (&tmp, &(s[i]), sizeof (sorter));
memcpy (&(s[i]), &(s[j]), sizeof (sorter));
memcpy (&(s[j]), &tmp, sizeof (sorter));
}
for (i = 0; i < size; i++) printf ("%s %.3f\n", s[i].string, 100 * (float) s[i].num / total);
printf ("\n");
ht_destroy (ht);
free (s);
}