Exemple #1
0
        /// <summary>
        /// Finds all permutations of chars that form multiword
        /// anagrams when the first word is prefixed by wordPrefix.
        /// For each of these permutations, adds the words in phrasePrefix
        /// appended by the permutation to StringSet, with the word order
        /// reversed.
        /// </summary>
        /// <param name="phrasePrefix">The phrase to prefix 
        /// each anagram with.</param>
        /// <param name="wordPrefix">The string to include as
        /// the prefix for the first word in each anagram.</param>
        /// <param name="chars">The characters for which to find
        /// anagrams.</param>
        private ImmutableTrie FindAnagrams(ITrie lastWord, Queue<char> chars)
        {
            if (lastWord == null)
                return null;

            ImmutableTrie[] tries = new ImmutableTrie[_map.CharacterCount];
            bool nonNull = false;

            if (chars.Count == 0)
            {
                if (lastWord.IsEmpty())
                {
                    Key k = new Key(chars, _map);
                    ImmutableTrie result = null;
                    if (_dictionary.TryGetValue(k, out result))
                    {
                        tries[_map.GetLocation(' ')] = result;
                    }
                    else
                    {
                        tries[_map.GetLocation(' ')] = new ImmutableTrie(new ImmutableTrie[0], true, _map);
                        _dictionary.Add(k, tries[_map.GetLocation(' ')]);
                    }
                    if(tries[_map.GetLocation(' ')] != null)
                        nonNull = true;
                }
                else //if (prefix.Length == 0)
                    return null;
                    //_anagrams.Add("");
            }
            else
            {
                if (lastWord.IsEmpty())
                {
                    //char x = chars.Dequeue();
                    tries[_map.GetLocation(' ')] = FindAnagrams(trie, chars);
                    if (tries[_map.GetLocation(' ')] != null)
                        nonNull = true;
                    //chars.Enqueue(x);
                    //tries[_map.GetLocation(' ')] = new ImmutableTrie(new ImmutableTrie[0], true, _map);
                }
                char c = 'Z';
                for (int i = 0; i < chars.Count; i++)
                {
                    //if (c == chars.Peek())
                      //  continue;
                    c = chars.Dequeue();
                    tries[_map.GetLocation(c)] = FindAnagrams(lastWord.Continuations(c), chars);
                    if (tries[_map.GetLocation(c)] != null)
                        nonNull = true;
                    chars.Enqueue(c);
                }
            }
            if(nonNull)
                return new ImmutableTrie(tries, false, _map);
            return null;
        }