From 77d7071a18485b132cdeeecbbeebad82de061eec Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Fri, 5 May 2017 15:44:44 +0300 Subject: [PATCH] Refactoring --- dotnet/WhiteRabbit/Program.cs | 35 +++++++++++++------------ dotnet/WhiteRabbit/StringsProcessor.cs | 36 ++++++++++++++------------ dotnet/WhiteRabbit/WhiteRabbit.csproj | 1 - 3 files changed, 37 insertions(+), 35 deletions(-) diff --git a/dotnet/WhiteRabbit/Program.cs b/dotnet/WhiteRabbit/Program.cs index 750b292..0ebed3a 100644 --- a/dotnet/WhiteRabbit/Program.cs +++ b/dotnet/WhiteRabbit/Program.cs @@ -78,26 +78,27 @@ stopwatch.Restart(); - processor.CheckPhrases(phraseSet => - { - phraseSet.ComputeMD5(); - for (var i = 0; i < phraseSet.Length; i++) - { - Debug.Assert( - sourceChars == ToOrderedChars(ToString(phraseSet, i)), - $"StringsProcessor produced incorrect anagram: {ToString(phraseSet, i)}"); - - if (Vector.EqualsAny(expectedHashesFirstComponents, new Vector(phraseSet.GetMD5(i)))) - { - var phrase = ToString(phraseSet, i); - var hash = ComputeFullMD5(phrase); - Console.WriteLine($"Found phrase for {hash} ({phraseSet.GetMD5(i):x8}): {phrase}; time from start is {stopwatch.Elapsed}"); - } - } - }); + processor.CheckPhrases(phraseSet => ProcessPhraseSet(phraseSet, expectedHashesFirstComponents, stopwatch)); Console.WriteLine($"Done; time from start: {stopwatch.Elapsed}"); + } + + private static void ProcessPhraseSet(PhraseSet phraseSet, Vector expectedHashesFirstComponents, Stopwatch stopwatch) + { + phraseSet.ComputeMD5(); + for (var i = 0; i < phraseSet.Length; i++) + { + /*Debug.Assert( + sourceChars == ToOrderedChars(ToString(phraseSet, i)), + $"StringsProcessor produced incorrect anagram: {ToString(phraseSet, i)}");*/ + if (Vector.EqualsAny(expectedHashesFirstComponents, new Vector(phraseSet.GetMD5(i)))) + { + var phrase = ToString(phraseSet, i); + var hash = ComputeFullMD5(phrase); + Console.WriteLine($"Found phrase for {hash} ({phraseSet.GetMD5(i):x8}): {phrase}; time from start is {stopwatch.Elapsed}"); + } + } } // Code taken from http://stackoverflow.com/a/321404/831314 diff --git a/dotnet/WhiteRabbit/StringsProcessor.cs b/dotnet/WhiteRabbit/StringsProcessor.cs index 0ddbb3a..150b62a 100644 --- a/dotnet/WhiteRabbit/StringsProcessor.cs +++ b/dotnet/WhiteRabbit/StringsProcessor.cs @@ -66,23 +66,7 @@ var sums = this.VectorsProcessor.GenerateSequences(); // converting sequences of vectors to the sequences of words... - Parallel.ForEach(sums, new ParallelOptions { MaxDegreeOfParallelism = Constants.NumberOfThreads }, sum => - { - var phraseSet = new PhraseSet(Constants.PhrasesPerSet); - var filter = ComputeFilter(sum); - var wordsVariants = this.ConvertVectorsToWordIndexes(sum); - foreach (var wordsArray in Flattener.Flatten(wordsVariants)) - { - //Console.WriteLine(new string(wordsArray.SelectMany(wordIndex => this.AllWords[wordIndex].Original).Select(b => (char)b).ToArray())); - - var permutations = PrecomputedPermutationsGenerator.HamiltonianPermutations(wordsArray.Length, filter); - for (var i = 0; i < permutations.Length; i += Constants.PhrasesPerSet) - { - phraseSet.FillPhraseSet(this.AllWords, wordsArray, permutations, i, this.NumberOfCharacters); - action(phraseSet); - } - } - }); + Parallel.ForEach(sums, new ParallelOptions { MaxDegreeOfParallelism = Constants.NumberOfThreads }, sum => ProcessSum(sum, action)); } public long GetPhrasesCount() @@ -133,5 +117,23 @@ return result; } + + private void ProcessSum(int[] sum, Action action) + { + var phraseSet = new PhraseSet(Constants.PhrasesPerSet); + var filter = ComputeFilter(sum); + var wordsVariants = this.ConvertVectorsToWordIndexes(sum); + foreach (var wordsArray in Flattener.Flatten(wordsVariants)) + { + //Console.WriteLine(new string(wordsArray.SelectMany(wordIndex => this.AllWords[wordIndex].Original).Select(b => (char)b).ToArray())); + + var permutations = PrecomputedPermutationsGenerator.HamiltonianPermutations(wordsArray.Length, filter); + for (var i = 0; i < permutations.Length; i += Constants.PhrasesPerSet) + { + phraseSet.FillPhraseSet(this.AllWords, wordsArray, permutations, i, this.NumberOfCharacters); + action(phraseSet); + } + } + } } } diff --git a/dotnet/WhiteRabbit/WhiteRabbit.csproj b/dotnet/WhiteRabbit/WhiteRabbit.csproj index 4355ab4..2739e27 100644 --- a/dotnet/WhiteRabbit/WhiteRabbit.csproj +++ b/dotnet/WhiteRabbit/WhiteRabbit.csproj @@ -61,7 +61,6 @@ -