Class Combinations

java.lang.Object
org.apache.commons.numbers.combinatorics.Combinations
All Implemented Interfaces:
Iterable<int[]>

public final class Combinations extends Object implements Iterable<int[]>
Utility to create combinations (n, k) of k elements in a set of n elements.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static class 
    Defines a lexicographic ordering of the combinations.
    private static class 
    Lexicographic combinations iterator.
    private static class 
    Iterator with just one element to handle degenerate cases (full array, empty array) for combination iterator.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final int
    Number of elements in each combination.
    private final int
    Size of the set from which combinations are drawn.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Combinations(int n, int k)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Comparator<int[]>
    Creates a comparator.
    int
    Gets the number of elements in each combination.
    int
    Gets the size of the set from which combinations are drawn.
    Iterator<int[]>
    Creates an iterator whose range is the k-element subsets of {0, ..., n - 1} represented as int[] arrays.
    of(int n, int k)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Field Details

    • n

      private final int n
      Size of the set from which combinations are drawn.
    • k

      private final int k
      Number of elements in each combination.
  • Constructor Details

    • Combinations

      private Combinations(int n, int k)
      Parameters:
      n - Size of the set from which subsets are selected.
      k - Size of the subsets to be enumerated.
      Throws:
      IllegalArgumentException - if n < 0.
      IllegalArgumentException - if k > n or k < 0.
  • Method Details

    • of

      public static Combinations of(int n, int k)
      Parameters:
      n - Size of the set from which subsets are selected.
      k - Size of the subsets to be enumerated.
      Returns:
      a new instance.
      Throws:
      IllegalArgumentException - if n < 0.
      IllegalArgumentException - if k > n or k < 0.
    • 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()
      Creates an iterator 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.
      Specified by:
      iterator in interface Iterable<int[]>
    • comparator

      public Comparator<int[]> comparator()
      Creates a comparator. When performing a comparison, if an element of the array is not within the interval [0, n), an IllegalArgumentException will be thrown.
      Returns:
      a comparator.