public Bag subtract(Bag subtrahend) { string m = guts; string s = subtrahend.guts; string difference = ""; while (true) { if (s.Length == 0) return new Bag(difference + m); if (m.Length == 0) return null; { char s0 = s[0]; char m0 = m[0]; if (m0 > s0) return null; if (m0 < s0) { m = m.Substring(1); difference += m0; continue; } System.Diagnostics.Trace.Assert(m0 == s0, "internal error! Aggggh"); m = m.Substring(1); s = s.Substring(1); } } }
private void anagrams_Click(object sender, EventArgs e) { input.Enabled = false; Bag input_bag = new Bag(input.Text); listView1.Items.Clear(); fileToolStripMenuItem.Enabled = false; start_time = DateTime.Now; elapsed_time.Text = "00:00:00"; timer1.Enabled = true; ProgressBar.Value = 0; Anagrams.anagrams(input_bag, dictionary, 0, // bottom of main loop delegate() { ProgressBar.PerformStep(); Application.DoEvents(); }, // done pruning delegate(uint recursion_level, List<bag_and_anagrams> pruned_dict) { if (recursion_level == 0) { ProgressBar.Maximum = pruned_dict.Count; Application.DoEvents(); } }, // found a top-level anagram delegate(strings words) { string display_me = ""; foreach (string s in words) { if (display_me.Length > 0) display_me += " "; display_me += s; } listView1.Items.Add(display_me); listView1.EnsureVisible(listView1.Items.Count - 1); toolStripStatusLabel1.Text = listView1.Items.Count.ToString() + " anagrams so far"; if (listView1.Items.Count % 1000 == 0) { Application.DoEvents(); } }); timer1.Enabled = false; toolStripStatusLabel1.Text = String.Format("Done. {0} anagrams", listView1.Items.Count); if (listView1.Items.Count > 0) listView1.EnsureVisible(0); input.Enabled = true; input.Focus(); // the leading spaces work around a bug in the control: I // want the text centered, but that doesn't work. // Another workaround is for me to handle the // DrawColumnHeader event myself, but I'm too lazy to do that. listView1.Columns[0].Text = " Click to sort"; fileToolStripMenuItem.Enabled = true; }
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(); }
private static string subtract_strings(string minuend, string subtrahend) { Bag m = new Bag(minuend); Bag s = new Bag(subtrahend); Bag diff = m.subtract(s); if (diff == null) return null; return diff.AsString(); }
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(); }
private void Form1_Load(object sender, EventArgs e) { Bag.test(); }
private void anagrams_Click(object sender, EventArgs e) { input.Enabled = false; Bag input_bag = new Bag(input.Text); listView1.Items.Clear(); fileToolStripMenuItem.Enabled = false; start_time = DateTime.Now; elapsed_time.Text = "00:00:00"; timer1.Enabled = true; ProgressBar.Value = 0; Anagrams.anagrams(input_bag, dictionary, 0, // bottom of main loop delegate() { ProgressBar.PerformStep(); Application.DoEvents(); }, // done pruning delegate(uint recursion_level, List <bag_and_anagrams> pruned_dict) { if (recursion_level == 0) { ProgressBar.Maximum = pruned_dict.Count; Application.DoEvents(); } }, // found a top-level anagram delegate(strings words) { string display_me = ""; foreach (string s in words) { if (display_me.Length > 0) { display_me += " "; } display_me += s; } listView1.Items.Add(display_me); listView1.EnsureVisible(listView1.Items.Count - 1); toolStripStatusLabel1.Text = listView1.Items.Count.ToString() + " anagrams so far"; if (listView1.Items.Count % 1000 == 0) { Application.DoEvents(); } }); timer1.Enabled = false; toolStripStatusLabel1.Text = String.Format("Done. {0} anagrams", listView1.Items.Count); if (listView1.Items.Count > 0) { listView1.EnsureVisible(0); } input.Enabled = true; input.Focus(); // the leading spaces work around a bug in the control: I // want the text centered, but that doesn't work. // Another workaround is for me to handle the // DrawColumnHeader event myself, but I'm too lazy to do that. listView1.Columns[0].Text = " Click to sort"; fileToolStripMenuItem.Enabled = true; }