summaryrefslogtreecommitdiff
path: root/utils/Burg/map.c
blob: 588b485eab2f21a5726acb9062813ab41686db8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
char rcsid_map[] = "$Id$";

#include <stdio.h>
#include <string.h>
#include "b.h"
#include "fe.h"

Mapping globalMap;

static void growMapping ARGS((Mapping));
static int hash ARGS((Item_Set, int));

Mapping
newMapping(size) int size;
{
	Mapping m;

	m = (Mapping) zalloc(sizeof(struct mapping));
	assert(m);

	m->count = 0;
	m->hash = (List*) zalloc(size * sizeof(List));
	m->hash_size = size;
	m->max_size = STATES_INCR;
	m->set = (Item_Set*) zalloc(m->max_size * sizeof(Item_Set));
	assert(m->set);

	return m;
}

static void
growMapping(m) Mapping m;
{
	Item_Set *tmp;

	m->max_size += STATES_INCR;
	tmp = (Item_Set*) zalloc(m->max_size * sizeof(Item_Set));
	memcpy(tmp, m->set, m->count * sizeof(Item_Set));
	zfree(m->set);
	m->set = tmp;
}

static int
hash(ts, mod) Item_Set ts; int mod;
{
	register Item *p = ts->virgin;
	register int v;
	register Relevant r = ts->relevant;
	register int nt;

	if (!ts->op) {
		return 0;
	}

	v = 0;
	for (; (nt = *r) != 0; r++) {
		v ^= ((long)p[nt].rule) + (PRINCIPLECOST(p[nt].delta)<<4);
	}
	v >>= 4;
	v &= (mod-1);
	return v;
}

Item_Set
encode(m, ts, new) Mapping m; Item_Set ts; int *new;
{
	int h;
	List l;

	assert(m);
	assert(ts);
	assert(m->count <= m->max_size);

	if (grammarNts && errorState && m == globalMap) {
		List l;
		int found;

		found = 0;
		for (l = grammarNts; l; l = l->next) {
			Symbol s;
			s = (Symbol) l->x;

			if (ts->virgin[s->u.nt->num].rule) {
				found = 1;
				break;
			}
		}
		if (!found) {
			*new = 0;
			return errorState;
		}
	}

	*new = 0;
	h = hash(ts, m->hash_size);
	for (l = m->hash[h]; l; l = l->next) {
		Item_Set s = (Item_Set) l->x;
		if (ts->op == s->op && equivSet(ts, s)) {
			ts->num = s->num;
			return s;
		}
	}
	if (m->count >= m->max_size) {
		growMapping(m);
	}
	assert(m->count < m->max_size);
	m->set[m->count] = ts;
	ts->num = m->count++;
	*new = 1;
	m->hash[h] = newList(ts, m->hash[h]);
	return ts;
}

Item_Set
decode(m, t) Mapping m; ItemSetNum t;
{
	assert(m);
	assert(t);
	assert(m->count < m->max_size);
	assert(t < m->count);

	return m->set[t];
}

void
dumpMapping(m) Mapping m;
{
	int i;

	printf("BEGIN Mapping: Size=%d\n", m->count);
	for (i = 0; i < m->count; i++) {
		dumpItem_Set(m->set[i]);
	}
	printf("END Mapping\n");
}