/// <summary> /// Returns an automaton that accepts between <paramref name="min"/> and /// <paramref name="max"/> (including both) concatenated repetitions of the language /// of the given automaton. /// <para/> /// Complexity: linear in number of states and in <paramref name="min"/> and /// <paramref name="max"/>. /// </summary> public static Automaton Repeat(Automaton a, int min, int max) { if (min > max) { return(BasicAutomata.MakeEmpty()); } max -= min; a.ExpandSingleton(); Automaton b; if (min == 0) { b = BasicAutomata.MakeEmptyString(); } else if (min == 1) { b = (Automaton)a.Clone(); } else { IList <Automaton> @as = new List <Automaton>(); while (min-- > 0) { @as.Add(a); } b = Concatenate(@as); } if (max > 0) { Automaton d = (Automaton)a.Clone(); while (--max > 0) { Automaton c = (Automaton)a.Clone(); foreach (State p in c.GetAcceptStates()) { p.AddEpsilon(d.initial); } d = c; } foreach (State p in b.GetAcceptStates()) { p.AddEpsilon(d.initial); } b.deterministic = false; //b.clearHashCode(); b.ClearNumberedStates(); b.CheckMinimizeAlways(); } return(b); }
/// <summary> /// Returns an automaton that accepts the Kleene star (zero or more /// concatenated repetitions) of the language of the given automaton. Never /// modifies the input automaton language. /// <para/> /// Complexity: linear in number of states. /// </summary> public static Automaton Repeat(Automaton a) { a = a.CloneExpanded(); State s = new State(); s.accept = true; s.AddEpsilon(a.initial); foreach (State p in a.GetAcceptStates()) { p.AddEpsilon(s); } a.initial = s; a.deterministic = false; //a.clearHashCode(); a.ClearNumberedStates(); a.CheckMinimizeAlways(); return(a); }
/// <summary> /// Returns an automaton that accepts the concatenation of the languages of the /// given automata. /// <para/> /// Complexity: linear in number of states. /// </summary> public static Automaton Concatenate(Automaton a1, Automaton a2) { if (a1.IsSingleton && a2.IsSingleton) { return(BasicAutomata.MakeString(a1.singleton + a2.singleton)); } if (IsEmpty(a1) || IsEmpty(a2)) { return(BasicAutomata.MakeEmpty()); } // adding epsilon transitions with the NFA concatenation algorithm // in this case always produces a resulting DFA, preventing expensive // redundant determinize() calls for this common case. bool deterministic = a1.IsSingleton && a2.IsDeterministic; if (a1 == a2) { a1 = a1.CloneExpanded(); a2 = a2.CloneExpanded(); } else { a1 = a1.CloneExpandedIfRequired(); a2 = a2.CloneExpandedIfRequired(); } foreach (State s in a1.GetAcceptStates()) { s.accept = false; s.AddEpsilon(a2.initial); } a1.deterministic = deterministic; //a1.clearHashCode(); a1.ClearNumberedStates(); a1.CheckMinimizeAlways(); return(a1); }
/// <summary> /// Returns an automaton that accepts the concatenation of the languages of the /// given automata. /// <para/> /// Complexity: linear in total number of states. /// </summary> public static Automaton Concatenate(IList <Automaton> l) { if (l.Count == 0) { return(BasicAutomata.MakeEmptyString()); } bool all_singleton = true; foreach (Automaton a in l) { if (!a.IsSingleton) { all_singleton = false; break; } } if (all_singleton) { StringBuilder b = new StringBuilder(); foreach (Automaton a in l) { b.Append(a.singleton); } return(BasicAutomata.MakeString(b.ToString())); } else { foreach (Automaton a in l) { if (BasicOperations.IsEmpty(a)) { return(BasicAutomata.MakeEmpty()); } } HashSet <int> ids = new HashSet <int>(); foreach (Automaton a in l) { ids.Add(a.GetHashCode()); } bool has_aliases = ids.Count != l.Count; Automaton b = l[0]; if (has_aliases) { b = b.CloneExpanded(); } else { b = b.CloneExpandedIfRequired(); } ISet <State> ac = b.GetAcceptStates(); bool first = true; foreach (Automaton a in l) { if (first) { first = false; } else { if (a.IsEmptyString) { continue; } Automaton aa = a; if (has_aliases) { aa = aa.CloneExpanded(); } else { aa = aa.CloneExpandedIfRequired(); } ISet <State> ns = aa.GetAcceptStates(); foreach (State s in ac) { s.accept = false; s.AddEpsilon(aa.initial); if (s.accept) { ns.Add(s); } } ac = ns; } } b.deterministic = false; //b.clearHashCode(); b.ClearNumberedStates(); b.CheckMinimizeAlways(); return(b); } }