]> git.cworth.org Git - wordgame/blobdiff - dict.h
Increase the window size a bit
[wordgame] / dict.h
diff --git a/dict.h b/dict.h
index d87e7659031e3497a1158a94ffde33ed948e82d8..9fcb3790d3e8d44daf9b359457ff13981f965e78 100644 (file)
--- a/dict.h
+++ b/dict.h
@@ -1,7 +1,7 @@
 /*
  * Copyright © 2006 Carl Worth
  *
- * This program is free software; you can redistribute it and\/or modify
+ * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2, or (at your option)
  * any later version.
 #ifndef _DICT_H_
 #define _DICT_H_
 
-#include "dict-impl.h"
+/* Portably, schmortability. I want ease of programming. */
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+#ifndef FALSE
+# define FALSE 0
+#endif
+
+#ifndef TRUE
+# define TRUE 1
+#endif
+
+typedef int bool_t;
+
+typedef struct _trie {
+    uint32_t flags;
+    struct _trie *next[26];
+} trie_t;
 
-/* Only looks opaque. Perfectly stack-allocatable */
 typedef trie_t dict_t;
 typedef trie_t *dict_cursor_t;
 typedef uint32_t dict_entry_t;
@@ -46,10 +64,74 @@ dict_entry_t *
 dict_lookup (dict_t    *dict,
             const char *word);
 
+typedef bool_t
+(*dict_entry_predicate_t) (dict_entry_t entry);
+
+int
+dict_count_if (dict_t                  *dict,
+              dict_entry_predicate_t   predicate);
+
+int
+dict_count (dict_t *dict);
+
 /* Querying a dictionary entry. The dict interface uses 1 bit.
  * All of the remaining bits are available for application use.
  */
-#define DICT_ENTRY_IS_WORD(entry) ((entry) && ((*entry) & 0x01))
+#define DICT_ENTRY_FLAG_IS_WORD                (1<<0)
+
+#define DICT_ENTRY_IS_WORD(entry) ((entry) && ((*entry) & DICT_ENTRY_FLAG_IS_WORD))
+
+/* Printing the dictionary */
+int
+dict_print (dict_t *dict);
+
+int
+dict_print_of_length (dict_t   *dict,
+                     int        min_length,
+                     int        max_length);
+
+int
+dict_print_if (dict_t                  *dict,
+              dict_entry_predicate_t    predicate);
+
+int
+dict_print_of_length_if (dict_t                        *dict,
+                        int                     min_length,
+                        int                     max_length,
+                        dict_entry_predicate_t  predicate);
+
+int
+dict_print_by_length_if (dict_t                        *dict,
+                        dict_entry_predicate_t  predicate);
+
+/* More general callback-based iteration of all entries */
+typedef void (* dict_action_t) (void *closure, char *word, dict_entry_t *entry);
+
+int
+dict_for_each (dict_t          *dict,
+              dict_action_t     action,
+              void             *closure);
+
+int
+dict_for_each_of_length (dict_t                *dict,
+                        dict_action_t   action,
+                        void           *closure,
+                        int             min_length,
+                        int             max_length);
+
+int
+dict_for_each_if (dict_t                       *dict,
+                 dict_action_t                  action,
+                 void                          *closure,
+                 dict_entry_predicate_t         predicate);
+
+int
+dict_for_each_of_length_if (dict_t                     *dict,
+                           dict_action_t                action,
+                           void                        *closure,
+                           int                          min_length,
+                           int                          max_length,
+                           dict_entry_predicate_t       predicate);
 
 /* Character-by-character perusal of the dictionary */
 dict_cursor_t