forked from cotillion/cd-gamedriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapping.c
434 lines (378 loc) · 10.6 KB
/
mapping.c
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/* vim: set ts=8 : */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/time.h>
#include <sys/types.h> /* sys/types.h and netinet/in.h are here to enable include of comm.h below */
#include <sys/stat.h>
/* #include <netinet/in.h> Included in comm.h below */
#include <memory.h>
#include <stdio.h>
#include "config.h"
#include "lint.h"
#include "exec.h"
#include "interpret.h"
#include "object.h"
#include "instrs.h"
#include "comm.h"
#include "mapping.h"
#include "hash.h"
#include "simulate.h"
#include "mstring.h"
#include "inline_eqs.h"
#include "inline_svalue.h"
static void rehash_map(struct mapping *);
/* rehash when the mapping is filled to this many % */
#define FILL_FACTOR 250
#define FSIZE(s) (((s)*FILL_FACTOR)/100)
int num_mappings = 0, total_mapping_size = 0;
int
free_apairs(struct apair *p)
{
struct apair *next;
int pairs = 0;
for (;p;p = next) {
free_svalue(&p->arg);
free_svalue(&p->val);
next = p->next;
free((char *)p);
total_mapping_size -= sizeof(struct apair);
pairs++;
}
return pairs;
}
void
free_mapping(struct mapping *m)
{
unsigned int i;
if (!m->ref || --m->ref > 0)
return;
for (i = 0; i < m->size; i++)
m->card -= free_apairs(m->pairs[i]);
num_mappings--;
total_mapping_size -= (sizeof(struct apair *) * m->size +
sizeof(struct mapping));
free((char *)m->pairs);
free((char *)m);
}
struct mapping *
allocate_map(unsigned int msize)
{
struct mapping *m;
unsigned u;
unsigned int size;
for(size = 1, u = (msize*100)/FILL_FACTOR; u && size < MAX_MAPPING_SIZE; size <<= 1, u >>= 1)
;
num_mappings++;
m = (struct mapping *) xalloc(sizeof(struct mapping));
m->pairs = (struct apair **) xalloc(sizeof(struct apair *) * size);
(void)memset(m->pairs, 0, sizeof(struct apair *) * size);
total_mapping_size += sizeof(struct mapping) + sizeof(struct apair *) * size;
m->ref = 1;
m->card = 0;
m->size = size;
m->mcard = FSIZE(m->size);
return m;
}
unsigned int
card_mapping(struct mapping *m)
{
return m->card;
}
static INLINE struct apair *
newpair(struct apair *n, struct svalue *k, struct svalue *v, unsigned int h)
{
struct apair *p = (struct apair *)xalloc(sizeof(struct apair));
p->next = n;
p->hashval = h;
assign_svalue_no_free(&p->arg, k);
assign_svalue_no_free(&p->val, v);
total_mapping_size += sizeof(struct apair);
return p;
}
static INLINE unsigned int
hashsvalue(struct svalue *v)
{
switch(v->type) {
case T_NUMBER:
return (unsigned int)v->u.number;
case T_POINTER:
return ((unsigned long)v->u.vec >> 4);
case T_MAPPING:
return ((unsigned long)v->u.map >> 4);
case T_STRING:
return hash_string(v->u.string);
case T_OBJECT:
return ((unsigned long)v->u.ob >> 4);
case T_FLOAT:
return (unsigned long)v->u.number;
default:
return 0;
}
}
struct svalue *
get_map_lvalue(struct mapping *m, struct svalue *k, int c)
{
unsigned int hash, h;
struct apair *p;
hash = hashsvalue(k);
h = hash % m->size;
for (p = m->pairs[h]; p; p = p->next) {
if (p->hashval == hash && equal_svalue(k, &p->arg))
break;
}
if (!p) {
if (c) {
if (m->card >= MAX_MAPPING_SIZE) {
error("Too large mapping.\n");
return &const0;
}
m->pairs[h] = p = newpair(m->pairs[h], k, &const0, hash);
if (++m->card > m->mcard) {
/* We need to extend the hash table */
rehash_map(m);
}
} else {
/* Return address of a dummy location, with 0. */
return &const0;
}
}
return &p->val;
}
struct vector *
map_domain(struct mapping *m)
{
struct vector *d;
unsigned int cnt, i, k;
struct apair *p;
cnt = m->card;
d = allocate_array(cnt);
for (k = 0, i = 0; i < m->size; i++)
for(p = m->pairs[i]; p; p = p->next)
assign_svalue_no_free (&d->item[k++], &p->arg);
return d;
}
struct vector *
map_codomain(struct mapping *m)
{
struct vector *d;
unsigned int cnt, i, k;
struct apair *p;
cnt = m->card;
d = allocate_array(cnt);
for (k = 0, i = 0; i < m->size; i++)
for(p = m->pairs[i]; p; p = p->next)
assign_svalue_no_free(&d->item[k++], &p->val);
return d;
}
struct mapping *
make_mapping(struct vector *ind, struct vector *val)
{
struct mapping *m;
struct svalue tmp;
unsigned int i, max;
tmp.type = T_NUMBER;
#ifdef PURIFY
tmp.string_type = -1;
#endif
if (ind != NULL && val != NULL) {
max = ind->size < val->size ? ind->size : val->size;
m = allocate_map(max);
for (i = 0 ; i < max ; i++)
{
assign_svalue(get_map_lvalue(m, &ind->item[i], 1),
&val->item[i]);
}
} else if (ind != NULL && val == NULL) {
m = allocate_map(ind->size);
for (i = 0 ; i < ind->size ; i++)
{
tmp.u.number = i;
assign_svalue(get_map_lvalue(m, &ind->item[i], 1), &tmp);
}
} else if (ind == NULL && val != NULL) {
m = allocate_map(val->size);
for (i = 0 ; i < val->size ; i++)
{
tmp.u.number = i;
assign_svalue_no_free(get_map_lvalue(m, &tmp, 1), &val->item[i]);
}
} else {
m = allocate_map(0);
}
return m;
}
struct mapping *
add_mapping(struct mapping *m1, struct mapping *m2)
{
struct mapping *retm;
struct apair *p;
unsigned int i;
retm = allocate_map(m1->card + m2->card);
for (i = 0 ; i < m1->size ; i++)
{
for (p = m1->pairs[i]; p ; p = p->next)
assign_svalue(get_map_lvalue(retm, &p->arg, 1), &p->val);
}
for (i = 0 ; i < m2->size ; i++)
{
for (p = m2->pairs[i]; p ; p = p->next)
assign_svalue(get_map_lvalue(retm, &p->arg, 1), &p->val);
}
return retm;
}
void
addto_mapping(struct mapping *m1, struct mapping *m2)
{
struct apair *p;
unsigned int i;
for (i = 0 ; i < m2->size ; i++)
{
for (p = m2->pairs[i]; p ; p = p->next)
assign_svalue(get_map_lvalue(m1, &p->arg, 1), &p->val);
}
}
void
remove_from_mapping(struct mapping *m, struct svalue *val)
{
struct apair **p;
unsigned int h;
h = hashsvalue(val) % m->size;
for (p = &m->pairs[h]; *p; p = &(*p)->next)
if (equal_svalue(val, &(*p)->arg)) {
struct apair *del = *p;
*p = del->next;
del->next = NULL;
m->card--;
free_apairs(del);
return;
}
return;
}
struct mapping *
remove_mapping(struct mapping *m, struct svalue *val)
{
struct mapping *retm;
struct apair *p;
unsigned int i, h;
retm = allocate_map(m->size);
h = hashsvalue(val) & (m->size-1);
for (i = 0 ; i < m->size ; i++)
{
for (p = m->pairs[i] ; p ; p = p->next)
{
if (h == i)
{
if (!equal_svalue(val, &p->arg))
assign_svalue(get_map_lvalue(retm, &p->arg, 1), &p->val);
}
else
assign_svalue(get_map_lvalue(retm, &p->arg, 1), &p->val);
}
}
return retm;
}
static void
rehash_map(struct mapping *map)
{
register struct apair **pairs, *next, *ptr;
unsigned int i, hval;
unsigned int nsize;
nsize = map->size * 2;
if (nsize > MAX_MAPPING_SIZE) {
error("Too large mapping.\n");
return;
}
pairs = (struct apair **)xalloc(sizeof(struct apair *) * nsize);
(void)memset(pairs, 0, sizeof(struct apair *) * nsize);
for (i = 0 ; i < map->size ; i++) {
for (ptr = map->pairs[i]; ptr; ptr = next) {
hval = ptr->hashval & (nsize-1);
next = ptr->next;
ptr->next = pairs[hval];
pairs[hval] = ptr;
}
}
free((char *)map->pairs);
map->pairs = pairs;
total_mapping_size += sizeof(struct apair *) * (nsize - map->size);
map->size = nsize;
map->mcard = FSIZE(map->size);
}
struct mapping *
copy_mapping(struct mapping *m)
{
struct mapping *cm;
struct apair *pair;
unsigned int i;
num_mappings++;
cm = (struct mapping *) xalloc(sizeof(struct mapping));
cm->pairs = (struct apair **) xalloc(sizeof(struct apair *) * m->size);
(void)memset(cm->pairs, 0, sizeof(struct apair *) * m->size);
total_mapping_size += sizeof(struct mapping) + sizeof(struct apair *) * m->size;
cm->ref = 1;
cm->size = m->size;
cm->card = m->card;
cm->mcard = m->mcard;
for (i = 0; i < m->size; i++)
for(pair = m->pairs[i]; pair; pair = pair->next)
cm->pairs[i] = newpair(cm->pairs[i], &pair->arg, &pair->val, pair->hashval);
return cm;
}
/* Runs all codomain elements of a mapping through fun
and replaces each value in the codomain map by the value returned
by fun.
*/
struct mapping *
map_map(struct mapping *map, struct closure *fun)
{
extern void push_mapping(struct mapping *, bool_t);
struct mapping *r;
struct apair *p;
unsigned int i;
r = copy_mapping(map); /* copy entire mapping */
push_mapping(r, 0);
for (i = 0 ; i < r->size ; i++) {
for(p = r->pairs[i]; p; p = p->next) {
push_svalue(&p->val);
(void)call_var(1, fun);
assign_svalue(&p->val, sp); /* replace old value */
pop_stack(); /* and pop it */
}
}
sp--;
return r;
}
/* EFUN: filter (mapping part)
Runs all codomaing elements of an map through fun
and returns a mapping holding those elements that fun
returned non-zero for.
*/
struct mapping *
filter_map(struct mapping *map, struct closure *fun)
{
struct mapping *r;
struct apair **p;
unsigned int i;
r = copy_mapping(map);
for (i = 0; i < r->size; i++) {
for(p = &r->pairs[i]; *p;) {
push_svalue(&(*p)->val);
(void)call_var(1, fun);
if (sp->type == T_NUMBER && sp->u.number == 0) {
/* remove it from the list */
struct apair *ap = *p;
*p = (*p)->next; /* remove element, but don't move p */
free_svalue(&ap->arg);
free_svalue(&ap->val);
free(ap);
total_mapping_size -= sizeof(struct apair);
r->card--;
} else {
p = &(*p)->next; /* step to next */
}
pop_stack();
}
}
return r;
}