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