Beispiel #1
0
 public void CreateOutTransition(State target, int character)
 {
     if (HasTransition(character))
       {
     throw new InvalidOperationException("Transition already exists.");
       }
       SetTransition(target, character);
 }
Beispiel #2
0
 public State Clone(int alphabetSize)
 {
     var state = new State(alphabetSize);
       for (var i = 0; i < alphabetSize; ++i)
       {
     if (HasTransition(i))
     {
       state.CreateOutTransition(Advance(i), i);
     }
       }
       return state;
 }
Beispiel #3
0
 public StateManager(int alphabetSize, int wordLength)
 {
     _alphabetSize = alphabetSize;
       _heights = wordLength + 1;
       _uniqueStates = new Dictionary<State, State>[_heights];
       for (var i = 0; i < _heights; ++i)
       {
     _uniqueStates[i] = new Dictionary<State, State>(new StateComparer());
       }
       _finalStates = new Dictionary<int, FinalState>();
       StartingState = new State(_alphabetSize);
       InsertUniqueState(StartingState, _heights - 1);
 }
 /// <summary>
 /// Adds the rest of the word to the dfa.
 /// </summary>
 /// <param name="parent">The state to attack the suffix to.</param>
 /// <param name="word">The current word.</param>
 /// <param name="wordPos">The index of the first letter of the suffix.</param>
 private void AddSuffix(State parent, WordWithValue word, int wordPos)
 {
     var states = new Stack<State>();
       states.Push(parent);
       // Create new States.
       var i = wordPos;
       while (i < _wordLength - 1)
       {
     var prevState = states.Peek();
     var newState = new State(_alphabetSize);
     prevState.CreateOutTransition(newState, word.Word[i]);
     states.Push(newState);
     i += 1;
       }
       // Connect to final State.
       var final = _stateManager.GetOrCreateFinalState(word.Value);
       states.Peek().CreateOutTransition(final, word.Word[_wordLength - 1]);
       // Merge new States with unique States.
       i -= 1;
       MergeStates(word.Word, i, states);
 }
Beispiel #5
0
 public void RedirectOutTransition(State target, int character)
 {
     if (HasTransition(character))
       {
     var nextState = Advance(character);
     nextState._incomingTransitions -= 1;
     SetTransition(target, character);
       }
       else
       {
     throw new InvalidOperationException("Transition doesn't exist.");
       }
 }
Beispiel #6
0
 private void SetTransition(State target, int character)
 {
     _targetStates[character] = target;
       target._incomingTransitions += 1;
 }
Beispiel #7
0
 public State TryGetEquivalentUniqueState(State state, int height)
 {
     var statesAtHeight = _uniqueStates[height];
       State uniqueState;
       var isRedundant = statesAtHeight.TryGetValue(state, out uniqueState);
       return isRedundant ? uniqueState : null;
 }
Beispiel #8
0
 public void RemoveUniqueState(State state, int height)
 {
     if (!_uniqueStates[height].Remove(state))
       {
     throw new InvalidOperationException("State isn't unique.");
       }
 }
Beispiel #9
0
 public void InsertUniqueState(State state, int height)
 {
     _uniqueStates[height].Add(state, state);
 }