1 /***** spin: pangen4.h *****/
3 /* Copyright (c) 1997-2003 by Lucent Technologies, Bell Laboratories. */
4 /* All Rights Reserved. This software is for educational purposes only. */
5 /* No guarantee whatsoever is expressed or implied by the distribution of */
6 /* this code. Permission is given to distribute this code provided that */
7 /* this introductory message is not removed and no monies are exchanged. */
8 /* Software written by Gerard J. Holzmann. For tool documentation see: */
9 /* http://spinroot.com/ */
10 /* Send all bug-reports and/or questions to: bugs@spinroot.com */
12 /* The DFA code below was written by Anuj Puri and Gerard J. Holzmann in */
13 /* May 1997, and was inspired by earlier work on data compression using */
14 /* sharing tree data structures and graph-encoded sets by J-Ch. Gregoire */
15 /* (INRS Telecom, Quebec, Canada) and D.Zampunieris (Univ.Namur, Belgium) */
17 /* The splay routine code included here is based on the public domain */
18 /* version written by D. Sleator <sleator@cs.cmu.edu> in 1992. */
20 static char *Dfa
[] = {
24 "#define uchar unsigned char",
26 "#define ulong unsigned long",
27 "#define ushort unsigned short",
30 "#define HASH(y,n) (n)*(((long)y))",
31 "#define INRANGE(e,h) ((h>=e->From && h<=e->To)||(e->s==1 && e->S==h))",
33 "extern char *emalloc(unsigned long); /* imported routine */",
34 "extern void dfa_init(ushort); /* 4 exported routines */",
35 "extern int dfa_member(ulong);",
36 "extern int dfa_store(uchar *);",
37 "extern void dfa_stats(void);",
39 "typedef struct Edge {",
40 " uchar From, To; /* max range 0..255 */",
41 " uchar s, S; /* if s=1, S is singleton */",
42 " struct Vertex *Dst;",
46 "typedef struct Vertex {",
47 " ulong key, num; /* key for splay tree, nr incoming edges */",
48 " uchar from[2], to[2]; /* in-node predefined edge info */",
49 " struct Vertex *dst[2];/* most nodes have 2 or more edges */",
50 " struct Edge *Succ; /* in case there are more edges */",
51 " struct Vertex *lnk, *left, *right; /* splay tree plumbing */",
54 "static Edge *free_edges;",
55 "static Vertex *free_vertices;",
56 "static Vertex **layers; /* one splay tree of nodes per layer */",
57 "static Vertex **path; /* run of word in the DFA */",
58 "static Vertex *R, *F, *NF; /* Root, Final, Not-Final */",
59 "static uchar *word, *lastword;/* string, and last string inserted */",
60 "static int dfa_depth, iv=0, nv=0, pfrst=0, Tally;",
62 "static void insert_it(Vertex *, int); /* splay-tree code */",
63 "static void delete_it(Vertex *, int);",
64 "static Vertex *find_it(Vertex *, Vertex *, uchar, int);",
67 "recyc_edges(Edge *e)",
70 " recyc_edges(e->Nxt);",
71 " e->Nxt = free_edges;",
76 "new_edge(Vertex *dst)",
81 " free_edges = e->Nxt;",
82 " e->From = e->To = e->s = e->S = 0;",
83 " e->Nxt = (Edge *) 0;",
85 " e = (Edge *) emalloc(sizeof(Edge));",
92 "recyc_vertex(Vertex *v)",
94 " recyc_edges(v->Succ);",
95 " v->Succ = (Edge *) free_vertices;",
96 " free_vertices = v;",
104 " if (free_vertices)",
105 " { v = free_vertices;",
106 " free_vertices = (Vertex *) v->Succ;",
107 " v->Succ = (Edge *) 0;",
110 " v = (Vertex *) emalloc(sizeof(Vertex));",
117 "allDelta(Vertex *v, int n)",
118 "{ Vertex *dst = new_vertex();",
129 "insert_edge(Vertex *v, Edge *e)",
130 "{ /* put new edge first */",
132 " { v->dst[0] = e->Dst;",
133 " v->from[0] = e->From;",
134 " v->to[0] = e->To;",
139 " { v->from[1] = v->from[0]; v->from[0] = e->From;",
140 " v->to[1] = v->to[0]; v->to[0] = e->To;",
141 " v->dst[1] = v->dst[0]; v->dst[0] = e->Dst;",
145 " { int f = v->from[1];",
146 " int t = v->to[1];",
147 " Vertex *d = v->dst[1];",
148 " v->from[1] = v->from[0]; v->from[0] = e->From;",
149 " v->to[1] = v->to[0]; v->to[0] = e->To;",
150 " v->dst[1] = v->dst[0]; v->dst[0] = e->Dst;",
155 " e->Nxt = v->Succ;",
160 "copyRecursive(Vertex *v, Edge *e)",
162 " if (e->Nxt) copyRecursive(v, e->Nxt);",
163 " f = new_edge(e->Dst);",
164 " f->From = e->From;",
168 " f->Nxt = v->Succ;",
173 "copyEdges(Vertex *to, Vertex *from)",
175 " for (i = 0; i < 2; i++)",
176 " { to->from[i] = from->from[i];",
177 " to->to[i] = from->to[i];",
178 " to->dst[i] = from->dst[i];",
180 " if (from->Succ) copyRecursive(to, from->Succ);",
184 "cacheDelta(Vertex *v, int h, int first)",
185 "{ static Edge *ov, tmp; int i;",
187 " if (!first && INRANGE(ov,h))",
188 " return ov; /* intercepts about 10%% */",
189 " for (i = 0; i < 2; i++)",
190 " if (v->dst[i] && h >= v->from[i] && h <= v->to[i])",
191 " { tmp.From = v->from[i];",
192 " tmp.To = v->to[i];",
193 " tmp.Dst = v->dst[i];",
194 " tmp.s = tmp.S = 0;",
198 " for (ov = v->Succ; ov; ov = ov->Nxt)",
199 " if (INRANGE(ov,h)) return ov;",
201 " Uerror(\"cannot get here, cacheDelta\");",
202 " return (Edge *) 0;",
206 "Delta(Vertex *v, int h) /* v->delta[h] */",
209 " if (v->dst[0] && h >= v->from[0] && h <= v->to[0])",
210 " return v->dst[0]; /* oldest edge */",
211 " if (v->dst[1] && h >= v->from[1] && h <= v->to[1])",
212 " return v->dst[1];",
213 " for (e = v->Succ; e; e = e->Nxt)",
214 " if (INRANGE(e,h))",
216 " Uerror(\"cannot happen Delta\");",
217 " return (Vertex *) 0;",
221 "numDelta(Vertex *v, int d)",
226 " for (i = 0; i < 2; i++)",
228 " { cnt = v->dst[i]->num + d*(1 + v->to[i] - v->from[i]);",
229 " if (d == 1 && cnt < v->dst[i]->num) goto bad;",
230 " v->dst[i]->num = cnt;",
232 " for (e = v->Succ; e; e = e->Nxt)",
233 " { cnt = e->Dst->num + d*(1 + e->To - e->From + e->s);",
234 " if (d == 1 && cnt < e->Dst->num)",
235 "bad: Uerror(\"too many incoming edges\");",
236 " e->Dst->num = cnt;",
241 "setDelta(Vertex *v, int h, Vertex *newdst) /* v->delta[h] = newdst; */",
242 "{ Edge *e, *f = (Edge *) 0, *g;",
245 " /* remove the old entry, if there */",
246 " for (i = 0; i < 2; i++)",
247 " if (v->dst[i] && h >= v->from[i] && h <= v->to[i])",
248 " { if (h == v->from[i])",
249 " { if (h == v->to[i])",
250 " { v->dst[i] = (Vertex *) 0;",
251 " v->from[i] = v->to[i] = 0;",
254 " } else if (h == v->to[i])",
257 " { g = new_edge(v->dst[i]);/* same dst */",
258 " g->From = v->from[i];",
259 " g->To = h-1; /* left half */",
260 " v->from[i] = h+1; /* right half */",
261 " insert_edge(v, g);",
265 " for (e = v->Succ; e; f = e, e = e->Nxt)",
266 " { if (e->s == 1 && e->S == h)",
267 " { e->s = e->S = 0;",
270 " if (h >= e->From && h <= e->To)",
271 " { if (h == e->From)",
272 " { if (h == e->To)",
274 " { e->From = e->To = e->S;",
281 " } else if (h == e->To)",
283 " } else /* split */",
284 " { g = new_edge(e->Dst); /* same dst */",
285 " g->From = e->From;",
286 " g->To = h-1; /* g=left half */",
287 " e->From = h+1; /* e=right half */",
288 " g->Nxt = e->Nxt; /* insert g */",
289 " e->Nxt = g; /* behind e */",
290 " break; /* done */",
293 "rem_tst: if (e->From > e->To)",
294 " { if (e->s == 0) {",
298 " v->Succ = e->Nxt;",
299 " e->Nxt = (Edge *) 0;",
302 " { e->From = e->To = e->S;",
308 " /* check if newdst is already there */",
309 " for (i = 0; i < 2; i++)",
310 " if (v->dst[i] == newdst)",
311 " { if (h+1 == (int) v->from[i])",
312 " { v->from[i] = h;",
315 " if (h == (int) v->to[i]+1)",
319 " for (e = v->Succ; e; e = e->Nxt)",
320 " { if (e->Dst == newdst)",
321 " { if (h+1 == (int) e->From)",
323 " if (e->s == 1 && e->S+1 == e->From)",
324 " { e->From = e->S;",
329 " if (h == (int) e->To+1)",
331 " if (e->s == 1 && e->S == e->To+1)",
342 " /* add as a new edge */",
343 " e = new_edge(newdst);",
344 " e->From = e->To = h;",
345 " insert_edge(v, e);",
349 "cheap_key(Vertex *v)",
353 " { vk2 = (ulong) v->dst[0];",
354 " if ((ulong) v->dst[1] > vk2)",
355 " vk2 = (ulong) v->dst[1];",
356 " } else if (v->dst[1])",
357 " vk2 = (ulong) v->dst[1]; ",
360 " for (e = v->Succ; e; e = e->Nxt)",
361 " if ((ulong) e->Dst > vk2)",
362 " vk2 = (ulong) e->Dst;",
364 " Tally = (vk2>>2)&(TWIDTH-1);",
369 "mk_key(Vertex *v) /* not sensitive to order */",
370 "{ ulong m = 0, vk2 = 0;",
374 " { m += HASH(v->dst[0], v->to[0] - v->from[0] + 1);",
375 " vk2 = (ulong) v->dst[0]; ",
378 " { m += HASH(v->dst[1], v->to[1] - v->from[1] + 1);",
379 " if ((ulong) v->dst[1] > vk2) vk2 = (ulong) v->dst[1]; ",
381 " for (e = v->Succ; e; e = e->Nxt)",
382 " { m += HASH(e->Dst, e->To - e->From + 1 + e->s);",
383 " if ((ulong) e->Dst > vk2) vk2 = (ulong) e->Dst; ",
385 " Tally = (vk2>>2)&(TWIDTH-1);",
390 "mk_special(int sigma, Vertex *n, Vertex *v)",
391 "{ ulong m = 0, vk2 = 0;",
395 " for (i = 0; i < 2; i++)",
397 " { if (sigma >= v->from[i] && sigma <= v->to[i])",
398 " { m += HASH(v->dst[i], v->to[i]-v->from[i]);",
399 " if ((ulong) v->dst[i] > vk2",
400 " && v->to[i] > v->from[i])",
401 " vk2 = (ulong) v->dst[i]; ",
403 " { m += HASH(v->dst[i], v->to[i]-v->from[i]+1);",
404 " if ((ulong) v->dst[i] > vk2)",
405 " vk2 = (ulong) v->dst[i]; ",
407 " for (f = v->Succ; f; f = f->Nxt)",
408 " { if (sigma >= f->From && sigma <= f->To)",
409 " { m += HASH(f->Dst, f->To - f->From + f->s);",
410 " if ((ulong) f->Dst > vk2",
411 " && f->To - f->From + f->s > 0)",
412 " vk2 = (ulong) f->Dst; ",
413 " } else if (f->s == 1 && sigma == f->S)",
414 " { m += HASH(f->Dst, f->To - f->From + 1);",
415 " if ((ulong) f->Dst > vk2) vk2 = (ulong) f->Dst; ",
417 " { m += HASH(f->Dst, f->To - f->From + 1 + f->s);",
418 " if ((ulong) f->Dst > vk2) vk2 = (ulong) f->Dst; ",
421 " if ((ulong) n > vk2) vk2 = (ulong) n; ",
422 " Tally = (vk2>>2)&(TWIDTH-1);",
428 "dfa_init(ushort nr_layers)",
429 "{ int i; Vertex *r, *t;",
431 " dfa_depth = nr_layers; /* one byte per layer */",
432 " path = (Vertex **) emalloc((dfa_depth+1)*sizeof(Vertex *));",
433 " layers = (Vertex **) emalloc(TWIDTH*(dfa_depth+1)*sizeof(Vertex *));",
434 " lastword = (uchar *) emalloc((dfa_depth+1)*sizeof(uchar));",
435 " lastword[dfa_depth] = lastword[0] = 255;",
436 " path[0] = R = new_vertex(); F = new_vertex();",
438 " for (i = 1, r = R; i < dfa_depth; i++, r = t)",
439 " t = allDelta(r, i-1);",
440 " NF = allDelta(r, i-1);",
444 "static void complement_dfa(void) { Vertex *tmp = F; F = NF; NF = tmp; }",
448 "tree_stats(Vertex *t)",
449 "{ Edge *e; double cnt=0.0;",
450 " if (!t) return 0;",
451 " if (!t->key) return 0;",
452 " t->key = 0; /* precaution */",
453 " if (t->dst[0]) cnt++;",
454 " if (t->dst[1]) cnt++;",
455 " for (e = t->Succ; e; e = e->Nxt)",
457 " cnt += tree_stats(t->lnk);",
458 " cnt += tree_stats(t->left);",
459 " cnt += tree_stats(t->right);",
465 "{ int i, j; double cnt = 0.0;",
466 " for (j = 0; j < TWIDTH; j++)",
467 " for (i = 0; i < dfa_depth+1; i++)",
468 " cnt += tree_stats(layers[i*TWIDTH+j]);",
469 " printf(\"Minimized Automaton:\t%%6d nodes and %%6g edges\\n\",",
474 "dfa_member(ulong n)",
475 "{ Vertex **p, **q;",
476 " uchar *w = &word[n];",
479 " p = &path[n]; q = (p+1);",
480 " for (i = n; i < dfa_depth; i++)",
481 " *q++ = Delta(*p++, *w++);",
482 " return (*p == F);",
486 "dfa_store(uchar *sv)",
487 "{ Vertex **p, **q, *s, *y, *old, *new = F;",
488 " uchar *w, *u = lastword;",
492 " while (*w++ == *u++) /* find first byte that differs */",
494 " pfrst = (int) (u - lastword) - 1;",
495 " memcpy(&lastword[pfrst], &sv[pfrst], dfa_depth-pfrst);",
496 " if (pfrst > iv) pfrst = iv;",
497 " if (pfrst > nv) pfrst = nv;",
499 " p = &path[pfrst]; q = (p+1); w = &word[pfrst];",
500 " for (i = pfrst; i < dfa_depth; i++)",
501 " *q++ = Delta(*p++, *w++); /* (*p)->delta[*w++]; */",
503 " if (*p == F) return 1; /* it's already there */",
508 " new = find_it(path[iv], old, word[iv], iv);",
509 " } while (new && iv > 0);",
512 " nv = k = 0; s = path[0];",
513 " for (j = 1; j <= iv; ++j) ",
514 " if (path[j]->num > 1)",
515 " { y = new_vertex();",
516 " copyEdges(y, path[j]);",
519 " delete_it(s, j-1);",
520 " setDelta(s, word[j-1], y);",
521 " insert_it(s, j-1);",
522 " y->num = 1; /* initial value 1 */",
524 " path[j]->num--; /* only 1 moved from j to y */",
530 " y = Delta(s, word[iv]);",
532 " delete_it(s, iv); ",
533 " setDelta(s, word[iv], old);",
534 " insert_it(s, iv); ",
537 " for (j = iv+1; j < dfa_depth; j++)",
538 " if (path[j]->num == 0)",
539 " { numDelta(path[j], -1);",
540 " delete_it(path[j], j);",
541 " recyc_vertex(path[j]);",
548 "splay(ulong i, Vertex *t)",
549 "{ Vertex N, *l, *r, *y;",
551 " if (!t) return t;",
552 " N.left = N.right = (Vertex *) 0;",
555 " { if (i < t->key)",
556 " { if (!t->left) break;",
557 " if (i < t->left->key)",
559 " t->left = y->right;",
562 " if (!t->left) break;",
567 " } else if (i > t->key)",
568 " { if (!t->right) break;",
569 " if (i > t->right->key)",
571 " t->right = y->left;",
574 " if (!t->right) break;",
582 " l->right = t->left;",
583 " r->left = t->right;",
584 " t->left = N.right;",
585 " t->right = N.left;",
590 "insert_it(Vertex *v, int L)",
591 "{ Vertex *new, *t;",
595 " nr = ((L*TWIDTH)+Tally);",
600 " { layers[nr] = v;",
606 " new->left = t->left;",
608 " t->left = (Vertex *) 0;",
609 " } else if (i > t->key)",
611 " new->right = t->right;",
613 " t->right = (Vertex *) 0;",
614 " } else /* it's already there */",
615 " { v->lnk = t->lnk; /* put in linked list off v */",
619 " layers[nr] = new;",
623 "checkit(Vertex *h, Vertex *v, Vertex *n, uchar sigma)",
627 " for (k = 0; k < 2; k++)",
629 " { if (sigma >= h->from[k] && sigma <= h->to[k])",
630 " { if (h->dst[k] != n) goto no_match;",
632 " for (i = h->from[k]; i <= h->to[k]; i++)",
633 " { if (i == sigma) continue;",
634 " g = cacheDelta(v, i, j); j = 0;",
635 " if (h->dst[k] != g->Dst)",
637 " if (g->s == 0 || g->S != i)",
640 " for (f = h->Succ; f; f = f->Nxt)",
641 " { if (INRANGE(f,sigma))",
642 " { if (f->Dst != n) goto no_match;",
644 " for (i = f->From; i <= f->To; i++)",
645 " { if (i == sigma) continue;",
646 " g = cacheDelta(v, i, j); j = 0;",
647 " if (f->Dst != g->Dst)",
649 " if (g->s == 1 && i == g->S)",
653 " if (f->s && f->S != sigma)",
654 " { g = cacheDelta(v, f->S, 1);",
655 " if (f->Dst != g->Dst)",
659 " if (h->Succ || h->dst[0] || h->dst[1]) return 1;",
665 "find_it(Vertex *v, Vertex *n, uchar sigma, int L)",
669 " i = mk_special(sigma,n,v);",
670 " nr = ((L*TWIDTH)+Tally);",
673 " if (!t) return (Vertex *) 0;",
674 " layers[nr] = t = splay(i, t);",
676 " for (z = t; z; z = z->lnk)",
677 " if (checkit(z, v, n, sigma))",
680 " return (Vertex *) 0;",
684 "delete_it(Vertex *v, int L)",
688 " i = cheap_key(v);",
689 " nr = ((L*TWIDTH)+Tally);",
695 " { Vertex *z, *y = (Vertex *) 0;",
696 " for (z = t; z && z != v; y = z, z = z->lnk)",
698 " if (z != v) goto bad;",
700 " { y->lnk = z->lnk;",
701 " z->lnk = (Vertex *) 0;",
704 " } else if (z->lnk) /* z == t == v */",
706 " y->left = t->left;",
707 " y->right = t->right;",
708 " t->left = t->right = t->lnk = (Vertex *) 0;",
712 " /* delete the node itself */",
716 " { x = splay(i, t->left);",
717 " x->right = t->right;",
719 " t->left = t->right = t->lnk = (Vertex *) 0;",
723 "bad: Uerror(\"cannot happen delete\");",
This page took 0.046301 seconds and 4 git commands to generate.