From b570a06f2bc0bd75e19e55c9f77e08cd7fa2536d Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Tue, 21 Mar 2017 11:09:25 +0300 Subject: [PATCH] Improved debugging --- WhiteRabbit/Program.cs | 10 ++++++++++ WhiteRabbit/StringsProcessor.cs | 5 +++++ WhiteRabbit/VectorsProcessor.cs | 6 ++++++ 3 files changed, 21 insertions(+) diff --git a/WhiteRabbit/Program.cs b/WhiteRabbit/Program.cs index 3ea8fc0..32dbb6e 100644 --- a/WhiteRabbit/Program.cs +++ b/WhiteRabbit/Program.cs @@ -86,5 +86,15 @@ yield return Encoding.ASCII.GetBytes(line); } } + +#if SINGLE_THREADED + private static void ForAll(this IEnumerable source, Action action) + { + foreach (var entry in source) + { + action(entry); + } + } +#endif } } diff --git a/WhiteRabbit/StringsProcessor.cs b/WhiteRabbit/StringsProcessor.cs index 30f9233..1a3b6fb 100644 --- a/WhiteRabbit/StringsProcessor.cs +++ b/WhiteRabbit/StringsProcessor.cs @@ -15,6 +15,7 @@ // Dictionary of vectors to array of words represented by this vector var vectorsToWords = words + .Where(word => word != null && word.Length > 0) .Select(word => new { word, vector = this.VectorsConverter.GetVector(word) }) .Where(tuple => tuple.vector != null) .Select(tuple => new { tuple.word, vector = tuple.vector.Value }) @@ -41,7 +42,11 @@ private int NumberOfCharacters { get; } +#if SINGLE_THREADED + public IEnumerable GeneratePhrases() +#else public ParallelQuery GeneratePhrases() +#endif { // 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(); diff --git a/WhiteRabbit/VectorsProcessor.cs b/WhiteRabbit/VectorsProcessor.cs index da2b4da..7eb61a3 100644 --- a/WhiteRabbit/VectorsProcessor.cs +++ b/WhiteRabbit/VectorsProcessor.cs @@ -37,10 +37,16 @@ private ImmutableArray Dictionary { get; } // Produces all sequences of vectors with the target sum +#if SINGLE_THREADED + public IEnumerable GenerateSequences() +#else public ParallelQuery GenerateSequences() +#endif { return GenerateUnorderedSequences(this.Target, GetVectorNorm(this.Target, this.Target), this.MaxVectorsCount, this.Dictionary, 0) +#if !SINGLE_THREADED .AsParallel() +#endif .Select(Enumerable.ToArray) .SelectMany(GeneratePermutations); }