Class IntDistinctTuples

  • All Implemented Interfaces:
    IntCombinatorialPort, java.io.Serializable

    public final class IntDistinctTuples
    extends java.lang.Object
    implements IntCombinatorialPort
    Iterator over all distinct N-tuples, which can be chosen from N sets of integers. More formally, for N integer arrays: array1, array2,...,arrayN, this class allows to iterate over all possible integer arrays of the form [i1, i2,...,iN], where all numbers numbers ij are different and i1 is chosen from array1, i2 is chosen from array2 and so on.

    Consider the example:

     int[] a1 = {1, 2, 3};
     int[] a2 = {2, 3};
     IntDistinctTuples tuples = new IntDistinctTuples(a1, a2);
     int[] tuple;
     while ((tuple = tuples.take()) != null)
          System.out.println(Arrays.toString(tuple));
     
    This code will produce the following sequence:
     [1, 2]
     [1, 3]
     [2, 3]
     [3, 2]
     

    This class is implemented via output port pattern and the calculation of the next tuple occurs only on the invocation of take().

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

    Since:
    1.0
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      IntDistinctTuples​(int[]... sets)
      Create iterator over distinct tuples formed from the specified arrays (each set will be sorted in place)
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int[] getReference()
      Returns the reference to the current iteration element
      private void init()  
      void reset()
      Resets the iteration
      int[] take()
      Calculates and returns the next tuple, or null if no more distinct tuples exist.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • previousMask

        private final java.util.BitSet previousMask
      • setMasks

        private final java.util.BitSet[] setMasks
      • combination

        private final int[] combination
      • temp

        private final java.util.BitSet temp
      • state

        private byte state
    • Constructor Detail

      • IntDistinctTuples

        public IntDistinctTuples​(int[]... sets)
        Create iterator over distinct tuples formed from the specified arrays (each set will be sorted in place)
    • Method Detail

      • init

        private void init()
      • take

        public int[] take()
        Calculates and returns the next tuple, or null if no more distinct tuples exist.
        Specified by:
        take in interface IntCombinatorialPort
        Returns:
        the next tuple, or null if no more distinct tuples exist
      • getReference

        public int[] getReference()
        Description copied from interface: IntCombinatorialPort
        Returns the reference to the current iteration element
        Specified by:
        getReference in interface IntCombinatorialPort
        Returns:
        the reference to the current iteration element