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 ---------------------------------------------------*/
35 typedef struct tcell {
36 struct tcell *hop[NDIR];
41 typedef struct node_track {
48 /*----- Utility functions -------------------------------------------------*/
52 * Arguments: @long a@ = the target value
54 * Returns: %$\lfloor \sqrt{a} \rfloor$%
56 * Use: Computes an integer square root. This algorithm is
57 * Newton-Raphson with integers. I've stolen this more-or-less
58 * wholesale fom the multiprecision integer square-root function
66 /* Initial guess is half the input length. */
67 for (i = q = a; q; q >>= 2) i >>= 1;
69 /* Do the main series iteration. */
72 if (q > -2*i && q <= 0) return (i);
79 /*----- Main code ---------------------------------------------------------*/
83 * Arguments: @tcell *c@ = pointer to start cell
84 * @const char *p@ = string to match
86 * Returns: Nonzero for a match, zero if no match was found.
88 * Use: Searches for a match in a trackword.
91 static int track(tcell *c, const char *p)
95 if (*p++ != c->ch) return (0);
99 for (cc = c->hop; *cc; cc++) {
100 if ((*cc)->f) continue;
101 if (track(*cc, p)) { c->f = 0; return (1); }
108 /* --- Node matcher --- */
110 static int n_track(node *nn, const char *p, size_t sz)
112 node_track *n = (node_track *)nn;
116 for (c = n->c; c->ch; c++)
117 if (track(c, p)) return (1);
121 /* --- Node creation --- */
123 node *trackword(const char *const *av)
128 const char *p = av[0], *q, *l;
129 size_t sz = strlen(p);
130 tcell *c, **cc, **ccc;
132 /* Work out the dimensions. */
137 die("bad trackword `%s': not square, and no line markers", p);
141 if (!x) die("bad trackword `%s': no columns", p);
150 die("bad trackword `%s': inconsistent line lengths", p);
155 if (!y) die("bad trackword `%s': no rows", p);
157 /* Build the match node. */
158 n = xmalloc(sizeof(*n));
161 n->c = xmalloc((x*y + 1)*sizeof(tcell));
165 for (j = 0; j < y; j++) {
167 for (i = 0; i < x; i++) {
173 #define SOK (j < y - 1)
175 #define EOK (i < x - 1)
177 if (NOK) *cc++ = c - x;
178 if (SOK) *cc++ = c + x;
179 if (WOK) *cc++ = c - 1;
180 if (EOK) *cc++ = c + 1;
182 if (NOK && WOK) *cc++ = c - x - 1;
183 if (NOK && EOK) *cc++ = c - x + 1;
184 if (SOK && WOK) *cc++ = c + x - 1;
185 if (SOK && EOK) *cc++ = c + x + 1;
198 /* Now prune out bogus links. */
199 for (c = n->c; c->ch; c++) {
201 for (cc = c->hop; *cc; cc++)
202 if (isalpha((unsigned char)(*cc)->ch)) *ccc++ = *cc;
210 /*----- That's all, folks -------------------------------------------------*/