Ejemplo n.º 1
0
        /// <summary>
        /// Internal method that recursively finds anagram matches in the DAWG.
        /// </summary>
        /// <param name="soFar">The string that has been built from the frequencies.</param>
        /// <param name="length">The current length of the buffer of characters.</param>
        /// <param name="currentLine">The line number for both DAWG files.</param>
        /// <param name="frequencyInts">The frequencies of various letters in
        /// the input word.</param>
        /// <param name="resultList">Results built up from the input word.</param>
        /// <param name="superHere">Contains bits set that quickly tell us what
        /// letters we have.</param>
        private void FindRecurse(char[] soFar, int length, int currentLine,
                                 int[] frequencyInts, List <string> resultList, int superHere)
        {
            // Try to store this path as a result if it has the full word bit set.
            if ((superHere & _fullWord) != 0)
            {
                char[] newArray = new char[length];
                for (int i = length - 1; i >= 0; --i)
                {
                    newArray[i] = soFar[i];
                }
                resultList.Add(new string(newArray));
            }

            // Check all letters.
            int characterOrder = 0;

            for (int charIndex = 0; charIndex < 26; charIndex++)
            {
                // See if word continues to this letter.
                if ((superHere & (1 << charIndex)) != 0)
                {
                    // We can make this letter. Now, do we have it in our word?
                    int freq = frequencyInts[charIndex];
                    if (freq > 0)
                    {
                        // We have this letter in our rack, but have we used it already?
                        char wordChar = (char)(charIndex + _offset);
                        int  freqPass = FrequencyInWord(soFar, wordChar, length);

                        // if (remaining > 0)
                        if (freq > freqPass)
                        {
                            // Get the new line for our numbers.
                            int newLine = _dawg.GetNodeAt(currentLine, characterOrder);

                            int newSuper = _dawg.GetSuperAt(newLine);

                            // Make the new string.
                            soFar[length] = wordChar;

                            // Recurse onto the next letter.
                            FindRecurse(soFar, length + 1, newLine, frequencyInts,
                                        resultList, newSuper);
                        }
                    }
                    // Increment the current character's order index.
                    characterOrder++;
                }
            }
        }