3 * $Id: trackword.c,v 1.3 2001/02/07 09:08:44 mdw Exp $
7 * (c) 2001 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Anag: a simple wordgame helper.
14 * Anag is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Anag is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Anag; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
31 * $Log: trackword.c,v $
32 * Revision 1.3 2001/02/07 09:08:44 mdw
33 * Optimize the graph by removing edges to non-matching cells. Make all of
34 * the graph links be contiguous so the main loop can give up earlier.
36 * Revision 1.2 2001/02/04 19:52:53 mdw
39 * Revision 1.1 2001/02/04 17:14:42 mdw
44 /*----- Header files ------------------------------------------------------*/
48 /*----- Data structures ---------------------------------------------------*/
52 typedef struct tcell
{
53 struct tcell
*hop
[NDIR
];
58 typedef struct node_track
{
65 /*----- Utility functions -------------------------------------------------*/
69 * Arguments: @long a@ = the target value
71 * Returns: %$\lfloor \sqrt{a} \rfloor$%
73 * Use: Computes an integer square root. This algorithm is
74 * Newton-Raphson with integers. I've stolen this more-or-less
75 * wholesale fom the multiprecision integer square-root function
83 /* --- Initial guess is half the input length --- */
85 for (i
= q
= a
; q
; q
>>= 2)
88 /* --- Do the main series iteration --- */
92 if (!q
|| (q
< 0 && -q
<= 2 * i
))
102 /*----- Main code ---------------------------------------------------------*/
106 * Arguments: @tcell *c@ = pointer to start cell
107 * @const char *p@ = string to match
109 * Returns: Nonzero for a match, zero if no match was found.
111 * Use: Searches for a match in a trackword.
114 static int track(tcell
*c
, const char *p
)
124 for (cc
= c
->hop
; *cc
; cc
++) {
136 /* --- Node matcher --- */
138 static int n_track(node
*nn
, const char *p
, size_t sz
)
140 node_track
*n
= (node_track
*)nn
;
145 for (c
= n
->c
; c
->ch
; c
++) {
152 /* --- Node creation --- */
154 node
*trackword(const char *const *av
)
159 const char *p
= av
[0], *q
, *l
;
160 size_t sz
= strlen(p
);
161 tcell
*c
, **cc
, **ccc
;
163 /* --- Work out the dimensions --- */
169 die("bad trackword `%s': not square, and no line markers", p
);
174 die("bad trackword `%s': no columns", p
);
185 die("bad trackword `%s': inconsistent line lengths", p
);
191 die("bad trackword `%s': no rows", p
);
193 /* --- Build the match node --- */
195 n
= xmalloc(sizeof(*n
));
198 n
->c
= xmalloc((x
* y
+ 1) * sizeof(tcell
));
202 for (j
= 0; j
< y
; j
++) {
205 for (i
= 0; i
< x
; i
++) {
211 #define SOK (j < y - 1)
213 #define EOK (i < x - 1)
215 if (NOK
) *cc
++ = c
- x
;
216 if (SOK
) *cc
++ = c
+ x
;
217 if (WOK
) *cc
++ = c
- 1;
218 if (EOK
) *cc
++ = c
+ 1;
220 if (NOK
&& WOK
) *cc
++ = c
- x
- 1;
221 if (NOK
&& EOK
) *cc
++ = c
- x
+ 1;
222 if (SOK
&& WOK
) *cc
++ = c
+ x
- 1;
223 if (SOK
&& EOK
) *cc
++ = c
+ x
+ 1;
236 /* --- Now prune out bogus links --- */
238 for (c
= n
->c
; c
->ch
; c
++) {
240 for (cc
= c
->hop
; *cc
; cc
++) {
241 if (isalpha((unsigned char)(*cc
)->ch
)) {
253 /*----- That's all, folks -------------------------------------------------*/