1 package org.simantics.scl.runtime;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Comparator;
6 import java.util.Iterator;
9 import org.simantics.scl.runtime.function.Function;
10 import org.simantics.scl.runtime.function.FunctionImpl1;
11 import org.simantics.scl.runtime.function.FunctionImpl2;
12 import org.simantics.scl.runtime.tuple.Tuple2;
14 import gnu.trove.map.hash.TCustomHashMap;
15 import gnu.trove.set.hash.THashSet;
16 import gnu.trove.strategy.HashingStrategy;
19 @SuppressWarnings({"rawtypes", "unchecked"})
22 public static List map(Function f, List l) {
23 ArrayList result = new ArrayList(l.size());
25 result.add(f.apply(a));
29 public static void iter(Function f, List l) {
34 public static List filter(Function p, List l) {
35 ArrayList result = new ArrayList(Math.min(10, l.size()));
37 if((Boolean)p.apply(a))
42 public static List filterJust(List l) {
43 ArrayList result = new ArrayList(Math.min(10, l.size()));
50 public static List reverse(List l) {
51 ArrayList result = new ArrayList(l.size());
52 for(int i=l.size()-1;i>=0;--i)
57 public static Object foldl(Function f, Object initial, List l) {
59 initial = f.apply(initial, a);
63 // (b -> Maybe (a, b)) -> b -> [a]
64 public static List unfoldr(Function f, Object state) {
65 ArrayList result = new ArrayList();
67 Object r = f.apply(state);
76 public static Object foldr(Function f, Object initial, List l) {
77 for(int i=l.size()-1;i>=0;--i)
78 initial = f.apply(initial, l.get(i));
82 public static Object foldl1(Function f, List l) {
83 Iterator it = l.iterator();
84 Object initial = it.next();
86 initial = f.apply(initial, it.next());
90 public static Object foldr1(Function f, List l) {
92 Object initial = l.get(i);
94 initial = f.apply(initial, l.get(i));
98 public static List _pp(List a, List b) {
99 ArrayList result = new ArrayList(a.size() + b.size());
105 public static List concat(List l) {
108 size += ((List)e).size();
109 ArrayList result = new ArrayList(size);
111 result.addAll((List)e);
115 public static List append(List a, List b) {
116 ArrayList result = new ArrayList(a.size() + b.size());
122 public static List concatMap(Function f, List l) {
123 return concat(map(f, l));
126 public static int length(List l) {
130 public static boolean forall(Function p, List l) {
132 if(!(Boolean)p.apply(e))
137 public static boolean exists(Function p, List l) {
139 if((Boolean)p.apply(e))
144 public static Object get(List l, double i) {
145 return l.get((int)i);
148 private static final FunctionImpl2 BUILD_FUNC = new FunctionImpl2() {
150 public Object apply(Object p0, Object p1) {
151 ((ArrayList)p0).add(p1);
156 public static List build(Function f) {
157 return (List)f.apply(new ArrayList(), BUILD_FUNC);
160 public static List range(int from, int to) {
161 ArrayList result = new ArrayList();
169 public static List newList() {
170 return new ArrayList(2);
173 public static void add(List a, Object b) {
177 public static List zip(List a, List b) {
178 int len = Math.min(a.size(), b.size());
179 ArrayList result = new ArrayList(len);
180 for(int i=0;i<len;++i)
181 result.add(new Tuple2(a.get(i), b.get(i)));
185 public static List zipWith(Function f, List a, List b) {
186 int len = Math.min(a.size(), b.size());
187 ArrayList result = new ArrayList(len);
188 for(int i=0;i<len;++i)
189 result.add(f.apply(a.get(i), b.get(i)));
193 public static Tuple2 unzip(List in) {
195 ArrayList a = new ArrayList(len);
196 ArrayList b = new ArrayList(len);
197 for(int i=0;i<len;++i) {
198 Tuple2 tuple = (Tuple2)in.get(i);
202 return new Tuple2(a, b);
205 public static Function indexWith(final Function hash, final Function eq, List<Tuple2> l) {
206 final TCustomHashMap<Object,Object> map = new TCustomHashMap<Object,Object>(
207 new HashingStrategy<Object>() {
208 private static final long serialVersionUID = 3130052128660420673L;
211 public int computeHashCode(Object object) {
212 return (Integer)hash.apply(object);
216 public boolean equals(Object o1, Object o2) {
217 return (Boolean)eq.apply(o1, o2);
222 return new FunctionImpl1<Object,Object>() {
224 public Object apply(Object p0) {
230 // groupWith :: (a -> Integer) -> (a -> a -> Boolean) -> (a -> b) -> [a] -> [(b, [a])]
231 @SuppressWarnings("serial")
232 public static ArrayList<Tuple2> groupWith(final Function hash, final Function eq, Function keyFunction, Function valueFunction, List<Object> input) {
233 final TCustomHashMap<Object,ArrayList<Object>> map = new TCustomHashMap<Object,ArrayList<Object>>(
234 new HashingStrategy<Object>() {
236 public int computeHashCode(Object object) {
237 return (Integer)hash.apply(object);
241 public boolean equals(Object o1, Object o2) {
242 return (Boolean)eq.apply(o1, o2);
245 ArrayList<Tuple2> result = new ArrayList<Tuple2>();
246 for(Object o : input) {
247 Object key = keyFunction.apply(o);
248 ArrayList<Object> l = map.get(key);
250 l = new ArrayList<Object>();
252 result.add(new Tuple2(key, l));
254 l.add(valueFunction.apply(o));
259 public static List sortWith(final Function compare, List l) {
260 Object[] result = l.toArray(new Object[l.size()]);
261 Arrays.sort(result, new Comparator() {
263 public int compare(Object o1, Object o2) {
264 return (Integer)compare.apply(o1, o2);
267 return Arrays.asList(result);
270 public static List uniqueWith(Function compare, List l) {
271 ArrayList result = new ArrayList(Math.min(10, l.size()));
273 for(int i=0;i<l.size();++i) {
274 Object el = l.get(i);
275 for(int j=0;j<result.size();++j)
276 if(compare.apply(el, result.get(j)).equals(Boolean.TRUE))
283 public static List deleteAllBy(Function compare, List a, List b) {
284 ArrayList result = new ArrayList(Math.min(10, a.size()));
288 if(compare.apply(el, el2).equals(Boolean.TRUE))
295 public static List<Object> unique(List<Object> l) {
296 THashSet<Object> set = new THashSet<Object>();
299 return Arrays.asList(set.toArray(new Object[set.size()]));