diff options
Diffstat (limited to 'subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/CombinationNodePairingPermutations.java')
-rw-r--r-- | subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/CombinationNodePairingPermutations.java | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/CombinationNodePairingPermutations.java b/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/CombinationNodePairingPermutations.java new file mode 100644 index 00000000..eacd3a2a --- /dev/null +++ b/subprojects/store/src/main/java/tools/refinery/store/statecoding/stateequivalence/CombinationNodePairingPermutations.java | |||
@@ -0,0 +1,57 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.statecoding.stateequivalence; | ||
7 | |||
8 | import java.util.ArrayList; | ||
9 | import java.util.List; | ||
10 | |||
11 | class CombinationNodePairingPermutations { | ||
12 | private final List<List<int[]>> permutations = new ArrayList<>(); | ||
13 | |||
14 | public CombinationNodePairingPermutations(int max) { | ||
15 | initializePermutations(max); | ||
16 | } | ||
17 | |||
18 | public List<int[]> getPermutations(int max) { | ||
19 | if (max >= permutations.size()) { | ||
20 | throw new IllegalArgumentException("Only permutations up to %d elements are supported".formatted(max)); | ||
21 | } | ||
22 | return permutations.get(max); | ||
23 | } | ||
24 | |||
25 | /** | ||
26 | * Generates and stores permutations up to a given size. If the number would be more than a limit, it provides a | ||
27 | * single permutation only. | ||
28 | * | ||
29 | * @param max The max number in the permutation | ||
30 | * @return A complete list of permutations of numbers 0...max, or a single permutation. | ||
31 | */ | ||
32 | private List<int[]> initializePermutations(int max) { | ||
33 | if (max < permutations.size()) { | ||
34 | return permutations.get(max); | ||
35 | } | ||
36 | if (max == 0) { | ||
37 | List<int[]> result = new ArrayList<>(); | ||
38 | result.add(new int[1]); | ||
39 | permutations.add(result); | ||
40 | return result; | ||
41 | } | ||
42 | List<int[]> result = new ArrayList<>(); | ||
43 | List<int[]> previousPermutations = initializePermutations(max - 1); | ||
44 | for (var permutation : previousPermutations) { | ||
45 | for (int pos = 0; pos <= max; pos++) { | ||
46 | int[] newPermutation = new int[max + 1]; | ||
47 | System.arraycopy(permutation, 0, newPermutation, 0, pos); | ||
48 | newPermutation[pos] = max; | ||
49 | if (max - (pos + 1) >= 0) | ||
50 | System.arraycopy(permutation, pos + 1, newPermutation, pos + 1 + 1, max - (pos + 1)); | ||
51 | result.add(newPermutation); | ||
52 | } | ||
53 | } | ||
54 | permutations.add(result); | ||
55 | return result; | ||
56 | } | ||
57 | } | ||