Beispiel #1
0
        public static void Main(string[] args)
        {
            CheckArguments(args);

            // ReSharper disable once PossibleNullReferenceException
            var path = args[0];
            var word = args[1];

            using (var profiler = new Profiler("Anagram experiment"))
            {
                var dictionary = AnagramDictionary.Create().WithFile(path);
                profiler.Print("Creating anagram dictionary");

                var anagrams = dictionary.LookUpWord(word);
                profiler.Print($"Looking up anagrams of {word}");

                Console.WriteLine("Anagrams:");
                if (anagrams.Any())
                {
                    foreach (var anagram in anagrams)
                    {
                        Console.WriteLine($"- {anagram}");
                    }
                }
                else
                {
                    Console.WriteLine("No anagram found.");
                }
                profiler.Print("Printing anagrams");
            }

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
Beispiel #2
0
        public static AnagramDictionary WithFile(this AnagramDictionary anagramDictionary, string path)
        {
            ValidatePath(path);
            var taskFactory = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.None);
            var readTask    = taskFactory.StartNew(() =>
                                                   Parallel.ForEach(File.ReadLines(path, Encoding.UTF8).AsParallel(),
                                                                    anagramDictionary.AddToWordList));

            Task.WaitAll(readTask);
            anagramDictionary.CompleteWordList();

            var fillTask = taskFactory.StartNew(() =>
                                                Parallel.ForEach(anagramDictionary.GetWordList(), anagramDictionary.AddToDictionary));

            Task.WaitAll(fillTask);

            return(anagramDictionary);
        }