chiark / gitweb /
Monoalphabetic match filter.
authormdw <mdw>
Mon, 15 Sep 2003 02:48:55 +0000 (02:48 +0000)
committermdw <mdw>
Mon, 15 Sep 2003 02:48:55 +0000 (02:48 +0000)
Makefile.am
anag.c
anag.h
mono.c [new file with mode: 0644]

index d9566bdbef8226f02d21788cd26e4354ce33cfa0..f3c4ee31d2b8c6b577a41a717378e0b7f95d9481 100644 (file)
@@ -1,6 +1,6 @@
 ## -*-makefile-*-
 ##
 ## -*-makefile-*-
 ##
-## $Id: Makefile.am,v 1.8 2002/08/11 12:59:00 mdw Exp $
+## $Id: Makefile.am,v 1.9 2003/09/15 02:48:55 mdw Exp $
 ##
 ## Makefile for Anag
 ##
 ##
 ## Makefile for Anag
 ##
@@ -28,6 +28,9 @@
 ##----- Revision history ----------------------------------------------------
 ##
 ## $Log: Makefile.am,v $
 ##----- Revision history ----------------------------------------------------
 ##
 ## $Log: Makefile.am,v $
+## Revision 1.9  2003/09/15 02:48:55  mdw
+## Monoalphabetic match filter.
+##
 ## Revision 1.8  2002/08/11 12:59:00  mdw
 ## New Tcl/Tk interface; regular expression support.
 ##
 ## Revision 1.8  2002/08/11 12:59:00  mdw
 ## New Tcl/Tk interface; regular expression support.
 ##
@@ -68,7 +71,7 @@ EXTRA_SCRIPTS = anag-gui
 java_DATA = @JARFILES@
 
 anag_SOURCES = \
 java_DATA = @JARFILES@
 
 anag_SOURCES = \
-       anag.c anag.h wildcard.c anagram.c trackword.c regexp.c util.c
+       anag.c anag.h wildcard.c anagram.c mono.c trackword.c regexp.c util.c
 EXTRA_anag_SOURCES = regexp.c
 anag.jar: AnagGUI.class
        jar cf anag.jar Anag*.class
 EXTRA_anag_SOURCES = regexp.c
 anag.jar: AnagGUI.class
        jar cf anag.jar Anag*.class
diff --git a/anag.c b/anag.c
index 6be2a4e6d3c1c0366314b0b980c4e78476f25b8c..56aa565e9f82f33654c51a525b5e703d33925059 100644 (file)
--- a/anag.c
+++ b/anag.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
 /* -*-c-*-
  *
- * $Id: anag.c,v 1.5 2002/08/11 12:58:09 mdw Exp $
+ * $Id: anag.c,v 1.6 2003/09/15 02:48:54 mdw Exp $
  *
  * Main driver for anag
  *
  *
  * Main driver for anag
  *
@@ -29,6 +29,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: anag.c,v $
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: anag.c,v $
+ * Revision 1.6  2003/09/15 02:48:54  mdw
+ * Monoalphabetic match filter.
+ *
  * Revision 1.5  2002/08/11 12:58:09  mdw
  * Added support for regular expression matching, if supported by the C
  * library.
  * Revision 1.5  2002/08/11 12:58:09  mdw
  * Added support for regular expression matching, if supported by the C
  * library.
@@ -89,6 +92,7 @@ The basic tests in the expression are:\n\
 -subgram WORD          matches words which only use letters in WORD\n\
 -wildcard PATTERN      matches with wildcards `*' and `?'\n\
 -trackword WORD                matches words which can be found in a trackword\n\
 -subgram WORD          matches words which only use letters in WORD\n\
 -wildcard PATTERN      matches with wildcards `*' and `?'\n\
 -trackword WORD                matches words which can be found in a trackword\n\
+-mono PATTERN          matches words isomorphic to the given PATTERN\n\
 "
 #ifdef HAVE_REGCOMP
 "\
 "
 #ifdef HAVE_REGCOMP
 "\
@@ -118,7 +122,7 @@ enum {
   O_HELP, O_VERSION, O_USAGE,
   O_FILE,
   O_AND, O_OR, O_NOT, O_LPAREN, O_RPAREN,
   O_HELP, O_VERSION, O_USAGE,
   O_FILE,
   O_AND, O_OR, O_NOT, O_LPAREN, O_RPAREN,
-  O_ANAG, O_SUBG, O_WILD, O_TRACK, O_REGEXP,
+  O_ANAG, O_SUBG, O_WILD, O_TRACK, O_REGEXP, O_MONO,
   O_EOF
 };
 
   O_EOF
 };
 
@@ -148,6 +152,7 @@ static const struct opt opttab[] = {
   { "subgram",         1,      0,              O_SUBG },
   { "wildcard",                1,      0,              O_WILD },
   { "trackword",       1,      0,              O_TRACK },
   { "subgram",         1,      0,              O_SUBG },
   { "wildcard",                1,      0,              O_WILD },
   { "trackword",       1,      0,              O_TRACK },
+  { "mono",            1,      0,              O_MONO },
 #ifdef HAVE_REGCOMP
   { "regexp",          1,      0,              O_REGEXP },
 #endif
 #ifdef HAVE_REGCOMP
   { "regexp",          1,      0,              O_REGEXP },
 #endif
@@ -354,6 +359,7 @@ static void p_factor(p_ctx *p, node **nn)
 #ifdef HAVE_REGCOMP
       case O_REGEXP: *nn = regexp(p->a + 1); break;
 #endif
 #ifdef HAVE_REGCOMP
       case O_REGEXP: *nn = regexp(p->a + 1); break;
 #endif
+      case O_MONO: *nn = mono(p->a + 1); break;
       default: die("syntax error near `%s': unexpected token", *p->a);
     }
     p_next(p);
       default: die("syntax error near `%s': unexpected token", *p->a);
     }
     p_next(p);
diff --git a/anag.h b/anag.h
index 97c4bef9e8e81a84e74673d16671da4cf0563a95..5c9822c73a110cd698bce4c2b224ec26cb8bd5e1 100644 (file)
--- a/anag.h
+++ b/anag.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
 /* -*-c-*-
  *
- * $Id: anag.h,v 1.2 2002/08/11 12:58:09 mdw Exp $
+ * $Id: anag.h,v 1.3 2003/09/15 02:48:55 mdw Exp $
  *
  * External definitions for Anag
  *
  *
  * External definitions for Anag
  *
@@ -29,6 +29,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: anag.h,v $
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: anag.h,v $
+ * Revision 1.3  2003/09/15 02:48:55  mdw
+ * Monoalphabetic match filter.
+ *
  * Revision 1.2  2002/08/11 12:58:09  mdw
  * Added support for regular expression matching, if supported by the C
  * library.
  * Revision 1.2  2002/08/11 12:58:09  mdw
  * Added support for regular expression matching, if supported by the C
  * library.
@@ -78,6 +81,7 @@ extern node *anagram(const char *const */*av*/);
 extern node *subgram(const char *const */*av*/);
 extern node *wildcard(const char *const */*av*/);
 extern node *trackword(const char *const */*av*/);
 extern node *subgram(const char *const */*av*/);
 extern node *wildcard(const char *const */*av*/);
 extern node *trackword(const char *const */*av*/);
