repeated.c
changeset 5 c87681fff7d3
child 6 bc41369f4587
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/repeated.c	Mon Aug 13 16:27:41 2007 +0200
@@ -0,0 +1,107 @@
+#include <stdio.h>
+#include "dictre.h"
+
+extern struct Def defs[];
+extern int ndefs;
+
+enum
+{
+    MAXLEN = 200
+};
+
+struct HashElement
+{
+    struct HashElement *next;
+    struct Def *def;
+    int index;
+};
+
+static struct HashElement *dhash[MAXLEN];
+static int ndhash[MAXLEN];
+static struct HashElement *dhash_last[MAXLEN];
+
+void init_repeated()
+{
+    int i;
+    for(i=0; i<MAXLEN; ++i)
+    {
+        ndhash[i] = 0;
+    }
+}
+
+void remove_def(int i)
+{
+    ndefs--;
+    for(i=i; i<ndefs; ++i)
+        defs[i] = defs[i+1];
+}
+
+/*
+static void remove_hashdef(struct Def *ptr, int hash)
+{
+    int i;
+    struct HashElement *root = dhash[hash];
+
+    for(i=0; i<ndhash[hash]; ++i)
+        if (root[i] == ptr)
+            break;
+
+    ndhash[hash]--;
+    for(; i<ndhash[hash]; ++i)
+        root[i] = root[i+1];
+}
+*/
+
+static int calc_hash(struct Def *ptr)
+{
+    int hash;
+
+    hash = ptr->length % MAXLEN;
+    return hash;
+}
+
+void new_hashdef(struct Def *ptr, int index)
+{
+    int hash;
+    struct HashElement *el;
+
+    hash = calc_hash(ptr);
+
+    el = (struct HashElement *) fastmalloc(sizeof(*el));
+    el->def = ptr;
+    el->next = 0;
+    el->index = index;
+
+    /* Let the last point to the new element */
+    if (ndhash[hash] != 0)
+    {
+        dhash_last[hash]->next = el;
+    } else /* 0 elements in row */
+    {
+        dhash[hash] = el;
+    }
+    ndhash[hash] += 1;
+    dhash_last[hash] = el;
+}
+
+int def_repeated(struct Def *ptr)
+{
+    int hash;
+    int i;
+    struct HashElement *h;
+
+    hash = calc_hash(ptr);
+
+    h = dhash[hash];
+    for(i=0; i < ndhash[hash]; ++i)
+    {
+        struct Def *hdef = h->def;
+        if (hdef->length == ptr->length
+                && (strncmp(hdef->d, ptr->d, ptr->length) == 0))
+            /* Repeated found !*/
+            return h->index;
+    }
+
+    /* Not found */
+    return -1;
+}