Refactoring

master
Inga 🏳‍🌈 7 years ago
parent d13b94c3b6
commit efd160cb97
  1. 39
      dotnet/WhiteRabbit/Program.cs
  2. 10
      dotnet/WhiteRabbit/StringsProcessor.cs
  3. 7
      dotnet/WhiteRabbit/VectorsProcessor.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<uint>(hashesFirstComponents[i])))
{
Debug.Assert(
sourceChars == ToOrderedChars(ToString(phraseSet, i)),
$"StringsProcessor produced incorrect anagram: {ToString(phraseSet, i)}");
if (Vector.EqualsAny(expectedHashesFirstComponents, new Vector<uint>(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<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (var entry in source)
{
action(entry);
}
}
#endif
}
}

@ -59,22 +59,20 @@
private int NumberOfCharacters { get; }
#if SINGLE_THREADED
public IEnumerable<PhraseSet> GeneratePhrases()
#else
public ParallelQuery<PhraseSet> GeneratePhrases()
#endif
public void CheckPhrases(Action<PhraseSet> 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()

@ -48,16 +48,9 @@
private ImmutableArray<int> NormsIndex { get; }
// Produces all sets of vectors with the target sum
#if SINGLE_THREADED
public IEnumerable<int[]> GenerateSequences()
#else
public ParallelQuery<int[]> GenerateSequences()
#endif
{
return this.GenerateUnorderedSequences(this.Target, GetVectorNorm(this.Target, this.Target), this.MaxVectorsCount, 0)
#if !SINGLE_THREADED
.AsParallel()
#endif
.Select(Enumerable.ToArray);
}

Loading…
Cancel
Save