From 91e06ef9123d1229b6319dba7a668916f411df18 Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Sun, 3 Jun 2018 20:19:50 -0300 Subject: 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. --- .../main/java/bjc/utils/funcutils/SetUtils.java | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 base/src/main/java/bjc/utils/funcutils/SetUtils.java (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 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 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; + } + + public static Set toSet(T... elms) { + Set set = new HashSet<>(); + + for(T elm : elms) { + set.add(elm); + } + + return set; + } +} -- cgit v1.2.3