public static anagrams anagrams(Bag bag, List <bag_and_anagrams> dictionary, uint recursion_level, bottom_of_main_loop bottom, done_pruning done_pruning_callback, found_anagram success_callback) { anagrams rv = new anagrams(); List <bag_and_anagrams> pruned = prune(bag, dictionary, done_pruning_callback, recursion_level); int pruned_initial_size = pruned.Count; while (pruned.Count > 0) { bag_and_anagrams 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); }
private void Form1_Shown(object sender, EventArgs e) { System.IO.Stream wordlist_stream; System.Reflection.Assembly thisExe; thisExe = System.Reflection.Assembly.GetExecutingAssembly(); wordlist_stream = thisExe.GetManifestResourceStream("Anagrams.words"); System.Diagnostics.Trace.Assert(wordlist_stream != null, "Uh oh, can't find word list inside myself!"); toolStripStatusLabel1.Text = "Compiling dictionary ..."; ProgressBar.Value = 0; ProgressBar.Maximum = (int)wordlist_stream.Length; listView1_Resize(sender, e); using (StreamReader sr = new StreamReader(wordlist_stream)) { 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 = sr.ReadLine()) != null) { // TODO -- filter out nonletters. Thus "god's" // should become "gods". And since both of those // are likely to appear, we need to ensure that we // only store one. line = line.ToLower(); if (!acceptable(line)) { continue; } 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 + 1); // the +1 is for the line ending character, I'd guess. Application.DoEvents(); } // Now convert the hash table, which isn't useful for // actually generating anagrams, into a list, which is. dictionary = new List <bag_and_anagrams>(); foreach (DictionaryEntry de in stringlists_by_bag) { dictionary.Add(new bag_and_anagrams((Bag)de.Key, (strings)de.Value)); } // Now just for amusement, sort the list so that the biggest bags // come first. This might make more interesting anagrams appear first. bag_and_anagrams[] sort_me = new bag_and_anagrams[dictionary.Count]; dictionary.CopyTo(sort_me); Array.Sort(sort_me); dictionary.Clear(); dictionary.InsertRange(0, sort_me); } toolStripStatusLabel1.Text = "Compiling dictionary ... done."; listView1.Enabled = true; input.Enabled = true; input.Focus(); }