View Javadoc

1   /*
2    * VariatorWithRepetition.java
3    *
4    * Created on 7-mrt-2006
5    *
6    * Copyright (C) 2006 Hendrik Maryns <hendrik@sfs.uni-tuebingen.de>.
7    *
8    * This program is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License
10   * as published by the Free Software Foundation; either version 2
11   * of the License, or (at your option) any later version.
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19   */
20  package combinatorics;
21  
22  import java.util.Arrays;
23  
24  /**
25   * A class that sequentially returns all variations with repetition of a certain 
26   * number out of an array of given elements.
27   *
28   * @author <a href="mailto:hendrik.maryns@uni-tuebingen.de">Hendrik Maryns</a>
29   * @param <T>  The type of the elements of which variations are to be 
30   *         returned.
31   */
32  public final class VariatorWithRepetition<T> extends CombinatoricOperator<T> {
33  
34  	/**
35  	 * Initialise a new variator, with given elements and size of the arrays 
36  	 * to be returned.
37  	 *
38  	 * @param elements
39  	 *       	The elements of which variations have to be computed.
40  	 * @param r
41  	 *       	The size of the variations to compute.
42  	 * @pre    	r should not be smaller than 0.
43  	 *     | 0 <= r
44  	 * @post	The total number of iterations is set to the number of elements
45  	 * 			to the rth power.
46  	 *     | new.getTotal() == BigInteger.valueOf(elements.length).pow(r)
47  	 * @post  The number of variations left is set to the total number.
48  	 *     | new.getNumLeft() == new.getTotal()
49  	 */
50  	public VariatorWithRepetition(final T[] elements, final int r) {
51  		super(elements, r);
52  	}
53  
54  	/**
55  	 * Initialise the array of indices.  For variations with repetition, it 
56  	 * needs to be initialised with all 0s.
57  	 */
58  	@Override
59  	protected void initialiseIndices() {
60  		Arrays.fill(this.indices, 0);
61  	}
62  	
63  	/**
64  	 * Compute the total number of elements to return.
65  	 *
66  	 * @see CombinatoricOperator#initialiseTotal()
67  	 */
68  	@Override
69  	protected double initialiseTotal(final int n, final int r) {
70  		return  Math.pow(n, r);
71  	}
72  
73  	/**
74  	 * Compute the next array of indices.
75  	 *
76  	 * @see CombinatoricOperator#computeNext()
77  	 */
78  	@Override
79  	protected void computeNext() {
80  		int i = this.indices.length - 1;
81  		final int n = this.elements.length;
82  		while (++this.indices[i] == n && i > 0) {
83  			this.indices[i--] = 0;
84  		}
85  	}
86  
87  }