Beispiel #1
0
        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());
            }

            var deterministic = a1.IsSingleton && a2.IsDeterministic;

            if (a1 == a2)
            {
                a1 = a1.CloneExpanded();
                a2 = a2.CloneExpanded();
            }
            else
            {
                a1 = a1.CloneExpandedIfRequired();
                a2 = a2.CloneExpandedIfRequired();
            }

            foreach (var s in a1.GetAcceptStates())
            {
                s.Accept = false;
                s.AddEpsilon(a2.Initial);
            }

            a1.IsDeterministic = deterministic;
            a1.ClearHashCode();
            a1.CheckMinimizeAlways();
            return(a1);
        }
Beispiel #2
0
        public static Automaton Intersection(Automaton a1, Automaton a2)
        {
            if (a1.IsSingleton)
            {
                return(a2.Run(a1.Singleton) ? a1.CloneIfRequired() : BasicAutomata.MakeEmpty());
            }

            if (a2.IsSingleton)
            {
                return(a1.Run(a2.Singleton) ? a2.CloneIfRequired() : BasicAutomata.MakeEmpty());
            }

            if (a1 == a2)
            {
                return(a1.CloneIfRequired());
            }

            var transitions1 = Automaton.GetSortedTransitions(a1.GetStates());
            var transitions2 = Automaton.GetSortedTransitions(a2.GetStates());
            var c            = new Automaton();
            var worklist     = new LinkedList <StatePair>();
            var newstates    = new Dictionary <StatePair, StatePair>();
            var p            = new StatePair(c.Initial, a1.Initial, a2.Initial);

            _ = worklist.AddLast(p);
            newstates.Add(p, p);
            while (worklist.Count > 0)
            {
                p          = worklist.RemoveAndReturnFirst();
                p.S.Accept = p.FirstState.Accept && p.SecondState.Accept;
                var t1 = transitions1[p.FirstState.Number];
                var t2 = transitions2[p.SecondState.Number];
                for (int n1 = 0, b2 = 0; n1 < t1.Length; n1++)
                {
                    while (b2 < t2.Length && t2[b2].Max < t1[n1].Min)
                    {
                        b2++;
                    }

                    for (var n2 = b2; n2 < t2.Length && t1[n1].Max >= t2[n2].Min; n2++)
                    {
                        if (t2[n2].Max >= t1[n1].Min)
                        {
                            var q = new StatePair(t1[n1].To, t2[n2].To);
                            _ = newstates.TryGetValue(q, out var r);
                            if (r == null)
                            {
                                q.S = new State();
                                _   = worklist.AddLast(q);
                                newstates.Add(q, q);
                                r = q;
                            }

                            var min = t1[n1].Min > t2[n2].Min ? t1[n1].Min : t2[n2].Min;
                            var max = t1[n1].Max < t2[n2].Max ? t1[n1].Max : t2[n2].Max;
                            p.S.Transitions.Add(new Transition(min, max, r.S));
                        }
                    }
                }
            }

            c.IsDeterministic = a1.IsDeterministic && a2.IsDeterministic;
            c.RemoveDeadTransitions();
            c.CheckMinimizeAlways();
            return(c);
        }
Beispiel #3
0
        public static Automaton Concatenate(IList <Automaton> l)
        {
            if (l.Count == 0)
            {
                return(BasicAutomata.MakeEmptyString());
            }

            var allSingleton = l.All(_ => _.IsSingleton);

            if (allSingleton)
            {
                var b = new StringBuilder();
                foreach (var a in l)
                {
                    _ = b.Append(a.Singleton);
                }

                return(BasicAutomata.MakeString(b.ToString()));
            }
            else
            {
                if (l.Any(_ => _.IsEmpty))
                {
                    return(BasicAutomata.MakeEmpty());
                }

                var ids = new HashSet <int>();
                foreach (var a in l)
                {
                    _ = ids.Add(RuntimeHelpers.GetHashCode(a));
                }

                var hasAliases = ids.Count != l.Count;
                var b          = l[0];
                b = hasAliases ? b.CloneExpanded() : b.CloneExpandedIfRequired();

                var ac    = b.GetAcceptStates();
                var first = true;
                foreach (var a in l)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        if (a.IsEmptyString())
                        {
                            continue;
                        }

                        var aa = a;
                        aa = hasAliases ? aa.CloneExpanded() : aa.CloneExpandedIfRequired();

                        var ns = aa.GetAcceptStates();
                        foreach (var 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);
            }
        }
 public static Automaton MakeString(string s) => BasicAutomata.MakeString(s);
 public static Automaton MakeCharSet(string set) => BasicAutomata.MakeCharSet(set);
 public static Automaton MakeChar(char c) => BasicAutomata.MakeChar(c);