diff options
author | Oszkar Semerath <Oszkar Semerath@DESKTOP-DNR7JQ7> | 2021-07-26 01:18:03 +0200 |
---|---|---|
committer | Oszkar Semerath <Oszkar Semerath@DESKTOP-DNR7JQ7> | 2021-07-26 01:18:03 +0200 |
commit | 49f3565457b25fb62ea8e491614deb97c0d0d72c (patch) | |
tree | b470febe7883be01bd1bef9ea6e3441e191e8fc1 /Solvers | |
parent | Commit -> restore test (diff) | |
download | VIATRA-Generator-49f3565457b25fb62ea8e491614deb97c0d0d72c.tar.gz VIATRA-Generator-49f3565457b25fb62ea8e491614deb97c0d0d72c.tar.zst VIATRA-Generator-49f3565457b25fb62ea8e491614deb97c0d0d72c.zip |
Diffcursor, first implementation
Diffstat (limited to 'Solvers')
3 files changed, 147 insertions, 2 deletions
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.solver.data/src/org/eclipse/viatra/solver/data/map/DiffCursor.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.solver.data/src/org/eclipse/viatra/solver/data/map/DiffCursor.java new file mode 100644 index 00000000..d93d26d6 --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.solver.data/src/org/eclipse/viatra/solver/data/map/DiffCursor.java | |||
@@ -0,0 +1,5 @@ | |||
1 | package org.eclipse.viatra.solver.data.map; | ||
2 | |||
3 | public interface DiffCursor<KEY, VALUE> { | ||
4 | |||
5 | } \ No newline at end of file | ||
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.solver.data/src/org/eclipse/viatra/solver/data/map/internal/MapCursor.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.solver.data/src/org/eclipse/viatra/solver/data/map/internal/MapCursor.java index e77d073f..bfbf8fcb 100644 --- a/Solvers/VIATRA-Solver/org.eclipse.viatra.solver.data/src/org/eclipse/viatra/solver/data/map/internal/MapCursor.java +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.solver.data/src/org/eclipse/viatra/solver/data/map/internal/MapCursor.java | |||
@@ -3,6 +3,7 @@ package org.eclipse.viatra.solver.data.map.internal; | |||
3 | import java.util.ArrayDeque; | 3 | import java.util.ArrayDeque; |
4 | import java.util.ConcurrentModificationException; | 4 | import java.util.ConcurrentModificationException; |
5 | import java.util.Deque; | 5 | import java.util.Deque; |
6 | import java.util.Iterator; | ||
6 | 7 | ||
7 | import org.eclipse.viatra.solver.data.map.Cursor; | 8 | import org.eclipse.viatra.solver.data.map.Cursor; |
8 | import org.eclipse.viatra.solver.data.map.VersionedMap; | 9 | import org.eclipse.viatra.solver.data.map.VersionedMap; |
@@ -13,8 +14,8 @@ public class MapCursor<KEY,VALUE> implements Cursor<KEY,VALUE> { | |||
13 | static int IndexFinish = -2; | 14 | static int IndexFinish = -2; |
14 | 15 | ||
15 | // Tree stack | 16 | // Tree stack |
16 | Deque<Node<KEY,VALUE>> nodeStack; | 17 | ArrayDeque<Node<KEY,VALUE>> nodeStack; |
17 | Deque<Integer> nodeIndexStack; | 18 | ArrayDeque<Integer> nodeIndexStack; |
18 | int dataIndex; | 19 | int dataIndex; |
19 | 20 | ||
20 | // Values | 21 | // Values |
@@ -74,4 +75,39 @@ public class MapCursor<KEY,VALUE> implements Cursor<KEY,VALUE> { | |||
74 | public boolean isDirty() { | 75 | public boolean isDirty() { |
75 | return this.map.hashCode() != this.creationHash; | 76 | return this.map.hashCode() != this.creationHash; |
76 | } | 77 | } |
78 | |||
79 | /** | ||
80 | * | ||
81 | * @param <KEY> | ||
82 | * @param <VALUE> | ||
83 | * @param cursor1 | ||
84 | * @param cursor2 | ||
85 | * @returnv Positive number if cursor 1 is behind, negative number if cursor 2 is behind, and 0 if they are at the same position. | ||
86 | */ | ||
87 | public static <KEY,VALUE> int compare(MapCursor<KEY,VALUE> cursor1, MapCursor<KEY,VALUE> cursor2) { | ||
88 | // two cursors are equally deep | ||
89 | Iterator<Integer> stack1 = cursor1.nodeIndexStack.descendingIterator(); | ||
90 | Iterator<Integer> stack2 = cursor2.nodeIndexStack.descendingIterator(); | ||
91 | if(stack1.hasNext()) { | ||
92 | if(!stack2.hasNext()) { | ||
93 | // stack 2 has no more element, thus stack 1 is deeper | ||
94 | return 1; | ||
95 | } | ||
96 | int val1 = stack1.next(); | ||
97 | int val2 = stack2.next(); | ||
98 | if(val1 < val2) { | ||
99 | return -1; | ||
100 | } else if(val2 < val1) { | ||
101 | return 1; | ||
102 | } | ||
103 | } | ||
104 | if(stack2.hasNext()) { | ||
105 | // stack 2 has more element, thus stack 2 is deeper | ||
106 | return 1; | ||
107 | } | ||
108 | return Integer.compare(cursor1.dataIndex, cursor2.dataIndex); | ||
109 | |||
110 | |||
111 | |||
112 | } | ||
77 | } | 113 | } |
diff --git a/Solvers/VIATRA-Solver/org.eclipse.viatra.solver.data/src/org/eclipse/viatra/solver/data/map/internal/MapDiffCursor.java b/Solvers/VIATRA-Solver/org.eclipse.viatra.solver.data/src/org/eclipse/viatra/solver/data/map/internal/MapDiffCursor.java new file mode 100644 index 00000000..3e0a823e --- /dev/null +++ b/Solvers/VIATRA-Solver/org.eclipse.viatra.solver.data/src/org/eclipse/viatra/solver/data/map/internal/MapDiffCursor.java | |||
@@ -0,0 +1,104 @@ | |||
1 | package org.eclipse.viatra.solver.data.map.internal; | ||
2 | |||
3 | import org.eclipse.viatra.solver.data.map.Cursor; | ||
4 | import org.eclipse.viatra.solver.data.map.DiffCursor; | ||
5 | |||
6 | /** | ||
7 | * A cursor representing the difference between two states of a map. | ||
8 | * @author Oszkar Semerath | ||
9 | * | ||
10 | */ | ||
11 | public class MapDiffCursor<KEY,VALUE> implements DiffCursor<KEY, VALUE> { | ||
12 | /** | ||
13 | * Default value representing missing elements. | ||
14 | */ | ||
15 | VALUE defaultValue; | ||
16 | MapCursor<KEY,VALUE> cursor1; | ||
17 | MapCursor<KEY,VALUE> cursor2; | ||
18 | |||
19 | // Values | ||
20 | KEY key; | ||
21 | VALUE value1; | ||
22 | VALUE value2; | ||
23 | |||
24 | // State | ||
25 | /** | ||
26 | * Positive number if cursor 1 is behind, negative number if cursor 2 is behind, and 0 if they are at the same position. | ||
27 | */ | ||
28 | int cursorRelation; | ||
29 | |||
30 | public MapDiffCursor(VALUE defaultValue, Cursor<KEY, VALUE> cursor1, Cursor<KEY, VALUE> cursor2) { | ||
31 | super(); | ||
32 | this.defaultValue = defaultValue; | ||
33 | this.cursor1 = (MapCursor<KEY, VALUE>) cursor1; | ||
34 | this.cursor2 = (MapCursor<KEY, VALUE>) cursor2; | ||
35 | |||
36 | this.updateState(); | ||
37 | this.moveToConsistentState(); | ||
38 | } | ||
39 | |||
40 | public KEY getKey() { | ||
41 | return key; | ||
42 | } | ||
43 | public VALUE getValue1() { | ||
44 | return value1; | ||
45 | } | ||
46 | public VALUE getValue2() { | ||
47 | return value2; | ||
48 | } | ||
49 | public boolean isTerminated() { | ||
50 | return cursor1.isTerminated() && cursor2.isTerminated(); | ||
51 | } | ||
52 | |||
53 | protected void updateState() { | ||
54 | this.cursorRelation = MapCursor.compare(cursor1, cursor2); | ||
55 | if(cursorRelation > 0 || cursor2.isTerminated()) { | ||
56 | this.key = cursor1.getKey(); | ||
57 | this.value1 = cursor1.getValue(); | ||
58 | this.value2 = defaultValue; | ||
59 | } else if(cursorRelation < 0|| cursor1.isTerminated()){ | ||
60 | this.key = cursor2.getKey(); | ||
61 | this.value1 = defaultValue; | ||
62 | this.value2 = cursor1.getValue(); | ||
63 | } else { | ||
64 | // cursor1 = cursor2 | ||
65 | if(!cursor1.getKey().equals(cursor2.getKey())) { | ||
66 | throw new IllegalStateException( | ||
67 | "Cursor comarison tells that they are in the same state, but keys are different. " + cursor1.getKey() + " - " + cursor2.getKey()); | ||
68 | } | ||
69 | this.key = cursor1.getKey(); | ||
70 | this.value1 = cursor1.getValue(); | ||
71 | this.value2 = defaultValue; | ||
72 | } | ||
73 | } | ||
74 | |||
75 | protected boolean canStop() { | ||
76 | return this.value1 != this.value2; | ||
77 | } | ||
78 | protected void moveOne() { | ||
79 | if(isTerminated()) { | ||
80 | throw new IllegalStateException("DiffCursor tries to move when terminated!"); | ||
81 | } | ||
82 | if(this.cursorRelation > 0 || cursor2.isTerminated()) { | ||
83 | cursor1.move(); | ||
84 | } else if(this.cursorRelation < 0 || cursor1.isTerminated()) { | ||
85 | cursor2.move(); | ||
86 | } else { | ||
87 | cursor1.move(); | ||
88 | cursor2.move(); | ||
89 | } | ||
90 | updateState(); | ||
91 | } | ||
92 | protected void moveToConsistentState() { | ||
93 | while(!canStop() && !isTerminated()) { | ||
94 | moveOne(); | ||
95 | } | ||
96 | } | ||
97 | |||
98 | public void move() { | ||
99 | if(!isTerminated()) { | ||
100 | moveOne(); | ||
101 | moveToConsistentState(); | ||
102 | } | ||
103 | } | ||
104 | } | ||