aboutsummaryrefslogtreecommitdiffstats
path: root/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/VersionedMapImpl.java
diff options
context:
space:
mode:
Diffstat (limited to 'model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/VersionedMapImpl.java')
-rw-r--r--model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/VersionedMapImpl.java168
1 files changed, 168 insertions, 0 deletions
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
new file mode 100644
index 00000000..715a9d74
--- /dev/null
+++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/VersionedMapImpl.java
@@ -0,0 +1,168 @@
1package org.eclipse.viatra.solver.data.map.internal;
2
3import java.util.Iterator;
4import java.util.LinkedList;
5import java.util.List;
6
7import org.eclipse.viatra.solver.data.map.ContinousHashProvider;
8import org.eclipse.viatra.solver.data.map.Cursor;
9import org.eclipse.viatra.solver.data.map.DiffCursor;
10import org.eclipse.viatra.solver.data.map.VersionedMap;
11import org.eclipse.viatra.solver.data.map.VersionedMapStoreImpl;
12
13/**
14 * Not threadSafe in itself
15 * @author Oszkar Semerath
16 *
17 * @param <KEY>
18 * @param <VALUE>
19 */
20public class VersionedMapImpl<KEY,VALUE> implements VersionedMap<KEY,VALUE>{
21 protected final VersionedMapStoreImpl<KEY,VALUE> store;
22
23 protected final ContinousHashProvider<? super KEY> hashProvider;
24 protected final VALUE defaultValue;
25 protected Node<KEY,VALUE> root;
26
27 public VersionedMapImpl(
28 VersionedMapStoreImpl<KEY,VALUE> store,
29 ContinousHashProvider<? super KEY> hashProvider,
30 VALUE defaultValue)
31 {
32 this.store = store;
33 this.hashProvider = hashProvider;
34 this.defaultValue = defaultValue;
35 this.root = null;
36 }
37 public VersionedMapImpl(
38 VersionedMapStoreImpl<KEY,VALUE> store,
39 ContinousHashProvider<? super KEY> hashProvider,
40 VALUE defaultValue, Node<KEY,VALUE> data)
41 {
42 this.store = store;
43 this.hashProvider = hashProvider;
44 this.defaultValue = defaultValue;
45 this.root = data;
46 }
47
48 public VALUE getDefaultValue() {
49 return defaultValue;
50 }
51 public ContinousHashProvider<? super KEY> getHashProvider() {
52 return hashProvider;
53 }
54 @Override
55 public void put(KEY key, VALUE value) {
56 if(root!=null) {
57 root = root.putValue(key, value, hashProvider, defaultValue, hashProvider.getHash(key, 0), 0);
58 } else {
59 root = MutableNode.initialize(key, value, hashProvider, defaultValue);
60 }
61 }
62
63 @Override
64 public void putAll(Cursor<KEY, VALUE> cursor) {
65 if(cursor.getDependingMaps().contains(this)) {
66 List<KEY> keys = new LinkedList<>();
67 List<VALUE> values = new LinkedList<>();
68 while(cursor.move()) {
69 keys.add(cursor.getKey());
70 values.add(cursor.getValue());
71 }
72 Iterator<KEY> keyIterator = keys.iterator();
73 Iterator<VALUE> valueIterator = values.iterator();
74 while(keyIterator.hasNext()) {
75 this.put(keyIterator.next(), valueIterator.next());
76 }
77 } else {
78 while(cursor.move()) {
79 this.put(cursor.getKey(), cursor.getValue());
80 }
81 }
82 }
83
84 @Override
85 public VALUE get(KEY key) {
86 if(root!=null) {
87 return root.getValue(key, hashProvider, defaultValue, hashProvider.getHash(key, 0), 0);
88 } else {
89 return defaultValue;
90 }
91 }
92 @Override
93 public long getSize() {
94 if(root == null) {
95 return 0;
96 } else {
97 return root.getSize();
98 }
99 }
100
101 @Override
102 public Cursor<KEY, VALUE> getCursor() {
103 MapCursor<KEY,VALUE> cursor = new MapCursor<>(this.root,this);
104 return cursor;
105 }
106 @Override
107 public DiffCursor<KEY, VALUE> getDiffCursor(long toVersion) {
108 Cursor<KEY, VALUE> fromCursor = this.getCursor();
109 VersionedMap<KEY, VALUE> toMap = this.store.createMap(toVersion);
110 Cursor<KEY, VALUE> toCursor = toMap.getCursor();
111 return new MapDiffCursor<KEY, VALUE>(this.hashProvider,this.defaultValue, fromCursor, toCursor);
112
113 }
114
115
116 @Override
117 public long commit() {
118 return this.store.commit(root,this);
119 }
120 public void setRoot(Node<KEY, VALUE> root) {
121 this.root = root;
122 }
123
124 @Override
125 public void restore(long state) {
126 root = this.store.revert(state);
127 }
128
129 @Override
130 public int hashCode() {
131 final int prime = 31;
132 int result = 1;
133 result = prime * result + ((root == null) ? 0 : root.hashCode());
134 return result;
135 }
136
137 @Override
138 public boolean equals(Object obj) {
139 if (this == obj)
140 return true;
141 if (obj == null)
142 return false;
143 if (getClass() != obj.getClass())
144 return false;
145 VersionedMapImpl<?,?> other = (VersionedMapImpl<?,?>) obj;
146 if (root == null) {
147 if (other.root != null)
148 return false;
149 } else if (!root.equals(other.root))
150 return false;
151 return true;
152 }
153 public void prettyPrint() {
154 StringBuilder s = new StringBuilder();
155 if(this.root != null) {
156 this.root.prettyPrint(s, 0, -1);
157 System.out.println(s.toString());
158 } else {
159 System.out.println("empty tree");
160 }
161 }
162 public void checkIntegrity() {
163 if(this.root != null) {
164 this.root.checkIntegrity(hashProvider, defaultValue, 0);
165 }
166 }
167
168}