Refactoring

master
Inga 🏳‍🌈 7 years ago
parent d13b94c3b6
commit efd160cb97
  1. 13
      dotnet/WhiteRabbit/Program.cs
  2. 10
      dotnet/WhiteRabbit/StringsProcessor.cs
  3. 7
      dotnet/WhiteRabbit/VectorsProcessor.cs

@ -78,8 +78,7 @@
stopwatch.Restart();
processor.GeneratePhrases()
.ForAll(phraseSet =>
processor.CheckPhrases(phraseSet =>
{
var hashesFirstComponents = MD5Digest.Compute(phraseSet);
for (var i = 0; i < Constants.PhrasesPerSet; i++)
@ -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