]> git.cworth.org Git - wordgame/commitdiff
Eliminate useless layer of dict wrapping trie.
authorCarl Worth <cworth@raht.cworth.org>
Thu, 21 Sep 2006 06:51:04 +0000 (23:51 -0700)
committerCarl Worth <cworth@raht.cworth.org>
Thu, 21 Sep 2006 06:51:04 +0000 (23:51 -0700)
dict-impl.h
dict.c
dict.h
grid.c

index 2c5514d17c8cf5fe9d6bb3b1e179f42f8a183166..030df6fd2f071a82b62e37eec022c04c648bcf11 100644 (file)
@@ -83,8 +83,4 @@ trie_print_seen (trie_t *trie, string_t *word);
 int
 trie_print_unseen (trie_t *trie, string_t *word);
 
-struct _dict {
-    trie_t *trie;
-};
-
 #endif /* _DICT_IMPL_H_ */
diff --git a/dict.c b/dict.c
index a3f088f0e084277416ce6ba3bd754cdee5abfacb..a783ffcef36809a732f1b2789314ed838ae7891c 100644 (file)
--- 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);
 }
diff --git a/dict.h b/dict.h
index 16a345cf0a7f970f78799be533a539f2c37a688b..e58c8b4c4e3c992ca82ff67c0c04039af43fc8f1 100644 (file)
--- a/dict.h
+++ b/dict.h
@@ -22,7 +22,7 @@
 #include "dict-impl.h"
 
 /* Only looks opaque. Perfectly stack-allocatable */
-typedef struct _dict dict_t;
+typedef trie_t dict_t;
 
 void
 dict_init (dict_t *dict);
diff --git a/grid.c b/grid.c
index 78c6c56d1370564c5165541a37f5be2192346174..c7858080b1fa84d473392b000f9d1fbb5369fe89 100644 (file)
--- a/grid.c
+++ b/grid.c
@@ -39,7 +39,7 @@ typedef struct _board {
     char letters[4][4];
 
     /* Private, transient state used by enumerate */
-    trie_t *result_trie;
+    dict_t *result_trie;
 } board_t;
 
 int
@@ -155,7 +155,7 @@ board_solve (board_t *board, dict_t *dict, trie_t *solution)
 
     for (y = 0; y < 4; y++)
        for (x = 0; x < 4; x++)
-           board_enumerate (board, x, y, seen, &word, dict->trie);
+           board_enumerate (board, x, y, seen, &word, dict);
 
     string_fini (&word);
 }
@@ -207,7 +207,7 @@ main (void)
                else
                    t->flags |= TRIE_FLAGS_SEEN;
            } else {
-               t = trie_find (dict.trie, response);
+               t = trie_find (&dict, response);
                if (t && (t->flags & TRIE_FLAGS_IS_WORD))
                    printf ("(a good word, but it's not in the puzzle)\n");
                else