repeated.c
author viric <viriketo@gmail.com>
Fri, 30 Mar 2012 18:54:55 +0200
changeset 31 2bcc08d0f1d8
parent 6 bc41369f4587
permissions -rw-r--r--
Fixing an error message
#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;
    /*
    printf("New index: %i\n", 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 !*/
        {
            /*
            printf("Found: l1: %i l2: %i => %i\n", ptr->length, hdef->length,
                    h->index);
                    */
            return h->index;
        }
        h = h->next;
    }

    /* Not found */
    return -1;
}