8
#define unless(C) if(!(C))
15
symbol *p = (symbol *) (HEAD + (char *) malloc(HEAD + (CREATE_SIZE + 1) * sizeof(symbol)));
17
if (p == (symbol *) (HEAD))
19
CAPACITY(p) = CREATE_SIZE;
20
SET_SIZE(p, CREATE_SIZE);
27
free((char *) p - HEAD);
31
in_grouping(struct SN_env * z, unsigned char *s, int min, int max)
39
(ch > max || (ch -= min) < 0 ||
40
(s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
48
in_grouping_b(struct SN_env * z, unsigned char *s, int min, int max)
53
int ch = z->p[z->c - 1];
56
(ch > max || (ch -= min) < 0 ||
57
(s[ch >> 3] & (0X1 << (ch & 0X7))) == 0)
65
out_grouping(struct SN_env * z, unsigned char *s, int min, int max)
73
(ch > max || (ch -= min) < 0 ||
74
(s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) return 0;
81
out_grouping_b(struct SN_env * z, unsigned char *s, int min, int max)
86
int ch = z->p[z->c - 1];
89
(ch > max || (ch -= min) < 0 ||
90
(s[ch >> 3] & (0X1 << (ch & 0X7))) == 0) return 0;
98
in_range(struct SN_env * z, int min, int max)
106
(ch > max || ch < min)
114
in_range_b(struct SN_env * z, int min, int max)
119
int ch = z->p[z->c - 1];
122
(ch > max || ch < min)
130
out_range(struct SN_env * z, int min, int max)
138
(ch > max || ch < min) return 0;
145
out_range_b(struct SN_env * z, int min, int max)
150
int ch = z->p[z->c - 1];
153
(ch > max || ch < min) return 0;
160
eq_s(struct SN_env * z, int s_size, symbol * s)
162
if (z->l - z->c < s_size ||
163
memcmp(z->p + z->c, s, s_size * sizeof(symbol)) != 0)
170
eq_s_b(struct SN_env * z, int s_size, symbol * s)
172
if (z->c - z->lb < s_size ||
173
memcmp(z->p + z->c - s_size, s, s_size * sizeof(symbol)) != 0)
180
eq_v(struct SN_env * z, symbol * p)
182
return eq_s(z, SIZE(p), p);
186
eq_v_b(struct SN_env * z, symbol * p)
188
return eq_s_b(z, SIZE(p), p);
192
find_among(struct SN_env * z, struct among * v, int v_size)
199
symbol *q = z->p + c;
206
int first_key_inspected = 0;
210
int k = i + ((j - i) >> 1);
212
int common = common_i < common_j ? common_i : common_j; /* smaller */
218
for (i = common; i < w->s_size; i++)
225
diff = q[common] - w->s[i];
244
break; /* v->s has been inspected */
246
break; /* only one item in v */
249
* - but now we need to go round once more to get v->s
250
* inspected. This looks messy, but is actually the optimal
254
if (first_key_inspected)
256
first_key_inspected = 1;
262
if (common_i >= w->s_size)
264
z->c = c + w->s_size;
265
if (w->function == 0)
268
int res = w->function(z);
270
z->c = c + w->s_size;
281
/* find_among_b is for backwards processing. Same comments apply */
284
find_among_b(struct SN_env * z, struct among * v, int v_size)
291
symbol *q = z->p + c - 1;
298
int first_key_inspected = 0;
302
int k = i + ((j - i) >> 1);
304
int common = common_i < common_j ? common_i : common_j;
310
for (i = w->s_size - 1 - common; i >= 0; i--)
312
if (c - common == lb)
317
diff = q[-common] - w->s[i];
339
if (first_key_inspected)
341
first_key_inspected = 1;
347
if (common_i >= w->s_size)
349
z->c = c - w->s_size;
350
if (w->function == 0)
353
int res = w->function(z);
355
z->c = c - w->s_size;
368
increase_size(symbol * p, int n)
370
int new_size = n + 20;
371
symbol *q = (symbol *) (HEAD + (char *) malloc(HEAD + (new_size + 1) * sizeof(symbol)));
373
CAPACITY(q) = new_size;
374
memmove(q, p, CAPACITY(p) * sizeof(symbol));
379
/* to replace symbols between c_bra and c_ket in z->p by the
384
replace_s(struct SN_env * z, int c_bra, int c_ket, int s_size, const symbol * s)
386
int adjustment = s_size - (c_ket - c_bra);
387
int len = SIZE(z->p);
391
if (adjustment + len > CAPACITY(z->p))
392
z->p = increase_size(z->p, adjustment + len);
393
memmove(z->p + c_ket + adjustment, z->p + c_ket, (len - c_ket) * sizeof(symbol));
394
SET_SIZE(z->p, adjustment + len);
398
else if (z->c > c_bra)
401
unless(s_size == 0) memmove(z->p + c_bra, s, s_size * sizeof(symbol));
406
slice_check(struct SN_env * z)
411
z->l <= SIZE(z->p))) /* this line could be removed */
413
fprintf(stderr, "faulty slice operation:\n");
420
slice_from_s(struct SN_env * z, int s_size, symbol * s)
423
replace_s(z, z->bra, z->ket, s_size, s);
427
slice_from_v(struct SN_env * z, symbol * p)
429
slice_from_s(z, SIZE(p), p);
433
slice_del(struct SN_env * z)
435
slice_from_s(z, 0, NULL);
439
insert_s(struct SN_env * z, int bra, int ket, int s_size, symbol * s)
441
int adjustment = replace_s(z, bra, ket, s_size, s);
444
z->bra += adjustment;
446
z->ket += adjustment;
450
insert_v(struct SN_env * z, int bra, int ket, symbol * p)
452
int adjustment = replace_s(z, bra, ket, SIZE(p), p);
455
z->bra += adjustment;
457
z->ket += adjustment;
461
slice_to(struct SN_env * z, symbol * p)
465
int len = z->ket - z->bra;
467
if (CAPACITY(p) < len)
468
p = increase_size(p, len);
469
memmove(p, z->p + z->bra, len * sizeof(symbol));
476
assign_to(struct SN_env * z, symbol * p)
480
if (CAPACITY(p) < len)
481
p = increase_size(p, len);
482
memmove(p, z->p, len * sizeof(symbol));
488
debug(struct SN_env * z, int number, int line_count)
491
int limit = SIZE(z->p);
493
/* if (number >= 0) printf("%3d (line %4d): '", number, line_count); */
495
printf("%3d (line %4d): [%d]'", number, line_count, limit);
496
for (i = 0; i <= limit; i++)