From 7c9bc8bb8d194c897a0ba21f1e04fd3953832f7b Mon Sep 17 00:00:00 2001 From: Aidan Brady Date: Sun, 4 Aug 2013 17:45:15 -0400 Subject: [PATCH] Added algorithm to split HashSets --- src/resonantinduction/base/SetUtil.java | 40 +++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/resonantinduction/base/SetUtil.java b/src/resonantinduction/base/SetUtil.java index 724fc554..a635c33b 100644 --- a/src/resonantinduction/base/SetUtil.java +++ b/src/resonantinduction/base/SetUtil.java @@ -48,4 +48,44 @@ public class SetUtil return toReturn; } + + public static Set[] split(Set set, int divide) + { + int remain = set.size()%divide; + int size = (set.size()/divide)-remain; + + Set[] toReturn = new HashSet[divide]; + + for(Set iterSet : toReturn) + { + Set removed = new HashSet(); + + int toAdd = size; + + if(remain > 0) + { + remain--; + toAdd++; + } + + for(V obj : set) + { + if(toAdd == 0) + { + break; + } + + iterSet.add(obj); + removed.add(obj); + toAdd--; + } + + for(V obj : removed) + { + set.remove(obj); + } + } + + return toReturn; + } }