Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 115 lines (97 sloc) 2.171 kb
27a7fb2 @fmela Anagram finder -- initial commit.
authored
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5
6 #include "dict.h"
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
7 #include "dict_private.h"
27a7fb2 @fmela Anagram finder -- initial commit.
authored
8
9 void *xmalloc(size_t size);
10 char *xstrdup(const char *s);
11
506fc04 @fmela Use struct typedef
authored
12 typedef struct WordList WordList;
13 struct WordList {
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
14 char *word;
15 WordList *next;
27a7fb2 @fmela Anagram finder -- initial commit.
authored
16 };
17
18 int
19 main(int argc, char *argv[])
20 {
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
21 if (argc != 2) {
22 printf("Expected filename argument.\n");
23 exit(1);
24 }
27a7fb2 @fmela Anagram finder -- initial commit.
authored
25
415ddc5 C99 variable declarations.
Farooq Mela authored
26 FILE *fp = fopen(argv[1], "r");
27 if (!fp) {
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
28 printf("Unable to open file '%s'.\n", argv[1]);
29 exit(1);
30 }
27a7fb2 @fmela Anagram finder -- initial commit.
authored
31
415ddc5 C99 variable declarations.
Farooq Mela authored
32 rb_tree *tree = rb_tree_new(dict_str_cmp, NULL);
27a7fb2 @fmela Anagram finder -- initial commit.
authored
33
415ddc5 C99 variable declarations.
Farooq Mela authored
34 char buf[512];
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
35 while (fgets(buf, sizeof(buf), fp)) {
36 if (isupper(buf[0])) /* Disregard proper nouns. */
37 continue;
27a7fb2 @fmela Anagram finder -- initial commit.
authored
38
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
39 strtok(buf, "\r\n");
415ddc5 C99 variable declarations.
Farooq Mela authored
40 int freq[256] = { 0 };
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
41 memset(freq, 0, sizeof(freq));
27a7fb2 @fmela Anagram finder -- initial commit.
authored
42
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
43 ASSERT(buf[0] != '\0');
27a7fb2 @fmela Anagram finder -- initial commit.
authored
44
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
45 for (char *p = buf; *p; p++)
46 freq[tolower(*p)]++;
27a7fb2 @fmela Anagram finder -- initial commit.
authored
47
415ddc5 C99 variable declarations.
Farooq Mela authored
48 char name[1024];
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
49 char *p = name;
415ddc5 C99 variable declarations.
Farooq Mela authored
50 for (int i=1; i<256; i++) {
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
51 if (freq[i]) {
52 ASSERT(freq[i] < 10);
27a7fb2 @fmela Anagram finder -- initial commit.
authored
53
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
54 *p++ = i;
55 *p++ = '0' + freq[i];
56 }
27a7fb2 @fmela Anagram finder -- initial commit.
authored
57 }
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
58 *p = 0;
59
b27c1ed @fmela Change signature of insert method.
authored
60 WordList* word = xmalloc(sizeof(*word));
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
61 word->word = xstrdup(buf);
b27c1ed @fmela Change signature of insert method.
authored
62 WordList** wordp = (WordList**)rb_tree_insert(tree, xstrdup(name), NULL);
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
63 word->next = *wordp;
64 *wordp = word;
65 }
66
415ddc5 C99 variable declarations.
Farooq Mela authored
67 rb_itor *itor = rb_itor_new(tree);
40c2ab2 @fmela Fix memory leaks.
authored
68 for (rb_itor_first(itor); rb_itor_valid(itor); rb_itor_next(itor)) {
4731b5d @fmela Make iterator data function return pointer to data.
authored
69 WordList *word = *rb_itor_data(itor);
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
70 ASSERT(word != NULL);
71 if (word->next) {
72 int count = 1;
73 while (word->next)
74 count++, word = word->next;
75 printf("%2d:[", count);
4731b5d @fmela Make iterator data function return pointer to data.
authored
76 word = *rb_itor_data(itor);
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
77 while (word) {
78 printf("%s%c", word->word, word->next ? ',' : ']');
79 word = word->next;
80 }
81 printf("\n");
82 }
4731b5d @fmela Make iterator data function return pointer to data.
authored
83 word = *rb_itor_data(itor);
90d8ae0 @fmela Free memory when done.
authored
84 while (word) {
85 WordList *next = word->next;
86 free(word->word);
87 free(word);
88 word = next;
89 }
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
90 } while (rb_itor_next(itor));
90d8ae0 @fmela Free memory when done.
authored
91 rb_itor_free(itor);
92 rb_tree_free(tree);
27a7fb2 @fmela Anagram finder -- initial commit.
authored
93
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
94 return 0;
27a7fb2 @fmela Anagram finder -- initial commit.
authored
95 }
96
97 char *
98 xstrdup(const char *s)
99 {
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
100 size_t len = strlen(s) + 1;
101 return memcpy(xmalloc(len), s, len);
27a7fb2 @fmela Anagram finder -- initial commit.
authored
102 }
103
104 void *
105 xmalloc(size_t size)
106 {
e6a2847 @fmela Change insert interface to be more useful (also eliminates the need for ...
authored
107 ASSERT(size > 0);
108 void *p = malloc(size);
109 if (!p) {
110 fprintf(stderr, "Out of memory\n");
111 abort();
112 }
113 return p;
27a7fb2 @fmela Anagram finder -- initial commit.
authored
114 }
Something went wrong with that request. Please try again.