Optimized anagrams count computation

unmanaged
Inga 🏳‍🌈 8 years ago
parent 5584ea843d
commit 55d721ffae
  1. 37
      dotnet/WhiteRabbit/PrecomputedPermutationsGenerator.cs
  2. 16
      dotnet/WhiteRabbit/StringsProcessor.cs

@ -5,22 +5,13 @@
internal static class PrecomputedPermutationsGenerator internal static class PrecomputedPermutationsGenerator
{ {
private static PermutationsGenerator.Permutation[][] Permutations { get; } = new[] private static PermutationsGenerator.Permutation[][] Permutations { get; } = Enumerable.Range(0, 8).Select(GeneratePermutations).ToArray();
{
GeneratePermutations(0), private static long[] PermutationsNumbers { get; } = GeneratePermutationsNumbers().Take(19).ToArray();
GeneratePermutations(1),
GeneratePermutations(2), public static PermutationsGenerator.Permutation[] HamiltonianPermutations(int n) => Permutations[n];
GeneratePermutations(3),
GeneratePermutations(4), public static long GetPermutationsNumber(int n) => PermutationsNumbers[n];
GeneratePermutations(5),
GeneratePermutations(6),
GeneratePermutations(7),
};
public static PermutationsGenerator.Permutation[] HamiltonianPermutations(int n)
{
return Permutations[n];
}
private static PermutationsGenerator.Permutation[] GeneratePermutations(int n) private static PermutationsGenerator.Permutation[] GeneratePermutations(int n)
{ {
@ -32,5 +23,19 @@
return result.Concat(Enumerable.Repeat(result[0], Constants.PhrasesPerSet - (result.Length % Constants.PhrasesPerSet))).ToArray(); return result.Concat(Enumerable.Repeat(result[0], Constants.PhrasesPerSet - (result.Length % Constants.PhrasesPerSet))).ToArray();
} }
private static IEnumerable<long> GeneratePermutationsNumbers()
{
long result = 1;
yield return result;
var i = 1;
while (true)
{
result *= i;
yield return result;
i++;
}
}
} }
} }

@ -68,9 +68,8 @@
public long GetPhrasesCount() public long GetPhrasesCount()
{ {
return this.VectorsProcessor.GenerateSequences() return this.VectorsProcessor.GenerateSequences()
.Select(this.ConvertVectorsToWords) .Select(this.ConvertVectorsToWordsNumber)
.SelectMany(Flattener.Flatten) .Sum(tuple => tuple.Item2 * PrecomputedPermutationsGenerator.GetPermutationsNumber(tuple.Item1));
.Sum(words => (long)PrecomputedPermutationsGenerator.HamiltonianPermutations(words.Length).Count());
} }
private byte[][][] ConvertVectorsToWords(int[] vectors) private byte[][][] ConvertVectorsToWords(int[] vectors)
@ -85,6 +84,17 @@
return words; return words;
} }
private Tuple<int, long> ConvertVectorsToWordsNumber(int[] vectors)
{
long result = 1;
for (var i = 0; i < vectors.Length; i++)
{
result *= this.WordsDictionary[vectors[i]].Length;
}
return Tuple.Create(vectors.Length, result);
}
private IEnumerable<PhraseSet> ConvertWordsToPhrases(byte[][] words) private IEnumerable<PhraseSet> ConvertWordsToPhrases(byte[][] words)
{ {
var permutations = PrecomputedPermutationsGenerator.HamiltonianPermutations(words.Length); var permutations = PrecomputedPermutationsGenerator.HamiltonianPermutations(words.Length);

Loading…
Cancel
Save