view src/ctrie_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
children ef73b96fdcae
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
5 #include "list.h"
6 #include "ctrie.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 ctrie * ctrie = ctrie_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 ctrie_insert(ctrie, words[i], words[i+1]);
37 i += 2;
38 }
40 ctrie_dump_raw(ctrie);
41 ctrie_dump(ctrie);
42 ctrie_walk_keys(ctrie, print_val);
43 printf("ctrie is storing %zd words in %zd keys\n", ctrie_count(ctrie), ctrie_size(ctrie));
45 ////////////////////////////////////////////
46 printf("===================================================\n");
47 char word[100];;
48 strcpy(word, "any");
49 printf("searching for %s\n", word);
51 void * data = ctrie_find(ctrie, 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 = ctrie_get_subkeys(ctrie, 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 ctrie_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 = ctrie_get_subkeys(ctrie, 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 ctrie_free_subkey_list(list);
96 }
98 ////////////////////////////////////////////
99 printf("===================================================\n");
100 strcpy(word, "anybody");
101 printf("remove '%s'\n", word);
102 ctrie_remove(ctrie, word);
104 strcpy(word, "ann");
105 printf("remove '%s'\n", word);
106 ctrie_remove(ctrie, word);
108 strcpy(word, "help");
109 printf("remove '%s'\n", word);
110 ctrie_remove(ctrie, word);
112 strcpy(word, "a");
113 printf("remove '%s'\n", word);
114 ctrie_remove(ctrie, word);
116 ctrie_dump(ctrie);
117 printf("ctrie is storing %zd words in %zd keys\n", ctrie_count(ctrie), ctrie_size(ctrie));
119 ////////////////////////////////////////////
120 printf("===================================================\n");
121 strcpy(word, "anybody");
122 printf("re-inserting '%s'\n", word);
123 ctrie_insert(ctrie, word, "fake definition");
124 ctrie_dump(ctrie);
125 printf("ctrie is storing %zd words in %zd keys\n", ctrie_count(ctrie), ctrie_size(ctrie));
127 exit(EXIT_SUCCESS);
128 }