-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathslib_xtr.cc
633 lines (596 loc) · 17.7 KB
/
slib_xtr.cc
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*
* COPYRIGHT (c) 1988-1994 BY *
* PARADIGM ASSOCIATES INCORPORATED, CAMBRIDGE, MASSACHUSETTS. *
* See the source file SLIB.C for more information. *
Array-hacking code moved to another source file.
* Functions *not* used in Edinburgh Speech Tools
* arrays, hash tables,
*/
#include <cstdio>
#include <cstring>
#include <setjmp.h>
#include <cstdlib>
#include <cctype>
#include "siod.h"
#include "siodp.h"
using namespace std;
static LISP bashnum = NIL;
static LISP array_gc_relocate(LISP ptr)
{LISP nw;
if ((nw = heap) >= heap_end) gc_fatal_error();
heap = nw+1;
memcpy(nw,ptr,sizeof(struct obj));
return(nw);}
static void array_gc_scan(LISP ptr)
{long j;
if TYPEP(ptr,tc_lisp_array)
for(j=0;j < ptr->storage_as.lisp_array.dim; ++j)
ptr->storage_as.lisp_array.data[j] =
gc_relocate(ptr->storage_as.lisp_array.data[j]);}
static LISP array_gc_mark(LISP ptr)
{long j;
if TYPEP(ptr,tc_lisp_array)
for(j=0;j < ptr->storage_as.lisp_array.dim; ++j)
gc_mark(ptr->storage_as.lisp_array.data[j]);
return(NIL);}
static void array_gc_free(LISP ptr)
{switch (ptr->type)
{case tc_string:
wfree(ptr->storage_as.string.data);
break;
case tc_double_array:
wfree(ptr->storage_as.double_array.data);
break;
case tc_long_array:
wfree(ptr->storage_as.long_array.data);
break;
case tc_lisp_array:
wfree(ptr->storage_as.lisp_array.data);
break;}}
static int array_prin1(LISP ptr,FILE *f)
{
int j;
switch (ptr->type)
{case tc_string:
fput_st(f,"\"");
fput_st(f,ptr->storage_as.string.data);
fput_st(f,"\"");
break;
case tc_double_array:
fput_st(f,"#(");
for(j=0; j < ptr->storage_as.double_array.dim; ++j)
{sprintf(tkbuffer,"%g",ptr->storage_as.double_array.data[j]);
fput_st(f,tkbuffer);
if ((j + 1) < ptr->storage_as.double_array.dim)
fput_st(f," ");}
fput_st(f,")");
break;
case tc_long_array:
fput_st(f,"#(");
for(j=0; j < ptr->storage_as.long_array.dim; ++j)
{sprintf(tkbuffer,"%ld",ptr->storage_as.long_array.data[j]);
fput_st(f,tkbuffer);
if ((j + 1) < ptr->storage_as.long_array.dim)
fput_st(f," ");}
fput_st(f,")");
break;
case tc_lisp_array:
fput_st(f,"#(");
for(j=0; j < ptr->storage_as.lisp_array.dim; ++j)
{lprin1f(ptr->storage_as.lisp_array.data[j],f);
if ((j + 1) < ptr->storage_as.lisp_array.dim)
fput_st(f," ");}
fput_st(f,")");
break;
}
return 0;
}
static LISP aref1(LISP a,LISP i)
{long k;
if NFLONUMP(i) err("bad index to aref",i);
k = (long) FLONM(i);
if (k < 0) err("negative index to aref",i);
switch (a->type)
{case tc_string:
if (k >= a->storage_as.string.dim) err("index too large",i);
return(flocons((double) a->storage_as.string.data[k]));
case tc_double_array:
if (k >= a->storage_as.double_array.dim) err("index too large",i);
return(flocons(a->storage_as.double_array.data[k]));
case tc_long_array:
if (k >= a->storage_as.long_array.dim) err("index too large",i);
return(flocons(a->storage_as.long_array.data[k]));
case tc_lisp_array:
if (k >= a->storage_as.lisp_array.dim) err("index too large",i);
return(a->storage_as.lisp_array.data[k]);
default:
return(err("invalid argument to aref",a));}}
static void err1_aset1(LISP i)
{err("index to aset too large",i);}
static void err2_aset1(LISP v)
{err("bad value to store in array",v);}
static LISP aset1(LISP a,LISP i,LISP v)
{long k;
if NFLONUMP(i) err("bad index to aset",i);
k = (long) FLONM(i);
if (k < 0) err("negative index to aset",i);
switch (a->type)
{case tc_string:
if NFLONUMP(v) err2_aset1(v);
if (k >= a->storage_as.string.dim) err1_aset1(i);
a->storage_as.string.data[k] = (char) FLONM(v);
return(v);
case tc_double_array:
if NFLONUMP(v) err2_aset1(v);
if (k >= a->storage_as.double_array.dim) err1_aset1(i);
a->storage_as.double_array.data[k] = FLONM(v);
return(v);
case tc_long_array:
if NFLONUMP(v) err2_aset1(v);
if (k >= a->storage_as.long_array.dim) err1_aset1(i);
a->storage_as.long_array.data[k] = (long) FLONM(v);
return(v);
case tc_lisp_array:
if (k >= a->storage_as.lisp_array.dim) err1_aset1(i);
a->storage_as.lisp_array.data[k] = v;
return(v);
default:
return(err("invalid argument to aset",a));}}
static LISP cons_array(LISP dim,LISP kind)
{LISP a;
long flag,n,j;
if (NFLONUMP(dim) || (FLONM(dim) < 0))
return(err("bad dimension to cons-array",dim));
else
n = (long) FLONM(dim);
flag = no_interrupt(1);
a = cons(NIL,NIL);
if EQ(cintern("double"),kind)
{a->type = tc_double_array;
a->storage_as.double_array.dim = n;
a->storage_as.double_array.data = (double *) must_malloc(n *
sizeof(double));
for(j=0;j<n;++j) a->storage_as.double_array.data[j] = 0.0;}
else if EQ(cintern("long"),kind)
{a->type = tc_long_array;
a->storage_as.long_array.dim = n;
a->storage_as.long_array.data = (long *) must_malloc(n * sizeof(long));
for(j=0;j<n;++j) a->storage_as.long_array.data[j] = 0;}
else if EQ(cintern("string"),kind)
{a->type = tc_string;
a->storage_as.double_array.dim = n+1;
a->storage_as.string.data = (char *) must_malloc(n+1);
a->storage_as.string.data[n] = 0;
for(j=0;j<n;++j) a->storage_as.string.data[j] = ' ';}
else if (EQ(cintern("lisp"),kind) || NULLP(kind))
{a->type = tc_lisp_array;
a->storage_as.lisp_array.dim = n;
a->storage_as.lisp_array.data = (LISP *) must_malloc(n * sizeof(LISP));
for(j=0;j<n;++j) a->storage_as.lisp_array.data[j] = NIL;}
else
err("bad type of array",kind);
no_interrupt(flag);
return(a);}
#define HASH_COMBINE(_h1,_h2,_mod) ((((_h1) * 17 + 1) ^ (_h2)) % (_mod))
static long c_sxhash(LISP obj,long n)
{long hash;
unsigned char *s;
LISP tmp;
struct user_type_hooks *p;
STACK_CHECK(&obj);
INTERRUPT_CHECK();
switch TYPE(obj)
{case tc_nil:
return(0);
case tc_cons:
hash = c_sxhash(CAR(obj),n);
for(tmp=CDR(obj);CONSP(tmp);tmp=CDR(tmp))
hash = HASH_COMBINE(hash,c_sxhash(CAR(tmp),n),n);
hash = HASH_COMBINE(hash,c_sxhash(tmp,n),n);
return(hash);
case tc_symbol:
for(hash=0,s=(unsigned char *)PNAME(obj);*s;++s)
hash = HASH_COMBINE(hash,*s,n);
return(hash);
case tc_subr_0:
case tc_subr_1:
case tc_subr_2:
case tc_subr_3:
case tc_subr_4:
case tc_lsubr:
case tc_fsubr:
case tc_msubr:
for(hash=0,s=(unsigned char *) obj->storage_as.subr.name;*s;++s)
hash = HASH_COMBINE(hash,*s,n);
return(hash);
case tc_flonum:
return(((unsigned long)FLONM(obj)) % n);
default:
p = get_user_type_hooks(TYPE(obj));
if (p->c_sxhash)
return((*p->c_sxhash)(obj,n));
else
return(0);}}
static LISP sxhash(LISP obj,LISP n)
{return(flocons(c_sxhash(obj,FLONUMP(n) ? (long) FLONM(n) : 10000)));}
static LISP array_equal(LISP a,LISP b)
{long j,len;
switch(TYPE(a))
{case tc_string:
len = a->storage_as.string.dim;
if (len != b->storage_as.string.dim) return(NIL);
if (memcmp(a->storage_as.string.data,b->storage_as.string.data,len) == 0)
return(truth);
else
return(NIL);
case tc_long_array:
len = a->storage_as.long_array.dim;
if (len != b->storage_as.long_array.dim) return(NIL);
if (memcmp(a->storage_as.long_array.data,
b->storage_as.long_array.data,
len * sizeof(long)) == 0)
return(truth);
else
return(NIL);
case tc_double_array:
len = a->storage_as.double_array.dim;
if (len != b->storage_as.double_array.dim) return(NIL);
for(j=0;j<len;++j)
if (a->storage_as.double_array.data[j] !=
b->storage_as.double_array.data[j])
return(NIL);
return(truth);
case tc_lisp_array:
len = a->storage_as.lisp_array.dim;
if (len != b->storage_as.lisp_array.dim) return(NIL);
for(j=0;j<len;++j)
if NULLP(equal(a->storage_as.lisp_array.data[j],
b->storage_as.lisp_array.data[j]))
return(NIL);
return(truth);
default:
return(errswitch());}}
static long array_sxhash(LISP a,long n)
{long j,len,hash;
unsigned char *char_data;
unsigned long *long_data;
double *double_data;
switch(TYPE(a))
{case tc_string:
len = a->storage_as.string.dim;
for(j=0,hash=0,char_data=(unsigned char *)a->storage_as.string.data;
j < len;
++j,++char_data)
hash = HASH_COMBINE(hash,*char_data,n);
return(hash);
case tc_long_array:
len = a->storage_as.long_array.dim;
for(j=0,hash=0,long_data=(unsigned long *)a->storage_as.long_array.data;
j < len;
++j,++long_data)
hash = HASH_COMBINE(hash,*long_data % n,n);
return(hash);
case tc_double_array:
len = a->storage_as.double_array.dim;
for(j=0,hash=0,double_data=a->storage_as.double_array.data;
j < len;
++j,++double_data)
hash = HASH_COMBINE(hash,(unsigned long)*double_data % n,n);
return(hash);
case tc_lisp_array:
len = a->storage_as.lisp_array.dim;
for(j=0,hash=0; j < len; ++j)
hash = HASH_COMBINE(hash,
c_sxhash(a->storage_as.lisp_array.data[j],n),
n);
return(hash);
default:
errswitch();
return(0);}}
static long href_index(LISP table,LISP key)
{long index;
if NTYPEP(table,tc_lisp_array) err("not a hash table",table);
index = c_sxhash(key,table->storage_as.lisp_array.dim);
if ((index < 0) || (index >= table->storage_as.lisp_array.dim))
{err("sxhash inconsistency",table);
return(0);}
else
return(index);}
static LISP href(LISP table,LISP key)
{return(cdr(assoc(key,
table->storage_as.lisp_array.data[href_index(table,key)])));}
static LISP hset(LISP table,LISP key,LISP value)
{long index;
LISP cell,l;
index = href_index(table,key);
l = table->storage_as.lisp_array.data[index];
if NNULLP(cell = assoc(key,l))
return(setcdr(cell,value));
cell = cons(key,value);
table->storage_as.lisp_array.data[index] = cons(cell,l);
return(value);}
static LISP make_list(LISP x,LISP v)
{long n;
LISP l;
n = get_c_int(x);
l = NIL;
while(n > 0)
{l = cons(v,l); --n;}
return(l);}
static void put_long(long i,FILE *f)
{fwrite(&i,sizeof(long),1,f);}
static long get_long(FILE *f)
{
long i;
if (fread(&i,sizeof(long),1,f) == 1)
return i;
else
return -1;
}
static long fast_print_table(LISP obj,LISP table)
{FILE *f;
LISP ht,index;
f = get_c_file(car(table),(FILE *) NULL);
if NULLP(ht = car(cdr(table)))
return(1);
index = href(ht,obj);
if NNULLP(index)
{putc(FO_fetch,f);
put_long(get_c_int(index),f);
return(0);}
if NULLP(index = car(cdr(cdr(table))))
return(1);
hset(ht,obj,index);
FLONM(bashnum) = 1.0;
setcar(cdr(cdr(table)),flocons(get_c_int(bashnum)+get_c_int(index)));
putc(FO_store,f);
put_long(get_c_int(index),f);
return(1);}
static LISP fast_print(LISP obj,LISP table)
{FILE *f;
long len;
LISP tmp;
struct user_type_hooks *p;
STACK_CHECK(&obj);
f = get_c_file(car(table),(FILE *) NULL);
switch(TYPE(obj))
{case tc_nil:
putc(tc_nil,f);
return(NIL);
case tc_cons:
for(len=0,tmp=obj;CONSP(tmp);tmp=CDR(tmp)) {INTERRUPT_CHECK();++len;}
if (len == 1)
{putc(tc_cons,f);
fast_print(car(obj),table);
fast_print(cdr(obj),table);}
else if NULLP(tmp)
{putc(FO_list,f);
put_long(len,f);
for(tmp=obj;CONSP(tmp);tmp=CDR(tmp))
fast_print(CAR(tmp),table);}
else
{putc(FO_listd,f);
put_long(len,f);
for(tmp=obj;CONSP(tmp);tmp=CDR(tmp))
fast_print(CAR(tmp),table);
fast_print(tmp,table);}
return(NIL);
case tc_flonum:
putc(tc_flonum,f);
fwrite(&obj->storage_as.flonum.data,
sizeof(obj->storage_as.flonum.data),
1,
f);
return(NIL);
case tc_symbol:
if (fast_print_table(obj,table))
{putc(tc_symbol,f);
len = strlen(PNAME(obj));
if (len >= TKBUFFERN)
err("symbol name too long",obj);
put_long(len,f);
fwrite(PNAME(obj),len,1,f);
return(truth);}
else
return(NIL);
default:
p = get_user_type_hooks(TYPE(obj));
if (p->fast_print)
return((*p->fast_print)(obj,table));
else
return(err("cannot fast-print",obj));}}
static LISP fast_read(LISP table)
{FILE *f;
LISP tmp,l;
struct user_type_hooks *p;
int c;
long len;
f = get_c_file(car(table),(FILE *) NULL);
c = getc(f);
if (c == EOF) return(table);
switch(c)
{case FO_fetch:
len = get_long(f);
FLONM(bashnum) = len;
return(href(car(cdr(table)),bashnum));
case FO_store:
len = get_long(f);
tmp = fast_read(table);
hset(car(cdr(table)),flocons(len),tmp);
return(tmp);
case tc_nil:
return(NIL);
case tc_cons:
tmp = fast_read(table);
return(cons(tmp,fast_read(table)));
case FO_list:
case FO_listd:
len = get_long(f);
FLONM(bashnum) = len;
l = make_list(bashnum,NIL);
tmp = l;
while(len > 1)
{CAR(tmp) = fast_read(table);
tmp = CDR(tmp);
--len;}
CAR(tmp) = fast_read(table);
if (c == FO_listd)
CDR(tmp) = fast_read(table);
return(l);
case tc_flonum:
tmp = newcell(tc_flonum);
if (fread(&tmp->storage_as.flonum.data,
sizeof(tmp->storage_as.flonum.data),
1,
f) == 1)
return(tmp);
else
return NIL;
case tc_symbol:
len = get_long(f);
if (len >= TKBUFFERN)
err("symbol name too long",NIL);
if (fread(tkbuffer,len,1,f) == 1)
{
tkbuffer[len] = 0;
return(rintern(tkbuffer));
}
else
err("failed to write array",NIL);
default:
p = get_user_type_hooks(c);
if (p->fast_read)
return(*p->fast_read)(c,table);
else
return(err("unknown fast-read opcode",flocons(c)));}}
static LISP array_fast_print(LISP ptr,LISP table)
{int j,len;
FILE *f;
f = get_c_file(car(table),(FILE *) NULL);
switch (ptr->type)
{case tc_string:
putc(tc_string,f);
len = ptr->storage_as.string.dim;
put_long(len,f);
fwrite(ptr->storage_as.string.data,len,1,f);
return(NIL);
case tc_double_array:
putc(tc_double_array,f);
len = ptr->storage_as.double_array.dim * sizeof(double);
put_long(len,f);
fwrite(ptr->storage_as.double_array.data,len,1,f);
return(NIL);
case tc_long_array:
putc(tc_long_array,f);
len = ptr->storage_as.long_array.dim * sizeof(long);
put_long(len,f);
fwrite(ptr->storage_as.long_array.data,len,1,f);
return(NIL);
case tc_lisp_array:
putc(tc_lisp_array,f);
len = ptr->storage_as.lisp_array.dim;
put_long(len,f);
for(j=0; j < len; ++j)
fast_print(ptr->storage_as.lisp_array.data[j],table);
return(NIL);
default:
return(errswitch());}}
static LISP array_fast_read(int code,LISP table)
{long j,len,iflag;
FILE *f;
LISP ptr;
f = get_c_file(car(table),(FILE *) NULL);
switch (code)
{case tc_string:
len = get_long(f);
ptr = strcons(len,NULL);
if (fread(ptr->storage_as.string.data,len,1,f) == 1)
ptr->storage_as.string.data[len] = 0;
return(ptr);
case tc_double_array:
len = get_long(f);
iflag = no_interrupt(1);
ptr = newcell(tc_double_array);
ptr->storage_as.double_array.dim = len;
ptr->storage_as.double_array.data =
(double *) must_malloc(len * sizeof(double));
if (fread(ptr->storage_as.double_array.data,sizeof(double),len,f) != (unsigned)len)
return NULL;
no_interrupt(iflag);
return(ptr);
case tc_long_array:
len = get_long(f);
iflag = no_interrupt(1);
ptr = newcell(tc_long_array);
ptr->storage_as.long_array.dim = len;
ptr->storage_as.long_array.data =
(long *) must_malloc(len * sizeof(long));
if (fread(ptr->storage_as.long_array.data,sizeof(long),len,f) != (unsigned)len)
return 0;
no_interrupt(iflag);
return(ptr);
case tc_lisp_array:
len = get_long(f);
FLONM(bashnum) = len;
ptr = cons_array(bashnum,NIL);
for(j=0; j < len; ++j)
ptr->storage_as.lisp_array.data[j] = fast_read(table);
return(ptr);
default:
return(errswitch());}}
static void init_storage_xtr1(long type)
{long j;
struct user_type_hooks *p;
set_gc_hooks(type,
FALSE,
array_gc_relocate,
array_gc_mark,
array_gc_scan,
array_gc_free,
NULL,
&j);
set_print_hooks(type,array_prin1, NULL);
p = get_user_type_hooks(type);
p->fast_print = array_fast_print;
p->fast_read = array_fast_read;
p->equal = array_equal;
p->c_sxhash = array_sxhash;}
static void init_storage_xtr(void)
{gc_protect(&bashnum);
bashnum = newcell(tc_flonum);
init_storage_xtr1(tc_string);
init_storage_xtr1(tc_double_array);
init_storage_xtr1(tc_long_array);
init_storage_xtr1(tc_lisp_array);}
void init_subrs_xtr(void)
{
init_storage_xtr();
init_subr_2("aref",aref1,
"(aref ARRAY INDEX)\n\
Return ARRAY[INDEX]");
init_subr_3("aset",aset1,
"(aset ARRAY INDEX VAL)\n\
Set ARRAY[INDEX] = VAL");
init_subr_2("cons-array",cons_array,
"(cons-array DIM KIND)\n\
Construct array of size DIM and type KIND. Where KIND may be one of\n\
double, long, string or lisp.");
init_subr_2("sxhash",sxhash,
"(sxhash OBJ N)\n\
Return hashing value for OBJ, in range n.");
init_subr_2("href",href,
"(href TABLE KEY)\n\
Return value in hash table TABLE with KEY.");
init_subr_3("hset",hset,
"(hset TABLE KEY VALUE)\n\
Set hash table TABLE KEY to VALUE.");
init_subr_1("fast-read",fast_read,
"(fast-read TABLE)\n\
");
init_subr_2("fast-print",fast_print,
"(fast-print P TABLE)\n\
");
init_subr_2("make-list",make_list,
"(make-list SIZE VALUE)\n\
Return list of SIZE with each member VALUE.");
}