X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=dict.h;h=ab962c20469bfae94808dbc8e2271a169edfd3b1;hb=a46e558ad8d3243c76a9a6cafb6100be4f3f2dae;hp=4d4885c7e9423651b592d85e1c6e28cc4c9b1f31;hpb=2eb18813b84ae8ad4faeb92b1d8570599135d537;p=wordgame diff --git a/dict.h b/dict.h index 4d4885c..ab962c2 100644 --- a/dict.h +++ b/dict.h @@ -19,6 +19,10 @@ #ifndef _DICT_H_ #define _DICT_H_ +/* Portably, schmortability. I want ease of programming. */ +#define _GNU_SOURCE +#include +#include #include #ifndef FALSE @@ -31,67 +35,100 @@ typedef int bool_t; -typedef struct _string { - size_t size; - char *s; - size_t len; -} string_t; +typedef struct _trie { + uint32_t flags; + struct _trie *next[26]; +} trie_t; -void -string_init (string_t *string); +typedef trie_t dict_t; +typedef trie_t *dict_cursor_t; +typedef uint32_t dict_entry_t; +/* Initialization and cleanup */ void -string_fini (string_t *string); +dict_init (dict_t *dict); void -string_append_char (string_t *string, char c); +dict_fini (dict_t *dict); +/* Adding new words */ void -string_chop (string_t *string); - +dict_add_word (dict_t *dict, + const char *word); void -chomp (char *s); - -#define TRIE_FLAGS_IS_WORD (1<<0) -#define TRIE_FLAGS_SEEN (1<<1) +dict_add_words_from_file (dict_t *dict, + const char *filename); -typedef struct _trie { - uint32_t flags; - struct _trie *next[26]; -} trie_t; +/* Looking up an entry in the dictionary */ +dict_entry_t * +dict_lookup (dict_t *dict, + const char *word); typedef bool_t -(*trie_predicate_t) (trie_t *trie); +(*dict_entry_predicate_t) (dict_entry_t entry); -#define TRIE_CHAR_TO_INDEX(c) (tolower (c) - 'a') -#define TRIE_INDEX_TO_CHAR(i) (i + 'a') +int +dict_count (dict_t *dict, + dict_entry_predicate_t predicate); -trie_t * -trie_create (void); +/* Querying a dictionary entry. The dict interface uses 1 bit. + * All of the remaining bits are available for application use. + */ +#define DICT_ENTRY_FLAG_IS_WORD (1<<0) -void -trie_add (trie_t *trie, - const char *word_chunk); +#define DICT_ENTRY_IS_WORD(entry) ((entry) && ((*entry) & DICT_ENTRY_FLAG_IS_WORD)) -trie_t * -trie_find (trie_t *trie, - const char *word_chunk); +/* Printing the dictionary */ +int +dict_print (dict_t *dict); int -trie_print_seen (trie_t *trie, string_t *word); +dict_print_by_length (dict_t *dict); int -trie_print_unseen (trie_t *trie, string_t *word); +dict_print_if (dict_t *dict, + dict_entry_predicate_t predicate); -typedef struct _dict { - trie_t *trie; -} dict_t; +int +dict_print_by_length_if (dict_t *dict, + dict_entry_predicate_t predicate); -void -dict_init (dict_t *dict, - const char *filename); +/* More general callback-based iteration of all entries */ +typedef void (* dict_action_t) (void *closure, char *word, dict_entry_t *entry); -void -dict_fini (dict_t *dict); +int +dict_for_each (dict_t *dict, + dict_action_t action, + void *closure); + +int +dict_for_each_by_length (dict_t *dict, + dict_action_t action, + void *closure); + +int +dict_for_each_if (dict_t *dict, + dict_action_t action, + void *closure, + dict_entry_predicate_t predicate); + +int +dict_for_each_by_length_if (dict_t *dict, + dict_action_t action, + void *closure, + dict_entry_predicate_t predicate); + +/* Character-by-character perusal of the dictionary */ +dict_cursor_t +dict_root (dict_t *dict); + +dict_cursor_t +dict_cursor_next (dict_cursor_t cursor, + char next); + +dict_entry_t * +dict_cursor_resolve (dict_cursor_t cursor); + +#define DICT_CURSOR_NIL NULL #endif /* _DICT_H_ */