Class IntCombinationsWithPermutations

java.lang.Object
cc.redberry.combinatorics.IntCombinatorialIterator
cc.redberry.combinatorics.IntCombinationsWithPermutations
All Implemented Interfaces:
CombinatorialIterator<int[]>, Serializable, Iterable<int[]>, Iterator<int[]>

public final class IntCombinationsWithPermutations extends IntCombinatorialIterator
Iterator over over all possible unique combinations with permutations (i.e. {0,1} and {1,0} both will appear in the iteration) of k numbers, which can be chosen from the set of n numbers (0,1,2,...,n). The total number of such combinations will be n!/(n-k)!.

For example, for k=2 and n=3, it will produce the following arrays sequence: [0,1], [1,0], [0,2], [2,0], [1,2], [2,1].

The iterator is implemented such that each next combination will be calculated only on the invocation of method next() (no pre-calculation of results).

Note: method next() returns the same reference on each invocation.

Internally this class uses combination of IntCombinations and IntPermutations.

Since:
1.0
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • permutation

      private final int[] permutation
    • combination

      private final int[] combination
    • combinationPermutation

      private final int[] combinationPermutation
    • permutationsGenerator

      private final IntPermutations permutationsGenerator
    • combinationsGenerator

      private final IntCombinations combinationsGenerator
    • k

      private final int k
  • Constructor Details

    • IntCombinationsWithPermutations

      public IntCombinationsWithPermutations(int n, int k)
      Constructs the iterator with the desired n and k
  • Method Details

    • hasNext

      public boolean hasNext()
    • next

      public int[] next()
      Calculates and returns the next combination.
      Returns:
      the next combination
    • remove

      public void remove()
      Throws UnsupportedOperationException.
    • reset

      public void reset()
      Description copied from interface: CombinatorialIterator
      Resets the iteration
    • current

      public int[] current()
      Description copied from interface: CombinatorialIterator
      Returns the reference on the current iteration element.
      Returns:
      the reference on the current iteration element