diff options
Diffstat (limited to 'Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/CollectionsUtil.xtend')
-rw-r--r-- | Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/CollectionsUtil.xtend | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/CollectionsUtil.xtend b/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/CollectionsUtil.xtend new file mode 100644 index 00000000..25cddc05 --- /dev/null +++ b/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/CollectionsUtil.xtend | |||
@@ -0,0 +1,82 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.util | ||
2 | |||
3 | import java.util.HashSet | ||
4 | import java.util.LinkedList | ||
5 | import java.util.List | ||
6 | import java.util.Map | ||
7 | import java.util.Set | ||
8 | import org.eclipse.xtext.xbase.lib.Functions.Function1 | ||
9 | import java.util.HashMap | ||
10 | import java.util.LinkedHashMap | ||
11 | |||
12 | class CollectionsUtil { | ||
13 | public def static <FROM,TO> TO lookup(FROM from, Map<? super FROM,TO> map) { | ||
14 | if(map.containsKey(from)) { | ||
15 | return map.get(from) | ||
16 | } else throw new IllegalArgumentException(''' | ||
17 | The map does not contains the key "«from.toString»"! | ||
18 | --- Elements: --- | ||
19 | «FOR entry : map.entrySet SEPARATOR '\n'»«entry.key» -> «entry.value»«ENDFOR» | ||
20 | -----------------'''); | ||
21 | } | ||
22 | public def <FROM,TO> TO ifThenElse(FROM source, Function1<FROM,Boolean> condition, Function1<FROM,TO> ifTrue, Function1<FROM,TO> ifFalse) { | ||
23 | if(condition.apply(source)) { | ||
24 | return ifTrue.apply(source) | ||
25 | } | ||
26 | else { | ||
27 | return ifFalse.apply(source) | ||
28 | } | ||
29 | } | ||
30 | public def static <Key,Value> Union(Map<Key,Value> a, Map<Key,Value> b) { | ||
31 | (a.keySet + b.keySet).toInvertedMap[key | | ||
32 | if(a.containsKey(key)) a.get(key) | ||
33 | else b.get(key) | ||
34 | ] | ||
35 | } | ||
36 | public def static <Key,Value> putOrAddToSet(Map<Key,Set<Value>> map, Key key, Value value) { | ||
37 | if(map.containsKey(key)) { | ||
38 | map.get(key).add(value) | ||
39 | }else{ | ||
40 | val set = new HashSet<Value>() => [it.add(value)] | ||
41 | map.put(key, set) | ||
42 | } | ||
43 | } | ||
44 | public def static <Key,Value> putOrAddToList(Map<Key,List<Value>> map, Key key, Value value) { | ||
45 | if(map.containsKey(key)) { | ||
46 | map.get(key).add(value) | ||
47 | }else{ | ||
48 | val set = new LinkedList<Value>() => [it.add(value)] | ||
49 | map.put(key, set) | ||
50 | } | ||
51 | } | ||
52 | def public static <From,To,Property> Map<From,To> copyMap(Map<From,To> oldMap, Iterable<To> newValues, Function1<To,Property> indexExtractor) { | ||
53 | val Map<Property,To> valueIndexes = oldMap.values.toMap[to|indexExtractor.apply(to)]; | ||
54 | val res = oldMap.mapValues[value | indexExtractor.apply(value).lookup(valueIndexes)] | ||
55 | return res | ||
56 | } | ||
57 | def public static <From,To> Map<To,From> bijectiveInverse(Map<From,To> m) { m.keySet.toMap[x|x.lookup(m)] } | ||
58 | def public static <From,To> Map<To,List<From>> inverse(Map<From,To> m) { | ||
59 | val res = new LinkedHashMap<To,List<From>> | ||
60 | m.entrySet.forEach[res.putOrAddToList(it.value,it.key)] | ||
61 | return res | ||
62 | } | ||
63 | |||
64 | def public static <Type> List<Type> transitiveClosurePlus(Type source, Function1<Type,Iterable<Type>> next) { | ||
65 | val res = new LinkedList() | ||
66 | transitiveClosureHelper(res,source,next) | ||
67 | return res | ||
68 | } | ||
69 | def public static <Type> List<Type> transitiveClosureStar(Type source, Function1<Type,Iterable<Type>> next) { | ||
70 | val res = new LinkedList() | ||
71 | res += source | ||
72 | transitiveClosureHelper(res,source,next) | ||
73 | return res | ||
74 | } | ||
75 | def private static <Type> void transitiveClosureHelper(List<Type> result, Type actual, Function1<Type,Iterable<Type>> next) { | ||
76 | val front = next.apply(actual) | ||
77 | for(elementInFront : front) { | ||
78 | result += elementInFront | ||
79 | transitiveClosureHelper(result,elementInFront,next) | ||
80 | } | ||
81 | } | ||
82 | } | ||