package positronic.math; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Vector; /** * An Iterator over the power set of a collection. The power set of a collection * is the set of all subsets of that collection. * * To use this class, one passes a collection to one of the constructors. The * PowerSet object produced is an Iterator, and one may use its hasNext and next * methods to produce all nonempty subsets of the given collection. Strictly * speaking, the power set of a collection includes the null or empty set, but * for practical reasons this class does not produce this set. Each application * of the next method produces an instance of the ArrayList class; this instance * contains a subset of the given collection. * * @author Kerry Michael Soileau *
 * ksoileau2@yahoo.com
 * http://kerrysoileau.com/index.html
 * 
* @version 1.2, 05/04/11 */ public class PowerSet implements Iterator> { //Demo: public static void main(String[] args) { PowerSet powerSet1 = new PowerSet(new Object[]{"a","a'","b","b'"}); System.out.println(powerSet1); } private Object[] array; private boolean[] membership; public PowerSet(ArrayList a) { this(a.toArray()); } public PowerSet(Collection c) { this(c.toArray()); } public PowerSet(Object[] array) { this.array=array; if(array!=null) this.membership=new boolean[this.array.length]; } public PowerSet(Vector a) { this(a.toArray()); } /** * Returns true if the PowerSet has more subsets. (In other * words, returns true if next would return a subset * rather than throwing an exception.) * * @return true if the PowerSet has more subsets. */ @Override public boolean hasNext() { if(this.membership!=null) { for(int i=0;i next() { boolean ok=false; for(int i=0;i vec=new ArrayList(); for(int i=0;iremove * operation is not supported by this Iterator. */ @Override public void remove() { throw new UnsupportedOperationException( "The PowerSet class does not support the remove method."); } @Override public String toString() { PowerSet work=new PowerSet(this.array); String ret="{\n"; while(work.hasNext()) ret+=work.next()+"\n"; return ret+"}"; } }