aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-query/src/main/java/tools/refinery/store/query/equality/SubstitutingLiteralHashCodeHelper.java
blob: 754f697690fadfe8dd5026a9e9bcf674ced105d9 (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
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.query.equality;

import tools.refinery.store.query.dnf.SymbolicParameter;
import tools.refinery.store.query.term.Variable;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class SubstitutingLiteralHashCodeHelper implements LiteralHashCodeHelper {
	private final Map<Variable, Integer> assignedHashCodes = new LinkedHashMap<>();

	// 0 is for {@code null}, so we start with 1.
	private int next = 1;

	public SubstitutingLiteralHashCodeHelper() {
		this(List.of());
	}

	public SubstitutingLiteralHashCodeHelper(List<SymbolicParameter> parameters) {
		for (var parameter : parameters) {
			getVariableHashCode(parameter.getVariable());
		}
	}

	@Override
	public int getVariableHashCode(Variable variable) {
		if (variable == null) {
			return 0;
		}
		return assignedHashCodes.computeIfAbsent(variable, key -> {
			int sequenceNumber = next;
			next++;
			return variable.hashCodeWithSubstitution(sequenceNumber);
		});
	}
}