private bool AddWordInternal(string word)
        {
            ConcurrentWordStateMachineState currentState = InitialState;

            foreach (var letter in FormatWord(word))
            {
                if (currentState.LetterDictionary.ContainsKey(letter))
                {
                    currentState = currentState.LetterDictionary[letter];
                }
                else
                {
                    ConcurrentWordStateMachineState newState = new ConcurrentWordStateMachineState(currentState, letter);
                    currentState.LetterDictionary[letter] = newState;
                    currentState = newState;
                }
            }
            if (currentState.IsWordEnd)
            {
                return(false);
            }
            else
            {
                currentState.IsWordEnd = true;
                return(true);
            }
        }
 public bool RemoveWord(string word)
 {
     try
     {
         StateMachineLock.EnterWriteLock();
         ConcurrentWordStateMachineState currentState = InitialState;
         foreach (var letter in FormatWord(word))
         {
             if (currentState.LetterDictionary.ContainsKey(letter))
             {
                 currentState = currentState.LetterDictionary[letter];
             }
             else
             {
                 return(false);
             }
         }
         if (currentState.LetterDictionary.Count == 0)
         {
             while (currentState.PreviousState.LetterDictionary.Count == 1)
             {
                 currentState = currentState.PreviousState;
             }
             currentState.PreviousState.LetterDictionary.Remove(currentState.Letter);
         }
         else
         {
             currentState.IsWordEnd = false;
         }
         return(true);
     }
     finally
     {
         StateMachineLock.ExitWriteLock();
     }
 }
 public ConcurrentWordStateMachineState(ConcurrentWordStateMachineState previousState, char letter)
 {
     this.PreviousState    = previousState;
     this.LetterDictionary = new Dictionary <char, ConcurrentWordStateMachineState>();
     this.Letter           = letter;
 }
 public ConcurrentWordStateMachine()
 {
     InitialState     = new ConcurrentWordStateMachineState(null);
     StateMachineLock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
 }