summaryrefslogtreecommitdiffstats
path: root/generic/regc_color.c
blob: 4a8a87c98a3c3cb348c0f3c4ab3e0c1758c7fb2e (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
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
/*
 * colorings of characters
 * This file is #included by regcomp.c.
 *
 * Note that there are some incestuous relationships between this code and
 * NFA arc maintenance, which perhaps ought to be cleaned up sometime.
 */



/*
 * If this declaration draws a complaint about a negative array size,
 * then CHRBITS is defined incorrectly for the chr type.
 */
static char isCHRBITSright[NEGIFNOT(sizeof(chr)*CHAR_BIT == CHRBITS)];



#define	CISERR()	VISERR(cm->v)
#define	CERR(e)		VERR(cm->v, (e))



/*
 - newcm - get new colormap
 ^ static struct colormap *newcm(struct vars *);
 */
static struct colormap *	/* NULL for allocation failure */
newcm(v)
struct vars *v;
{
	struct colormap *cm;
	int i;
	int j;
	union tree *t;
	union tree *nextt;
	struct colordesc *cd;

	cm = (struct colormap *)MALLOC(sizeof(struct colormap));
	if (cm == NULL) {
		ERR(REG_ESPACE);
		return NULL;
	}
	cm->magic = CMMAGIC;
	cm->v = v;
	cm->rest = WHITE;
	cm->filled = 0;

	cm->ncds = NINLINECDS;
	cm->cd = cm->cds;
	for (cd = cm->cd; cd < CDEND(cm); cd++) {
		cd->nchrs = 0;
		cd->sub = NOSUB;
		cd->arcs = NULL;
		cd->flags = 0;
	}
	cm->cd[WHITE].nchrs = CHR_MAX - CHR_MIN + 1;

	/* treetop starts as NULLs if there are lower levels */
	t = cm->tree;
	if (NBYTS > 1)
		for (i = BYTTAB-1; i >= 0; i--)
			t->tptr[i] = NULL;
	/* if no lower levels, treetop and last fill block are the same */

	/* fill blocks point to next fill block... */
	for (t = &cm->tree[1], j = NBYTS-2; j > 0; t = nextt, j--) {
		nextt = t + 1;
		for (i = BYTTAB-1; i >= 0; i--)
			t->tptr[i] = t + 1;
	}
	/* ...except last which is solid white */
	t = &cm->tree[NBYTS-1];
	for (i = BYTTAB-1; i >= 0; i--)
		t->tcolor[i] = WHITE;


	return cm;
}

/*
 - freecm - free a colormap
 ^ static VOID freecm(struct colormap *);
 */
static VOID
freecm(cm)
struct colormap *cm;
{
	cm->magic = 0;
	if (NBYTS > 1)
		cmtreefree(cm, cm->tree, 0);
	if (cm->cd != cm->cds)
		FREE(cm->cd);
	FREE(cm);
}

/*
 - cmtreefree - free a non-terminal part of a colormap tree
 ^ static VOID cmtreefree(struct colormap *, union tree *, int);
 */
static VOID
cmtreefree(cm, tree, level)
struct colormap *cm;
union tree *tree;
int level;			/* level number (top == 0) of this block */
{
	int i;
	union tree *t;
	union tree *fillt = &cm->tree[level+1];

	assert(level < NBYTS-1);	/* this level has pointers */
	for (i = BYTTAB-1; i >= 0; i--) {
		t = tree->tptr[i];
		if (t != NULL && t != fillt) {
			if (level < NBYTS-2)	/* more pointer blocks below */
				cmtreefree(cm, t, level+1);
			FREE(t);
		}
	}
}

/*
 - fillcm - fill in a colormap, so no NULLs remain
 * The point of this is that the tree traversal can then be a fixed set
 * of table lookups with no conditional branching.  It might be better
 * to do reallocation for a more compacted structure, on the order of
 * what's done for NFAs, but the colormap can be quite large and a total
 * rebuild of it could be costly.
 ^ static VOID fillcm(struct colormap *);
 */
static VOID
fillcm(cm)
struct colormap *cm;
{
	if (!cm->filled && NBYTS > 1)
		cmtreefill(cm, cm->tree, 0);
	cm->filled = 1;
}

/*
 - cmtreefill - fill a non-terminal part of a colormap tree
 ^ static VOID cmtreefill(struct colormap *, union tree *, int);
 */
static VOID
cmtreefill(cm, tree, level)
struct colormap *cm;
union tree *tree;
int level;			/* level number (top == 0) of this block */
{
	int i;
	union tree *t;
	union tree *fillt = &cm->tree[level+1];

	assert(level < NBYTS-1);	/* this level has pointers */
	for (i = BYTTAB-1; i >= 0; i--) {
		t = tree->tptr[i];
		if (t == fillt)			/* oops */
			{}
		else if (t == NULL)
			tree->tptr[i] = fillt;
		else if (level < NBYTS-2)	/* more pointer blocks below */
			cmtreefill(cm, t, level+1);
	}
}

/*
 - getcolor - get the color of a character from a colormap
 ^ static color getcolor(struct colormap *, pchr);
 */
static color
getcolor(cm, c)
struct colormap *cm;
pchr c;
{
	uchr uc = c;
	int shift;
	int b;
	union tree *t;

	assert(cm->magic == CMMAGIC);

	t = cm->tree;
	for (shift = BYTBITS * (NBYTS - 1); t != NULL; shift -= BYTBITS) {
		b = (uc >> shift) & BYTMASK;
		if (shift == 0)		/* reached the bottom */
			return t->tcolor[b];
		t = t->tptr[b];
	}

	/* we fell off an incomplete part of the tree */
	assert(!cm->filled);
	return cm->rest;
}

/*
 - setcolor - set the color of a character in a colormap
 ^ static color setcolor(struct colormap *, pchr, pcolor);
 */
static color			/* previous color */
setcolor(cm, c, co)
struct colormap *cm;
pchr c;
pcolor co;
{
	uchr uc = c;
	int shift;
	int i;
	int b;
	int bottom;
	union tree *t;
	union tree *lastt;
	color prev;

	assert(cm->magic == CMMAGIC);
	if (CISERR() || co == COLORLESS)
		return COLORLESS;

	t = cm->tree;
	for (shift = BYTBITS * (NBYTS - 1); shift > 0; shift -= BYTBITS) {
		b = (uc >> shift) & BYTMASK;
		lastt = t;
		t = t->tptr[b];
		if (t == NULL) {	/* fell off an incomplete part */
			bottom = (shift <= BYTBITS) ? 1 : 0;
			t = (union tree *)MALLOC((bottom) ?
				sizeof(struct colors) : sizeof(struct ptrs));
			if (t == NULL) {
				CERR(REG_ESPACE);
				return COLORLESS;
			}
			if (bottom)
				for (i = BYTTAB-1; i >= 0; i--)
					t->tcolor[i] = cm->rest;
			else
				for (i = BYTTAB-1; i >= 0; i--)
					t->tptr[i] = NULL;
			lastt->tptr[b] = t;
		}
	}
	assert(shift == 0 && t != NULL);	/* we hit bottom; it's there */

	b = uc & BYTMASK;
	prev = t->tcolor[b];
	t->tcolor[b] = (color)co;
	return prev;
}

/*
 - maxcolor - report largest color number in use
 ^ static color maxcolor(struct colormap *);
 */
static color
maxcolor(cm)
struct colormap *cm;
{
	struct colordesc *cd;
	struct colordesc *end;
	struct colordesc *lastused;

	if (CISERR())
		return COLORLESS;

	lastused = NULL;
	end = CDEND(cm);
	for (cd = cm->cd; cd < end; cd++)
		if (!UNUSEDCOLOR(cd))
			lastused = cd;
	assert(lastused != NULL);
	return (color)(lastused - cm->cd);
}

/*
 - newcolor - find a new color (must be subject of setcolor at once)
 * Beware:  may relocate the colordescs.
 ^ static color newcolor(struct colormap *);
 */
static color			/* COLORLESS for error */
newcolor(cm)
struct colormap *cm;
{
	struct colordesc *cd;
	struct colordesc *end;
	struct colordesc *firstnew;
	size_t n;

	if (CISERR())
		return COLORLESS;

	end = CDEND(cm);
	for (cd = cm->cd; cd < end; cd++)
		if (UNUSEDCOLOR(cd)) {
			assert(cd->arcs == NULL);
			return (color)(cd - cm->cd);
		}

	/* oops, must allocate more */
	n = cm->ncds * 2;
	if (cm->cd == cm->cds) {
		cd = (struct colordesc *)MALLOC(sizeof(struct colordesc) * n);
		if (cd != NULL)
			memcpy(VS(cd), VS(cm->cds), cm->ncds *
						sizeof(struct colordesc));
	} else {
		cd = (struct colordesc *)REALLOC(cm->cd,
						n * sizeof(struct colordesc));
	}
	if (cd == NULL) {
		CERR(REG_ESPACE);
		return COLORLESS;
	}
	cm->cd = cd;
	firstnew = CDEND(cm);
	cm->ncds = n;
	end = CDEND(cm);
	for (cd = firstnew; cd < end; cd++) {
		cd->nchrs = 0;
		cd->sub = NOSUB;
		cd->arcs = NULL;
		cd->flags = 0;
	}
	assert(firstnew < CDEND(cm) && UNUSEDCOLOR(firstnew));
	return (color)(firstnew - cm->cd);
}

/*
 - pseudocolor - allocate a false color, to be managed by other means
 ^ static color pseudocolor(struct colormap *);
 */
static color
pseudocolor(cm)
struct colormap *cm;
{
	color co;

	co = newcolor(cm);
	if (CISERR())
		return COLORLESS;
	cm->cd[co].nchrs = 1;
	cm->cd[co].flags = PSEUDO;
	return co;
}

/*
 - subcolor - allocate a new subcolor (if necessary) to this chr
 ^ static color subcolor(struct colormap *, pchr c);
 */
static color
subcolor(cm, c)
struct colormap *cm;
pchr c;
{
	color co;			/* current color of c */
	color sco;			/* new subcolor */

	co = getcolor(cm, c);
	sco = cm->cd[co].sub;
	if (sco == NOSUB) {		/* must create subcolor */
		if (cm->cd[co].nchrs == 1)	/* shortcut */
			return co;
		sco = newcolor(cm);
		if (sco == COLORLESS)
			return COLORLESS;
		cm->cd[co].sub = sco;
		cm->cd[sco].sub = sco;	/* self-referential subcolor ptr */
	}

	if (co == sco)		/* repeated character */
		return co;	/* no further action needed */
	cm->cd[co].nchrs--;
	cm->cd[sco].nchrs++;
	setcolor(cm, c, sco);
	return sco;
}

/*
 - okcolors - promote subcolors to full colors
 ^ static VOID okcolors(struct nfa *, struct colormap *);
 */
static VOID
okcolors(nfa, cm)
struct nfa *nfa;
struct colormap *cm;
{
	struct colordesc *cd;
	struct colordesc *end = CDEND(cm);
	struct colordesc *scd;
	struct arc *a;
	color co;
	color sco;

	for (cd = cm->cd, co = 0; cd < end; cd++, co++) {
		sco = cd->sub;
		if (sco == NOSUB) {
			/* has no subcolor, no further action */
		} else if (sco == co) {
			/* is subcolor, let parent deal with it */
		} else if (cd->nchrs == 0) {
			/* parent empty, its arcs change color to subcolor */
			cd->sub = NOSUB;
			scd = &cm->cd[sco];
			assert(scd->nchrs > 0);
			assert(scd->sub == sco);
			scd->sub = NOSUB;
			while ((a = cd->arcs) != NULL) {
				assert(a->co == co);
				/* uncolorchain(cm, a); */
				cd->arcs = a->colorchain;
				a->co = sco;
				/* colorchain(cm, a); */
				a->colorchain = scd->arcs;
				scd->arcs = a;
			}
		} else {
			/* parent's arcs must gain parallel subcolor arcs */
			cd->sub = NOSUB;
			scd = &cm->cd[sco];
			assert(scd->nchrs > 0);
			assert(scd->sub == sco);
			scd->sub = NOSUB;
			for (a = cd->arcs; a != NULL; a = a->colorchain) {
				assert(a->co == co);
				newarc(nfa, a->type, sco, a->from, a->to);
			}
		}
	}
}

/*
 - colorchain - add this arc to the color chain of its color
 ^ static VOID colorchain(struct colormap *, struct arc *);
 */
static VOID
colorchain(cm, a)
struct colormap *cm;
struct arc *a;
{
	struct colordesc *cd = &cm->cd[a->co];

	a->colorchain = cd->arcs;
	cd->arcs = a;
}

/*
 - uncolorchain - delete this arc from the color chain of its color
 ^ static VOID uncolorchain(struct colormap *, struct arc *);
 */
static VOID
uncolorchain(cm, a)
struct colormap *cm;
struct arc *a;
{
	struct colordesc *cd = &cm->cd[a->co];
	struct arc *aa;

	aa = cd->arcs;
	if (aa == a)		/* easy case */
		cd->arcs = a->colorchain;
	else {
		for (; aa != NULL && aa->colorchain != a; aa = aa->colorchain)
			continue;
		assert(aa != NULL);
		aa->colorchain = a->colorchain;
	}
	a->colorchain = NULL;	/* paranoia */
}

/*
 - singleton - is this character in its own color?
 ^ static int singleton(struct colormap *, pchr c);
 */
static int			/* predicate */
singleton(cm, c)
struct colormap *cm;
pchr c;
{
	color co;			/* color of c */

	co = getcolor(cm, c);
	if (cm->cd[co].nchrs == 1 && cm->cd[co].sub == NOSUB)
		return 1;
	return 0;
}

/*
 - rainbow - add arcs of all full colors (but one) between specified states
 ^ static VOID rainbow(struct nfa *, struct colormap *, int, pcolor,
 ^ 	struct state *, struct state *);
 */
static VOID
rainbow(nfa, cm, type, but, from, to)
struct nfa *nfa;
struct colormap *cm;
int type;
pcolor but;			/* COLORLESS if no exceptions */
struct state *from;
struct state *to;
{
	struct colordesc *cd;
	struct colordesc *end = CDEND(cm);
	color co;

	for (cd = cm->cd, co = 0; cd < end && !CISERR(); cd++, co++)
		if (!UNUSEDCOLOR(cd) && cd->sub != co && co != but &&
							!(cd->flags&PSEUDO))
			newarc(nfa, type, co, from, to);
}

/*
 - colorcomplement - add arcs of complementary colors
 * The calling sequence ought to be reconciled with cloneouts().
 ^ static VOID colorcomplement(struct nfa *, struct colormap *, int,
 ^ 	struct state *, struct state *, struct state *);
 */
static VOID
colorcomplement(nfa, cm, type, of, from, to)
struct nfa *nfa;
struct colormap *cm;
int type;
struct state *of;		/* complements of this guy's PLAIN outarcs */
struct state *from;
struct state *to;
{
	struct colordesc *cd;
	struct colordesc *end = CDEND(cm);
	color co;

	assert(of != from);
	for (cd = cm->cd, co = 0; cd < end && !CISERR(); cd++, co++)
		if (!UNUSEDCOLOR(cd) && !(cd->flags&PSEUDO))
			if (findarc(of, PLAIN, co) == NULL)
				newarc(nfa, type, co, from, to);
}



#ifdef REG_DEBUG

/*
 - dumpcolors - debugging output
 ^ static VOID dumpcolors(struct colormap *, FILE *);
 */
static VOID
dumpcolors(cm, f)
struct colormap *cm;
FILE *f;
{
	struct colordesc *cd;
	struct colordesc *end;
	color co;
	chr c;

	if (cm->filled) {
		fprintf(f, "filled\n");
		if (NBYTS > 1)
			fillcheck(cm, cm->tree, 0, f);
	}
	end = CDEND(cm);
	for (cd = cm->cd + 1, co = 1; cd < end; cd++, co++)	/* skip 0 */
		if (cd->nchrs > 0) {
			if (cd->flags&PSEUDO)
				fprintf(f, "#%2ld(ps): ", (long)co);
			else
				fprintf(f, "#%2ld(%2d): ", (long)co, cd->nchrs);
			for (c = CHR_MIN; c < CHR_MAX; c++)
				if (getcolor(cm, c) == co)
					dumpchr(c, f);
			assert(c == CHR_MAX);
			if (getcolor(cm, c) == co)
				dumpchr(c, f);
			fprintf(f, "\n");
		}
}

/*
 - fillcheck - check proper filling of a tree
 ^ static VOID fillcheck(struct colormap *, union tree *, int, FILE *);
 */
static VOID
fillcheck(cm, tree, level, f)
struct colormap *cm;
union tree *tree;
int level;			/* level number (top == 0) of this block */
FILE *f;
{
	int i;
	union tree *t;
	union tree *fillt = &cm->tree[level+1];

	assert(level < NBYTS-1);	/* this level has pointers */
	for (i = BYTTAB-1; i >= 0; i--) {
		t = tree->tptr[i];
		if (t == NULL)
			fprintf(f, "NULL found in filled tree!\n");
		else if (t == fillt)
			{}
		else if (level < NBYTS-2)	/* more pointer blocks below */
			fillcheck(cm, t, level+1, f);
	}
}

/*
 - dumpchr - print a chr
 * Kind of char-centric but works well enough for debug use.
 ^ static VOID dumpchr(pchr, FILE *);
 */
static VOID
dumpchr(c, f)
pchr c;
FILE *f;
{
	if (c == '\\')
		fprintf(f, "\\\\");
	else if (c > ' ' && c <= '~')
		putc((char)c, f);
	else
		fprintf(f, "\\0%lo", (long)c);
}

#endif				/* ifdef REG_DEBUG */