Class Combinations

java.lang.Object
org.hipparchus.util.Combinations
All Implemented Interfaces:
Iterable<int[]>

public class Combinations extends Object implements Iterable<int[]>
Utility to create combinations (n, k) of k elements in a set of n elements.
See Also:
  • Constructor Details

    • Combinations

      public Combinations(int n, int k)
      Creates an instance whose range is the k-element subsets of {0, ..., n - 1} represented as int[] arrays.

      The iteration order is lexicographic: the arrays returned by the iterator are sorted in descending order and they are visited in lexicographic order with significance from right to left. For example, new Combinations(4, 2).iterator() returns an iterator that will generate the following sequence of arrays on successive calls to next():
      [0, 1], [0, 2], [1, 2], [0, 3], [1, 3], [2, 3]

      If k == 0 an iterator containing an empty array is returned; if k == n an iterator containing [0, ..., n - 1] is returned.
      Parameters:
      n - Size of the set from which subsets are selected.
      k - Size of the subsets to be enumerated.
      Throws:
      MathIllegalArgumentException - if n < 0.
      MathIllegalArgumentException - if k > n.
  • Method Details

    • getN

      public int getN()
      Gets the size of the set from which combinations are drawn.
      Returns:
      the size of the universe.
    • getK

      public int getK()
      Gets the number of elements in each combination.
      Returns:
      the size of the subsets to be enumerated.
    • iterator

      public Iterator<int[]> iterator()
      Specified by:
      iterator in interface Iterable<int[]>
    • comparator

      public Comparator<int[]> comparator()
      Defines a lexicographic ordering of combinations. The returned comparator allows to compare any two combinations that can be produced by this instance's iterator. Its compare(int[],int[]) method will throw exceptions if passed combinations that are inconsistent with this instance:
      • if the array lengths are not equal to k,
      • if an element of the array is not within the interval [0, n).
      Returns:
      a lexicographic comparator.