view src/trie_test.c @ 13:7886d2da8cc4

Trie working OK. Started on compact trie, ctrie.
author Eris Caffee <discordia@eldalin.com>
date Tue, 02 Oct 2012 10:13:07 -0500
parents d359966ed8de
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "list.h"
6 #include "trie.h"
8 void print_val(char * key, void * data)
9 {
10 printf("%s\t\t%s\n", key, data);
11 }
13 int main(int argc, char ** argv)
14 {
15 struct trie * trie = trie_new();
17 char * words[] = { "hello", "interjection (used to express a greeting, answer a telephone, or attract attention.)",
18 "help", "to give or provide what is necessary to accomplish a task or satisfy a need.",
19 "helper", "a person or thing that helps or gives assistance, support, etc",
20 "helping", "the act of a person or thing that helps.",
21 "an", "indefinite article. the form of a before an initial vowel sound.",
22 "a", "indefinite article. Used before an initial consonant sound.",
23 "anne", "a female name",
24 "any", "one, a, an, or some; one or more without specification or identification",
25 "anyway", "in any case; anyhow; nonetheless; regardless",
26 "anna", "a female name",
27 "anyhow", "in any way whatever",
28 "anybody", "any person",
29 "Apple", "A computer manufacturer.",
30 "ABLE", "having necessary power, skill, resources, or qualifications",
31 NULL, NULL
32 };
33 int i = 0;
34 while (words[i])
35 {
36 trie_insert(trie, words[i], words[i+1]);
37 i += 2;
38 }
40 trie_dump_raw(trie);
41 trie_dump(trie);
42 trie_walk_keys(trie, print_val);
43 printf("trie is storing %zd words in %zd keys\n", trie_count(trie), trie_size(trie));
45 ////////////////////////////////////////////
46 printf("===================================================\n");
47 char word[100];;
48 strcpy(word, "any");
49 printf("searching for %s\n", word);
51 void * data = trie_find(trie, word);
52 if (NULL == data)
53 printf("Failed to find 'any'\n");
54 else
55 printf("any: %s\n", (char *) data);
58 ////////////////////////////////////////////
59 printf("===================================================\n");
60 strcpy(word, "an");
61 printf("Looking for completions of '%s':\n", word);
62 struct list * list = trie_get_subkeys(trie, word, 0);
63 if (NULL == list)
64 printf("Nothing found for '%s'\n", word);
65 else
66 {
67 printf("list size is %d\n", list_size(list));
68 struct list_iterator * it = list_begin(list);
69 struct list_iterator * next = it;
70 while (NULL != next)
71 {
72 printf("%s\n", list_value(it));
73 next = list_next(it);
74 }
75 trie_free_subkey_list(list);
76 }
78 ////////////////////////////////////////////
79 printf("===================================================\n");
80 strcpy(word, "a");
81 printf("Looking for first 5 completions of '%s':\n", word);
82 list = trie_get_subkeys(trie, word, 5);
83 if (NULL == list)
84 printf("Nothing found for '%s'\n", word);
85 else
86 {
87 printf("list size is %d\n", list_size(list));
88 struct list_iterator * it = list_begin(list);
89 struct list_iterator * next = it;
90 while (NULL != next)
91 {
92 printf("%s\n", list_value(it));
93 next = list_next(it);
94 }
95 trie_free_subkey_list(list);
96 }
98 ////////////////////////////////////////////
99 printf("===================================================\n");
100 strcpy(word, "anybody");
101 printf("remove '%s'\n", word);
102 trie_remove(trie, word);
104 strcpy(word, "ann");
105 printf("remove '%s'\n", word);
106 trie_remove(trie, word);
108 strcpy(word, "help");
109 printf("remove '%s'\n", word);
110 trie_remove(trie, word);
112 strcpy(word, "a");
113 printf("remove '%s'\n", word);
114 trie_remove(trie, word);
116 trie_dump(trie);
117 printf("trie is storing %zd words in %zd keys\n", trie_count(trie), trie_size(trie));
119 ////////////////////////////////////////////
120 printf("===================================================\n");
121 strcpy(word, "anybody");
122 printf("re-inserting '%s'\n", word);
123 trie_insert(trie, word, "fake definition");
124 trie_dump(trie);
125 printf("trie is storing %zd words in %zd keys\n", trie_count(trie), trie_size(trie));
127 exit(EXIT_SUCCESS);
128 }