org.metacsp.utility.PowerSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of meta-csp-framework Show documentation
Show all versions of meta-csp-framework Show documentation
A Java API for Meta-CSP based reasoning
package org.metacsp.utility;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class PowerSet {
public static Set> powerSet(Set originalSet) {
Set> sets = new HashSet>();
if (originalSet.isEmpty()) {
sets.add(new HashSet());
return sets;
}
List list = new ArrayList(originalSet);
T head = list.get(0);
Set rest = new HashSet(list.subList(1, list.size()));
for (Set set : powerSet(rest)) {
Set newSet = new HashSet();
newSet.add(head);
newSet.addAll(set);
sets.add(newSet);
sets.add(set);
}
return sets;
}
}