X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=dict.c;h=a783ffcef36809a732f1b2789314ed838ae7891c;hb=003aa44c1c2fecd15ff55f62aea88cda1a83be85;hp=a3f088f0e084277416ce6ba3bd754cdee5abfacb;hpb=17755d5a91f1470057bf2308984a25e7432bcd8a;p=wordgame diff --git a/dict.c b/dict.c index a3f088f..a783ffc 100644 --- a/dict.c +++ b/dict.c @@ -116,6 +116,19 @@ trie_init (trie_t *trie) trie->next[i] = NULL; } +void +trie_destroy (trie_t *trie); + +static void +trie_fini (trie_t *trie) +{ + int i; + + for (i = 0; i < 26; i++) + if (trie->next[i]) + trie_destroy (trie->next[i]); +} + trie_t * trie_create (void) { @@ -131,11 +144,7 @@ trie_create (void) void trie_destroy (trie_t *trie) { - int i; - - for (i = 0; i < 26; i++) - if (trie->next[i]) - trie_destroy (trie->next[i]); + trie_fini (trie); free (trie); } @@ -227,9 +236,15 @@ trie_print_unseen (trie_t *trie, string_t *word) } void -dict_init (dict_t *dict) +dict_init (dict_t *dict) { - dict->trie = trie_create (); + trie_init (dict); +} + +void +dict_fini (dict_t *dict) +{ + trie_fini (dict); } void @@ -253,7 +268,7 @@ dict_add_words_from_file (dict_t *dict, if (bytes_read == -1) break; chomp (line); - trie_add (dict->trie, line); + trie_add (dict, line); } if (line) free (line); @@ -261,12 +276,6 @@ dict_add_words_from_file (dict_t *dict, fclose (file); } -void -dict_fini (dict_t *dict) -{ - trie_destroy (dict->trie); -} - void dict_print (dict_t *dict) { @@ -274,7 +283,7 @@ dict_print (dict_t *dict) string_init (&string); - trie_print (dict->trie, &string, NULL); + trie_print (dict, &string, NULL); string_fini (&string); }