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
|
package org.eclipse.viatra.solver.data.map.internal;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
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;
import org.eclipse.viatra.solver.data.map.VersionedMapStoreImpl;
/**
* Not threadSafe in itself
* @author Oszkar Semerath
*
* @param <K>
* @param <V>
*/
public class VersionedMapImpl<K,V> implements VersionedMap<K,V>{
protected final VersionedMapStoreImpl<K,V> store;
protected final ContinousHashProvider<K> hashProvider;
protected final V defaultValue;
protected Node<K,V> root;
public VersionedMapImpl(
VersionedMapStoreImpl<K,V> store,
ContinousHashProvider<K> hashProvider,
V defaultValue)
{
this.store = store;
this.hashProvider = hashProvider;
this.defaultValue = defaultValue;
this.root = null;
}
public VersionedMapImpl(
VersionedMapStoreImpl<K,V> store,
ContinousHashProvider<K> hashProvider,
V defaultValue, Node<K,V> data)
{
this.store = store;
this.hashProvider = hashProvider;
this.defaultValue = defaultValue;
this.root = data;
}
public V getDefaultValue() {
return defaultValue;
}
public ContinousHashProvider<K> getHashProvider() {
return hashProvider;
}
@Override
public void put(K key, V value) {
if(root!=null) {
root = root.putValue(key, value, hashProvider, defaultValue, hashProvider.getHash(key, 0), 0);
} else {
root = MutableNode.initialize(key, value, hashProvider, defaultValue);
}
}
@Override
public void putAll(Cursor<K, V> cursor) {
if(cursor.getDependingMaps().contains(this)) {
List<K> keys = new LinkedList<>();
List<V> values = new LinkedList<>();
while(cursor.move()) {
keys.add(cursor.getKey());
values.add(cursor.getValue());
}
Iterator<K> keyIterator = keys.iterator();
Iterator<V> valueIterator = values.iterator();
while(keyIterator.hasNext()) {
this.put(keyIterator.next(), valueIterator.next());
}
} else {
while(cursor.move()) {
this.put(cursor.getKey(), cursor.getValue());
}
}
}
@Override
public V get(K key) {
if(root!=null) {
return root.getValue(key, hashProvider, defaultValue, hashProvider.getHash(key, 0), 0);
} else {
return defaultValue;
}
}
@Override
public long getSize() {
if(root == null) {
return 0;
} else {
return root.getSize();
}
}
@Override
public Cursor<K, V> getCursor() {
return new MapCursor<>(this.root,this);
}
@Override
public DiffCursor<K, V> getDiffCursor(long toVersion) {
Cursor<K, V> fromCursor = this.getCursor();
VersionedMap<K, V> toMap = this.store.createMap(toVersion);
Cursor<K, V> toCursor = toMap.getCursor();
return new MapDiffCursor<>(this.hashProvider,this.defaultValue, fromCursor, toCursor);
}
@Override
public long commit() {
return this.store.commit(root,this);
}
public void setRoot(Node<K, V> root) {
this.root = root;
}
@Override
public void restore(long state) {
root = this.store.revert(state);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((root == null) ? 0 : root.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
VersionedMapImpl<?,?> other = (VersionedMapImpl<?,?>) obj;
if (root == null) {
if (other.root != null)
return false;
} else if (!root.equals(other.root))
return false;
return true;
}
public void prettyPrint() {
StringBuilder s = new StringBuilder();
if(this.root != null) {
this.root.prettyPrint(s, 0, -1);
System.out.println(s.toString());
} else {
System.out.println("empty tree");
}
}
public void checkIntegrity() {
if(this.root != null) {
this.root.checkIntegrity(hashProvider, defaultValue, 0);
}
}
}
|