repeated.c
author viric@mandarina
Sat, 08 Sep 2007 09:13:15 +0200
changeset 29 84abeba4ef3b
parent 6 bc41369f4587
permissions -rw-r--r--
Removing binary.
viric@5
     1
#include <stdio.h>
viric@5
     2
#include "dictre.h"
viric@5
     3
viric@5
     4
extern struct Def defs[];
viric@5
     5
extern int ndefs;
viric@5
     6
viric@5
     7
enum
viric@5
     8
{
viric@5
     9
    MAXLEN = 200
viric@5
    10
};
viric@5
    11
viric@5
    12
struct HashElement
viric@5
    13
{
viric@5
    14
    struct HashElement *next;
viric@5
    15
    struct Def *def;
viric@5
    16
    int index;
viric@5
    17
};
viric@5
    18
viric@5
    19
static struct HashElement *dhash[MAXLEN];
viric@5
    20
static int ndhash[MAXLEN];
viric@5
    21
static struct HashElement *dhash_last[MAXLEN];
viric@5
    22
viric@5
    23
void init_repeated()
viric@5
    24
{
viric@5
    25
    int i;
viric@5
    26
    for(i=0; i<MAXLEN; ++i)
viric@5
    27
    {
viric@5
    28
        ndhash[i] = 0;
viric@5
    29
    }
viric@5
    30
}
viric@5
    31
viric@5
    32
void remove_def(int i)
viric@5
    33
{
viric@5
    34
    ndefs--;
viric@5
    35
    for(i=i; i<ndefs; ++i)
viric@5
    36
        defs[i] = defs[i+1];
viric@5
    37
}
viric@5
    38
viric@5
    39
/*
viric@5
    40
static void remove_hashdef(struct Def *ptr, int hash)
viric@5
    41
{
viric@5
    42
    int i;
viric@5
    43
    struct HashElement *root = dhash[hash];
viric@5
    44
viric@5
    45
    for(i=0; i<ndhash[hash]; ++i)
viric@5
    46
        if (root[i] == ptr)
viric@5
    47
            break;
viric@5
    48
viric@5
    49
    ndhash[hash]--;
viric@5
    50
    for(; i<ndhash[hash]; ++i)
viric@5
    51
        root[i] = root[i+1];
viric@5
    52
}
viric@5
    53
*/
viric@5
    54
viric@5
    55
static int calc_hash(struct Def *ptr)
viric@5
    56
{
viric@5
    57
    int hash;
viric@5
    58
viric@5
    59
    hash = ptr->length % MAXLEN;
viric@5
    60
    return hash;
viric@5
    61
}
viric@5
    62
viric@5
    63
void new_hashdef(struct Def *ptr, int index)
viric@5
    64
{
viric@5
    65
    int hash;
viric@5
    66
    struct HashElement *el;
viric@5
    67
viric@5
    68
    hash = calc_hash(ptr);
viric@5
    69
viric@5
    70
    el = (struct HashElement *) fastmalloc(sizeof(*el));
viric@5
    71
    el->def = ptr;
viric@5
    72
    el->next = 0;
viric@5
    73
    el->index = index;
viric@6
    74
    /*
viric@6
    75
    printf("New index: %i\n", index);
viric@6
    76
    */
viric@5
    77
viric@5
    78
    /* Let the last point to the new element */
viric@5
    79
    if (ndhash[hash] != 0)
viric@5
    80
    {
viric@5
    81
        dhash_last[hash]->next = el;
viric@5
    82
    } else /* 0 elements in row */
viric@5
    83
    {
viric@5
    84
        dhash[hash] = el;
viric@5
    85
    }
viric@5
    86
    ndhash[hash] += 1;
viric@5
    87
    dhash_last[hash] = el;
viric@5
    88
}
viric@5
    89
viric@5
    90
int def_repeated(struct Def *ptr)
viric@5
    91
{
viric@5
    92
    int hash;
viric@5
    93
    int i;
viric@5
    94
    struct HashElement *h;
viric@5
    95
viric@5
    96
    hash = calc_hash(ptr);
viric@5
    97
viric@5
    98
    h = dhash[hash];
viric@5
    99
    for(i=0; i < ndhash[hash]; ++i)
viric@5
   100
    {
viric@5
   101
        struct Def *hdef = h->def;
viric@5
   102
        if (hdef->length == ptr->length
viric@5
   103
                && (strncmp(hdef->d, ptr->d, ptr->length) == 0))
viric@5
   104
            /* Repeated found !*/
viric@6
   105
        {
viric@6
   106
            /*
viric@6
   107
            printf("Found: l1: %i l2: %i => %i\n", ptr->length, hdef->length,
viric@6
   108
                    h->index);
viric@6
   109
                    */
viric@5
   110
            return h->index;
viric@6
   111
        }
viric@6
   112
        h = h->next;
viric@5
   113
    }
viric@5
   114
viric@5
   115
    /* Not found */
viric@5
   116
    return -1;
viric@5
   117
}