diff options
author | OszkarSemerath <semerath@mit.bme.hu> | 2021-08-11 03:08:50 +0200 |
---|---|---|
committer | OszkarSemerath <semerath@mit.bme.hu> | 2021-08-11 03:08:50 +0200 |
commit | 902e56e32bc087fe645a84e67d1cf2972aded925 (patch) | |
tree | 30f6b544e1f7f35682fa00e37b4aa3efb1aa87a0 /model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/MutableNode.java | |
parent | Fixed multithread test (diff) | |
download | refinery-902e56e32bc087fe645a84e67d1cf2972aded925.tar.gz refinery-902e56e32bc087fe645a84e67d1cf2972aded925.tar.zst refinery-902e56e32bc087fe645a84e67d1cf2972aded925.zip |
Map.put returns old value, ugly solution
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 | 25 |
1 files changed, 17 insertions, 8 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 2f794a7b..9e63b941 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 | |||
@@ -77,7 +77,7 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
77 | 77 | ||
78 | @SuppressWarnings("unchecked") | 78 | @SuppressWarnings("unchecked") |
79 | @Override | 79 | @Override |
80 | public Node<K, V> putValue(K key, V value, ContinousHashProvider<? super K> hashProvider, V defaultValue, int hash, | 80 | public Node<K, V> putValue(K key, V value, OldValueBox<V> oldValue, ContinousHashProvider<? super K> hashProvider, V defaultValue, int hash, |
81 | int depth) { | 81 | int depth) { |
82 | int selectedHashFragment = hashFragment(hash, shiftDepth(depth)); | 82 | int selectedHashFragment = hashFragment(hash, shiftDepth(depth)); |
83 | K keyCandidate = (K) content[2 * selectedHashFragment]; | 83 | K keyCandidate = (K) content[2 * selectedHashFragment]; |
@@ -86,18 +86,20 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
86 | if (keyCandidate.equals(key)) { | 86 | if (keyCandidate.equals(key)) { |
87 | // The key is equals to an existing key -> update entry | 87 | // The key is equals to an existing key -> update entry |
88 | if (value == defaultValue) { | 88 | if (value == defaultValue) { |
89 | return removeEntry(selectedHashFragment); | 89 | return removeEntry(selectedHashFragment, oldValue); |
90 | } else { | 90 | } else { |
91 | return updateValue(value, selectedHashFragment); | 91 | return updateValue(value, oldValue, selectedHashFragment); |
92 | } | 92 | } |
93 | } else { | 93 | } else { |
94 | // The key is not equivalent to an existing key on the same hash bin | 94 | // The key is not equivalent to an existing key on the same hash bin |
95 | // -> split entry if it is necessary | 95 | // -> split entry if it is necessary |
96 | if (value == defaultValue) { | 96 | if (value == defaultValue) { |
97 | // Value is default -> do not need to add new node | 97 | // Value is default -> do not need to add new node |
98 | oldValue.setOldValue(defaultValue); | ||
98 | return this; | 99 | return this; |
99 | } else { | 100 | } else { |
100 | // Value is not default -> Split entry data to a new node | 101 | // Value is not default -> Split entry data to a new node |
102 | oldValue.setOldValue(defaultValue); | ||
101 | return moveDownAndSplit(hashProvider, key, value, keyCandidate, hash, depth, selectedHashFragment); | 103 | return moveDownAndSplit(hashProvider, key, value, keyCandidate, hash, depth, selectedHashFragment); |
102 | } | 104 | } |
103 | } | 105 | } |
@@ -106,24 +108,27 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
106 | Node<K, V> nodeCandidate = (Node<K, V>) content[2 * selectedHashFragment + 1]; | 108 | Node<K, V> nodeCandidate = (Node<K, V>) content[2 * selectedHashFragment + 1]; |
107 | if (nodeCandidate != null) { | 109 | if (nodeCandidate != null) { |
108 | // If it has value, it is a subnode -> upate that | 110 | // If it has value, it is a subnode -> upate that |
109 | Node<K, V> newNode = nodeCandidate.putValue(key, value, hashProvider, defaultValue, | 111 | Node<K, V> newNode = nodeCandidate.putValue(key, value, oldValue, hashProvider, defaultValue, |
110 | newHash(hashProvider, key, hash, depth + 1), depth + 1); | 112 | newHash(hashProvider, key, hash, depth + 1), depth + 1); |
111 | return updateWithSubNode(selectedHashFragment, newNode, value.equals(defaultValue)); | 113 | return updateWithSubNode(selectedHashFragment, newNode, value.equals(defaultValue)); |
112 | } else { | 114 | } else { |
113 | // If it does not have value, put it in the empty place | 115 | // If it does not have value, put it in the empty place |
114 | if (value == defaultValue) { | 116 | if (value == defaultValue) { |
115 | // dont need to add new key-value pair | 117 | // dont need to add new key-value pair |
118 | oldValue.setOldValue(defaultValue); | ||
116 | return this; | 119 | return this; |
117 | } else { | 120 | } else { |
118 | return addEntry(key, value, selectedHashFragment); | 121 | return addEntry(key, value, oldValue, selectedHashFragment); |
119 | } | 122 | } |
120 | 123 | ||
121 | } | 124 | } |
122 | } | 125 | } |
123 | } | 126 | } |
124 | 127 | ||
125 | private Node<K, V> addEntry(K key, V value, int selectedHashFragment) { | 128 | @SuppressWarnings("unchecked") |
129 | private Node<K, V> addEntry(K key, V value, OldValueBox<V> oldValue, int selectedHashFragment) { | ||
126 | content[2 * selectedHashFragment] = key; | 130 | content[2 * selectedHashFragment] = key; |
131 | oldValue.setOldValue((V) content[2 * selectedHashFragment + 1]); | ||
127 | content[2 * selectedHashFragment + 1] = value; | 132 | content[2 * selectedHashFragment + 1] = value; |
128 | updateHash(); | 133 | updateHash(); |
129 | return this; | 134 | return this; |
@@ -136,7 +141,9 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
136 | * @param selectedHashFragment | 141 | * @param selectedHashFragment |
137 | * @return | 142 | * @return |
138 | */ | 143 | */ |
139 | Node<K, V> updateValue(V value, int selectedHashFragment) { | 144 | @SuppressWarnings("unchecked") |
145 | Node<K, V> updateValue(V value, OldValueBox<V> oldValue, int selectedHashFragment) { | ||
146 | oldValue.setOldValue((V) content[2 * selectedHashFragment + 1]); | ||
140 | content[2 * selectedHashFragment + 1] = value; | 147 | content[2 * selectedHashFragment + 1] = value; |
141 | updateHash(); | 148 | updateHash(); |
142 | return this; | 149 | return this; |
@@ -247,8 +254,10 @@ public class MutableNode<K, V> extends Node<K, V> { | |||
247 | return subNode; | 254 | return subNode; |
248 | } | 255 | } |
249 | 256 | ||
250 | Node<K, V> removeEntry(int selectedHashFragment) { | 257 | @SuppressWarnings("unchecked") |
258 | Node<K, V> removeEntry(int selectedHashFragment, OldValueBox<V> oldValue) { | ||
251 | content[2 * selectedHashFragment] = null; | 259 | content[2 * selectedHashFragment] = null; |
260 | oldValue.setOldValue((V) content[2 * selectedHashFragment + 1]); | ||
252 | content[2 * selectedHashFragment + 1] = null; | 261 | content[2 * selectedHashFragment + 1] = null; |
253 | if (hasContent()) { | 262 | if (hasContent()) { |
254 | updateHash(); | 263 | updateHash(); |