/*
* 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.
}
static int
-trie_count (trie_t *trie,
- trie_predicate_t predicate)
+trie_count_if (trie_t *trie,
+ trie_predicate_t predicate)
{
int i;
int count = 0;
- if ((predicate) (trie))
+ if (predicate == NULL || (predicate) (trie))
count = 1;
for (i = 0; i < 26; i++) {
if (trie->next[i] == NULL)
continue;
- count += trie_count (trie->next[i], predicate);
+ count += trie_count_if (trie->next[i], predicate);
}
return count;
}
static int
-trie_for_each (trie_t *trie,
- string_t *string,
- trie_predicate_t predicate,
- int length,
- int min_length,
- int max_length,
- dict_action_t action,
- void *closure)
+trie_for_each_of_length_if (trie_t *trie,
+ dict_action_t action,
+ void *closure,
+ int min_length,
+ int max_length,
+ trie_predicate_t predicate,
+ string_t *string,
+ int length)
+
{
char c;
int i;
{
count = 1;
- action (closure, string->s, &trie->flags);
+ if (action)
+ action (closure, string->s, &trie->flags);
}
- if (length == max_length)
+ if (max_length > 0 && length == max_length)
return count;
/* Loop over each element, appending the character and recursing. */
c = TRIE_INDEX_TO_CHAR (i);
string_append_char (string, c);
- count += trie_for_each (trie->next[i], string, predicate,
- length + 1, min_length, max_length,
- action, closure);
+ count += trie_for_each_of_length_if (trie->next[i],
+ action, closure,
+ min_length, max_length,
+ predicate,
+ string, length + 1);
string_chop (string);
}
}
int
-dict_count (dict_t *dict,
- dict_entry_predicate_t predicate)
-{
- dict_entry_predicate = predicate;
- return trie_count (dict, dict_predicate);
-}
-
-int
-dict_for_each_if (dict_t *dict,
- dict_action_t action,
- void *closure,
- dict_entry_predicate_t predicate)
-
+dict_count_if (dict_t *dict,
+ dict_entry_predicate_t predicate)
{
- int count;
- string_t string;
-
- string_init (&string);
-
dict_entry_predicate = predicate;
- count = trie_for_each (dict, &string, dict_predicate,
- 0, 0, -1, action, closure);
-
- string_fini (&string);
-
- return count;
+ return trie_count_if (dict, dict_predicate);
}
int
-dict_for_each (dict_t *dict,
- dict_action_t action,
- void *closure)
+dict_count (dict_t *dict)
{
- return dict_for_each_if (dict, action, closure, NULL);
+ return dict_count_if (dict, NULL);
}
int
-dict_for_each_by_length_if (dict_t *dict,
+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)
{
- int length, total, words, count = 0;
+ int count;
string_t string;
string_init (&string);
dict_entry_predicate = predicate;
- total = trie_count (dict, dict_predicate);
-
- length = 1;
- do {
- words = trie_for_each (dict, &string, dict_predicate,
- 0, length, length,
- action, closure);
- if (words)
- count += words;
- length++;
- } while (count < total);
+ count = trie_for_each_of_length_if (dict,
+ action, closure,
+ min_length, max_length,
+ dict_predicate,
+ &string, 0);
string_fini (&string);
}
int
-dict_for_each_by_length (dict_t *dict,
+dict_for_each_of_length (dict_t *dict,
dict_action_t action,
- void *closure)
+ void *closure,
+ int min_length,
+ int max_length)
{
- return dict_for_each_by_length_if (dict, action, closure, NULL);
+ return dict_for_each_of_length_if (dict,
+ action, closure,
+ min_length, max_length,
+ NULL);
+}
+
+int
+dict_for_each_if (dict_t *dict,
+ dict_action_t action,
+ void *closure,
+ dict_entry_predicate_t predicate)
+
+{
+ return dict_for_each_of_length_if (dict,
+ action, closure,
+ 0, 0,
+ predicate);
+}
+
+int
+dict_for_each (dict_t *dict,
+ dict_action_t action,
+ void *closure)
+{
+ return dict_for_each_if (dict, action, closure, NULL);
}
static void
dict_action_print (void *closure, char *word, dict_entry_t *entry)
{
- int *length_of_last = closure;
- int length = strlen (word);
+ int *first = closure;
- if (length == *length_of_last)
+ if (*first)
+ *first = 0;
+ else
printf(" ");
- else if (*length_of_last)
- printf("\n");
printf ("%s", word);
-
- *length_of_last = length;
}
int
-dict_print (dict_t *dict)
+dict_print_of_length_if (dict_t *dict,
+ int min_length,
+ int max_length,
+ dict_entry_predicate_t predicate)
{
- int length_of_last = 0;
+ int first = TRUE;
- return dict_for_each (dict,
- dict_action_print, &length_of_last);
+ return dict_for_each_of_length_if (dict,
+ dict_action_print, &first,
+ min_length, max_length,
+ predicate);
}
int
-dict_print_by_length (dict_t *dict)
+dict_print_by_length_if (dict_t *dict,
+ dict_entry_predicate_t predicate)
{
- int length_of_last = 0;
+ int length, total, words, count = 0;
+
+ total = dict_count_if (dict, predicate);
+
+ length = 1;
+ do {
+ words = dict_print_of_length_if (dict, length, length, predicate);
+ if (words) {
+ count += words;
+ printf ("\n");
+ }
+ length++;
+ } while (count < total);
- return dict_for_each_by_length (dict,
- dict_action_print, &length_of_last);
+ return count;
+}
+
+int
+dict_print_of_length (dict_t *dict,
+ int min_length,
+ int max_length)
+{
+ return dict_print_of_length_if (dict, min_length, max_length, NULL);
}
int
dict_print_if (dict_t *dict,
dict_entry_predicate_t predicate)
{
- int length_of_last = 0;
-
- return dict_for_each_if (dict,
- dict_action_print, &length_of_last,
- predicate);
+ return dict_print_of_length_if (dict, 0, 0, predicate);
}
int
-dict_print_by_length_if (dict_t *dict,
- dict_entry_predicate_t predicate)
+dict_print (dict_t *dict)
{
- int length_of_last = 0;
-
- return dict_for_each_by_length_if (dict,
- dict_action_print, &length_of_last,
- predicate);
+ return dict_print_if (dict, NULL);
}