summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/funcutils/SetUtils.java
diff options
context:
space:
mode:
authorBenjamin J. Culkin <bjculkin@mix.wvu.edu>2018-10-16 06:11:39 -0300
committerBenjamin J. Culkin <bjculkin@mix.wvu.edu>2018-10-16 06:11:39 -0300
commitd2be5b73d7a5653ad5c8273c17284346baa6f1c7 (patch)
tree9d3c6adb193f53588bd5d004fdf80c0381685351 /base/src/main/java/bjc/utils/funcutils/SetUtils.java
parent0308029629a12711b849ea7765639b9b1f9e03d2 (diff)
parentd1d01769e7c55f7f62dc01cadf420d5f63424584 (diff)
Merge branch 'master' of github.com:bculkin2442/bjc-utils2
Diffstat (limited to 'base/src/main/java/bjc/utils/funcutils/SetUtils.java')
-rw-r--r--base/src/main/java/bjc/utils/funcutils/SetUtils.java24
1 files changed, 23 insertions, 1 deletions
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 <T> Set<Set<T>> powerSet(Set<T> originalSet) {
Set<Set<T>> sets = new HashSet<Set<T>>();
+ // Special-case empty input
if (originalSet.isEmpty()) {
sets.add(new HashSet<T>());
return sets;
@@ -16,13 +27,18 @@ public class SetUtils {
List<T> list = new ArrayList<T>(originalSet);
+ // Add original set to list.
T head = list.get(0);
+ // Trim leading element from set.
Set<T> rest = new HashSet<T>(list.subList(1, list.size()));
- for (Set<T> set : powerSet(rest)) {
+ Set<Set<T>> remSets = powerSet(rest);
+
+ for (Set<T> set : remSets) {
Set<T> newSet = new HashSet<T>();
+ // 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 <T> Set<T> toSet(T... elms) {
Set<T> set = new HashSet<>();