diff options
author | OszkarSemerath <oszka@152.66.252.189> | 2017-06-10 19:05:05 +0200 |
---|---|---|
committer | OszkarSemerath <oszka@152.66.252.189> | 2017-06-10 19:05:05 +0200 |
commit | 60f01f46ba232ed6416054f0a6115cb2a9b70b4e (patch) | |
tree | 5edf8aeb07abc51f3fec63bbd15c926e1de09552 /Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/SetWithCustomEquivalence.xtend | |
parent | Initial commit, migrating from SVN (diff) | |
download | VIATRA-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/SetWithCustomEquivalence.xtend')
-rw-r--r-- | Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/SetWithCustomEquivalence.xtend | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/SetWithCustomEquivalence.xtend b/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/SetWithCustomEquivalence.xtend new file mode 100644 index 00000000..ae2cfb04 --- /dev/null +++ b/Framework/hu.bme.mit.inf.dslreasoner.logic.model/src/hu/bme/mit/inf/dslreasoner/util/SetWithCustomEquivalence.xtend | |||
@@ -0,0 +1,103 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.util | ||
2 | |||
3 | import org.eclipse.xtext.xbase.lib.Functions.Function1 | ||
4 | import java.util.HashMap | ||
5 | import java.util.Set | ||
6 | import java.util.Collection | ||
7 | import java.util.HashSet | ||
8 | |||
9 | class SetWithCustomEquivalence<Type,Representation> implements Set<Type>{ | ||
10 | val Function1<Type,Representation> representer; | ||
11 | val HashMap<Representation,Type> map | ||
12 | |||
13 | public new(Function1<Type,Representation> representer) { | ||
14 | this.representer = representer | ||
15 | this.map = new HashMap<Representation,Type> | ||
16 | } | ||
17 | public new(Function1<Type,Representation> representer, Collection<? extends Type> initialElements) { | ||
18 | this.representer = representer | ||
19 | this.map = new HashMap | ||
20 | initialElements.forEach[add] | ||
21 | } | ||
22 | |||
23 | override add(Type arg0) { | ||
24 | val representation = representer.apply(arg0) | ||
25 | if(!map.containsKey(representation)) { | ||
26 | map.put(representation,arg0); | ||
27 | return true | ||
28 | } else return false | ||
29 | } | ||
30 | |||
31 | override addAll(Collection<? extends Type> arg0) { | ||
32 | val originalSize = this.size | ||
33 | arg0.forEach[add(it)] | ||
34 | return (this.size != originalSize) | ||
35 | } | ||
36 | |||
37 | override clear() { | ||
38 | map.clear | ||
39 | } | ||
40 | |||
41 | override contains(Object arg0) { | ||
42 | try { | ||
43 | val rep = this.representer.apply(arg0 as Type) | ||
44 | return map.containsKey(rep) | ||
45 | } catch (ClassCastException e) { | ||
46 | return false | ||
47 | } | ||
48 | } | ||
49 | |||
50 | override containsAll(Collection<?> arg0) { | ||
51 | arg0.forall[it.contains] | ||
52 | } | ||
53 | |||
54 | override isEmpty() { | ||
55 | return map.isEmpty | ||
56 | } | ||
57 | |||
58 | override iterator() { | ||
59 | return map.values.iterator | ||
60 | } | ||
61 | |||
62 | override remove(Object arg0) { | ||
63 | try { | ||
64 | val rep = this.representer.apply(arg0 as Type) | ||
65 | return map.remove(rep) != null | ||
66 | } catch (ClassCastException e) { | ||
67 | return false | ||
68 | } | ||
69 | } | ||
70 | |||
71 | override removeAll(Collection<?> arg0) { | ||
72 | val originalSize = this.size | ||
73 | arg0.forEach[remove(it)] | ||
74 | return (this.size != originalSize) | ||
75 | } | ||
76 | |||
77 | override retainAll(Collection<?> arg0) { | ||
78 | val Set<Representation> representationsOfArg0 = new HashSet | ||
79 | for(element: arg0) { | ||
80 | try { | ||
81 | representationsOfArg0 += this.representer.apply(element as Type) | ||
82 | } catch(ClassCastException e) {} | ||
83 | } | ||
84 | val originalSize = this.size | ||
85 | for(r:this.map.keySet) { | ||
86 | if(!representationsOfArg0.contains(r)) | ||
87 | this.map.remove(r) | ||
88 | } | ||
89 | return (this.size != originalSize) | ||
90 | } | ||
91 | |||
92 | override size() { | ||
93 | return this.map.size | ||
94 | } | ||
95 | |||
96 | override toArray() { | ||
97 | map.values.toArray | ||
98 | } | ||
99 | |||
100 | override <T> toArray(T[] arg0) { | ||
101 | map.values.toArray(arg0) | ||
102 | } | ||
103 | } \ No newline at end of file | ||