/// <summary>
        /// Creates the specified number set.
        /// </summary>
        /// <param name="numberSet">The number set.</param>
        /// <returns></returns>
        public static IEnumerable <int[]> New(int[] numberSet)
        {
            var permutationEnumerator = PermutationEnumerator.Create(numberSet.Length).GetEnumerator();

            while (permutationEnumerator.MoveNext())
            {
                var permutation = permutationEnumerator.Current;
                var result      = new int[numberSet.Length];
                for (var i = 0; i < numberSet.Length; i++)
                {
                    result[i] = numberSet[permutation[i]];
                }

                yield return(result);
            }
        }
Пример #2
0
        protected internal static IEnumerable <int[]> CreateInternal(int[] numberSet)
        {
            for (var shiftCount = 0; shiftCount < numberSet.Length; shiftCount++)
            {
                int[] result = new int[numberSet.Length];
                int   count  = shiftCount;
                for (int i = 0; i < numberSet.Length; i++)
                {
                    int index = count + i;
                    if (index >= numberSet.Length)
                    {
                        index -= numberSet.Length;
                    }

                    result[i] = numberSet[index];
                }

                yield return(result);
            }

            // Initialize the permutation
            // simply always make 4 buckets
            var buckets = new Dictionary <int, List <int> >();

            for (int i = 0; i < numberSet.Length; i++)
            {
                int        bucketNum = i % 4;
                List <int> bucket    = buckets.Get(bucketNum);
                if (bucket == null)
                {
                    bucket             = new List <int>();
                    buckets[bucketNum] = bucket;
                }

                bucket.Add(numberSet[i]);
            }

            var permutationEnumerator = PermutationEnumerator.Create(4).GetEnumerator();

            // we throw the first one away, it is the same as a shift result
            permutationEnumerator.MoveNext();

            while (permutationEnumerator.MoveNext())
            {
                yield return(Translate(numberSet, buckets, permutationEnumerator.Current));
            }
        }
        private void TryPermutation(
            int numElements,
            int[][] expectedValues)
        {
            /*
             * Total: 4 * 3 * 2 = 24 = 6!  (6 faculty)
             *
             * Example:8
             * n / 6 = first number        == index 1, total {1}, remains {0, 2, 3}
             * remainder 8 - 1 * 6         == 2
             * n / 2 = second number       == index 1, total {1, 2}, remain {0, 3}
             * remainder 2 - 1 * 2         == 0
             *                          == total {1, 2, 0, 3}
             *
             * Example:21   out {0, 1, 2, 3}
             * 21 / 6                      == index 3 -> in {3}, out {0, 1, 2}
             * remainder 21 - 3 * 6        == 3
             * 3 / 2 = second number       == index 1 -> in {3, 1}, remain {0, 2}
             * remainder 3 - 1 * 2         == 1
             *                          == index 1 -> in {3, 1, 2} out {0}
             */
            var enumeration = PermutationEnumerator.Create(numElements).GetEnumerator();
            int count       = 0;

            while (enumeration.MoveNext())
            {
                int[] result   = enumeration.Current;
                int[] expected = expectedValues[count];

                Log.Debug(".tryPermutation result=" + CompatExtensions.RenderAny(result));
                Log.Debug(".tryPermutation expected=" + CompatExtensions.RenderAny(result));

                count++;
                Assert.IsTrue(Arrays.AreEqual(result, expected),
                              "Mismatch in count=" + count);
            }

            Assert.AreEqual(count, expectedValues.Length);
            Assert.That(enumeration.MoveNext(), Is.False);
        }
Пример #4
0
 public void TestInvalid()
 {
     Assert.That(() => PermutationEnumerator.Create(0), Throws.ArgumentException);
 }