diff options
author | 2021-08-11 03:08:50 +0200 | |
---|---|---|
committer | 2021-08-11 03:08:50 +0200 | |
commit | 902e56e32bc087fe645a84e67d1cf2972aded925 (patch) | |
tree | 30f6b544e1f7f35682fa00e37b4aa3efb1aa87a0 /model-data | |
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')
6 files changed, 35 insertions, 19 deletions
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/VersionedMap.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/VersionedMap.java index 2f2cb7a2..3a35b9f0 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/VersionedMap.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/VersionedMap.java | |||
@@ -4,7 +4,7 @@ public interface VersionedMap<K,V> extends Versioned{ | |||
4 | public V get(K key); | 4 | public V get(K key); |
5 | public Cursor<K,V> getAll(); | 5 | public Cursor<K,V> getAll(); |
6 | 6 | ||
7 | public void put(K key, V value); | 7 | public V put(K key, V value); |
8 | public void putAll(Cursor<K,V> cursor); | 8 | public void putAll(Cursor<K,V> cursor); |
9 | 9 | ||
10 | public long getSize(); | 10 | public long getSize(); |
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/VersionedMapStoreImpl.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/VersionedMapStoreImpl.java index 4f3d8b10..83d0e8cd 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/VersionedMapStoreImpl.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/VersionedMapStoreImpl.java | |||
@@ -4,6 +4,7 @@ import java.util.ArrayList; | |||
4 | import java.util.Arrays; | 4 | import java.util.Arrays; |
5 | import java.util.Collections; | 5 | import java.util.Collections; |
6 | import java.util.HashMap; | 6 | import java.util.HashMap; |
7 | import java.util.HashSet; | ||
7 | import java.util.List; | 8 | import java.util.List; |
8 | import java.util.Map; | 9 | import java.util.Map; |
9 | import java.util.Set; | 10 | import java.util.Set; |
@@ -79,7 +80,7 @@ public class VersionedMapStoreImpl<K, V> implements VersionedMapStore<K, V> { | |||
79 | 80 | ||
80 | @Override | 81 | @Override |
81 | public synchronized Set<Long> getStates() { | 82 | public synchronized Set<Long> getStates() { |
82 | return states.keySet(); | 83 | return new HashSet<>(states.keySet()); |
83 | } | 84 | } |
84 | 85 | ||
85 | @Override | 86 | @Override |
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/ImmutableNode.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/ImmutableNode.java index 99df4d48..04a9b19a 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/ImmutableNode.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/ImmutableNode.java | |||
@@ -129,7 +129,7 @@ public class ImmutableNode<K, V> extends Node<K, V> { | |||
129 | 129 | ||
130 | @SuppressWarnings("unchecked") | 130 | @SuppressWarnings("unchecked") |
131 | @Override | 131 | @Override |
132 | public Node<K,V> putValue(K key, V value, ContinousHashProvider<? super K> hashProvider, V defaultValue, int hash, int depth) { | 132 | public Node<K,V> putValue(K key, V value, OldValueBox<V> oldValue, ContinousHashProvider<? super K> hashProvider, V defaultValue, int hash, int depth) { |
133 | int selectedHashFragment = hashFragment(hash,shiftDepth(depth)); | 133 | int selectedHashFragment = hashFragment(hash,shiftDepth(depth)); |
134 | int bitposition = 1 << selectedHashFragment; | 134 | int bitposition = 1 << selectedHashFragment; |
135 | if((dataMap & bitposition) != 0) { | 135 | if((dataMap & bitposition) != 0) { |
@@ -139,23 +139,25 @@ public class ImmutableNode<K, V> extends Node<K, V> { | |||
139 | if(value == defaultValue) { | 139 | if(value == defaultValue) { |
140 | // delete | 140 | // delete |
141 | MutableNode<K, V> mutable = this.toMutable(); | 141 | MutableNode<K, V> mutable = this.toMutable(); |
142 | return mutable.removeEntry(selectedHashFragment); | 142 | return mutable.removeEntry(selectedHashFragment,oldValue); |
143 | } else if(value == content[keyIndex+1]) { | 143 | } else if(value == content[keyIndex+1]) { |
144 | // dont change | 144 | // dont change |
145 | oldValue.setOldValue(value); | ||
145 | return this; | 146 | return this; |
146 | } else { | 147 | } else { |
147 | // update existing value | 148 | // update existing value |
148 | MutableNode<K, V> mutable = this.toMutable(); | 149 | MutableNode<K, V> mutable = this.toMutable(); |
149 | return mutable.updateValue(value, selectedHashFragment); | 150 | return mutable.updateValue(value, oldValue, selectedHashFragment); |
150 | } | 151 | } |
151 | } else { | 152 | } else { |
152 | if(value == defaultValue) { | 153 | if(value == defaultValue) { |
153 | // dont change | 154 | // dont change |
155 | oldValue.setOldValue(defaultValue); | ||
154 | return this; | 156 | return this; |
155 | } else { | 157 | } else { |
156 | // add new key + value | 158 | // add new key + value |
157 | MutableNode<K, V> mutable = this.toMutable(); | 159 | MutableNode<K, V> mutable = this.toMutable(); |
158 | return mutable.putValue(key, value, hashProvider, defaultValue, hash, depth); | 160 | return mutable.putValue(key, value, oldValue, hashProvider, defaultValue, hash, depth); |
159 | } | 161 | } |
160 | } | 162 | } |
161 | } else if((nodeMap & bitposition)!=0) { | 163 | } else if((nodeMap & bitposition)!=0) { |
@@ -164,7 +166,7 @@ public class ImmutableNode<K, V> extends Node<K, V> { | |||
164 | ImmutableNode<K,V> subNode = (ImmutableNode<K,V>) content[keyIndex]; | 166 | ImmutableNode<K,V> subNode = (ImmutableNode<K,V>) content[keyIndex]; |
165 | int newDepth = depth+1; | 167 | int newDepth = depth+1; |
166 | int newHash = newHash(hashProvider, key, hash, newDepth); | 168 | int newHash = newHash(hashProvider, key, hash, newDepth); |
167 | Node<K,V> newsubNode = subNode.putValue(key, value, hashProvider, defaultValue, newHash, newDepth); | 169 | Node<K,V> newsubNode = subNode.putValue(key, value, oldValue, hashProvider, defaultValue, newHash, newDepth); |
168 | 170 | ||
169 | if(subNode == newsubNode) { | 171 | if(subNode == newsubNode) { |
170 | // nothing changed | 172 | // nothing changed |
@@ -176,7 +178,7 @@ public class ImmutableNode<K, V> extends Node<K, V> { | |||
176 | } else { | 178 | } else { |
177 | // add new key + value | 179 | // add new key + value |
178 | MutableNode<K, V> mutable = this.toMutable(); | 180 | MutableNode<K, V> mutable = this.toMutable(); |
179 | return mutable.putValue(key, value, hashProvider, defaultValue, hash, depth); | 181 | return mutable.putValue(key, value, oldValue, hashProvider, defaultValue, hash, depth); |
180 | } | 182 | } |
181 | } | 183 | } |
182 | 184 | ||
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(); |
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/Node.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/Node.java index acf7a463..d40f980a 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/Node.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/Node.java | |||
@@ -58,7 +58,7 @@ public abstract class Node<K,V>{ | |||
58 | 58 | ||
59 | 59 | ||
60 | public abstract V getValue(K key, ContinousHashProvider<? super K> hashProvider, V defaultValue, int hash, int depth); | 60 | public abstract V getValue(K key, ContinousHashProvider<? super K> hashProvider, V defaultValue, int hash, int depth); |
61 | public abstract Node<K,V> putValue(K key, V value, ContinousHashProvider<? super K> hashProvider, V defaultValue, int hash, int depth); | 61 | public abstract Node<K,V> putValue(K key, V value, OldValueBox<V> old, ContinousHashProvider<? super K> hashProvider, V defaultValue, int hash, int depth); |
62 | public abstract long getSize(); | 62 | public abstract long getSize(); |
63 | 63 | ||
64 | abstract MutableNode<K, V> toMutable(); | 64 | abstract MutableNode<K, V> toMutable(); |
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/VersionedMapImpl.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/VersionedMapImpl.java index ef962f13..de41e602 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/VersionedMapImpl.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/VersionedMapImpl.java | |||
@@ -24,6 +24,8 @@ public class VersionedMapImpl<K,V> implements VersionedMap<K,V>{ | |||
24 | protected final V defaultValue; | 24 | protected final V defaultValue; |
25 | protected Node<K,V> root; | 25 | protected Node<K,V> root; |
26 | 26 | ||
27 | private OldValueBox<V> oldValueBox = new OldValueBox<>(); | ||
28 | |||
27 | public VersionedMapImpl( | 29 | public VersionedMapImpl( |
28 | VersionedMapStoreImpl<K,V> store, | 30 | VersionedMapStoreImpl<K,V> store, |
29 | ContinousHashProvider<K> hashProvider, | 31 | ContinousHashProvider<K> hashProvider, |
@@ -52,11 +54,13 @@ public class VersionedMapImpl<K,V> implements VersionedMap<K,V>{ | |||
52 | return hashProvider; | 54 | return hashProvider; |
53 | } | 55 | } |
54 | @Override | 56 | @Override |
55 | public void put(K key, V value) { | 57 | public V put(K key, V value) { |
56 | if(root!=null) { | 58 | if(root!=null) { |
57 | root = root.putValue(key, value, hashProvider, defaultValue, hashProvider.getHash(key, 0), 0); | 59 | root = root.putValue(key, value, oldValueBox, hashProvider, defaultValue, hashProvider.getHash(key, 0), 0); |
60 | return oldValueBox.getOldValue(); | ||
58 | } else { | 61 | } else { |
59 | root = MutableNode.initialize(key, value, hashProvider, defaultValue); | 62 | root = MutableNode.initialize(key, value, hashProvider, defaultValue); |
63 | return defaultValue; | ||
60 | } | 64 | } |
61 | } | 65 | } |
62 | 66 | ||