X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.runtime%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fruntime%2FLists.java;h=5259ce6315a38b5b9b1ab64f94ce452cb82930ca;hp=a5722763551855d4d60525066732ba9943420d8c;hb=8c1a41b09c7fbc5760f048ff9d3e5f9b468f8fbd;hpb=5887fe12f827ef474590d61c86d0de32fca8550e diff --git a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/Lists.java b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/Lists.java index a57227635..5259ce631 100644 --- a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/Lists.java +++ b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/Lists.java @@ -2,6 +2,7 @@ package org.simantics.scl.runtime; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; @@ -12,6 +13,7 @@ import org.simantics.scl.runtime.function.FunctionImpl2; import org.simantics.scl.runtime.tuple.Tuple2; import gnu.trove.map.hash.TCustomHashMap; +import gnu.trove.map.hash.THashMap; import gnu.trove.set.hash.THashSet; import gnu.trove.strategy.HashingStrategy; @@ -227,9 +229,33 @@ public class Lists { }; } + public static Function index(List l) { + THashMap map = new THashMap(l.size()); + for(Tuple2 t : l) + map.put(t.c0, t.c1); + return new FunctionImpl1() { + @Override + public Object apply(Object p0) { + return map.get(p0); + } + }; + } + + public static Function indexBy(Function f, List l) { + THashMap map = new THashMap(l.size()); + for(Object o : l) + map.put(f.apply(o), o); + return new FunctionImpl1() { + @Override + public Object apply(Object p0) { + return map.get(p0); + } + }; + } + // groupWith :: (a -> Integer) -> (a -> a -> Boolean) -> (a -> b) -> [a] -> [(b, [a])] @SuppressWarnings("serial") - public static ArrayList groupWith(final Function hash, final Function eq, Function keyFunction, Function valueFunction, List input) { + public static List groupWith(final Function hash, final Function eq, Function keyFunction, Function valueFunction, List input) { final TCustomHashMap> map = new TCustomHashMap>( new HashingStrategy() { @Override @@ -256,6 +282,81 @@ public class Lists { return result; } + public static List group(List input) { + THashMap> groupMap = new THashMap>(); + ArrayList result = new ArrayList(); + for(Tuple2 t : input) { + Object key = t.c0; + ArrayList list = groupMap.get(key); + if(list == null) { + list = new ArrayList(); + groupMap.put(key, list); + result.add(new Tuple2(key, list)); + } + list.add(t.c1); + } + return result; + } + + public static List groupBy(Function f, List input) { + THashMap> groupMap = new THashMap>(); + ArrayList result = new ArrayList(); + for(Object value : input) { + Object key = f.apply(value); + ArrayList list = groupMap.get(key); + if(list == null) { + list = new ArrayList(); + groupMap.put(key, list); + result.add(new Tuple2(key, list)); + } + list.add(value); + } + return result; + } + + private static class GroupMapFunction extends FunctionImpl1> { + THashMap> groupMap; + public GroupMapFunction(THashMap> groupMap) { + this.groupMap = groupMap; + } + @Override + public List apply(Object p0) { + List result = groupMap.get(p0); + if(result == null) + return Collections.emptyList(); + else + return result; + } + } + + public static Function indexGroup(List input) { + THashMap> groupMap = new THashMap>(); + for(Tuple2 t : input) { + Object key = t.c0; + ArrayList list = groupMap.get(key); + if(list == null) { + list = new ArrayList(); + groupMap.put(key, list); + } + list.add(t.c1); + } + return new GroupMapFunction(groupMap); + } + + public static Function indexGroupBy(Function f, List input) { + THashMap> groupMap = new THashMap>(); + for(Object value : input) { + Object key = f.apply(value); + ArrayList list = groupMap.get(key); + if(list == null) { + list = new ArrayList(); + groupMap.put(key, list); + } + list.add(value); + } + return new GroupMapFunction(groupMap); + } + public static List sortWith(final Function compare, List l) { Object[] result = l.toArray(new Object[l.size()]); Arrays.sort(result, new Comparator() { @@ -292,10 +393,42 @@ public class Lists { return result; } + public static List listDifference(List a, List b) { + if(a.isEmpty() || b.isEmpty()) + return a; + THashSet setB = new THashSet(b); + for(int i=0;i unique(List l) { - THashSet set = new THashSet(); + THashSet set = new THashSet(l.size()); + ArrayList result = new ArrayList(l.size()); + for(Object el : l) + if(set.add(el)) + result.add(el); + return result; + } + + public static List uniqueBy(Function f, List l) { + THashSet set = new THashSet(l.size()); + ArrayList result = new ArrayList(l.size()); for(Object el : l) - set.add(el); - return Arrays.asList(set.toArray(new Object[set.size()])); + if(set.add(f.apply(el))) + result.add(el); + return result; } }