/// <summary> /// A clone of this automaton, expands if singleton. /// </summary> /// <returns> /// Returns a clone of this automaton, expands if singleton. /// </returns> internal Automaton CloneExpanded() { Automaton a = this.Clone(); a.ExpandSingleton(); return(a); }
/// <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> internal 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> /// Adds epsilon transitions to the given automaton. This method adds extra character interval /// transitions that are equivalent to the given set of epsilon transitions. /// </summary> /// <param name="a">The automaton.</param> /// <param name="pairs">A collection of <see cref="StatePair"/> objects representing pairs of /// source/destination states where epsilon transitions should be added.</param> internal static void AddEpsilons(Automaton a, ICollection <StatePair> pairs) { a.ExpandSingleton(); var forward = new Dictionary <State, HashSet <State> >(); var back = new Dictionary <State, HashSet <State> >(); foreach (StatePair p in pairs) { HashSet <State> to = forward[p.FirstState]; if (to == null) { to = new HashSet <State>(); forward.Add(p.FirstState, to); } to.Add(p.SecondState); HashSet <State> from = back[p.SecondState]; if (from == null) { from = new HashSet <State>(); back.Add(p.SecondState, from); } from.Add(p.FirstState); } var worklist = new LinkedList <StatePair>(pairs); var workset = new HashSet <StatePair>(pairs); while (worklist.Count != 0) { StatePair p = worklist.RemoveAndReturnFirst(); workset.Remove(p); HashSet <State> to = forward[p.SecondState]; HashSet <State> from = back[p.FirstState]; if (to != null) { foreach (State s in to) { var pp = new StatePair(p.FirstState, s); if (!pairs.Contains(pp)) { pairs.Add(pp); forward[p.FirstState].Add(s); back[s].Add(p.FirstState); worklist.AddLast(pp); workset.Add(pp); if (from != null) { foreach (State q in from) { var qq = new StatePair(q, p.FirstState); if (!workset.Contains(qq)) { worklist.AddLast(qq); workset.Add(qq); } } } } } } } // Add transitions. foreach (StatePair p in pairs) { p.FirstState.AddEpsilon(p.SecondState); } a.IsDeterministic = false; a.ClearHashCode(); a.CheckMinimizeAlways(); }