X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.runtime%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fruntime%2FLists.java;h=99c19d6b951cbe26c140d13b3ed8dcbc2aefbf06;hb=79b952a1ea3ae3f299c6d7aa612a98b7ae5db51a;hp=7149fc63ef9d8d8ddc9256aa9ce4319213a8c924;hpb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;p=simantics%2Fplatform.git 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 7149fc63e..99c19d6b9 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; @@ -144,16 +146,17 @@ public class Lists { public static Object get(List l, double i) { return l.get((int)i); } + + private static final FunctionImpl2 BUILD_FUNC = new FunctionImpl2() { + @Override + public Object apply(Object p0, Object p1) { + ((ArrayList)p0).add(p1); + return p0; + } + }; public static List build(Function f) { - return (List)f.apply(new ArrayList(), - new FunctionImpl2() { - @Override - public Object apply(Object p0, Object p1) { - ((List)p0).add(p1); - return p0; - } - }); + return (List)f.apply(new ArrayList(), BUILD_FUNC); } public static List range(int from, int to) { @@ -226,9 +229,45 @@ 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 indexSet(List l) { + THashSet set = new THashSet(l.size()); + for(Object obj : l) + set.add(obj); + return new FunctionImpl1() { + @Override + public Object apply(Object p0) { + return set.contains(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 @@ -255,6 +294,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() { @@ -291,10 +405,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; } }