<commit_before>package etomica.math.discrete;

import etomica.math.SpecialFunctions;
/**
 * @author kofke
 *
 * Iterator that returns different combinations of n integers taken k at a time.
 * For example, if constructed with n = 4 and k = 2, will return, in successive
 * calls to next(), the arrays {0,1},{0,2},{0,3},{1,2},{1,3},{2.3}.  Entries in
 * each array will be in numerical order.
 */

/* History
 * 10/30/03 (DAK) new
 */
 
public class CombinationIterator implements java.io.Serializable {

	private int[] index;
	private int n, k;
		
	/**
	 * Constructor for CombinationIterator.
	 */
	public CombinationIterator() {
		super();
	}

	public void reset(int n, int k) {
		if(n < 0 || k > n) throw new IllegalArgumentException("CombinationIterator must have 0 <= k <= n");
		this.n = n;
		this.k = k;
		index = new int[k];
		for(int i=0; i<k; i++) index[i] = i;
		if(k != 0) index[k-1]--;
	}
	
	public boolean hasNext() {
		if(index == null) return false;
		if(k == 0) return true;
		for(int j=0; j<k; j++) if(index[j] < n-k+j) return true;
		return false;
	}
	
	public int[] next() {
		if(k == 0) {
			index = null;
			return new int[] {};
		}
		int j = k-1;
		while(index[j] == n-k+j) j--;
		index[j]++;
		for(int i=j+1; i<k; i++) index[i] = index[i-1]+1;
		return index;
	}
	
	public static void main(String[] args) {
//		int n = Integer.parseInt(args[0]);
//		int k = Integer.parseInt(args[1]);
		int n = 10;
		int k = 10;
		CombinationIterator p = new CombinationIterator();
		p.reset(n,k);
		int count = 1;
		while(p.hasNext()) {
			int[] a = p.next();
			System.out.print(count++ +". {");
			for(int i=0; i<a.length-1; i++) System.out.print(a[i]+", ");
			if(a.length > 0) System.out.print(a[a.length-1]);
			System.out.println("}");
		}
		int s = SpecialFunctions.factorial(n)/SpecialFunctions.factorial(k)/SpecialFunctions.factorial(n-k);
		System.out.println("Expected total:" + s);
	}

}<commit_msg>change interface to make consistent with other iterators, which define reset with no arguments<commit_after>package etomica.math.discrete;

import etomica.math.SpecialFunctions;

/**
 * Iterator that returns different combinations of n integers taken k at a time.
 * For example, if constructed with n = 4 and k = 2, will return, in successive
 * calls to next(), the arrays {0,1},{0,2},{0,3},{1,2},{1,3},{2.3}. Entries in
 * each array will be in numerical order.
 * 
 * @author kofke
 * 
 */

public class CombinationIterator implements java.io.Serializable {

    /**
     * Constructor for CombinationIterator. Iterator requires reset before use.
     * Iterator parameters are final and cannot be changed after construction.
     * 
     * @param n
     *            the number of integers used to generate combinations
     * @param k
     *            the number of integers in each combination
     * 
     * @throws IllegalArgumentException
     *             if values do not satisfy 0 <= k <= n.
     */
    public CombinationIterator(int n, int k) {
        super();
        if (n < 0 || k > n) {
            throw new IllegalArgumentException("CombinationIterator must have 0 <= k <= n");
        }
        this.n = n;
        this.k = k;
        index = new int[k];
    }

    public void reset() {
        for (int i = 0; i < k; i++) {
            index[i] = i;
        }
        if (k != 0) {
            index[k - 1]--;
        } else {
            k0unset = false;
        }
    }

    public boolean hasNext() {
        if (k == 0) {
            return !k0unset;
        }
        for (int j = 0; j < k; j++) {
            if (index[j] < n - k + j) {
                return true;
            }
        }
        return false;
    }

    public int[] next() {
        if (k == 0) {
            k0unset = true;
            return index;
        }
        int j = k - 1;
        while (index[j] == n - k + j)
            j--;
        index[j]++;
        for (int i = j + 1; i < k; i++)
            index[i] = index[i - 1] + 1;
        return index;
    }

    public static void main(String[] args) {
        // int n = Integer.parseInt(args[0]);
        // int k = Integer.parseInt(args[1]);
        int n = 3;
        int k = 2;
        CombinationIterator p = new CombinationIterator(n, k);
        p.reset();
        int count = 1;
        while (p.hasNext()) {
            int[] a = p.next();
            System.out.print(count++ + ". {");
            for (int i = 0; i < a.length - 1; i++)
                System.out.print(a[i] + ", ");
            if (a.length > 0)
                System.out.print(a[a.length - 1]);
            System.out.println("}");
        }
        int s = SpecialFunctions.factorial(n) / SpecialFunctions.factorial(k) / SpecialFunctions.factorial(n - k);
        System.out.println("Expected total:" + s);
    }

    private final int[] index;
    private final int n, k;
    private boolean k0unset = true;
    private static final long serialVersionUID = 1L;

}
{'fraction_non_alphanumeric': 0.14929006085192698, 'fraction_numerical': 0.017038539553752535, 'mean_word_length': 2.097361809045226, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2881810', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1613, 'n_words': 489}