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

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

The newest version!
package net.sf.javagimmicks.math;

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

/**
 * A class that sequentially returns all variations with repetition of a certain
 * number out of an array of given elements.
 * 
 * @author Hendrik Maryns
 * @param 
 *           The type of the elements of which variations are to be returned.
 */
public class VariatorWithRepetition extends CombinatoricOperator
{

   /**
    * Initialise a new variator, 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);
   }

   /**
    * Initialise a new variator, 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()
   {
      Arrays.fill(indices, 0);
   }

   @Override
   protected BigInteger calculateTotal(final int n, final int r)
   {
      return BigInteger.valueOf(n).pow(r);
   }

   @Override
   protected void computeNext()
   {
      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