aboutsummaryrefslogtreecommitdiffstats
path: root/store/src/main/java/org/eclipse/viatra/solver/data/map/internal/MapDiffCursor.java
blob: 35d20539f62573d4fc0016c4e2942941854c18cd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package org.eclipse.viatra.solver.data.map.internal;

import java.util.List;
import java.util.stream.Stream;

import org.eclipse.viatra.solver.data.map.ContinousHashProvider;
import org.eclipse.viatra.solver.data.map.Cursor;
import org.eclipse.viatra.solver.data.map.DiffCursor;
import org.eclipse.viatra.solver.data.map.VersionedMap;

/**
 * A cursor representing the difference between two states of a map.
 * 
 * @author Oszkar Semerath
 *
 */
public class MapDiffCursor<K, V> implements DiffCursor<K, V>, Cursor<K, V> {
	/**
	 * Default value representing missing elements.
	 */
	private V defaultValue;
	private MapCursor<K, V> cursor1;
	private MapCursor<K, V> cursor2;
	private ContinousHashProvider<? super K> hashProvider;

	// Values
	private K key;
	private V fromValue;
	private V toValue;

	// State
	/**
	 * Positive number if cursor 1 is behind, negative number if cursor 2 is behind,
	 * and 0 if they are at the same position.
	 */
	private int cursorRelation;
	private HashClash hashClash = HashClash.NONE;

	public MapDiffCursor(ContinousHashProvider<? super K> hashProvider, V defaultValue, Cursor<K, V> cursor1,
			Cursor<K, V> cursor2) {
		super();
		this.hashProvider = hashProvider;
		this.defaultValue = defaultValue;
		this.cursor1 = (MapCursor<K, V>) cursor1;
		this.cursor2 = (MapCursor<K, V>) cursor2;
	}

	@Override
	public K getKey() {
		return key;
	}

	@Override
	public V getFromValue() {
		return fromValue;
	}

	@Override
	public V getToValue() {
		return toValue;
	}

	@Override
	public V getValue() {
		return getToValue();
	}

	public boolean isTerminated() {
		return cursor1.isTerminated() && cursor2.isTerminated();
	}

	@Override
	public boolean isDirty() {
		return this.cursor1.isDirty() || this.cursor2.isDirty();
	}

	@Override
	public List<VersionedMap<?, ?>> getDependingMaps() {
		return Stream.concat(cursor1.getDependingMaps().stream(), cursor2.getDependingMaps().stream()).toList();
	}

	protected void updateState() {
		if (!isTerminated()) {
			this.cursorRelation = MapCursor.compare(cursor1, cursor2);
			if (cursorRelation > 0 || cursor2.isTerminated()) {
				this.key = cursor1.getKey();
				this.fromValue = cursor1.getValue();
				this.toValue = defaultValue;
			} else if (cursorRelation < 0 || cursor1.isTerminated()) {
				this.key = cursor2.getKey();
				this.fromValue = defaultValue;
				this.toValue = cursor1.getValue();
			} else {
				// cursor1 = cursor2
				if (cursor1.getKey().equals(cursor2.getKey())) {
					this.key = cursor1.getKey();
					this.fromValue = cursor1.getValue();
					this.toValue = defaultValue;
				} else {
					resolveHashClashWithFirstEntry();
				}
			}
		}
	}

	protected void resolveHashClashWithFirstEntry() {
		int compareResult = this.hashProvider.compare(cursor1.key, cursor2.key);
		if (compareResult < 0) {
			this.hashClash = HashClash.STUCK_CURSOR_2;
			this.cursorRelation = 0;
			this.key = cursor1.key;
			this.fromValue = cursor1.value;
			this.toValue = defaultValue;
		} else if (compareResult > 0) {
			this.hashClash = HashClash.STUCK_CURSOR_1;
			this.cursorRelation = 0;
			this.key = cursor2.key;
			this.fromValue = defaultValue;
			this.toValue = cursor2.value;
		} else {
			throw new IllegalArgumentException("Inconsistent compare result for diffcursor");
		}
	}

	protected boolean isInHashClash() {
		return this.hashClash != HashClash.NONE;
	}

	protected void resolveHashClashWithSecondEntry() {
		switch (this.hashClash) {
		case STUCK_CURSOR_1:
			this.hashClash = HashClash.NONE;
			this.cursorRelation = 0;
			this.key = cursor1.key;
			this.fromValue = cursor1.value;
			this.toValue = defaultValue;
			break;
		case STUCK_CURSOR_2:
			this.hashClash = HashClash.NONE;
			this.cursorRelation = 0;
			this.key = cursor2.key;
			this.fromValue = defaultValue;
			this.toValue = cursor2.value;
			break;
		default:
			throw new IllegalArgumentException("Inconsistent compare result for diffcursor");
		}
	}

	protected boolean sameValues() {
		if (this.fromValue == null) {
			return this.toValue == null;
		} else {
			return this.fromValue.equals(this.toValue);
		}
	}

	protected boolean moveOne() {
		if (isTerminated()) {
			return false;
		}
		if (this.cursorRelation > 0 || cursor2.isTerminated()) {
			return cursor1.move();
		} else if (this.cursorRelation < 0 || cursor1.isTerminated()) {
			return cursor2.move();
		} else {
			boolean moved1 = cursor1.move();
			boolean moved2 = cursor2.move();
			return moved1 && moved2;
		}
	}

	private boolean skipNode() {
		if (isTerminated()) {
			throw new IllegalStateException("DiffCursor tries to skip when terminated!");
		}
		boolean update1 = cursor1.skipCurrentNode();
		boolean update2 = cursor2.skipCurrentNode();
		updateState();
		return update1 && update2;
	}

	protected boolean moveToConsistentState() {
		if (!isTerminated()) {
			boolean changed;
			boolean lastResult = true;
			do {
				changed = false;
				if (MapCursor.sameSubnode(cursor1, cursor2)) {
					lastResult = skipNode();
					changed = true;
				}
				if (sameValues()) {
					lastResult = moveOne();
					changed = true;
				}
				updateState();
			} while (changed && !isTerminated());
			return lastResult;
		} else {
			return false;
		}
	}

	public boolean move() {
		if (!isTerminated()) {
			if (isInHashClash()) {
				this.resolveHashClashWithSecondEntry();
				return true;
			} else {
				if (moveOne()) {
					return moveToConsistentState();
				} else {
					return false;
				}
			}

		} else
			return false;
	}
}