/* -*-c-*-
*
- * $Id: trackword.c,v 1.1 2001/02/04 17:14:42 mdw Exp $
+ * $Id: trackword.c,v 1.3 2001/02/07 09:08:44 mdw Exp $
*
* Matches trackwords
*
/*----- Revision history --------------------------------------------------*
*
* $Log: trackword.c,v $
+ * Revision 1.3 2001/02/07 09:08:44 mdw
+ * Optimize the graph by removing edges to non-matching cells. Make all of
+ * the graph links be contiguous so the main loop can give up earlier.
+ *
+ * Revision 1.2 2001/02/04 19:52:53 mdw
+ * Remove debugging.
+ *
* Revision 1.1 2001/02/04 17:14:42 mdw
* Initial checkin
*
/*----- Data structures ---------------------------------------------------*/
-enum { N, NE, E, SE, S, SW, W, NW, NDIR };
+#define NDIR 9
typedef struct tcell {
struct tcell *hop[NDIR];
static int track(tcell *c, const char *p)
{
- int i;
+ tcell **cc;
if (*p++ != c->ch)
return (0);
if (!*p)
return (1);
c->f = 1;
- for (i = 0; i < NDIR; i++) {
- if (!c->hop[i] || c->hop[i]->f)
+
+ for (cc = c->hop; *cc; cc++) {
+ if ((*cc)->f)
continue;
- if (track(c->hop[i], p)) {
+ if (track(*cc, p)) {
c->f = 0;
return (1);
}
unsigned i, j;
const char *p = av[0], *q, *l;
size_t sz = strlen(p);
- tcell *c;
+ tcell *c, **cc, **ccc;
/* --- Work out the dimensions --- */
for (i = 0; i < x; i++) {
c->ch = *q++;
c->f = 0;
+ cc = c->hop;
#define NOK (j > 0)
#define SOK (j < y - 1)
#define WOK (i > 0)
#define EOK (i < x - 1)
- c->hop[N] = NOK ? c - x : 0;
- c->hop[S] = SOK ? c + x : 0;
- c->hop[W] = WOK ? c - 1 : 0;
- c->hop[E] = EOK ? c + 1 : 0;
+ if (NOK) *cc++ = c - x;
+ if (SOK) *cc++ = c + x;
+ if (WOK) *cc++ = c - 1;
+ if (EOK) *cc++ = c + 1;
- c->hop[NW] = NOK && WOK ? c - x - 1 : 0;
- c->hop[NE] = NOK && EOK ? c - x + 1 : 0;
- c->hop[SW] = SOK && WOK ? c + x - 1 : 0;
- c->hop[SE] = SOK && EOK ? c + x + 1 : 0;
+ if (NOK && WOK) *cc++ = c - x - 1;
+ if (NOK && EOK) *cc++ = c - x + 1;
+ if (SOK && WOK) *cc++ = c + x - 1;
+ if (SOK && EOK) *cc++ = c + x + 1;
#undef NOK
#undef SOK
#undef WOK
#undef EOK
+ *cc++ = 0;
c++;
}
}
c->ch = 0;
- /* --- Done --- */
+ /* --- Now prune out bogus links --- */
- c = n->c;
- for (j = 0; j < y; j++) {
- for (i = 0; i < x; i++) {
- printf("%c ", c->ch);
- c++;
+ for (c = n->c; c->ch; c++) {
+ ccc = c->hop;
+ for (cc = c->hop; *cc; cc++) {
+ if (isalpha((unsigned char)(*cc)->ch)) {
+ *ccc++ = *cc;
+ }
}
- fputc('\n', stdout);
+ *ccc++ = 0;
}
+ /* --- Done --- */
+
return (&n->n);
}