From 60f01f46ba232ed6416054f0a6115cb2a9b70b4e Mon Sep 17 00:00:00 2001 From: OszkarSemerath Date: Sat, 10 Jun 2017 19:05:05 +0200 Subject: Migrating Additional projects --- .../mit/inf/dslreasoner/util/CollectionsUtil.xtend | 82 ++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/CollectionsUtil.xtend (limited to 'Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/CollectionsUtil.xtend') 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 @@ +package hu.bme.mit.inf.dslreasoner.util + +import java.util.HashSet +import java.util.LinkedList +import java.util.List +import java.util.Map +import java.util.Set +import org.eclipse.xtext.xbase.lib.Functions.Function1 +import java.util.HashMap +import java.util.LinkedHashMap + +class CollectionsUtil { + public def static TO lookup(FROM from, Map map) { + if(map.containsKey(from)) { + return map.get(from) + } else throw new IllegalArgumentException(''' + The map does not contains the key "«from.toString»"! + --- Elements: --- + «FOR entry : map.entrySet SEPARATOR '\n'»«entry.key» -> «entry.value»«ENDFOR» + -----------------'''); + } + public def TO ifThenElse(FROM source, Function1 condition, Function1 ifTrue, Function1 ifFalse) { + if(condition.apply(source)) { + return ifTrue.apply(source) + } + else { + return ifFalse.apply(source) + } + } + public def static Union(Map a, Map b) { + (a.keySet + b.keySet).toInvertedMap[key | + if(a.containsKey(key)) a.get(key) + else b.get(key) + ] + } + public def static putOrAddToSet(Map> map, Key key, Value value) { + if(map.containsKey(key)) { + map.get(key).add(value) + }else{ + val set = new HashSet() => [it.add(value)] + map.put(key, set) + } + } + public def static putOrAddToList(Map> map, Key key, Value value) { + if(map.containsKey(key)) { + map.get(key).add(value) + }else{ + val set = new LinkedList() => [it.add(value)] + map.put(key, set) + } + } + def public static Map copyMap(Map oldMap, Iterable newValues, Function1 indexExtractor) { + val Map valueIndexes = oldMap.values.toMap[to|indexExtractor.apply(to)]; + val res = oldMap.mapValues[value | indexExtractor.apply(value).lookup(valueIndexes)] + return res + } + def public static Map bijectiveInverse(Map m) { m.keySet.toMap[x|x.lookup(m)] } + def public static Map> inverse(Map m) { + val res = new LinkedHashMap> + m.entrySet.forEach[res.putOrAddToList(it.value,it.key)] + return res + } + + def public static List transitiveClosurePlus(Type source, Function1> next) { + val res = new LinkedList() + transitiveClosureHelper(res,source,next) + return res + } + def public static List transitiveClosureStar(Type source, Function1> next) { + val res = new LinkedList() + res += source + transitiveClosureHelper(res,source,next) + return res + } + def private static void transitiveClosureHelper(List result, Type actual, Function1> next) { + val front = next.apply(actual) + for(elementInFront : front) { + result += elementInFront + transitiveClosureHelper(result,elementInFront,next) + } + } +} -- cgit v1.2.3-54-g00ecf