From 327c2a35bde7a13d77f343464e41e19e4a214790 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sat, 13 Oct 2018 15:51:53 -0400 Subject: General cleanup and documentation. Cleanup files, and add missing comments in places. --- .../main/java/bjc/utils/funcutils/SetUtils.java | 24 +++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'base/src/main/java/bjc/utils/funcutils/SetUtils.java') diff --git a/base/src/main/java/bjc/utils/funcutils/SetUtils.java b/base/src/main/java/bjc/utils/funcutils/SetUtils.java index eac417c..d57ac00 100644 --- a/base/src/main/java/bjc/utils/funcutils/SetUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/SetUtils.java @@ -5,10 +5,21 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +/** + * Various utility functions dealing with sets. + * @author bjculkin + * + */ public class SetUtils { + /** + * Create a power-set (set of all subsets) of a given set. + * @param originalSet The set to create a power-set of. + * @return The power-set of the set. + */ public static Set> powerSet(Set originalSet) { Set> sets = new HashSet>(); + // Special-case empty input if (originalSet.isEmpty()) { sets.add(new HashSet()); return sets; @@ -16,13 +27,18 @@ public class SetUtils { List list = new ArrayList(originalSet); + // Add original set to list. T head = list.get(0); + // Trim leading element from set. Set rest = new HashSet(list.subList(1, list.size())); - for (Set set : powerSet(rest)) { + Set> remSets = powerSet(rest); + + for (Set set : remSets) { Set newSet = new HashSet(); + // Create a new set with the removed element. newSet.add(head); newSet.addAll(set); @@ -33,6 +49,12 @@ public class SetUtils { return sets; } + /** + * Utility method for set construction. + * @param elms The elements to stick in the set. + * @return A set containing the specified elements. + */ + @SafeVarargs public static Set toSet(T... elms) { Set set = new HashSet<>(); -- cgit v1.2.3