aboutsummaryrefslogtreecommitdiffstats
path: root/model-data/src/main/java/org/eclipse/viatra/solver/data/map/internal/VersionedMapImpl.java
blob: 715a9d7463c896b5ac686f080fd235734c12cd5f (plain) (blame)
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
168
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 <KEY>
 * @param <VALUE>
 */
public class VersionedMapImpl<KEY,VALUE> implements VersionedMap<KEY,VALUE>{
	protected final VersionedMapStoreImpl<KEY,VALUE> store;
	
	protected final ContinousHashProvider<? super KEY> hashProvider;
	protected final VALUE defaultValue;
	protected Node<KEY,VALUE> root;
	
	public VersionedMapImpl(
			VersionedMapStoreImpl<KEY,VALUE> store,
			ContinousHashProvider<? super KEY> hashProvider,
			VALUE defaultValue)
	{
		this.store = store;
		this.hashProvider = hashProvider;
		this.defaultValue = defaultValue;
		this.root = null;
	}
	public VersionedMapImpl(
			VersionedMapStoreImpl<KEY,VALUE> store,
			ContinousHashProvider<? super KEY> hashProvider,
			VALUE defaultValue, Node<KEY,VALUE> data)
	{
		this.store = store;
		this.hashProvider = hashProvider;
		this.defaultValue = defaultValue;
		this.root = data;
	}
		
	public VALUE getDefaultValue() {
		return defaultValue;
	}
	public ContinousHashProvider<? super KEY> getHashProvider() {
		return hashProvider;
	}
	@Override
	public void put(KEY key, VALUE 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<KEY, VALUE> cursor) {
		if(cursor.getDependingMaps().contains(this)) {
			List<KEY> keys = new LinkedList<>();
			List<VALUE> values = new LinkedList<>();
			while(cursor.move()) {
				keys.add(cursor.getKey());
				values.add(cursor.getValue());
			}
			Iterator<KEY> keyIterator = keys.iterator();
			Iterator<VALUE> valueIterator = values.iterator();
			while(keyIterator.hasNext()) {
				this.put(keyIterator.next(), valueIterator.next());
			}
		} else {
			while(cursor.move()) {
				this.put(cursor.getKey(), cursor.getValue());
			}
		}
	}
	
	@Override
	public VALUE get(KEY 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<KEY, VALUE> getCursor() {
		MapCursor<KEY,VALUE> cursor = new MapCursor<>(this.root,this);
		return cursor;
	}
	@Override
	public DiffCursor<KEY, VALUE> getDiffCursor(long toVersion) {
		Cursor<KEY, VALUE> fromCursor = this.getCursor();
		VersionedMap<KEY, VALUE> toMap = this.store.createMap(toVersion);
		Cursor<KEY, VALUE> toCursor = toMap.getCursor();
		return new MapDiffCursor<KEY, VALUE>(this.hashProvider,this.defaultValue, fromCursor, toCursor);
		
	}
	

	@Override
	public long commit() {
		return this.store.commit(root,this);
	}
	public void setRoot(Node<KEY, VALUE> 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);
		}
	}

}