//public static State ToNfa(this DfaState dfaStart) //{ // int total = 0; // dfaStart.ForEach((state) => { state.Index = total++; }); // var states = new State[total]; // for (int i = 0; i < states.Length; i++) // states[i] = new State(); // dfaStart.ForEach((dfaState) => // { // var start = states[dfaState.Index]; // var end = start; // if (dfaState.AllMarks.Count > 0) // { // end = new State(); // foreach (var mark in dfaState.AllMarks) // { // var state = new State(); // state.CopyIMarkFrom(mark); // state.Transition.Add(State.Epsilon, end); // start.Transition.Add(State.Epsilon, state); // } // } // for (int i = 0; i < dfaState.Transition.Length; i++) // if (dfaState.Transition[i] != null) // end.Transition.Add((byte)i, states[dfaState.Transition[i].Index]); // }); // return states[dfaStart.Index]; //} public static State ToNfa2(this DfaState dfaStart) { int total = 0; dfaStart.ForEach((state) => { state.Index = total++; }); var states = new State[total]; for (int i = 0; i < states.Length; i++) { states[i] = new State(); } dfaStart.ForEach((dfaState) => { var nfaState = states[dfaState.Index]; nfaState.AddRangeMarks(dfaState.AllMarks); //foreach (var mark in dfaState.AllMarks) //{ // var copyMark = new State(); // copyMark.CopyIMarkFrom(mark); // nfaState.PackedStates.Add(copyMark.Id); //} for (int i = 0; i < dfaState.Transition.Length; i++) { if (dfaState.Transition[i] != null) { nfaState.Transition.Add((byte)i, states[dfaState.Transition[i].Index]); } } }); return(states[dfaStart.Index]); }
public static int Minimize3(this DfaState start, bool showProgress) { if (showProgress) { Console.WriteLine("Minimize DFA v.3"); } var states = new List <DfaState>(3000000); start.ForEach((state) => { state.SetId = 0; states.Add(state); }); int id = 1; foreach (var state in states) { if (state.HasMarks) { foreach (var state2 in states) { if (state2.HasMarks) { if (state.IsSame(state2)) { if (state2.SetId > 0) { state.SetId = state2.SetId; break; } } } } if (state.SetId < 1) { state.SetId = id++; } } else { state.SetId = 0; } } if (GetFirstInSet(states, 0) == null) { return(-1); // nothing to minimize } DfaState[] firsts = null; int[] ids = null; for (int xid = 0; xid < id;) { xid = id; PrepareArrays(ref firsts, ref ids, id); foreach (var state in states) { if (firsts[state.SetId] == null) { firsts[state.SetId] = state; } else { if (IsTransitedToSameSets(firsts[state.SetId], state) == false) { if (ids[state.SetId] == 0) { ids[state.SetId] = id++; } state.NewSetId = ids[state.SetId]; } } } foreach (var state in states) { state.SetId = state.NewSetId; } Console.Write("{0}\r", id); } if (showProgress) { Console.Write("Create MiniDFA\r", id); } CreateMiniDfa(start, states, id); if (showProgress) { Console.WriteLine("Done ({0})\t\t\t\t", id); } return(id); }
public static int Minimize4(this DfaState start, bool showProgress) { var startTime = DateTime.Now; if (showProgress) { Console.WriteLine("Minimize DFA v.4"); } var states = new List <DfaState>(3000000); var sets = new List <HashSet <DfaState> >(50000); sets.Add(new HashSet <DfaState>()); start.ForEach((state) => { state.SetId = 0; sets[0].Add(state); states.Add(state); }); if (showProgress) { Console.WriteLine("Create list: {0}", DateTime.Now - startTime); } int id = 1; if (showProgress) { Console.Write("Proccessing marks...\r"); } var startTime2 = DateTime.Now; foreach (var state in sets[0]) { if (state.AllMarks.Count > 0) { for (int i = 1; i < sets.Count; i++) { var state2 = GetOne(sets[i]); if (state.IsSame(state2)) { CopyToSet(sets, state, state2.SetId); break; } } if (state.SetId == 0) { CopyToSet(sets, state, id++); } } } sets[0].RemoveWhere((state) => { return(state.SetId != 0); }); if (showProgress) { Console.WriteLine("Marks: {0}\t\t", DateTime.Now - startTime2); } for (int xid = 0; xid < id;) { xid = id; for (int i = 0; i < id; i++) { DfaState first = GetOne(sets[i]); bool splitted = false; foreach (var state in sets[i]) { if (IsTransitedToSameSets(first, state) == false) { state.NewSetId = id; splitted = true; } } if (splitted) { foreach (var state in sets[i]) { CopyToSet(sets, state, state.NewSetId); } sets[i].RemoveWhere((state) => { return(state.SetId != i); }); id++; Console.Write("{0}\r", id); } } } if (showProgress) { Console.Write("Create MiniDFA\r", id); } CreateMiniDfa(start, states, id); if (showProgress) { Console.WriteLine("Done ({0}, {1})\t\t\t\t", id, DateTime.Now - startTime); } return(id); }
public static int Minimize2(this DfaState start, bool showProgress) { if (showProgress) { Console.WriteLine("Minimize DFA v.2"); } var states = new List <DfaState>(); start.ForEach((state) => { state.SetId = 0; states.Add(state); }); int id = 1; if (showProgress) { Console.Write("Proccessing marks...\r"); } var startTime = DateTime.Now; foreach (var state in states) { if (state.HasMarks) { foreach (var state2 in states) { if (state.IsSame(state2)) { if (state2.SetId > 0) { state.SetId = state2.SetId; break; } } } if (state.SetId < 1) { state.SetId = id++; } } else { state.SetId = 0; } } if (showProgress) { Console.WriteLine("Marks: {0}\t\t", DateTime.Now - startTime); } if (GetFirstInSet(states, 0) == null) { return(-1); // nothing to minimize } for (int xid = 0; xid < id;) { xid = id; for (int i = 0; i < id; i++) { DfaState first = GetFirstInSet(states, i); bool splitted = false; foreach (var state in GetSet(states, i)) { if (IsTransitedToSameSets(first, state) == false) { state.NewSetId = id; splitted = true; } } if (splitted) { foreach (var state in GetSet(states, i)) { state.SetId = state.NewSetId; } id++; Console.Write("{0}\r", id); } } } if (showProgress) { Console.Write("Create MiniDFA\r", id); } CreateMiniDfa(start, states, id); if (showProgress) { Console.WriteLine("Done ({0}, {1})\t\t\t\t", id, DateTime.Now - startTime); } return(id); }