Refactoring

master
Inga 🏳‍🌈 8 years ago
parent 9423f1e34f
commit 77d7071a18
  1. 15
      dotnet/WhiteRabbit/Program.cs
  2. 36
      dotnet/WhiteRabbit/StringsProcessor.cs
  3. 1
      dotnet/WhiteRabbit/WhiteRabbit.csproj

@ -78,14 +78,19 @@
stopwatch.Restart(); stopwatch.Restart();
processor.CheckPhrases(phraseSet => processor.CheckPhrases(phraseSet => ProcessPhraseSet(phraseSet, expectedHashesFirstComponents, stopwatch));
Console.WriteLine($"Done; time from start: {stopwatch.Elapsed}");
}
private static void ProcessPhraseSet(PhraseSet phraseSet, Vector<uint> expectedHashesFirstComponents, Stopwatch stopwatch)
{ {
phraseSet.ComputeMD5(); phraseSet.ComputeMD5();
for (var i = 0; i < phraseSet.Length; i++) for (var i = 0; i < phraseSet.Length; i++)
{ {
Debug.Assert( /*Debug.Assert(
sourceChars == ToOrderedChars(ToString(phraseSet, i)), sourceChars == ToOrderedChars(ToString(phraseSet, i)),
$"StringsProcessor produced incorrect anagram: {ToString(phraseSet, i)}"); $"StringsProcessor produced incorrect anagram: {ToString(phraseSet, i)}");*/
if (Vector.EqualsAny(expectedHashesFirstComponents, new Vector<uint>(phraseSet.GetMD5(i)))) if (Vector.EqualsAny(expectedHashesFirstComponents, new Vector<uint>(phraseSet.GetMD5(i))))
{ {
@ -94,10 +99,6 @@
Console.WriteLine($"Found phrase for {hash} ({phraseSet.GetMD5(i):x8}): {phrase}; time from start is {stopwatch.Elapsed}"); Console.WriteLine($"Found phrase for {hash} ({phraseSet.GetMD5(i):x8}): {phrase}; time from start is {stopwatch.Elapsed}");
} }
} }
});
Console.WriteLine($"Done; time from start: {stopwatch.Elapsed}");
} }
// Code taken from http://stackoverflow.com/a/321404/831314 // Code taken from http://stackoverflow.com/a/321404/831314

@ -66,23 +66,7 @@
var sums = this.VectorsProcessor.GenerateSequences(); var sums = this.VectorsProcessor.GenerateSequences();
// converting sequences of vectors to the sequences of words... // converting sequences of vectors to the sequences of words...
Parallel.ForEach(sums, new ParallelOptions { MaxDegreeOfParallelism = Constants.NumberOfThreads }, sum => Parallel.ForEach(sums, new ParallelOptions { MaxDegreeOfParallelism = Constants.NumberOfThreads }, sum => ProcessSum(sum, 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);
}
}
});
} }
public long GetPhrasesCount() public long GetPhrasesCount()
@ -133,5 +117,23 @@
return result; return result;
} }
private void ProcessSum(int[] sum, Action<PhraseSet> 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);
}
}
}
} }
} }

@ -61,7 +61,6 @@
<Compile Include="ByteArrayEqualityComparer.cs" /> <Compile Include="ByteArrayEqualityComparer.cs" />
<Compile Include="Constants.cs" /> <Compile Include="Constants.cs" />
<Compile Include="Flattener.cs" /> <Compile Include="Flattener.cs" />
<Compile Include="MD5Digest.cs" />
<Compile Include="PhraseSet.cs" /> <Compile Include="PhraseSet.cs" />
<Compile Include="PrecomputedPermutationsGenerator.cs" /> <Compile Include="PrecomputedPermutationsGenerator.cs" />
<Compile Include="PermutationsGenerator.cs" /> <Compile Include="PermutationsGenerator.cs" />

Loading…
Cancel
Save