-
Notifications
You must be signed in to change notification settings - Fork 3
/
freq.c
151 lines (122 loc) · 2.69 KB
/
freq.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
/*
* freq.c -- simple word frequency counter
*/
#include <ctype.h>
#include <err.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define NBUCKETS 10007
/* entries in a bucket are a linked list of struct entry */
struct entry {
struct entry *next;
const char *word;
int count;
};
/* each bucket contains a pointer to the linked list of entries */
struct bucket {
struct entry *entries;
} H[NBUCKETS];
/* hash a string into an index into H[] */
unsigned hash(const char *s)
{
unsigned h = NBUCKETS ^ ((unsigned)*s++ << 2);
unsigned len = 0;
while (*s) {
len++;
h ^= (((unsigned)*s) << (len % 3)) +
((unsigned)*(s - 1) << ((len % 3 + 7)));
s++;
}
h ^= len;
return h % NBUCKETS;
}
/* bump the count for a word */
void count(const char *word)
{
unsigned h = hash(word);
struct entry *ep = H[h].entries;
for (; ep != NULL; ep = ep->next)
if (strcmp(word, ep->word) == 0) {
/* already in table, just bump the count */
ep->count++;
return;
}
/* allocate new entry in table */
if ((ep = calloc(1, sizeof(*ep))) == NULL)
err(1, "calloc");
if ((ep->word = strdup(word)) == NULL)
err(1, "strdup");
ep->count = 1;
/* add it to the front of the linked list */
ep->next = H[h].entries;
H[h].entries = ep;
}
#define MAXWORD 8192
/* break a test file into words and call count() on each one */
void count_all_words(const char *fname)
{
FILE *fp;
int c;
char word[MAXWORD];
char *ptr;
if ((fp = fopen(fname, "r")) == NULL)
err(1, "fopen: %s", fname);
ptr = NULL;
while ((c = getc(fp)) != EOF)
if (isalpha(c)) {
if (ptr == NULL) {
/* starting a new word */
ptr = word;
*ptr++ = c;
} else if (ptr < &word[MAXWORD - 1])
/* add character to current word */
*ptr++ = c;
else {
/* word too long, truncate it */
*ptr++ = '\0';
count(word);
ptr = NULL;
}
} else if (ptr != NULL) {
/* word ended, store it */
*ptr++ = '\0';
count(word);
ptr = NULL;
}
/* handle the last word */
if (ptr != NULL) {
/* word ended, store it */
*ptr++ = '\0';
count(word);
}
fclose(fp);
}
/* print all entries in the hash table */
void print_counts()
{
struct entry *ep;
for (int i = 0; i < NBUCKETS; i++)
for (ep = H[i].entries; ep != NULL; ep = ep->next)
printf("%d %s\n", ep->count, ep->word);
}
int main(int argc, char *argv[])
{
int pflag = 0;
int arg = 1; /* index into argv[] for first file name */
if (argc > 1 && strcmp(argv[1], "-p") == 0) {
pflag++;
arg++;
}
if (argv[arg] == NULL) {
fprintf(stderr, "usage: %s [-p] wordfiles...\n", argv[0]);
exit(1);
}
for (; arg < argc; arg++)
count_all_words(argv[arg]);
if (pflag)
print_counts();
exit(0);
}