All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.javagimmicks.math.combinatorics.VariatorWithRepetition Maven / Gradle / Ivy

There is a newer version: 0.99-alpha1
Show newest version
package net.sf.javagimmicks.math.combinatorics;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collection;

/**
 * A class that sequentially returns all variations with repetition of a given
 * length out of an array of given elements.
 * 

* Example: a call to * new VariatorWithRepetition(2, "a", "b", "c") would return the * following combinations: *

    *
  • [a, a]
  • *
  • [a, b]
  • *
  • [a, c]
  • *
  • [b, a]
  • *
  • [b, b]
  • *
  • [b, c]
  • *
  • [c, a]
  • *
  • [c, b]
  • *
  • [c, c]
  • *
* * @param * The type of the elements of which variations are to be returned. */ public class VariatorWithRepetition extends CombinatoricIterable { /** * Initialize a instance with given elements and size of the arrays to be * returned. * * @param elements * The elements of which variations have to be computed. * @param r * The size of the variations to compute. */ public VariatorWithRepetition(final T[] elements, final int r) { super(elements, r); } /** * Initialize a instance with given elements and size of the arrays to be * returned. * * @param elements * The elements of which variations have to be computed. * @param r * The size of the variations to compute. */ public VariatorWithRepetition(final int r, final T... elements) { super(elements, r); } /** * Initialize a new instance with given elements and size of the arrays to be * returned. * * @param elements * The elements of which variations have to be computed. * @param r * The size of the variations to compute. */ public VariatorWithRepetition(final Collection elements, final int r) { super(elements, r); } /** * Initialise the array of indices. For variations with repetition, it needs * to be initialised with all 0s */ @Override protected void initialiseIndices(final int[] indices) { Arrays.fill(indices, 0); } @Override protected BigInteger calculateTotal(final int n, final int r) { return BigInteger.valueOf(n).pow(r); } @Override protected void computeNext(final int[] indices) { int i = indices.length - 1; final int n = _elements.size(); while (++indices[i] == n && i > 0) { indices[i--] = 0; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy