X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=dict.c;h=50b741eb9cd5697e066d4df81e92f9c0ef6c9ef8;hb=7787438f3c3844ab9597320213f5bd19bc6426e1;hp=c364cb8110f066c9fbc71f65317fbb526778da84;hpb=2d604d8b8a380f7f3028c234282bfdcf12f95dad;p=wordgame diff --git a/dict.c b/dict.c index c364cb8..50b741e 100644 --- a/dict.c +++ b/dict.c @@ -26,7 +26,21 @@ #include "dict.h" -void * +typedef struct _string { + size_t size; + char *s; + size_t len; +} string_t; + +#define TRIE_FLAGS_IS_WORD (1<<0) + +typedef bool_t +(*trie_predicate_t) (trie_t *trie); + +#define TRIE_CHAR_TO_INDEX(c) (tolower (c) - 'a') +#define TRIE_INDEX_TO_CHAR(i) (i + 'a') + +static void * xmalloc (size_t size) { void *res; @@ -40,7 +54,7 @@ xmalloc (size_t size) return res; } -void * +static void * xrealloc (void *ptr, size_t size) { void *res; @@ -54,15 +68,17 @@ xrealloc (void *ptr, size_t size) return res; } -void +static void chomp (char *s) { int len = strlen (s); + if (len == 0) + return; if (s[len - 1] == '\n') s[len - 1] = '\0'; } -void +static void string_init (string_t *string) { string->size = 0; @@ -70,7 +86,7 @@ string_init (string_t *string) string->len = 0; } -void +static void string_fini (string_t *string) { string->size = 0; @@ -79,7 +95,7 @@ string_fini (string_t *string) string->len = 0; } -void +static void string_append_char (string_t *string, char c) { if (string->size == 0) { @@ -98,7 +114,7 @@ string_append_char (string_t *string, char c) string->s[string->len] = '\0'; } -void +static void string_chop (string_t *string) { if (string->len == 0) @@ -129,7 +145,7 @@ trie_fini (trie_t *trie) trie_destroy (trie->next[i]); } -trie_t * +static trie_t * trie_create (void) { trie_t *trie; @@ -149,7 +165,7 @@ trie_destroy (trie_t *trie) free (trie); } -void +static void trie_add (trie_t *trie, const char *word_chunk) { @@ -168,7 +184,7 @@ trie_add (trie_t *trie, trie_add (trie->next[i], word_chunk + 1); } -trie_t * +static trie_t * trie_find (trie_t *trie, const char *word_chunk) { @@ -180,7 +196,7 @@ trie_find (trie_t *trie, return trie; } -int +static int trie_count (trie_t *trie, trie_predicate_t predicate) { @@ -200,7 +216,7 @@ trie_count (trie_t *trie, return count; } -int +static int trie_print (trie_t *trie, string_t *string, trie_predicate_t predicate,