示例#1
0
        private void MainForm_Load(object sender, EventArgs e)
        {
            string filename = DICTIONARY_FOLDER + "/" + "quran-words.txt";

            using (StreamReader reader = File.OpenText(filename))
            {
                ResultLabel.Text    = "Compiling dictionary ...";
                ProgressBar.Minimum = 0;
                ProgressBar.Maximum = (int)reader.BaseStream.Length;
                ProgressBar.Value   = 0;
                ListView_Resize(sender, e);
                try
                {
                    String line;
                    // Read and display lines from the file until the end of the file is reached.
                    int       linesRead          = 0;
                    Hashtable stringlists_by_bag = new Hashtable();
                    while ((line = reader.ReadLine()) != null)
                    {
                        line = line.ToLower();

                        Bag aBag = new Bag(line);
                        if (!stringlists_by_bag.ContainsKey(aBag))
                        {
                            strings l = new strings();
                            l.Add(line);
                            stringlists_by_bag.Add(aBag, l);
                        }
                        else
                        {
                            strings l = (strings)stringlists_by_bag[aBag];
                            if (!l.Contains(line))
                            {
                                l.Add(line);
                            }
                        }
                        linesRead++;
                        ProgressBar.Increment((line.Length + 2) * 2);   // the +1 is for the line ending character, I'd guess.
                        // the *2 is to deal with unicode characters
                        Application.DoEvents();
                    }

                    // Now convert the hash table, which isn't useful for
                    // actually generating anagrams, into a list, which is.
                    m_dictionary = new List <BagAnagrams>();
                    foreach (DictionaryEntry de in stringlists_by_bag)
                    {
                        m_dictionary.Add(new BagAnagrams((Bag)de.Key, (strings)de.Value));
                    }
                    m_dictionary.Sort();

                    // Now just for amusement, sort the list so that the biggest bags
                    // come first.  This might make more interesting anagrams appear first.
                    BagAnagrams[] sort_me = new BagAnagrams[m_dictionary.Count];
                    m_dictionary.CopyTo(sort_me);
                    Array.Sort(sort_me);
                    m_dictionary.Clear();
                    m_dictionary.InsertRange(0, sort_me);
                }
                catch (Exception ex)
                {
                    throw new Exception("Dictionary: " + ex.Message);
                }

                ResultLabel.Text       = "Ready.";
                ListView.Enabled       = true;
                LettersTextBox.Enabled = true;
                LettersTextBox.Focus();
                TypeUniqueLettersToolStripMenuItem_Click(sender, e);
            }

            //Bag.Test();
        }
示例#2
0
        public static anagrams anagrams(Bag bag,
                                        List <BagAnagrams> dictionary,
                                        uint recursion_level,
                                        bottom_of_main_loop bottom,
                                        done_pruning done_pruning_callback,
                                        found_anagram success_callback)
        {
            anagrams           rv     = new anagrams();
            List <BagAnagrams> pruned = Prune(bag,
                                              dictionary,
                                              done_pruning_callback,
                                              recursion_level);
            int pruned_initial_size = pruned.Count;

            while (pruned.Count > 0)
            {
                BagAnagrams entry    = pruned[0];
                Bag         this_bag = entry.b;
                Bag         diff     = bag.subtract(this_bag);
                if (diff != null)
                {
                    if (diff.empty())
                    {
                        foreach (string w in entry.words)
                        {
                            strings loner = new strings();
                            loner.Add(w);
                            rv.Add(loner);
                            if (recursion_level == 0)
                            {
                                success_callback(loner);
                            }
                        }
                    }
                    else
                    {
                        anagrams from_smaller = anagrams(diff, pruned, recursion_level + 1,
                                                         bottom,
                                                         done_pruning_callback,
                                                         success_callback);
                        anagrams combined = Combine(entry.words, from_smaller);
                        foreach (strings an in combined)
                        {
                            rv.Add(an);
                            if (recursion_level == 0)
                            {
                                success_callback(an);
                            }
                        }
                    }
                }
                pruned.RemoveAt(0);
                if (recursion_level == 0)
                {
                    bottom();
                }

                Application.DoEvents();
            }
            return(rv);
        }