/// <summary> /// Accepts between <code>min</code> and <code>max</code> (including both) concatenated /// repetitions of the language of the given automaton. /// </summary> /// <param name="a">The automaton.</param> /// <param name="min">The minimum concatenated repetitions of the language of the given /// automaton.</param> /// <param name="max">The maximum concatenated repetitions of the language of the given /// automaton.</param> /// <returns> /// Returns an automaton that accepts between <code>min</code> and <code>max</code> /// (including both) concatenated repetitions of the language of the given automaton. /// </returns> /// <remarks> /// Complexity: linear in number of states and in <code>min</code> and <code>max</code>. /// </remarks> 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 = a.Clone(); } else { var @as = new List <Automaton>(); while (min-- > 0) { @as.Add(a); } b = BasicOperations.Concatenate(@as); } if (max > 0) { Automaton d = a.Clone(); while (--max > 0) { Automaton c = 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.IsDeterministic = false; b.ClearHashCode(); b.CheckMinimizeAlways(); } return(b); }
/// <summary> /// Accepts the Kleene star (zero or more concatenated repetitions) of the language of the /// given automaton. Never modifies the input automaton language. /// </summary> /// <param name="a">The automaton.</param> /// <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. /// </returns> /// <remarks> /// Complexity: linear in number of states. /// </remarks> public static Automaton Repeat(Automaton a) { a = a.CloneExpanded(); var s = new State(); s.Accept = true; s.AddEpsilon(a.Initial); foreach (State p in a.GetAcceptStates()) { p.AddEpsilon(s); } a.Initial = s; a.IsDeterministic = false; a.ClearHashCode(); a.CheckMinimizeAlways(); return(a); }
/// <summary> /// Reverses the language of the given (non-singleton) automaton while returning the set of /// new initial states. /// </summary> /// <param name="a">The automaton.</param> /// <returns></returns> public static HashSet <State> Reverse(Automaton a) { // Reverse all edges. var m = new Dictionary <State, HashSet <Transition> >(); HashSet <State> states = a.GetStates(); HashSet <State> accept = a.GetAcceptStates(); foreach (State r in states) { m.Add(r, new HashSet <Transition>()); r.Accept = false; } foreach (State r in states) { foreach (Transition t in r.Transitions) { m[t.To].Add(new Transition(t.Min, t.Max, r)); } } foreach (State r in states) { r.Transitions = m[r].ToList(); } // Make new initial+final states. a.Initial.Accept = true; a.Initial = new State(); foreach (State r in accept) { a.Initial.AddEpsilon(r); // Ensures that all initial states are reachable. } a.IsDeterministic = false; return(accept); }
public static Automaton Concatenate(Automaton a1, Automaton a2) { if (a1.IsSingleton && a2.IsSingleton) { return(BasicAutomata.MakeString(a1.Singleton + a2.Singleton)); } if (BasicOperations.IsEmpty(a1) || BasicOperations.IsEmpty(a2)) { return(BasicAutomata.MakeEmpty()); } 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.IsDeterministic = deterministic; a1.ClearHashCode(); a1.CheckMinimizeAlways(); return(a1); }
/// <summary> /// Reverses the language of the given (non-singleton) automaton while returning the set of /// new initial states. /// </summary> /// <param name="a">The automaton.</param> /// <returns></returns> public static HashSet<State> Reverse(Automaton a) { // Reverse all edges. var m = new Dictionary<State, HashSet<Transition>>(); HashSet<State> states = a.GetStates(); HashSet<State> accept = a.GetAcceptStates(); foreach (State r in states) { m.Add(r, new HashSet<Transition>()); r.Accept = false; } foreach (State r in states) { foreach (Transition t in r.Transitions) { m[t.To].Add(new Transition(t.Min, t.Max, r)); } } foreach (State r in states) { r.Transitions = m[r].ToList(); } // Make new initial+final states. a.Initial.Accept = true; a.Initial = new State(); foreach (State r in accept) { a.Initial.AddEpsilon(r); // Ensures that all initial states are reachable. } a.IsDeterministic = false; return accept; }
/// <summary> /// Accepts the Kleene star (zero or more concatenated repetitions) of the language of the /// given automaton. Never modifies the input automaton language. /// </summary> /// <param name="a">The automaton.</param> /// <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. /// </returns> /// <remarks> /// Complexity: linear in number of states. /// </remarks> public static Automaton Repeat(Automaton a) { a = a.CloneExpanded(); var s = new State(); s.Accept = true; s.AddEpsilon(a.Initial); foreach (State p in a.GetAcceptStates()) { p.AddEpsilon(s); } a.Initial = s; a.IsDeterministic = false; a.ClearHashCode(); a.CheckMinimizeAlways(); return a; }
public static Automaton Concatenate(Automaton a1, Automaton a2) { if (a1.IsSingleton && a2.IsSingleton) { return BasicAutomata.MakeString(a1.Singleton + a2.Singleton); } if (BasicOperations.IsEmpty(a1) || BasicOperations.IsEmpty(a2)) { return BasicAutomata.MakeEmpty(); } 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.IsDeterministic = deterministic; a1.ClearHashCode(); a1.CheckMinimizeAlways(); return a1; }
public static Automaton Concatenate(IList <Automaton> l) { if (l.Count == 0) { return(BasicAutomata.MakeEmptyString()); } bool allSingleton = l.All(a => a.IsSingleton); if (allSingleton) { var b = new StringBuilder(); foreach (Automaton a in l) { b.Append(a.Singleton); } return(BasicAutomata.MakeString(b.ToString())); } else { if (l.Any(a => a.IsEmpty)) { return(BasicAutomata.MakeEmpty()); } var ids = new HashSet <int>(); foreach (Automaton a in l) { ids.Add(RuntimeHelpers.GetHashCode(a)); } bool hasAliases = ids.Count != l.Count; Automaton b = l[0]; b = hasAliases ? b.CloneExpanded() : b.CloneExpandedIfRequired(); var ac = b.GetAcceptStates(); bool first = true; foreach (Automaton a in l) { if (first) { first = false; } else { if (a.IsEmptyString()) { continue; } Automaton aa = a; aa = hasAliases ? aa.CloneExpanded() : aa.CloneExpandedIfRequired(); HashSet <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.IsDeterministic = false; b.ClearHashCode(); b.CheckMinimizeAlways(); return(b); } }