Advertisement
honey_the_codewitch

toDfa TS

Dec 27th, 2023 (edited)
1,282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. toDfa(): FA {
  2.     const closure: FA[] = this.fillClosure();
  3.     const p: Set<number> = new Set<number>();
  4.     for (const ffa of closure) {
  5.         p.add(0);
  6.         for (const t of ffa.transitions) {
  7.             p.add(t.min);
  8.             if (t.max < 0x10ffff) {
  9.                 p.add(t.max + 1);
  10.             }
  11.         }
  12.     }
  13.     const points: number[] = [...p];
  14.     points.sort((x, y) => x - y);
  15.     const sets: Map<Set<FA>, Set<FA>> = new Map<Set<FA>, Set<FA>>();
  16.     const working: Set<FA>[] = [];
  17.     const dfaMap: Map<Set<FA>, FA> = new Map<Set<FA>, FA>();
  18.     const initial: Set<FA> = new Set<FA>();
  19.     initial.add(this);
  20.     sets.set(initial, initial);
  21.     working.push(initial);
  22.     const result: FA = new FA();
  23.     for (const ffa of initial) {
  24.         if (ffa.isAccepting()) {
  25.             result.acceptSymbol = ffa.acceptSymbol;
  26.             break;
  27.         }
  28.     }
  29.     dfaMap.set(initial, result);
  30.     while (working.length > 0) {
  31.         const s: Set<FA> = working[0];
  32.         working.shift();
  33.         const dfa: FA = dfaMap.get(s)!;
  34.         for (const q of s) {
  35.             if (q.isAccepting()) {
  36.                 dfa.acceptSymbol = q.acceptSymbol;
  37.                 break;
  38.             }
  39.         }
  40.         let i: number = 0;
  41.         for (const pnt of points) {
  42.             let set: Set<FA> = new Set<FA>();
  43.             for (const c of s) {
  44.                 for (let trns of c.transitions) {
  45.                     if (trns.min <= pnt && pnt <= trns.max) {
  46.                         set.add(trns.to);
  47.                     }
  48.                 }
  49.             }
  50.             if (!sets.has(set)) {
  51.                 sets.set(set, set);
  52.                 working.push(set);
  53.                 let newFa: FA = new FA();
  54.                 dfaMap.set(set, newFa);
  55.             }
  56.             const dst: FA = dfaMap.get(set)!;
  57.             const first: number = pnt;
  58.             let last: number;
  59.             if (i + 1 < points.length) {
  60.                 last = points[i + 1] - 1;
  61.             } else {
  62.                 last = 0x10ffff;
  63.             }
  64.             dfa.transitions.push(new FATransition(first, last, dst));
  65.             ++i;
  66.         }
  67.     }
  68.     for (const ffa of result.fillClosure()) {
  69.         const itrns: FATransition[] = [...ffa.transitions];
  70.         for (const trns of itrns) {
  71.             const acc: FA[] = trns.to.fillClosure().filter((value: FA, index: number, array: FA[]) => value.isAccepting());
  72.             if (acc.length == 0) {
  73.                 ffa.transitions.splice(ffa.transitions.indexOf(trns), 1);
  74.             }
  75.         }
  76.     }
  77.     return result;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement