3 * Matches anagrams and subgrams
5 * (c) 2001 Mark Wooding
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Anag: a simple wordgame helper.
12 * Anag is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * Anag is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Anag; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
31 /*----- Data structures ---------------------------------------------------*/
33 typedef struct node_gram {
36 unsigned f[UCHAR_MAX + 1];
39 /*----- Main code ---------------------------------------------------------*/
41 /* --- @compile@ --- *
43 * Arguments: @node_gram *n@ = pointer to node to fill in
44 * @const char *p@ = pointer to string to compile from
46 * Returns: The length of the string.
48 * Use: Fills in an anagram node with letter frequencies.
51 static size_t compile(node_gram *n, const char *p)
55 memset(n->f, 0, sizeof(n->f));
57 i = (unsigned char)*p++;
64 /* --- Node matcher --- */
66 static int n_gram(node *nn, const char *p, size_t sz)
68 node_gram *n = (node_gram *)nn;
69 unsigned f[UCHAR_MAX];
73 if (n->sz && sz != n->sz)
75 memcpy(f, n->f, sizeof(f));
78 i = (unsigned char)*p++;
89 /* --- Node creation --- */
91 node *anagram(const char *const *av)
93 node_gram *n = xmalloc(sizeof(*n));
95 n->sz = compile(n, av[0]);
99 node *subgram(const char *const *av)
101 node_gram *n = xmalloc(sizeof(*n));
108 /*----- That's all, folks -------------------------------------------------*/