Ejemplo n.º 1
0
        public void Add(char[] current)
        {
            Debug.Assert(this.register != null, "Automaton already built.");
            Debug.Assert(current.Length > 0, "Input sequences must not be empty.");
            Debug.Assert(
                this.previous == null ||
                LexicographicOrderComparer.Compare(this.previous.ToString().ToCharArray(), current) <= 0,
                "Input must be sorted: " + this.previous + " >= " + current);
            Debug.Assert(this.SetPrevious(current));

            // Descend in the automaton (find matching prefix).
            int   pos = 0;
            int   max = current.Length;
            State next;
            State state = root;

            while (pos < max && (next = state.GetLastChild(current[pos])) != null)
            {
                state = next;
                pos++;
            }

            if (state.HasChildren)
            {
                this.ReplaceOrRegister(state);
            }

            StringUnionOperations.AddSuffix(state, current, pos);
        }
Ejemplo n.º 2
0
        public static Fare.State Build(IEnumerable <char[]> input)
        {
            var builder = new StringUnionOperations();

            foreach (var chs in input)
            {
                builder.Add(chs);
            }

            return(StringUnionOperations.Convert(builder.Complete(), new Dictionary <State, Fare.State>()));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns a new (deterministic and minimal) automaton that accepts the union of the given
        /// set of strings. The input character sequences are internally sorted in-place, so the
        /// input array is modified. @see StringUnionOperations.
        /// </summary>
        /// <param name="strings">The strings.</param>
        /// <returns></returns>
        public static Automaton MakeStringUnion(params char[][] strings)
        {
            if (strings.Length == 0)
            {
                return(MakeEmpty());
            }

            Array.Sort(strings, StringUnionOperations.LexicographicOrderComparer);
            var a = new Automaton();

            a.Initial         = StringUnionOperations.Build(strings);
            a.IsDeterministic = true;
            a.Reduce();
            a.RecomputeHashCode();
            return(a);
        }
Ejemplo n.º 4
0
        private static Fare.State Convert(State s, IDictionary <State, Fare.State> visited)
        {
            Fare.State converted = visited[s];
            if (converted != null)
            {
                return(converted);
            }

            converted        = new Fare.State();
            converted.Accept = s.IsFinal;

            visited.Add(s, converted);
            int i = 0;

            char[] labels = s.TransitionLabels;
            foreach (State target in s.States)
            {
                converted.AddTransition(new Transition(labels[i++], StringUnionOperations.Convert(target, visited)));
            }

            return(converted);
        }