aboutsummaryrefslogtreecommitdiffstats
path: root/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/CollectionsUtil.xtend
diff options
context:
space:
mode:
authorLibravatar OszkarSemerath <oszka@152.66.252.189>2017-06-10 19:05:05 +0200
committerLibravatar OszkarSemerath <oszka@152.66.252.189>2017-06-10 19:05:05 +0200
commit60f01f46ba232ed6416054f0a6115cb2a9b70b4e (patch)
tree5edf8aeb07abc51f3fec63bbd15c926e1de09552 /Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/CollectionsUtil.xtend
parentInitial commit, migrating from SVN (diff)
downloadVIATRA-Generator-60f01f46ba232ed6416054f0a6115cb2a9b70b4e.tar.gz
VIATRA-Generator-60f01f46ba232ed6416054f0a6115cb2a9b70b4e.tar.zst
VIATRA-Generator-60f01f46ba232ed6416054f0a6115cb2a9b70b4e.zip
Migrating Additional projects
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.xtend82
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 @@
1package hu.bme.mit.inf.dslreasoner.util
2
3import java.util.HashSet
4import java.util.LinkedList
5import java.util.List
6import java.util.Map
7import java.util.Set
8import org.eclipse.xtext.xbase.lib.Functions.Function1
9import java.util.HashMap
10import java.util.LinkedHashMap
11
12class 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}