Ejemplo n.º 1
0
        /// <summary>
        /// Constructor... creates new Trie with given properrties
        /// </summary>
        /// <param name="children">Children tries in tree</param>
        /// <param name="containsEmptyString">Boolean if it is a word</param>
        /// <param name="map">Map of Chars to Indices and vice versa to use</param>
        public ImmutableTrie(ImmutableTrie[] children, bool containsEmptyString, CharMap map)
        {
            _containsEmptyString = containsEmptyString;
            _map = map;

            _children = (ImmutableTrie[])children.Clone();
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Private method to implement adding to IList
 /// </summary>
 /// <param name="list">List to add to</param>
 /// <param name="builder">StringBuilder of prefixes</param>
 /// <param name="trie">Trie to add from</param>
 private static void CopyToList(IList list, StringBuilder builder, ImmutableTrie trie)
 {
     if (trie != null)
     {
         for (int i = 0; i < trie._children.Length; i++)
         {
             builder.Append(trie._map.GetChar(i));
             CopyToList(list, builder, trie._children[i]);
             builder.Remove(builder.Length - 1, 1);
         }
         if (trie._containsEmptyString)
             list.Add(builder.ToString());
     }
 }
Ejemplo n.º 3
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;
        }