// Path: src/main/java/com/tiemens/secretshare/exceptions/SecretShareException.java
// public class SecretShareException
//     extends RuntimeException
// {
//     // ==================================================
//     // class static data
//     // ==================================================
// 
//     private static final long serialVersionUID = 2193317873773384997L;
// 
//     // ==================================================
//     // class static methods
//     // ==================================================
// 
// 
// 
//     // ==================================================
//     // instance data
//     // ==================================================
// 
//     // ==================================================
//     // factories
//     // ==================================================
// 
//     // ==================================================
//     // constructors
//     // ==================================================
// 
// 
//     /** Constructs a new runtime exception with the specified detail message.
//      * The cause is not initialized, and may subsequently be initialized by a
//      * call to {@link #initCause}.
//      *
//      * @param   message   the detail message. The detail message is saved for
//      *          later retrieval by the {@link #getMessage()} method.
//      */
//     public SecretShareException(final String message)
//     {
//         super(message);
//     }
// 
//     /**
//      * Constructs a new runtime exception with the specified detail message and
//      * cause.  <p>Note that the detail message associated with
//      * <code>cause</code> is <i>not</i> automatically incorporated in
//      * this runtime exception's detail message.
//      *
//      * @param  message the detail message (which is saved for later retrieval
//      *         by the {@link #getMessage()} method).
//      * @param  cause the cause (which is saved for later retrieval by the
//      *         {@link #getCause()} method).  (A <code>null</code> value is
//      *         permitted, and indicates that the cause is nonexistent or
//      *         unknown.)
//      */
//     public SecretShareException(final String message,
//                                 final Throwable cause)
//     {
//         super(message, cause);
//     }
// 
//     // ==================================================
//     // public methods
//     // ==================================================
// 
//     // ==================================================
//     // non public methods
//     // ==================================================
// }


import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import com.tiemens.secretshare.exceptions.SecretShareException;
/*******************************************************************************
 * Copyright (c) 2009, 2014 Tim Tiemens.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 *
 * Contributors:
 *     Tim Tiemens - initial API and implementation
 *******************************************************************************/
package com.tiemens.secretshare.math.combination;



public class CombinationGenerator<E>
        implements Iterator<List<E>>,
                   Iterable<List<E>>
{
    // ==================================================
    // class static data
    // ==================================================

    // ==================================================
    // class static methods
    // ==================================================

    public static void main(String[] args)
    {
        System.out.println("fact(5)=" + factorial(5));
        System.out.println("fact(3)=" + factorial(3));
        List<String> list = Arrays.asList("1", "2", "3", "4", "5");
        CombinationGenerator<String> combos = new CombinationGenerator<String>(list, 3);
        int count = 1;
        System.out.println("Total number=" + combos.getTotalNumberOfCombinations());
        for (List<String> combination : combos)
        {
            System.out.println(count + ": " + combination + " {" + combos.indexesAsString + "}");
            count++;
        }
    }

    // ==================================================
    // instance data
    // ==================================================

    private final List<E> list;

    // currentIndexes contains the indexes to use for the NEXT iteration
    private int[] currentIndexes;

    // indexesAsString contains either null or the CURRENT iteration's indexes
    private String indexesAsString;

    //
    private final BigInteger totalNumberOfCombinations;
    // ranges 0 to totalNumber, where "0" means "you haven't called next() yet"
    private BigInteger combinationNumber;

    // ==================================================
    // factories
    // ==================================================

    // ==================================================
    // constructors
    // ==================================================

    public CombinationGenerator(final List<E> inList,
                                final int inChoiceSize)
    {
        if (inChoiceSize < 1)
        {
// Path: src/main/java/com/tiemens/secretshare/exceptions/SecretShareException.java
// public class SecretShareException
//     extends RuntimeException
// {
//     // ==================================================
//     // class static data
//     // ==================================================
// 
//     private static final long serialVersionUID = 2193317873773384997L;
// 
//     // ==================================================
//     // class static methods
//     // ==================================================
// 
// 
// 
//     // ==================================================
//     // instance data
//     // ==================================================
// 
//     // ==================================================
//     // factories
//     // ==================================================
// 
//     // ==================================================
//     // constructors
//     // ==================================================
// 
// 
//     /** Constructs a new runtime exception with the specified detail message.
//      * The cause is not initialized, and may subsequently be initialized by a
//      * call to {@link #initCause}.
//      *
//      * @param   message   the detail message. The detail message is saved for
//      *          later retrieval by the {@link #getMessage()} method.
//      */
//     public SecretShareException(final String message)
//     {
//         super(message);
//     }
// 
//     /**
//      * Constructs a new runtime exception with the specified detail message and
//      * cause.  <p>Note that the detail message associated with
//      * <code>cause</code> is <i>not</i> automatically incorporated in
//      * this runtime exception's detail message.
//      *
//      * @param  message the detail message (which is saved for later retrieval
//      *         by the {@link #getMessage()} method).
//      * @param  cause the cause (which is saved for later retrieval by the
//      *         {@link #getCause()} method).  (A <code>null</code> value is
//      *         permitted, and indicates that the cause is nonexistent or
//      *         unknown.)
//      */
//     public SecretShareException(final String message,
//                                 final Throwable cause)
//     {
//         super(message, cause);
//     }
// 
//     // ==================================================
//     // public methods
//     // ==================================================
// 
//     // ==================================================
//     // non public methods
//     // ==================================================
// }


// Path: src/main/java/com/tiemens/secretshare/math/combination/CombinationGenerator.java
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import com.tiemens.secretshare.exceptions.SecretShareException;

/*******************************************************************************
 * Copyright (c) 2009, 2014 Tim Tiemens.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 *
 * Contributors:
 *     Tim Tiemens - initial API and implementation
 *******************************************************************************/
package com.tiemens.secretshare.math.combination;



public class CombinationGenerator<E>
        implements Iterator<List<E>>,
                   Iterable<List<E>>
{
    // ==================================================
    // class static data
    // ==================================================

    // ==================================================
    // class static methods
    // ==================================================

    public static void main(String[] args)
    {
        System.out.println("fact(5)=" + factorial(5));
        System.out.println("fact(3)=" + factorial(3));
        List<String> list = Arrays.asList("1", "2", "3", "4", "5");
        CombinationGenerator<String> combos = new CombinationGenerator<String>(list, 3);
        int count = 1;
        System.out.println("Total number=" + combos.getTotalNumberOfCombinations());
        for (List<String> combination : combos)
        {
            System.out.println(count + ": " + combination + " {" + combos.indexesAsString + "}");
            count++;
        }
    }

    // ==================================================
    // instance data
    // ==================================================

    private final List<E> list;

    // currentIndexes contains the indexes to use for the NEXT iteration
    private int[] currentIndexes;

    // indexesAsString contains either null or the CURRENT iteration's indexes
    private String indexesAsString;

    //
    private final BigInteger totalNumberOfCombinations;
    // ranges 0 to totalNumber, where "0" means "you haven't called next() yet"
    private BigInteger combinationNumber;

    // ==================================================
    // factories
    // ==================================================

    // ==================================================
    // constructors
    // ==================================================

    public CombinationGenerator(final List<E> inList,
                                final int inChoiceSize)
    {
        if (inChoiceSize < 1)
        {
            throw new SecretShareException("choice size cannot be less than 1:" + inChoiceSize);