X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=subanagram.c;fp=subanagram.c;h=b726a40712da4ce286fab03db3a5740975965f64;hb=5e131ba1815778bc6ea172cc4ca4f0c478edd316;hp=0000000000000000000000000000000000000000;hpb=17112b7f9d47ed17c86702c1bd815b0e1b9a6281;p=wordgame diff --git a/subanagram.c b/subanagram.c new file mode 100644 index 0000000..b726a40 --- /dev/null +++ b/subanagram.c @@ -0,0 +1,69 @@ +/* + * Copyright © 2006 Carl Worth + * + * 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. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA." + */ + +#include "word-game.h" + +#include + +static const char *subanagram_letters; +static char subanagram_enumerate_word[WORD_GAME_MAX_WORD_LENGTH]; +static dict_t *subanagram_enumerate_answers; + +static void +subanagram_enumerate (dict_cursor_t cursor, + uint8_t seen) +{ + char c; + unsigned int i, last; + uint8_t next_seen; + + if (cursor == DICT_CURSOR_NIL) + return; + + if (DICT_ENTRY_IS_WORD (dict_cursor_resolve (cursor))) + dict_add_word (subanagram_enumerate_answers, subanagram_enumerate_word); + + last = strlen (subanagram_enumerate_word); + + for (i = 0; i < strlen (subanagram_letters); i++) { + if (seen & (1 << i)) + continue; + next_seen = seen | (1 << i); + c = subanagram_letters[i]; + if (c == '?' || c == '_') { + for (c = 'a'; c <= 'z'; c++) { + subanagram_enumerate_word[last] = c; + subanagram_enumerate (dict_cursor_next (cursor, c), next_seen); + } + } else { + subanagram_enumerate_word[last] = c; + subanagram_enumerate (dict_cursor_next (cursor, c), next_seen); + } + subanagram_enumerate_word[last] = '\0'; + } +} + +void +subanagram_expand (const char *letters, dict_t *dict, dict_t *answers) +{ + subanagram_letters = letters; + memset (subanagram_enumerate_word, '\0', WORD_GAME_MAX_WORD_LENGTH); + subanagram_enumerate_answers = answers; + + subanagram_enumerate (dict_root (dict), 0); +}