From: Carl Worth <cworth@raht.cworth.org>
Date: Thu, 21 Sep 2006 06:51:04 +0000 (-0700)
Subject: Eliminate useless layer of dict wrapping trie.
X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=003aa44c1c2fecd15ff55f62aea88cda1a83be85;p=wordgame

Eliminate useless layer of dict wrapping trie.
---

diff --git a/dict-impl.h b/dict-impl.h
index 2c5514d..030df6f 100644
--- a/dict-impl.h
+++ b/dict-impl.h
@@ -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 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);
 }
diff --git a/dict.h b/dict.h
index 16a345c..e58c8b4 100644
--- 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 78c6c56..c785808 100644
--- 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