Exemple #1
0
        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());
        }
Exemple #2
0
        // 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 <BagAnagrams> Prune(Bag bag, List <BagAnagrams> dictionary, done_pruning done_pruning_callback, uint recursion_level)
        {
            List <BagAnagrams> rv = new List <BagAnagrams>();

            foreach (BagAnagrams 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);
        }
Exemple #3
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);
        }