diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-06-03 20:19:50 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-06-03 20:19:50 -0300 |
| commit | 91e06ef9123d1229b6319dba7a668916f411df18 (patch) | |
| tree | da662d1d82d14cacb4e53f132891067f311d467f /base/src/main/java/bjc/utils/funcutils/SetUtils.java | |
| parent | 3379c3eae37d45e8ab4c54438ffa4c7c8772bcb6 (diff) | |
Add some additional utilities
This adds some list/set utilities, including an implementation of 'plain
changes' for generating list permutations that I suspect needs some
debugging.
Diffstat (limited to 'base/src/main/java/bjc/utils/funcutils/SetUtils.java')
| -rw-r--r-- | base/src/main/java/bjc/utils/funcutils/SetUtils.java | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/base/src/main/java/bjc/utils/funcutils/SetUtils.java b/base/src/main/java/bjc/utils/funcutils/SetUtils.java new file mode 100644 index 0000000..eac417c --- /dev/null +++ b/base/src/main/java/bjc/utils/funcutils/SetUtils.java @@ -0,0 +1,45 @@ +package bjc.utils.funcutils; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class SetUtils { + public static <T> Set<Set<T>> powerSet(Set<T> originalSet) { + Set<Set<T>> sets = new HashSet<Set<T>>(); + + if (originalSet.isEmpty()) { + sets.add(new HashSet<T>()); + return sets; + } + + List<T> list = new ArrayList<T>(originalSet); + + T head = list.get(0); + + Set<T> rest = new HashSet<T>(list.subList(1, list.size())); + + for (Set<T> set : powerSet(rest)) { + Set<T> newSet = new HashSet<T>(); + + newSet.add(head); + newSet.addAll(set); + + sets.add(newSet); + sets.add(set); + } + + return sets; + } + + public static <T> Set<T> toSet(T... elms) { + Set<T> set = new HashSet<>(); + + for(T elm : elms) { + set.add(elm); + } + + return set; + } +} |