+extern node *mono(const char *const */*av*/);
 extern node *regexp(const char *const */*av*/);
 
 /*----- Error reporting ---------------------------------------------------*/
 extern node *regexp(const char *const */*av*/);
 
 /*----- Error reporting ---------------------------------------------------*/
diff --git a/mono.c b/mono.c
new file mode 100644 (file)
index 0000000..535f729
--- /dev/null
+++ b/mono.c
@@ -0,0 +1,105 @@
+/* -*-c-*-
+ *
+ * $Id: mono.c,v 1.1 2003/09/15 02:48:54 mdw Exp $
+ *
+ * Monoalphabetic matcher
+ *
+ * (c) 2003 Mark Wooding
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of Anag: a simple wordgame helper.
+ *
+ * Anag 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 of the License, or
+ * (at your option) any later version.
+ * 
+ * Anag 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 Anag; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: mono.c,v $
+ * Revision 1.1  2003/09/15 02:48:54  mdw
+ * Monoalphabetic match filter.
+ *
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include "anag.h"
+
+/*----- Data structures ---------------------------------------------------*/
+
+typedef struct node_mono {
+  node n;
+  unsigned len;
+  unsigned char *p;
+} node_mono;
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- Matching --- */
+
+static int n_mono(node *nn, const char *p, size_t sz)
+{
+  node_mono *n = (node_mono *)nn;
+  unsigned map[UCHAR_MAX], imap[UCHAR_MAX];
+  const unsigned char *q = n->p;
+  int ch, i;
+
+  if (sz != n->len)
+    return (0);
+  memset(map, 0, sizeof(map));
+  memset(imap, 0, sizeof(imap));
+  while (*p) {
+    ch = *p++;
+    i = *q++;
+    if (!map[i]) {
+      if (imap[ch])
+       return (0);
+      map[i] = ch;
+      imap[ch] = 1;
+    } else if (map[i] != ch)
+      return (0);
+  }
+  return (1);
+}
+
+/* --- Node creation --- */
+
+node *mono(const char *const *av)
+{
+  unsigned char map[UCHAR_MAX];
+  unsigned max;
+  int ch;
+  const char *p;
+  unsigned char *q;
+
+  node_mono *n = xmalloc(sizeof(*n));
+  n->n.func = n_mono;
+  memset(map, UCHAR_MAX, sizeof(map));
+  max = 0;
+  p = av[0];
+  n->len = strlen(p);
+  q = xmalloc(n->len);
+  n->p = q;
+  while (*p) {
+    ch = *p++;
+    if (map[ch] >= max)
+      map[ch] = max++;
+    *q++ = map[ch];
+  }
+  return (&n->n);
+}
+
+/*----- That's all, folks -------------------------------------------------*/