From efd160cb9719a382ed4f09b21a1a8af0dc264e6a Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Thu, 4 May 2017 18:08:20 +0300 Subject: [PATCH] Refactoring --- dotnet/WhiteRabbit/Program.cs | 39 +++++++++----------------- dotnet/WhiteRabbit/StringsProcessor.cs | 10 +++---- dotnet/WhiteRabbit/VectorsProcessor.cs | 7 ----- 3 files changed, 18 insertions(+), 38 deletions(-) diff --git a/dotnet/WhiteRabbit/Program.cs b/dotnet/WhiteRabbit/Program.cs index eae601b..7aadf19 100644 --- a/dotnet/WhiteRabbit/Program.cs +++ b/dotnet/WhiteRabbit/Program.cs @@ -78,24 +78,23 @@ stopwatch.Restart(); - processor.GeneratePhrases() - .ForAll(phraseSet => + processor.CheckPhrases(phraseSet => + { + var hashesFirstComponents = MD5Digest.Compute(phraseSet); + for (var i = 0; i < Constants.PhrasesPerSet; i++) { - var hashesFirstComponents = MD5Digest.Compute(phraseSet); - for (var i = 0; i < Constants.PhrasesPerSet; i++) + Debug.Assert( + sourceChars == ToOrderedChars(ToString(phraseSet, i)), + $"StringsProcessor produced incorrect anagram: {ToString(phraseSet, i)}"); + + if (Vector.EqualsAny(expectedHashesFirstComponents, new Vector(hashesFirstComponents[i]))) { - Debug.Assert( - sourceChars == ToOrderedChars(ToString(phraseSet, i)), - $"StringsProcessor produced incorrect anagram: {ToString(phraseSet, i)}"); - - if (Vector.EqualsAny(expectedHashesFirstComponents, new Vector(hashesFirstComponents[i]))) - { - var phrase = ToString(phraseSet, i); - var hash = ComputeFullMD5(phrase); - Console.WriteLine($"Found phrase for {hash} ({hashesFirstComponents[i]:x8}): {phrase}; time from start is {stopwatch.Elapsed}"); - } + var phrase = ToString(phraseSet, i); + var hash = ComputeFullMD5(phrase); + Console.WriteLine($"Found phrase for {hash} ({hashesFirstComponents[i]:x8}): {phrase}; time from start is {stopwatch.Elapsed}"); } - }); + } + }); Console.WriteLine($"Done; time from start: {stopwatch.Elapsed}"); @@ -145,15 +144,5 @@ { return new string(source.Where(ch => ch != ' ').OrderBy(ch => ch).ToArray()); } - -#if SINGLE_THREADED - private static void ForAll(this IEnumerable source, Action action) - { - foreach (var entry in source) - { - action(entry); - } - } -#endif } } diff --git a/dotnet/WhiteRabbit/StringsProcessor.cs b/dotnet/WhiteRabbit/StringsProcessor.cs index d48671e..1159171 100644 --- a/dotnet/WhiteRabbit/StringsProcessor.cs +++ b/dotnet/WhiteRabbit/StringsProcessor.cs @@ -59,22 +59,20 @@ private int NumberOfCharacters { get; } -#if SINGLE_THREADED - public IEnumerable GeneratePhrases() -#else - public ParallelQuery GeneratePhrases() -#endif + public void CheckPhrases(Action action) { // task of finding anagrams could be reduced to the task of finding sequences of dictionary vectors with the target sum var sums = this.VectorsProcessor.GenerateSequences(); // converting sequences of vectors to the sequences of words... - return from sum in sums + var result = from sum in sums.AsParallel() let filter = ComputeFilter(sum) let wordsVariants = this.ConvertVectorsToWordIndexes(sum) from wordsArray in Flattener.Flatten(wordsVariants) from phraseSet in this.ConvertWordsToPhrases(wordsArray, filter) select phraseSet; + + result.ForAll(action); } public long GetPhrasesCount() diff --git a/dotnet/WhiteRabbit/VectorsProcessor.cs b/dotnet/WhiteRabbit/VectorsProcessor.cs index d9a5490..d9384d0 100644 --- a/dotnet/WhiteRabbit/VectorsProcessor.cs +++ b/dotnet/WhiteRabbit/VectorsProcessor.cs @@ -48,16 +48,9 @@ private ImmutableArray NormsIndex { get; } // Produces all sets of vectors with the target sum -#if SINGLE_THREADED public IEnumerable GenerateSequences() -#else - public ParallelQuery GenerateSequences() -#endif { return this.GenerateUnorderedSequences(this.Target, GetVectorNorm(this.Target, this.Target), this.MaxVectorsCount, 0) -#if !SINGLE_THREADED - .AsParallel() -#endif .Select(Enumerable.ToArray); }