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

import org.eclipse.collections.api.map.primitive.IntIntMap;
import tools.refinery.store.statecoding.Morphism;

import java.util.List;

public class PermutationMorphism implements Morphism {
	private final IntIntMap object2PermutationGroup;
	private final List<? extends List<? extends IntIntMap>> permutationsGroups;
	private final int[] selection;
	private boolean hasNext;

	PermutationMorphism(IntIntMap object2PermutationGroup,
						List<? extends List<? extends IntIntMap>> permutationsGroups) {
		this.object2PermutationGroup = object2PermutationGroup;
		this.permutationsGroups = permutationsGroups;

		this.selection = new int[this.permutationsGroups.size()];
		this.hasNext = true;
	}

	public boolean next() {
		return next(0);
	}

	private boolean next(int position) {
		if (position >= permutationsGroups.size()) {
			this.hasNext = false;
			return false;
		}
		if (selection[position] + 1 < permutationsGroups.get(position).size()) {
			selection[position] = selection[position] + 1;
			return true;
		} else {
			selection[position] = 0;
			return next(position + 1);
		}
	}

	@Override
	public int get(int object) {
		if(!hasNext) {
			throw new IllegalArgumentException("No next permutation!");
		}

		final int groupIndex = object2PermutationGroup.get(object);
		final var selectedGroup = permutationsGroups.get(groupIndex);
		final int permutationIndex = selection[groupIndex];
		final var selectedPermutation = selectedGroup.get(permutationIndex);

		return selectedPermutation.get(object);
	}

	@Override
	public int getSize() {
		return object2PermutationGroup.size();
	}
}