diff options
author | 2021-08-01 23:15:14 +0200 | |
---|---|---|
committer | 2021-08-01 23:15:14 +0200 | |
commit | 5fcfaa77cb53a4516444bd43ce7c303c8f96020e (patch) | |
tree | e854882aa2eb0883aa649b7a525d1af70823d93d /model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/MutableNode.java | |
parent | Model representation is outdated. (diff) | |
download | refinery-5fcfaa77cb53a4516444bd43ce7c303c8f96020e.tar.gz refinery-5fcfaa77cb53a4516444bd43ce7c303c8f96020e.tar.zst refinery-5fcfaa77cb53a4516444bd43ce7c303c8f96020e.zip |
Sonar fixes
Diffstat (limited to 'model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/MutableNode.java')
-rw-r--r-- | model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/MutableNode.java | 74 |
1 files changed, 33 insertions, 41 deletions
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/MutableNode.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/MutableNode.java index 322e2128..2f794a7b 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/MutableNode.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/MutableNode.java | |||
@@ -10,18 +10,18 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
10 | protected Object[] content; | 10 | protected Object[] content; |
11 | 11 | ||
12 | protected MutableNode() { | 12 | protected MutableNode() { |
13 | this.content = new Object[2 * factor]; | 13 | this.content = new Object[2 * FACTOR]; |
14 | updateHash(); | 14 | updateHash(); |
15 | } | 15 | } |
16 | 16 | ||
17 | public static <K, V> MutableNode<K, V> initialize(K key, V value, | 17 | public static <K, V> MutableNode<K, V> initialize(K key, V value, ContinousHashProvider<? super K> hashProvider, |
18 | ContinousHashProvider<? super K> hashProvider, V defaultValue) { | 18 | V defaultValue) { |
19 | if (value == defaultValue) { | 19 | if (value == defaultValue) { |
20 | return null; | 20 | return null; |
21 | } else { | 21 | } else { |
22 | int hash = hashProvider.getHash(key, 0); | 22 | int hash = hashProvider.getHash(key, 0); |
23 | int fragment = hashFragment(hash, 0); | 23 | int fragment = hashFragment(hash, 0); |
24 | MutableNode<K, V> res = new MutableNode<K, V>(); | 24 | MutableNode<K, V> res = new MutableNode<>(); |
25 | res.content[2 * fragment] = key; | 25 | res.content[2 * fragment] = key; |
26 | res.content[2 * fragment + 1] = value; | 26 | res.content[2 * fragment + 1] = value; |
27 | res.updateHash(); | 27 | res.updateHash(); |
@@ -35,10 +35,10 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
35 | * @param node | 35 | * @param node |
36 | */ | 36 | */ |
37 | protected MutableNode(ImmutableNode<K, V> node) { | 37 | protected MutableNode(ImmutableNode<K, V> node) { |
38 | this.content = new Object[2 * factor]; | 38 | this.content = new Object[2 * FACTOR]; |
39 | int dataUsed = 0; | 39 | int dataUsed = 0; |
40 | int nodeUsed = 0; | 40 | int nodeUsed = 0; |
41 | for (int i = 0; i < factor; i++) { | 41 | for (int i = 0; i < FACTOR; i++) { |
42 | int bitposition = 1 << i; | 42 | int bitposition = 1 << i; |
43 | if ((node.dataMap & bitposition) != 0) { | 43 | if ((node.dataMap & bitposition) != 0) { |
44 | content[2 * i] = node.content[dataUsed * 2]; | 44 | content[2 * i] = node.content[dataUsed * 2]; |
@@ -54,8 +54,7 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
54 | 54 | ||
55 | @SuppressWarnings("unchecked") | 55 | @SuppressWarnings("unchecked") |
56 | @Override | 56 | @Override |
57 | public V getValue(K key, ContinousHashProvider<? super K> hashProvider, V defaultValue, int hash, | 57 | public V getValue(K key, ContinousHashProvider<? super K> hashProvider, V defaultValue, int hash, int depth) { |
58 | int depth) { | ||
59 | int selectedHashFragment = hashFragment(hash, shiftDepth(depth)); | 58 | int selectedHashFragment = hashFragment(hash, shiftDepth(depth)); |
60 | K keyCandidate = (K) this.content[2 * selectedHashFragment]; | 59 | K keyCandidate = (K) this.content[2 * selectedHashFragment]; |
61 | if (keyCandidate != null) { | 60 | if (keyCandidate != null) { |
@@ -78,8 +77,8 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
78 | 77 | ||
79 | @SuppressWarnings("unchecked") | 78 | @SuppressWarnings("unchecked") |
80 | @Override | 79 | @Override |
81 | public Node<K, V> putValue(K key, V value, ContinousHashProvider<? super K> hashProvider, | 80 | public Node<K, V> putValue(K key, V value, ContinousHashProvider<? super K> hashProvider, V defaultValue, int hash, |
82 | V defaultValue, int hash, int depth) { | 81 | int depth) { |
83 | int selectedHashFragment = hashFragment(hash, shiftDepth(depth)); | 82 | int selectedHashFragment = hashFragment(hash, shiftDepth(depth)); |
84 | K keyCandidate = (K) content[2 * selectedHashFragment]; | 83 | K keyCandidate = (K) content[2 * selectedHashFragment]; |
85 | if (keyCandidate != null) { | 84 | if (keyCandidate != null) { |
@@ -161,7 +160,7 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
161 | return null; | 160 | return null; |
162 | } | 161 | } |
163 | } else { | 162 | } else { |
164 | // check whether newNode is orphan; | 163 | // check whether newNode is orphan |
165 | MutableNode<K, V> immutableNewNode = newNode.isMutable(); | 164 | MutableNode<K, V> immutableNewNode = newNode.isMutable(); |
166 | if (immutableNewNode != null) { | 165 | if (immutableNewNode != null) { |
167 | int orphaned = immutableNewNode.isOrphaned(); | 166 | int orphaned = immutableNewNode.isOrphaned(); |
@@ -197,7 +196,7 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
197 | 196 | ||
198 | protected int isOrphaned() { | 197 | protected int isOrphaned() { |
199 | int dataFound = -2; | 198 | int dataFound = -2; |
200 | for (int i = 0; i < factor; i++) { | 199 | for (int i = 0; i < FACTOR; i++) { |
201 | if (content[i * 2] != null) { | 200 | if (content[i * 2] != null) { |
202 | if (dataFound >= 0) { | 201 | if (dataFound >= 0) { |
203 | return -1; | 202 | return -1; |
@@ -212,11 +211,10 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
212 | } | 211 | } |
213 | 212 | ||
214 | @SuppressWarnings("unchecked") | 213 | @SuppressWarnings("unchecked") |
215 | private Node<K, V> moveDownAndSplit(ContinousHashProvider<? super K> hashProvider, K newKey, | 214 | private Node<K, V> moveDownAndSplit(ContinousHashProvider<? super K> hashProvider, K newKey, V newValue, |
216 | V newValue, K previousKey, int hashOfNewKey, int depth, int selectedHashFragmentOfCurrentDepth) { | 215 | K previousKey, int hashOfNewKey, int depth, int selectedHashFragmentOfCurrentDepth) { |
217 | V previousValue = (V) content[2 * selectedHashFragmentOfCurrentDepth + 1]; | 216 | V previousValue = (V) content[2 * selectedHashFragmentOfCurrentDepth + 1]; |
218 | 217 | ||
219 | // final int newHash = newHash(hashProvider, newKey, hashOfNewKey, newDepth); | ||
220 | MutableNode<K, V> newSubNode = newNodeWithTwoEntries(hashProvider, previousKey, previousValue, | 218 | MutableNode<K, V> newSubNode = newNodeWithTwoEntries(hashProvider, previousKey, previousValue, |
221 | hashProvider.getHash(previousKey, hashDepth(depth)), newKey, newValue, hashOfNewKey, depth + 1); | 219 | hashProvider.getHash(previousKey, hashDepth(depth)), newKey, newValue, hashOfNewKey, depth + 1); |
222 | 220 | ||
@@ -226,20 +224,14 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
226 | return this; | 224 | return this; |
227 | } | 225 | } |
228 | 226 | ||
229 | private MutableNode<K, V> newNodeWithTwoEntries(ContinousHashProvider<? super K> hashProvider, K key1, | 227 | private MutableNode<K, V> newNodeWithTwoEntries(ContinousHashProvider<? super K> hashProvider, K key1, V value1, |
230 | V value1, int oldHash1, K key2, V value2, int oldHash2, int newdepth) { | 228 | int oldHash1, K key2, V value2, int oldHash2, int newdepth) { |
231 | // final int depthLimit = 4000; | ||
232 | // if(newdepth>depthLimit) { | ||
233 | // final int newHashes = 4000/numberOfFactors; | ||
234 | // throw new IllegalArgumentException( | ||
235 | // "Continuous hash was same " + newHashes + " times for non-equivalent objects " + key1 + " and " + key2 +"."); | ||
236 | // } | ||
237 | int newHash1 = newHash(hashProvider, key1, oldHash1, newdepth); | 229 | int newHash1 = newHash(hashProvider, key1, oldHash1, newdepth); |
238 | int newHash2 = newHash(hashProvider, key2, oldHash2, newdepth); | 230 | int newHash2 = newHash(hashProvider, key2, oldHash2, newdepth); |
239 | int newFragment1 = hashFragment(newHash1, shiftDepth(newdepth)); | 231 | int newFragment1 = hashFragment(newHash1, shiftDepth(newdepth)); |
240 | int newFragment2 = hashFragment(newHash2, shiftDepth(newdepth)); | 232 | int newFragment2 = hashFragment(newHash2, shiftDepth(newdepth)); |
241 | 233 | ||
242 | MutableNode<K, V> subNode = new MutableNode<K, V>(); | 234 | MutableNode<K, V> subNode = new MutableNode<>(); |
243 | if (newFragment1 != newFragment2) { | 235 | if (newFragment1 != newFragment2) { |
244 | subNode.content[newFragment1 * 2] = key1; | 236 | subNode.content[newFragment1 * 2] = key1; |
245 | subNode.content[newFragment1 * 2 + 1] = value1; | 237 | subNode.content[newFragment1 * 2 + 1] = value1; |
@@ -247,8 +239,8 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
247 | subNode.content[newFragment2 * 2] = key2; | 239 | subNode.content[newFragment2 * 2] = key2; |
248 | subNode.content[newFragment2 * 2 + 1] = value2; | 240 | subNode.content[newFragment2 * 2 + 1] = value2; |
249 | } else { | 241 | } else { |
250 | MutableNode<K, V> subSubNode = newNodeWithTwoEntries(hashProvider, key1, value1, newHash1, key2, | 242 | MutableNode<K, V> subSubNode = newNodeWithTwoEntries(hashProvider, key1, value1, newHash1, key2, value2, |
251 | value2, newHash2, newdepth + 1); | 243 | newHash2, newdepth + 1); |
252 | subNode.content[newFragment1 * 2 + 1] = subSubNode; | 244 | subNode.content[newFragment1 * 2 + 1] = subSubNode; |
253 | } | 245 | } |
254 | subNode.updateHash(); | 246 | subNode.updateHash(); |
@@ -270,7 +262,7 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
270 | @Override | 262 | @Override |
271 | public long getSize() { | 263 | public long getSize() { |
272 | int size = 0; | 264 | int size = 0; |
273 | for (int i = 0; i < factor; i++) { | 265 | for (int i = 0; i < FACTOR; i++) { |
274 | if (content[i * 2] != null) { | 266 | if (content[i * 2] != null) { |
275 | size++; | 267 | size++; |
276 | } else { | 268 | } else { |
@@ -297,8 +289,8 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
297 | @Override | 289 | @Override |
298 | boolean moveToNext(MapCursor<K, V> cursor) { | 290 | boolean moveToNext(MapCursor<K, V> cursor) { |
299 | // 1. try to move to data | 291 | // 1. try to move to data |
300 | if (cursor.dataIndex != MapCursor.IndexFinish) { | 292 | if (cursor.dataIndex != MapCursor.INDEX_FINISH) { |
301 | for (int index = cursor.dataIndex + 1; index < factor; index++) { | 293 | for (int index = cursor.dataIndex + 1; index < FACTOR; index++) { |
302 | if (this.content[index * 2] != null) { | 294 | if (this.content[index * 2] != null) { |
303 | // 1.1 found next data | 295 | // 1.1 found next data |
304 | cursor.dataIndex = index; | 296 | cursor.dataIndex = index; |
@@ -307,19 +299,19 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
307 | return true; | 299 | return true; |
308 | } | 300 | } |
309 | } | 301 | } |
310 | cursor.dataIndex = MapCursor.IndexFinish; | 302 | cursor.dataIndex = MapCursor.INDEX_FINISH; |
311 | } | 303 | } |
312 | 304 | ||
313 | // 2. look inside the subnodes | 305 | // 2. look inside the subnodes |
314 | for (int index = cursor.nodeIndexStack.peek() + 1; index < factor; index++) { | 306 | for (int index = cursor.nodeIndexStack.peek() + 1; index < FACTOR; index++) { |
315 | if (this.content[index * 2] == null && this.content[index * 2 + 1] != null) { | 307 | if (this.content[index * 2] == null && this.content[index * 2 + 1] != null) { |
316 | // 2.1 found next subnode, move down to the subnode | 308 | // 2.1 found next subnode, move down to the subnode |
317 | Node<K, V> subnode = (Node<K, V>) this.content[index * 2 + 1]; | 309 | Node<K, V> subnode = (Node<K, V>) this.content[index * 2 + 1]; |
318 | 310 | ||
319 | cursor.dataIndex = MapCursor.IndexStart; | 311 | cursor.dataIndex = MapCursor.INDEX_START; |
320 | cursor.nodeIndexStack.pop(); | 312 | cursor.nodeIndexStack.pop(); |
321 | cursor.nodeIndexStack.push(index); | 313 | cursor.nodeIndexStack.push(index); |
322 | cursor.nodeIndexStack.push(MapCursor.IndexStart); | 314 | cursor.nodeIndexStack.push(MapCursor.INDEX_START); |
323 | cursor.nodeStack.push(subnode); | 315 | cursor.nodeStack.push(subnode); |
324 | 316 | ||
325 | return subnode.moveToNext(cursor); | 317 | return subnode.moveToNext(cursor); |
@@ -350,7 +342,7 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
350 | builder.append("Mutable("); | 342 | builder.append("Mutable("); |
351 | // print content | 343 | // print content |
352 | boolean hadContent = false; | 344 | boolean hadContent = false; |
353 | for (int i = 0; i < factor; i++) { | 345 | for (int i = 0; i < FACTOR; i++) { |
354 | if (content[2 * i] != null) { | 346 | if (content[2 * i] != null) { |
355 | if (hadContent) { | 347 | if (hadContent) { |
356 | builder.append(","); | 348 | builder.append(","); |
@@ -366,7 +358,7 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
366 | } | 358 | } |
367 | builder.append(")"); | 359 | builder.append(")"); |
368 | // print subnodes | 360 | // print subnodes |
369 | for (int i = 0; i < factor; i++) { | 361 | for (int i = 0; i < FACTOR; i++) { |
370 | if (content[2 * i] == null && content[2 * i + 1] != null) { | 362 | if (content[2 * i] == null && content[2 * i + 1] != null) { |
371 | @SuppressWarnings("unchecked") | 363 | @SuppressWarnings("unchecked") |
372 | Node<K, V> subNode = (Node<K, V>) content[2 * i + 1]; | 364 | Node<K, V> subNode = (Node<K, V>) content[2 * i + 1]; |
@@ -380,14 +372,14 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
380 | @Override | 372 | @Override |
381 | public void checkIntegrity(ContinousHashProvider<? super K> hashProvider, V defaultValue, int depth) { | 373 | public void checkIntegrity(ContinousHashProvider<? super K> hashProvider, V defaultValue, int depth) { |
382 | // check for orphan nodes | 374 | // check for orphan nodes |
383 | if(depth>0) { | 375 | if (depth > 0) { |
384 | int orphaned = isOrphaned(); | 376 | int orphaned = isOrphaned(); |
385 | if(orphaned>=0) { | 377 | if (orphaned >= 0) { |
386 | throw new IllegalStateException("Orphaned node! " + orphaned + ": " + content[2 * orphaned]); | 378 | throw new IllegalStateException("Orphaned node! " + orphaned + ": " + content[2 * orphaned]); |
387 | } | 379 | } |
388 | } | 380 | } |
389 | // check the place of data | 381 | // check the place of data |
390 | for (int i = 0; i < factor; i++) { | 382 | for (int i = 0; i < FACTOR; i++) { |
391 | if (this.content[2 * i] != null) { | 383 | if (this.content[2 * i] != null) { |
392 | K key = (K) this.content[2 * i]; | 384 | K key = (K) this.content[2 * i]; |
393 | V value = (V) this.content[2 * i + 1]; | 385 | V value = (V) this.content[2 * i + 1]; |
@@ -405,7 +397,7 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
405 | } | 397 | } |
406 | } | 398 | } |
407 | // check subnodes | 399 | // check subnodes |
408 | for (int i = 0; i < factor; i++) { | 400 | for (int i = 0; i < FACTOR; i++) { |
409 | if (this.content[2 * i + 1] != null && this.content[2 * i] == null) { | 401 | if (this.content[2 * i + 1] != null && this.content[2 * i] == null) { |
410 | Node<K, V> subNode = (Node<K, V>) this.content[2 * i + 1]; | 402 | Node<K, V> subNode = (Node<K, V>) this.content[2 * i + 1]; |
411 | subNode.checkIntegrity(hashProvider, defaultValue, depth + 1); | 403 | subNode.checkIntegrity(hashProvider, defaultValue, depth + 1); |