Add filtering by length, and retaining only longest/shortest matches.
[anag] / longest.c
diff --git a/longest.c b/longest.c
new file mode 100644 (file)
index 0000000..30371bc
--- /dev/null
+++ b/longest.c
@@ -0,0 +1,124 @@
+/* -*-c-*-
+ *
+ * $Id$
+ *
+ * Remember the longest word seen so far
+ *
+ * (c) 2005 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.
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include "anag.h"
+
+/*----- Data structures ---------------------------------------------------*/
+
+typedef struct link {
+  struct link *next;
+  char *p;
+} link;
+
+typedef struct node_longest {
+  node n;
+  size_t len;
+  link *l, **lt;
+} node_longest;
+
+/*----- Main code ---------------------------------------------------------*/
+
+static void empty(node_longest *n)
+{
+  link *l, *ll;
+
+  for (l = n->l; l; l = ll) {
+    ll = l->next;
+    free(l->p);
+    free(l);
+  }
+  n->l = 0;
+  n->lt = &n->l;
+}
+
+static void insert(node_longest *n, const char *p, size_t sz)
+{
+  link *l;
+
+  n->len = sz;
+  l = xmalloc(sizeof(*l));
+  l->p = xmalloc(sz + 1);
+  memcpy(l->p, p, sz + 1);
+  l->next = 0;
+  *n->lt = l;
+  n->lt = &l->next;
+}
+
+static int aa_output(void *p)
+{
+  node_longest *n = p;
+  link *l;
+
+  if (!n->l) return (0);
+  for (l = n->l; l; l = l->next)
+    puts(l->p);
+  return (1);
+}
+
+static int n_longest(node *nn, const char *p, size_t sz)
+{
+  node_longest *n = (node_longest *)nn;
+  if (sz < n->len)
+    return (0);
+  if (sz > n->len)
+    empty(n);
+  insert(n, p, sz);
+  return (0);
+}
+
+static int n_shortest(node *nn, const char *p, size_t sz)
+{
+  node_longest *n = (node_longest *)nn;
+  if (n->len && sz > n->len)
+    return (0);
+  if (!n->len || sz < n->len)
+    empty(n);
+  insert(n, p, sz);
+  return (0);
+}
+
+static node *mklongest(int (*nf)(node *, const char *, size_t),
+                      int (*aa)(void *))
+{
+  node_longest *n = xmalloc(sizeof(*n));
+  n->n.func = nf;
+  n->len = 0;
+  n->l = 0;
+  n->lt = &n->l;
+  atend_register(aa, n);
+  return (&n->n);
+}
+
+node *longest(const char *const *av)
+  { return mklongest(n_longest, aa_output); }
+node *shortest(const char *const *av)
+  { return mklongest(n_shortest, aa_output); }
+
+/*----- That's all, folks -------------------------------------------------*/