コード例 #1
0
        /// <inheritdoc cref="GetEnumerator"/>
        public IEnumerator <int[]> iterator()
        {
            if (k == 0 ||
                k == n)
            {
                return(new SingletonIterator(MathArrays.natural(k)));
            }

            switch (iterationOrder)
            {
            case IterationOrder.LEXICOGRAPHIC:
                return(new LexicographicIterator(n, k));

            default:
                throw new MathInternalError();     // Should never happen.
            }
        }
コード例 #2
0
        /// <inheritdoc/>
        /// <remarks>
        /// This method calls <see cref="MathArrays.shuffle(int[],RandomGenerator)"/>
        /// in order to create a random shuffle of the set
        /// of natural numbers <c>{ 0, 1, ..., n - 1 </c>}.
        /// </remarks>
        /// <exception cref="NumberIsTooLargeException"> if <c>k > n</c>.</exception>
        /// <exception cref="NotStrictlyPositiveException"> if <c>k <= 0</c>.</exception>
        public int[] nextPermutation(int n, int k)
        {
            if (k > n)
            {
                throw new NumberIsTooLargeException <Int32, Int32>(new LocalizedFormats("PERMUTATION_EXCEEDS_N"), k, n, true);
            }
            if (k <= 0)
            {
                throw new NotStrictlyPositiveException <Int32>(new LocalizedFormats("PERMUTATION_SIZE"), k);
            }

            int[] index = MathArrays.natural(n);
            MathArrays.shuffle(index, getRandomGenerator());

            // Return a new array containing the first "k" entries of "index".
            return(MathArrays.copyOf(index, k));
        }