]> git.cworth.org Git - wordgame/blob - dict.h
Increase the window size a bit
[wordgame] / dict.h
1 /*
2  * Copyright © 2006 Carl Worth
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
17  */
18
19 #ifndef _DICT_H_
20 #define _DICT_H_
21
22 /* Portably, schmortability. I want ease of programming. */
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27
28 #ifndef FALSE
29 # define FALSE 0
30 #endif
31
32 #ifndef TRUE
33 # define TRUE 1
34 #endif
35
36 typedef int bool_t;
37
38 typedef struct _trie {
39     uint32_t flags;
40     struct _trie *next[26];
41 } trie_t;
42
43 typedef trie_t dict_t;
44 typedef trie_t *dict_cursor_t;
45 typedef uint32_t dict_entry_t;
46
47 /* Initialization and cleanup */
48 void
49 dict_init (dict_t *dict);
50
51 void
52 dict_fini (dict_t *dict);
53
54 /* Adding new words */
55 void
56 dict_add_word (dict_t           *dict,
57                const char       *word);
58 void
59 dict_add_words_from_file (dict_t        *dict,
60                           const char    *filename);
61
62 /* Looking up an entry in the dictionary */
63 dict_entry_t *
64 dict_lookup (dict_t     *dict,
65              const char *word);
66
67 typedef bool_t
68 (*dict_entry_predicate_t) (dict_entry_t entry);
69
70 int
71 dict_count_if (dict_t                   *dict,
72                dict_entry_predicate_t   predicate);
73
74 int
75 dict_count (dict_t *dict);
76
77 /* Querying a dictionary entry. The dict interface uses 1 bit.
78  * All of the remaining bits are available for application use.
79  */
80 #define DICT_ENTRY_FLAG_IS_WORD         (1<<0)
81
82 #define DICT_ENTRY_IS_WORD(entry) ((entry) && ((*entry) & DICT_ENTRY_FLAG_IS_WORD))
83
84 /* Printing the dictionary */
85 int
86 dict_print (dict_t *dict);
87
88 int
89 dict_print_of_length (dict_t    *dict,
90                       int        min_length,
91                       int        max_length);
92
93 int
94 dict_print_if (dict_t                   *dict,
95                dict_entry_predicate_t    predicate);
96
97 int
98 dict_print_of_length_if (dict_t                 *dict,
99                          int                     min_length,
100                          int                     max_length,
101                          dict_entry_predicate_t  predicate);
102
103 int
104 dict_print_by_length_if (dict_t                 *dict,
105                          dict_entry_predicate_t  predicate);
106
107 /* More general callback-based iteration of all entries */
108 typedef void (* dict_action_t) (void *closure, char *word, dict_entry_t *entry);
109
110 int
111 dict_for_each (dict_t           *dict,
112                dict_action_t     action,
113                void             *closure);
114
115 int
116 dict_for_each_of_length (dict_t         *dict,
117                          dict_action_t   action,
118                          void           *closure,
119                          int             min_length,
120                          int             max_length);
121
122 int
123 dict_for_each_if (dict_t                        *dict,
124                   dict_action_t                  action,
125                   void                          *closure,
126                   dict_entry_predicate_t         predicate);
127
128 int
129 dict_for_each_of_length_if (dict_t                      *dict,
130                             dict_action_t                action,
131                             void                        *closure,
132                             int                          min_length,
133                             int                          max_length,
134                             dict_entry_predicate_t       predicate);
135
136 /* Character-by-character perusal of the dictionary */
137 dict_cursor_t
138 dict_root (dict_t *dict);
139
140 dict_cursor_t
141 dict_cursor_next (dict_cursor_t cursor,
142                   char          next);
143
144 dict_entry_t *
145 dict_cursor_resolve (dict_cursor_t cursor);
146
147 #define DICT_CURSOR_NIL NULL
148
149 #endif /* _DICT_H_ */