static private 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()); }
// return a list that is like dictionary, but which contains only those items which can be made from the letters in bag. private static List <bag_and_anagrams> prune(Bag bag, List <bag_and_anagrams> dictionary, done_pruning done_pruning_callback, uint recursion_level) { List <bag_and_anagrams> rv = new List <bag_and_anagrams>(); foreach (bag_and_anagrams pair in dictionary) { Bag this_bag = pair.b; if (bag.subtract(this_bag) != null) { rv.Add(pair); } } done_pruning_callback(recursion_level, rv); return(rv); }
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 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(); }