mapFolding 0.5.1__py3-none-any.whl → 0.7.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mapFolding/__init__.py +6 -101
- mapFolding/basecamp.py +12 -10
- mapFolding/beDRY.py +96 -316
- mapFolding/filesystem.py +87 -0
- mapFolding/noHomeYet.py +20 -0
- mapFolding/oeis.py +39 -36
- mapFolding/reference/flattened.py +377 -0
- mapFolding/reference/hunterNumba.py +132 -0
- mapFolding/reference/irvineJavaPort.py +120 -0
- mapFolding/reference/jax.py +208 -0
- mapFolding/reference/lunnan.py +153 -0
- mapFolding/reference/lunnanNumpy.py +123 -0
- mapFolding/reference/lunnanWhile.py +121 -0
- mapFolding/reference/rotatedEntryPoint.py +240 -0
- mapFolding/reference/total_countPlus1vsPlusN.py +211 -0
- mapFolding/someAssemblyRequired/Z0Z_workbench.py +34 -0
- mapFolding/someAssemblyRequired/__init__.py +16 -0
- mapFolding/someAssemblyRequired/getLLVMforNoReason.py +21 -0
- mapFolding/someAssemblyRequired/ingredientsNumba.py +100 -0
- mapFolding/someAssemblyRequired/synthesizeCountingFunctions.py +7 -0
- mapFolding/someAssemblyRequired/synthesizeDataConverters.py +135 -0
- mapFolding/someAssemblyRequired/synthesizeNumba.py +91 -0
- mapFolding/someAssemblyRequired/synthesizeNumbaJob.py +417 -0
- mapFolding/someAssemblyRequired/synthesizeNumbaModules.py +91 -0
- mapFolding/someAssemblyRequired/transformationTools.py +425 -0
- mapFolding/someAssemblyRequired/whatWillBe.py +311 -0
- mapFolding/syntheticModules/__init__.py +0 -0
- mapFolding/syntheticModules/dataNamespaceFlattened.py +30 -0
- mapFolding/syntheticModules/numbaCount.py +90 -0
- mapFolding/syntheticModules/numbaCountExample.py +158 -0
- mapFolding/syntheticModules/numbaCountSequential.py +110 -0
- mapFolding/syntheticModules/numbaCount_doTheNeedful.py +13 -0
- mapFolding/syntheticModules/numba_doTheNeedful.py +12 -0
- mapFolding/syntheticModules/numba_doTheNeedfulExample.py +13 -0
- mapFolding/theDao.py +203 -227
- mapFolding/theSSOT.py +254 -123
- {mapFolding-0.5.1.dist-info → mapfolding-0.7.0.dist-info}/METADATA +10 -8
- mapfolding-0.7.0.dist-info/RECORD +50 -0
- {mapFolding-0.5.1.dist-info → mapfolding-0.7.0.dist-info}/WHEEL +1 -1
- {mapFolding-0.5.1.dist-info → mapfolding-0.7.0.dist-info}/top_level.txt +1 -0
- tests/__init__.py +0 -0
- tests/conftest.py +278 -0
- tests/test_computations.py +49 -0
- tests/test_filesystem.py +52 -0
- tests/test_oeis.py +128 -0
- tests/test_other.py +84 -0
- tests/test_tasks.py +50 -0
- mapFolding/theSSOTdatatypes.py +0 -156
- mapFolding-0.5.1.dist-info/RECORD +0 -14
- {mapFolding-0.5.1.dist-info → mapfolding-0.7.0.dist-info}/LICENSE +0 -0
- {mapFolding-0.5.1.dist-info → mapfolding-0.7.0.dist-info}/entry_points.txt +0 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
from typing import List
|
|
2
|
+
import numba
|
|
3
|
+
import numpy
|
|
4
|
+
|
|
5
|
+
@numba.jit(cache=True, nopython=True, fastmath=True)
|
|
6
|
+
def countFolds(listDimensions: List[int]) -> int:
|
|
7
|
+
"""
|
|
8
|
+
Count the number of distinct ways to fold a map with at least two positive dimensions.
|
|
9
|
+
|
|
10
|
+
Parameters:
|
|
11
|
+
listDimensions: A list of integers representing the dimensions of the map. Error checking and DRY code are impermissible in the numba and jax universes. Validate the list yourself before passing here. There might be some tools for that in this package unless I have become a pyL33t coder.
|
|
12
|
+
|
|
13
|
+
Returns:
|
|
14
|
+
foldsTotal: The total number of distinct folds for the given map dimensions.
|
|
15
|
+
"""
|
|
16
|
+
def integerSmall(value) -> numpy.uint8:
|
|
17
|
+
return numpy.uint8(value)
|
|
18
|
+
|
|
19
|
+
def integerLarge(value) -> numpy.uint64:
|
|
20
|
+
return numpy.uint64(value)
|
|
21
|
+
|
|
22
|
+
dtypeMedium = numpy.uint8
|
|
23
|
+
dtypeMaximum = numpy.uint16
|
|
24
|
+
|
|
25
|
+
leavesTotal = integerSmall(1)
|
|
26
|
+
for 个 in listDimensions:
|
|
27
|
+
leavesTotal = leavesTotal * integerSmall(个)
|
|
28
|
+
dimensionsTotal = integerSmall(len(listDimensions))
|
|
29
|
+
|
|
30
|
+
"""How to build a leaf connection graph, also called a "Cartesian Product Decomposition"
|
|
31
|
+
or a "Dimensional Product Mapping", with sentinels:
|
|
32
|
+
Step 1: find the cumulative product of the map's dimensions"""
|
|
33
|
+
cumulativeProduct = numpy.ones(dimensionsTotal + 1, dtype=dtypeMedium)
|
|
34
|
+
for dimension1ndex in range(1, dimensionsTotal + 1):
|
|
35
|
+
cumulativeProduct[dimension1ndex] = cumulativeProduct[dimension1ndex - 1] * listDimensions[dimension1ndex - 1]
|
|
36
|
+
|
|
37
|
+
"""Step 2: for each dimension, create a coordinate system """
|
|
38
|
+
"""coordinateSystem[dimension1ndex, leaf1ndex] holds the dimension1ndex-th coordinate of leaf leaf1ndex"""
|
|
39
|
+
coordinateSystem = numpy.zeros((dimensionsTotal + 1, leavesTotal + 1), dtype=dtypeMedium)
|
|
40
|
+
for dimension1ndex in range(1, dimensionsTotal + 1):
|
|
41
|
+
for leaf1ndex in range(1, leavesTotal + 1):
|
|
42
|
+
coordinateSystem[dimension1ndex, leaf1ndex] = ((leaf1ndex - 1) // cumulativeProduct[dimension1ndex - 1]) % listDimensions[dimension1ndex - 1] + 1
|
|
43
|
+
|
|
44
|
+
"""Step 3: create a huge empty connection graph"""
|
|
45
|
+
connectionGraph = numpy.zeros((dimensionsTotal + 1, leavesTotal + 1, leavesTotal + 1), dtype=dtypeMedium)
|
|
46
|
+
|
|
47
|
+
"""Step for... for... for...: fill the connection graph"""
|
|
48
|
+
for dimension1ndex in range(1, dimensionsTotal + 1):
|
|
49
|
+
for leaf1ndex in range(1, leavesTotal + 1):
|
|
50
|
+
for leafConnectee in range(1, leaf1ndex + 1):
|
|
51
|
+
connectionGraph[dimension1ndex, leaf1ndex, leafConnectee] = (0 if leafConnectee == 0
|
|
52
|
+
else ((leafConnectee if coordinateSystem[dimension1ndex, leafConnectee] == 1
|
|
53
|
+
else leafConnectee - cumulativeProduct[dimension1ndex - 1])
|
|
54
|
+
if (coordinateSystem[dimension1ndex, leaf1ndex] & 1) == (coordinateSystem[dimension1ndex, leafConnectee] & 1)
|
|
55
|
+
else (leafConnectee if coordinateSystem[dimension1ndex, leafConnectee] == listDimensions[dimension1ndex-1]
|
|
56
|
+
or leafConnectee + cumulativeProduct[dimension1ndex - 1] > leaf1ndex
|
|
57
|
+
else leafConnectee + cumulativeProduct[dimension1ndex - 1])))
|
|
58
|
+
|
|
59
|
+
"""Indices of array `track` (to "track" the execution state), which is a collection of one-dimensional arrays each of length `leavesTotal + 1`."""
|
|
60
|
+
leafAbove = numba.literally(0)
|
|
61
|
+
leafBelow = numba.literally(1)
|
|
62
|
+
countDimensionsGapped = numba.literally(2)
|
|
63
|
+
gapRangeStart = numba.literally(3)
|
|
64
|
+
track = numpy.zeros((4, leavesTotal + 1), dtype=dtypeMedium)
|
|
65
|
+
|
|
66
|
+
gapsWhere = numpy.zeros(integerLarge(integerLarge(leavesTotal) * integerLarge(leavesTotal) + 1), dtype=dtypeMaximum)
|
|
67
|
+
|
|
68
|
+
foldsTotal = integerLarge(0)
|
|
69
|
+
leaf1ndex = integerSmall(1)
|
|
70
|
+
gap1ndex = integerSmall(0)
|
|
71
|
+
|
|
72
|
+
while leaf1ndex > 0:
|
|
73
|
+
if leaf1ndex <= 1 or track[leafBelow, 0] == 1:
|
|
74
|
+
if leaf1ndex > leavesTotal:
|
|
75
|
+
foldsTotal += leavesTotal
|
|
76
|
+
else:
|
|
77
|
+
dimensionsUnconstrained = integerSmall(0)
|
|
78
|
+
"""Track possible gaps for leaf1ndex in each section"""
|
|
79
|
+
gap1ndexCeiling = track[gapRangeStart, leaf1ndex - 1]
|
|
80
|
+
|
|
81
|
+
"""Count possible gaps for leaf1ndex in each section"""
|
|
82
|
+
dimension1ndex = integerSmall(1)
|
|
83
|
+
while dimension1ndex <= dimensionsTotal:
|
|
84
|
+
if connectionGraph[dimension1ndex, leaf1ndex, leaf1ndex] == leaf1ndex:
|
|
85
|
+
dimensionsUnconstrained += 1
|
|
86
|
+
else:
|
|
87
|
+
leafConnectee = connectionGraph[dimension1ndex, leaf1ndex, leaf1ndex]
|
|
88
|
+
while leafConnectee != leaf1ndex:
|
|
89
|
+
gapsWhere[gap1ndexCeiling] = leafConnectee
|
|
90
|
+
if track[countDimensionsGapped, leafConnectee] == 0:
|
|
91
|
+
gap1ndexCeiling += 1
|
|
92
|
+
track[countDimensionsGapped, leafConnectee] += 1
|
|
93
|
+
leafConnectee = connectionGraph[dimension1ndex, leaf1ndex, track[leafBelow, leafConnectee]]
|
|
94
|
+
dimension1ndex += 1
|
|
95
|
+
|
|
96
|
+
"""If leaf1ndex is unconstrained in all sections, it can be inserted anywhere"""
|
|
97
|
+
if dimensionsUnconstrained == dimensionsTotal:
|
|
98
|
+
indexLeaf = integerSmall(0)
|
|
99
|
+
while indexLeaf < leaf1ndex:
|
|
100
|
+
gapsWhere[gap1ndexCeiling] = indexLeaf
|
|
101
|
+
gap1ndexCeiling += 1
|
|
102
|
+
indexLeaf += 1
|
|
103
|
+
|
|
104
|
+
"""Filter gaps that are common to all sections"""
|
|
105
|
+
indexMiniGap = gap1ndex
|
|
106
|
+
while indexMiniGap < gap1ndexCeiling:
|
|
107
|
+
gapsWhere[gap1ndex] = gapsWhere[indexMiniGap]
|
|
108
|
+
if track[countDimensionsGapped, gapsWhere[indexMiniGap]] == dimensionsTotal - dimensionsUnconstrained:
|
|
109
|
+
gap1ndex += 1
|
|
110
|
+
"""Reset track[countDimensionsGapped] for next iteration"""
|
|
111
|
+
track[countDimensionsGapped, gapsWhere[indexMiniGap]] = 0
|
|
112
|
+
indexMiniGap += 1
|
|
113
|
+
|
|
114
|
+
"""Recursive backtracking steps"""
|
|
115
|
+
while leaf1ndex > 0 and gap1ndex == track[gapRangeStart, leaf1ndex - 1]:
|
|
116
|
+
leaf1ndex -= 1
|
|
117
|
+
track[leafBelow, track[leafAbove, leaf1ndex]] = track[leafBelow, leaf1ndex]
|
|
118
|
+
track[leafAbove, track[leafBelow, leaf1ndex]] = track[leafAbove, leaf1ndex]
|
|
119
|
+
|
|
120
|
+
"""Place leaf in valid position"""
|
|
121
|
+
if leaf1ndex > 0:
|
|
122
|
+
gap1ndex -= 1
|
|
123
|
+
track[leafAbove, leaf1ndex] = gapsWhere[gap1ndex]
|
|
124
|
+
track[leafBelow, leaf1ndex] = track[leafBelow, track[leafAbove, leaf1ndex]]
|
|
125
|
+
track[leafBelow, track[leafAbove, leaf1ndex]] = leaf1ndex
|
|
126
|
+
track[leafAbove, track[leafBelow, leaf1ndex]] = leaf1ndex
|
|
127
|
+
"""Save current gap index"""
|
|
128
|
+
track[gapRangeStart, leaf1ndex] = gap1ndex
|
|
129
|
+
"""Move to next leaf"""
|
|
130
|
+
leaf1ndex += 1
|
|
131
|
+
|
|
132
|
+
return int(foldsTotal)
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Ported from the Java version by Sean A. Irvine:
|
|
3
|
+
https://github.com/archmageirvine/joeis/blob/80e3e844b11f149704acbab520bc3a3a25ac34ff/src/irvine/oeis/a001/A001415.java
|
|
4
|
+
|
|
5
|
+
Citation: mapFolding/citations/jOEIS.bibtex
|
|
6
|
+
"""
|
|
7
|
+
def foldings(p: list[int], res: int = 0, mod: int = 0) -> int:
|
|
8
|
+
"""
|
|
9
|
+
Compute the total number of foldings for a map with dimensions specified in p.
|
|
10
|
+
|
|
11
|
+
Parameters:
|
|
12
|
+
p: List of integers representing the dimensions of the map.
|
|
13
|
+
res: Residue for modulo operation (integer).
|
|
14
|
+
mod: Modulus for modulo operation (integer).
|
|
15
|
+
|
|
16
|
+
Returns:
|
|
17
|
+
total_count: The total number of foldings (integer).
|
|
18
|
+
"""
|
|
19
|
+
n = 1 # Total number of leaves
|
|
20
|
+
d = len(p) # Number of dimensions
|
|
21
|
+
for dimension in p:
|
|
22
|
+
n *= dimension
|
|
23
|
+
|
|
24
|
+
# Initialize arrays/lists
|
|
25
|
+
A = [0] * (n + 1) # Leaf above leaf m
|
|
26
|
+
B = [0] * (n + 1) # Leaf below leaf m
|
|
27
|
+
count = [0] * (n + 1) # Counts for potential gaps
|
|
28
|
+
gapter = [0] * (n + 1) # Indices for gap stack per leaf
|
|
29
|
+
gap = [0] * (n * n + 1) # Stack of potential gaps
|
|
30
|
+
|
|
31
|
+
# Compute arrays P, C, D as per the algorithm
|
|
32
|
+
P = [1] * (d + 1)
|
|
33
|
+
for i in range(1, d + 1):
|
|
34
|
+
P[i] = P[i - 1] * p[i - 1]
|
|
35
|
+
|
|
36
|
+
# C[i][m] holds the i-th coordinate of leaf m
|
|
37
|
+
C = [[0] * (n + 1) for _ in range(d + 1)]
|
|
38
|
+
for i in range(1, d + 1):
|
|
39
|
+
for m in range(1, n + 1):
|
|
40
|
+
C[i][m] = ((m - 1) // P[i - 1]) - ((m - 1) // P[i]) * p[i - 1] + 1
|
|
41
|
+
|
|
42
|
+
# D[i][l][m] computes the leaf connected to m in section i when inserting l
|
|
43
|
+
D = [[[0] * (n + 1) for _ in range(n + 1)] for _ in range(d + 1)]
|
|
44
|
+
for i in range(1, d + 1):
|
|
45
|
+
for l in range(1, n + 1):
|
|
46
|
+
for m in range(1, l + 1):
|
|
47
|
+
delta = C[i][l] - C[i][m]
|
|
48
|
+
if delta % 2 == 0:
|
|
49
|
+
# If delta is even
|
|
50
|
+
if C[i][m] == 1:
|
|
51
|
+
D[i][l][m] = m
|
|
52
|
+
else:
|
|
53
|
+
D[i][l][m] = m - P[i - 1]
|
|
54
|
+
else:
|
|
55
|
+
# If delta is odd
|
|
56
|
+
if C[i][m] == p[i - 1] or m + P[i - 1] > l:
|
|
57
|
+
D[i][l][m] = m
|
|
58
|
+
else:
|
|
59
|
+
D[i][l][m] = m + P[i - 1]
|
|
60
|
+
|
|
61
|
+
# Initialize variables for backtracking
|
|
62
|
+
total_count = 0 # Total number of foldings
|
|
63
|
+
g = 0 # Gap index
|
|
64
|
+
l = 1 # Current leaf
|
|
65
|
+
|
|
66
|
+
# Start backtracking loop
|
|
67
|
+
while l > 0:
|
|
68
|
+
# If we have processed all leaves, increment total count
|
|
69
|
+
if l > n:
|
|
70
|
+
total_count += 1
|
|
71
|
+
else:
|
|
72
|
+
dd = 0 # Number of sections where leaf l is unconstrained
|
|
73
|
+
gg = g # Temporary gap index
|
|
74
|
+
g = gapter[l - 1] # Reset gap index for current leaf
|
|
75
|
+
|
|
76
|
+
# Count possible gaps for leaf l in each section
|
|
77
|
+
for i in range(1, d + 1):
|
|
78
|
+
if D[i][l][l] == l:
|
|
79
|
+
dd += 1
|
|
80
|
+
else:
|
|
81
|
+
m = D[i][l][l]
|
|
82
|
+
while m != l:
|
|
83
|
+
if mod == 0 or l != mod or m % mod == res:
|
|
84
|
+
gap[gg] = m
|
|
85
|
+
if count[m] == 0:
|
|
86
|
+
gg += 1
|
|
87
|
+
count[m] += 1
|
|
88
|
+
m = D[i][l][B[m]]
|
|
89
|
+
|
|
90
|
+
# If leaf l is unconstrained in all sections, it can be inserted anywhere
|
|
91
|
+
if dd == d:
|
|
92
|
+
for m in range(l):
|
|
93
|
+
gap[gg] = m
|
|
94
|
+
gg += 1
|
|
95
|
+
|
|
96
|
+
# Filter gaps that are common to all sections
|
|
97
|
+
for j in range(g, gg):
|
|
98
|
+
gap[g] = gap[j]
|
|
99
|
+
if count[gap[j]] == d - dd:
|
|
100
|
+
g += 1
|
|
101
|
+
count[gap[j]] = 0 # Reset count for next iteration
|
|
102
|
+
|
|
103
|
+
# Recursive backtracking steps
|
|
104
|
+
while l > 0 and g == gapter[l - 1]:
|
|
105
|
+
# No more gaps to try, backtrack to previous leaf
|
|
106
|
+
l -= 1
|
|
107
|
+
B[A[l]] = B[l]
|
|
108
|
+
A[B[l]] = A[l]
|
|
109
|
+
|
|
110
|
+
if l > 0:
|
|
111
|
+
# Try next gap for leaf l
|
|
112
|
+
g -= 1
|
|
113
|
+
A[l] = gap[g]
|
|
114
|
+
B[l] = B[A[l]]
|
|
115
|
+
B[A[l]] = l
|
|
116
|
+
A[B[l]] = l
|
|
117
|
+
gapter[l] = g # Save current gap index
|
|
118
|
+
l += 1 # Move to next leaf
|
|
119
|
+
|
|
120
|
+
return total_count
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"""I was able to implement the algorithm with JAX, but I didn't see an advantage and it's a pain in the ass.
|
|
2
|
+
I don't maintain this module."""
|
|
3
|
+
from mapFolding import validateListDimensions, getLeavesTotal, makeConnectionGraph
|
|
4
|
+
from typing import List, Tuple
|
|
5
|
+
import jax
|
|
6
|
+
import jaxtyping
|
|
7
|
+
|
|
8
|
+
dtypeMedium = jax.numpy.uint32
|
|
9
|
+
dtypeMaximum = jax.numpy.uint32
|
|
10
|
+
|
|
11
|
+
def countFolds(listDimensions: List[int]) -> int:
|
|
12
|
+
listDimensionsPositive: List[int] = validateListDimensions(listDimensions)
|
|
13
|
+
|
|
14
|
+
n: int = getLeavesTotal(listDimensionsPositive)
|
|
15
|
+
d: int = len(listDimensions)
|
|
16
|
+
import numpy
|
|
17
|
+
D: numpy.ndarray = makeConnectionGraph(listDimensionsPositive)
|
|
18
|
+
connectionGraph = jax.numpy.asarray(D, dtype=dtypeMedium)
|
|
19
|
+
del listDimensionsPositive
|
|
20
|
+
|
|
21
|
+
return foldingsJAX(n, d, connectionGraph)
|
|
22
|
+
|
|
23
|
+
def foldingsJAX(leavesTotal: jaxtyping.UInt32, dimensionsTotal: jaxtyping.UInt32, connectionGraph: jaxtyping.Array) -> jaxtyping.UInt32:
|
|
24
|
+
|
|
25
|
+
def doNothing(argument):
|
|
26
|
+
return argument
|
|
27
|
+
|
|
28
|
+
def while_activeLeaf1ndex_greaterThan_0(comparisonValues: Tuple):
|
|
29
|
+
comparand = comparisonValues[6]
|
|
30
|
+
return comparand > 0
|
|
31
|
+
|
|
32
|
+
def countFoldings(allValues: Tuple[jaxtyping.Array, jaxtyping.Array, jaxtyping.Array, jaxtyping.Array, jaxtyping.Array, jaxtyping.UInt32, jaxtyping.UInt32, jaxtyping.UInt32]):
|
|
33
|
+
_0, leafBelow, _2, _3, _4, _5, activeLeaf1ndex, _7 = allValues
|
|
34
|
+
|
|
35
|
+
sentinel = leafBelow.at[0].get().astype(jax.numpy.uint32)
|
|
36
|
+
|
|
37
|
+
allValues = jax.lax.cond(findGapsCondition(sentinel, activeLeaf1ndex),
|
|
38
|
+
lambda argumentX: dao(findGapsDo(argumentX)),
|
|
39
|
+
lambda argumentY: jax.lax.cond(incrementCondition(sentinel, activeLeaf1ndex), lambda argumentZ: dao(incrementDo(argumentZ)), dao, argumentY),
|
|
40
|
+
allValues)
|
|
41
|
+
|
|
42
|
+
return allValues
|
|
43
|
+
|
|
44
|
+
def findGapsCondition(leafBelowSentinel, activeLeafNumber):
|
|
45
|
+
return jax.numpy.logical_or(jax.numpy.logical_and(leafBelowSentinel == 1, activeLeafNumber <= leavesTotal), activeLeafNumber <= 1)
|
|
46
|
+
|
|
47
|
+
def findGapsDo(allValues: Tuple[jaxtyping.Array, jaxtyping.Array, jaxtyping.Array, jaxtyping.Array, jaxtyping.Array, jaxtyping.UInt32, jaxtyping.UInt32, jaxtyping.UInt32]):
|
|
48
|
+
def for_dimension1ndex_in_range_1_to_dimensionsTotalPlus1(comparisonValues: Tuple):
|
|
49
|
+
return comparisonValues[-1] <= dimensionsTotal
|
|
50
|
+
|
|
51
|
+
def for_dimension1ndex_in_range_1_to_dimensionsTotalPlus1_do(for_dimension1ndex_in_range_1_to_dimensionsTotalPlus1Values: Tuple[jaxtyping.Array, jaxtyping.Array, jaxtyping.UInt32, jaxtyping.UInt32, jaxtyping.UInt32]):
|
|
52
|
+
def ifLeafIsUnconstrainedCondition(comparand):
|
|
53
|
+
return jax.numpy.equal(connectionGraph[comparand, activeLeaf1ndex, activeLeaf1ndex], activeLeaf1ndex)
|
|
54
|
+
|
|
55
|
+
def ifLeafIsUnconstrainedDo(unconstrainedValues: Tuple[jaxtyping.Array, jaxtyping.Array, jaxtyping.UInt32, jaxtyping.UInt32]):
|
|
56
|
+
unconstrained_unconstrainedLeaf = unconstrainedValues[3]
|
|
57
|
+
unconstrained_unconstrainedLeaf = 1 + unconstrained_unconstrainedLeaf
|
|
58
|
+
return (unconstrainedValues[0], unconstrainedValues[1], unconstrainedValues[2], unconstrained_unconstrainedLeaf)
|
|
59
|
+
|
|
60
|
+
def ifLeafIsUnconstrainedElse(unconstrainedValues: Tuple[jaxtyping.Array, jaxtyping.Array, jaxtyping.UInt32, jaxtyping.UInt32]):
|
|
61
|
+
def while_leaf1ndexConnectee_notEquals_activeLeaf1ndex(comparisonValues: Tuple[jaxtyping.Array, jaxtyping.Array, jaxtyping.UInt32, jaxtyping.UInt32]):
|
|
62
|
+
return comparisonValues[-1] != activeLeaf1ndex
|
|
63
|
+
|
|
64
|
+
def countGaps(countGapsDoValues: Tuple[jaxtyping.Array, jaxtyping.Array, jaxtyping.UInt32, jaxtyping.UInt32]):
|
|
65
|
+
countGapsCountDimensionsGapped, countGapsPotentialGaps, countGapsGap1ndexLowerBound, countGapsLeaf1ndexConnectee = countGapsDoValues
|
|
66
|
+
|
|
67
|
+
countGapsPotentialGaps = countGapsPotentialGaps.at[countGapsGap1ndexLowerBound].set(countGapsLeaf1ndexConnectee)
|
|
68
|
+
countGapsGap1ndexLowerBound = jax.numpy.where(jax.numpy.equal(countGapsCountDimensionsGapped[countGapsLeaf1ndexConnectee], 0), countGapsGap1ndexLowerBound + 1, countGapsGap1ndexLowerBound)
|
|
69
|
+
countGapsCountDimensionsGapped = countGapsCountDimensionsGapped.at[countGapsLeaf1ndexConnectee].add(1)
|
|
70
|
+
countGapsLeaf1ndexConnectee = connectionGraph.at[dimensionNumber, activeLeaf1ndex, leafBelow.at[countGapsLeaf1ndexConnectee].get()].get().astype(jax.numpy.uint32)
|
|
71
|
+
|
|
72
|
+
return (countGapsCountDimensionsGapped, countGapsPotentialGaps, countGapsGap1ndexLowerBound, countGapsLeaf1ndexConnectee)
|
|
73
|
+
|
|
74
|
+
unconstrained_countDimensionsGapped, unconstrained_gapsWhere, unconstrained_gap1ndexCeiling, unconstrained_unconstrainedLeaf = unconstrainedValues
|
|
75
|
+
|
|
76
|
+
leaf1ndexConnectee = connectionGraph.at[dimensionNumber, activeLeaf1ndex, activeLeaf1ndex].get().astype(jax.numpy.uint32)
|
|
77
|
+
|
|
78
|
+
countGapsValues = (unconstrained_countDimensionsGapped, unconstrained_gapsWhere, unconstrained_gap1ndexCeiling, leaf1ndexConnectee)
|
|
79
|
+
countGapsValues = jax.lax.while_loop(while_leaf1ndexConnectee_notEquals_activeLeaf1ndex, countGaps, countGapsValues)
|
|
80
|
+
unconstrained_countDimensionsGapped, unconstrained_gapsWhere, unconstrained_gap1ndexCeiling, leaf1ndexConnectee = countGapsValues
|
|
81
|
+
|
|
82
|
+
return (unconstrained_countDimensionsGapped, unconstrained_gapsWhere, unconstrained_gap1ndexCeiling, unconstrained_unconstrainedLeaf)
|
|
83
|
+
|
|
84
|
+
dimensions_countDimensionsGapped, dimensions_gapsWhere, dimensions_gap1ndexCeiling, dimensions_unconstrainedLeaf, dimensionNumber = for_dimension1ndex_in_range_1_to_dimensionsTotalPlus1Values
|
|
85
|
+
|
|
86
|
+
ifLeafIsUnconstrainedValues = (dimensions_countDimensionsGapped, dimensions_gapsWhere, dimensions_gap1ndexCeiling, dimensions_unconstrainedLeaf)
|
|
87
|
+
ifLeafIsUnconstrainedValues = jax.lax.cond(ifLeafIsUnconstrainedCondition(dimensionNumber), ifLeafIsUnconstrainedDo, ifLeafIsUnconstrainedElse, ifLeafIsUnconstrainedValues)
|
|
88
|
+
dimensions_countDimensionsGapped, dimensions_gapsWhere, dimensions_gap1ndexCeiling, dimensions_unconstrainedLeaf = ifLeafIsUnconstrainedValues
|
|
89
|
+
|
|
90
|
+
dimensionNumber = 1 + dimensionNumber
|
|
91
|
+
return (dimensions_countDimensionsGapped, dimensions_gapsWhere, dimensions_gap1ndexCeiling, dimensions_unconstrainedLeaf, dimensionNumber)
|
|
92
|
+
|
|
93
|
+
def almostUselessCondition(comparand):
|
|
94
|
+
return comparand == dimensionsTotal
|
|
95
|
+
|
|
96
|
+
def almostUselessConditionDo(for_leaf1ndex_in_range_activeLeaf1ndexValues: Tuple[jaxtyping.Array, jaxtyping.UInt32, jaxtyping.UInt32]):
|
|
97
|
+
def for_leaf1ndex_in_range_activeLeaf1ndex(comparisonValues):
|
|
98
|
+
return comparisonValues[-1] < activeLeaf1ndex
|
|
99
|
+
|
|
100
|
+
def for_leaf1ndex_in_range_activeLeaf1ndex_do(for_leaf1ndex_in_range_activeLeaf1ndexValues: Tuple[jaxtyping.Array, jaxtyping.UInt32, jaxtyping.UInt32]):
|
|
101
|
+
leafInRangePotentialGaps, gapNumberLowerBound, leafNumber = for_leaf1ndex_in_range_activeLeaf1ndexValues
|
|
102
|
+
leafInRangePotentialGaps = leafInRangePotentialGaps.at[gapNumberLowerBound].set(leafNumber)
|
|
103
|
+
gapNumberLowerBound = 1 + gapNumberLowerBound
|
|
104
|
+
leafNumber = 1 + leafNumber
|
|
105
|
+
return (leafInRangePotentialGaps, gapNumberLowerBound, leafNumber)
|
|
106
|
+
return jax.lax.while_loop(for_leaf1ndex_in_range_activeLeaf1ndex, for_leaf1ndex_in_range_activeLeaf1ndex_do, for_leaf1ndex_in_range_activeLeaf1ndexValues)
|
|
107
|
+
|
|
108
|
+
def for_range_from_activeGap1ndex_to_gap1ndexCeiling(comparisonValues: Tuple[jaxtyping.Array, jaxtyping.Array, jaxtyping.UInt32, jaxtyping.UInt32]):
|
|
109
|
+
return comparisonValues[-1] < gap1ndexCeiling
|
|
110
|
+
|
|
111
|
+
def miniGapDo(gapToGapValues: Tuple[jaxtyping.Array, jaxtyping.Array, jaxtyping.UInt32, jaxtyping.UInt32]):
|
|
112
|
+
gapToGapCountDimensionsGapped, gapToGapPotentialGaps, activeGapNumber, index = gapToGapValues
|
|
113
|
+
gapToGapPotentialGaps = gapToGapPotentialGaps.at[activeGapNumber].set(gapToGapPotentialGaps.at[index].get())
|
|
114
|
+
activeGapNumber = jax.numpy.where(jax.numpy.equal(gapToGapCountDimensionsGapped.at[gapToGapPotentialGaps.at[index].get()].get(), dimensionsTotal - unconstrainedLeaf), activeGapNumber + 1, activeGapNumber).astype(jax.numpy.uint32)
|
|
115
|
+
gapToGapCountDimensionsGapped = gapToGapCountDimensionsGapped.at[gapToGapPotentialGaps.at[index].get()].set(0)
|
|
116
|
+
index = 1 + index
|
|
117
|
+
return (gapToGapCountDimensionsGapped, gapToGapPotentialGaps, activeGapNumber, index)
|
|
118
|
+
|
|
119
|
+
_0, leafBelow, countDimensionsGapped, gapRangeStart, gapsWhere, _5, activeLeaf1ndex, activeGap1ndex = allValues
|
|
120
|
+
|
|
121
|
+
unconstrainedLeaf = jax.numpy.uint32(0)
|
|
122
|
+
dimension1ndex = jax.numpy.uint32(1)
|
|
123
|
+
gap1ndexCeiling = gapRangeStart.at[activeLeaf1ndex - 1].get().astype(jax.numpy.uint32)
|
|
124
|
+
activeGap1ndex = gap1ndexCeiling
|
|
125
|
+
for_dimension1ndex_in_range_1_to_dimensionsTotalPlus1Values = (countDimensionsGapped, gapsWhere, gap1ndexCeiling, unconstrainedLeaf, dimension1ndex)
|
|
126
|
+
for_dimension1ndex_in_range_1_to_dimensionsTotalPlus1Values = jax.lax.while_loop(for_dimension1ndex_in_range_1_to_dimensionsTotalPlus1, for_dimension1ndex_in_range_1_to_dimensionsTotalPlus1_do, for_dimension1ndex_in_range_1_to_dimensionsTotalPlus1Values)
|
|
127
|
+
countDimensionsGapped, gapsWhere, gap1ndexCeiling, unconstrainedLeaf, dimension1ndex = for_dimension1ndex_in_range_1_to_dimensionsTotalPlus1Values
|
|
128
|
+
del dimension1ndex
|
|
129
|
+
|
|
130
|
+
leaf1ndex = jax.numpy.uint32(0)
|
|
131
|
+
for_leaf1ndex_in_range_activeLeaf1ndexValues = (gapsWhere, gap1ndexCeiling, leaf1ndex)
|
|
132
|
+
for_leaf1ndex_in_range_activeLeaf1ndexValues = jax.lax.cond(almostUselessCondition(unconstrainedLeaf), almostUselessConditionDo, doNothing, for_leaf1ndex_in_range_activeLeaf1ndexValues)
|
|
133
|
+
gapsWhere, gap1ndexCeiling, leaf1ndex = for_leaf1ndex_in_range_activeLeaf1ndexValues
|
|
134
|
+
del leaf1ndex
|
|
135
|
+
|
|
136
|
+
indexMiniGap = activeGap1ndex
|
|
137
|
+
miniGapValues = (countDimensionsGapped, gapsWhere, activeGap1ndex, indexMiniGap)
|
|
138
|
+
miniGapValues = jax.lax.while_loop(for_range_from_activeGap1ndex_to_gap1ndexCeiling, miniGapDo, miniGapValues)
|
|
139
|
+
countDimensionsGapped, gapsWhere, activeGap1ndex, indexMiniGap = miniGapValues
|
|
140
|
+
del indexMiniGap
|
|
141
|
+
|
|
142
|
+
return (allValues[0], leafBelow, countDimensionsGapped, gapRangeStart, gapsWhere, allValues[5], activeLeaf1ndex, activeGap1ndex)
|
|
143
|
+
|
|
144
|
+
def incrementCondition(leafBelowSentinel, activeLeafNumber):
|
|
145
|
+
return jax.numpy.logical_and(activeLeafNumber > leavesTotal, leafBelowSentinel == 1)
|
|
146
|
+
|
|
147
|
+
def incrementDo(allValues: Tuple[jaxtyping.Array, jaxtyping.Array, jaxtyping.Array, jaxtyping.Array, jaxtyping.Array, jaxtyping.UInt32, jaxtyping.UInt32, jaxtyping.UInt32]):
|
|
148
|
+
foldingsSubTotal = allValues[5]
|
|
149
|
+
foldingsSubTotal = leavesTotal + foldingsSubTotal
|
|
150
|
+
return (allValues[0], allValues[1], allValues[2], allValues[3], allValues[4], foldingsSubTotal, allValues[6], allValues[7])
|
|
151
|
+
|
|
152
|
+
def dao(allValues: Tuple[jaxtyping.Array, jaxtyping.Array, jaxtyping.Array, jaxtyping.Array, jaxtyping.Array, jaxtyping.UInt32, jaxtyping.UInt32, jaxtyping.UInt32]):
|
|
153
|
+
def whileBacktrackingCondition(backtrackingValues: Tuple[jaxtyping.Array, jaxtyping.Array, jaxtyping.UInt32]):
|
|
154
|
+
comparand = backtrackingValues[2]
|
|
155
|
+
return jax.numpy.logical_and(comparand > 0, jax.numpy.equal(activeGap1ndex, gapRangeStart.at[comparand - 1].get()))
|
|
156
|
+
|
|
157
|
+
def whileBacktrackingDo(backtrackingValues: Tuple[jaxtyping.Array, jaxtyping.Array, jaxtyping.UInt32]):
|
|
158
|
+
backtrackAbove, backtrackBelow, activeLeafNumber = backtrackingValues
|
|
159
|
+
|
|
160
|
+
activeLeafNumber = activeLeafNumber - 1
|
|
161
|
+
backtrackBelow = backtrackBelow.at[backtrackAbove.at[activeLeafNumber].get()].set(backtrackBelow.at[activeLeafNumber].get())
|
|
162
|
+
backtrackAbove = backtrackAbove.at[backtrackBelow.at[activeLeafNumber].get()].set(backtrackAbove.at[activeLeafNumber].get())
|
|
163
|
+
|
|
164
|
+
return (backtrackAbove, backtrackBelow, activeLeafNumber)
|
|
165
|
+
|
|
166
|
+
def if_activeLeaf1ndex_greaterThan_0(activeLeafNumber):
|
|
167
|
+
return activeLeafNumber > 0
|
|
168
|
+
|
|
169
|
+
def if_activeLeaf1ndex_greaterThan_0_do(leafPlacementValues: Tuple[jaxtyping.Array, jaxtyping.Array, jaxtyping.Array, jaxtyping.UInt32, jaxtyping.UInt32]):
|
|
170
|
+
placeLeafAbove, placeLeafBelow, placeGapRangeStart, activeLeafNumber, activeGapNumber = leafPlacementValues
|
|
171
|
+
activeGapNumber = activeGapNumber - 1
|
|
172
|
+
placeLeafAbove = placeLeafAbove.at[activeLeafNumber].set(gapsWhere.at[activeGapNumber].get())
|
|
173
|
+
placeLeafBelow = placeLeafBelow.at[activeLeafNumber].set(placeLeafBelow.at[placeLeafAbove.at[activeLeafNumber].get()].get())
|
|
174
|
+
placeLeafBelow = placeLeafBelow.at[placeLeafAbove.at[activeLeafNumber].get()].set(activeLeafNumber)
|
|
175
|
+
placeLeafAbove = placeLeafAbove.at[placeLeafBelow.at[activeLeafNumber].get()].set(activeLeafNumber)
|
|
176
|
+
placeGapRangeStart = placeGapRangeStart.at[activeLeafNumber].set(activeGapNumber)
|
|
177
|
+
|
|
178
|
+
activeLeafNumber = 1 + activeLeafNumber
|
|
179
|
+
return (placeLeafAbove, placeLeafBelow, placeGapRangeStart, activeLeafNumber, activeGapNumber)
|
|
180
|
+
|
|
181
|
+
leafAbove, leafBelow, _2, gapRangeStart, gapsWhere, _5, activeLeaf1ndex, activeGap1ndex = allValues
|
|
182
|
+
|
|
183
|
+
whileBacktrackingValues = (leafAbove, leafBelow, activeLeaf1ndex)
|
|
184
|
+
whileBacktrackingValues = jax.lax.while_loop(whileBacktrackingCondition, whileBacktrackingDo, whileBacktrackingValues)
|
|
185
|
+
leafAbove, leafBelow, activeLeaf1ndex = whileBacktrackingValues
|
|
186
|
+
|
|
187
|
+
if_activeLeaf1ndex_greaterThan_0_values = (leafAbove, leafBelow, gapRangeStart, activeLeaf1ndex, activeGap1ndex)
|
|
188
|
+
if_activeLeaf1ndex_greaterThan_0_values = jax.lax.cond(if_activeLeaf1ndex_greaterThan_0(activeLeaf1ndex), if_activeLeaf1ndex_greaterThan_0_do, doNothing, if_activeLeaf1ndex_greaterThan_0_values)
|
|
189
|
+
leafAbove, leafBelow, gapRangeStart, activeLeaf1ndex, activeGap1ndex = if_activeLeaf1ndex_greaterThan_0_values
|
|
190
|
+
|
|
191
|
+
return (leafAbove, leafBelow, allValues[2], gapRangeStart, gapsWhere, allValues[5], activeLeaf1ndex, activeGap1ndex)
|
|
192
|
+
|
|
193
|
+
# Dynamic values
|
|
194
|
+
A = jax.numpy.zeros(leavesTotal + 1, dtype=dtypeMedium)
|
|
195
|
+
B = jax.numpy.zeros(leavesTotal + 1, dtype=dtypeMedium)
|
|
196
|
+
count = jax.numpy.zeros(leavesTotal + 1, dtype=dtypeMedium)
|
|
197
|
+
gapter = jax.numpy.zeros(leavesTotal + 1, dtype=dtypeMedium)
|
|
198
|
+
gap = jax.numpy.zeros(leavesTotal * leavesTotal + 1, dtype=dtypeMaximum)
|
|
199
|
+
|
|
200
|
+
foldingsTotal = jax.numpy.uint32(0)
|
|
201
|
+
l = jax.numpy.uint32(1)
|
|
202
|
+
g = jax.numpy.uint32(0)
|
|
203
|
+
|
|
204
|
+
foldingsValues = (A, B, count, gapter, gap, foldingsTotal, l, g)
|
|
205
|
+
foldingsValues = jax.lax.while_loop(while_activeLeaf1ndex_greaterThan_0, countFoldings, foldingsValues)
|
|
206
|
+
return foldingsValues[5]
|
|
207
|
+
|
|
208
|
+
foldingsJAX = jax.jit(foldingsJAX, static_argnums=(0, 1))
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"""
|
|
2
|
+
An unnecessarily literal translation of the original Atlas Autocode code by W. F. Lunnon to Python.
|
|
3
|
+
W. F. Lunnon, Multi-dimensional map-folding, The Computer Journal, Volume 14, Issue 1, 1971, Pages 75-80, https://doi.org/10.1093/comjnl/14.1.75
|
|
4
|
+
"""# NOTE not functional yet
|
|
5
|
+
def foldings(p, job=None):
|
|
6
|
+
"""An unnecessarily literal translation of the original Atlas Autocode code."""
|
|
7
|
+
p = list(p)
|
|
8
|
+
p.append(None) # NOTE mimics Atlas `array` type
|
|
9
|
+
p.insert(0, None) # NOTE mimics Atlas `array` type
|
|
10
|
+
|
|
11
|
+
if job is None:
|
|
12
|
+
global G
|
|
13
|
+
G = 0
|
|
14
|
+
def job(A, B):
|
|
15
|
+
global G
|
|
16
|
+
G = G + 1
|
|
17
|
+
return foldings(p, job)
|
|
18
|
+
# perform job (A, B) on each folding of a p[1] x ... x p[d] map,
|
|
19
|
+
# where A and B are the above and below vectors. p[d + 1] < 0 terminates p;
|
|
20
|
+
|
|
21
|
+
d: int
|
|
22
|
+
n: int
|
|
23
|
+
j: int
|
|
24
|
+
i: int
|
|
25
|
+
m: int
|
|
26
|
+
l: int
|
|
27
|
+
g: int
|
|
28
|
+
gg: int
|
|
29
|
+
dd: int
|
|
30
|
+
|
|
31
|
+
n = 1
|
|
32
|
+
i, d = 0, 0
|
|
33
|
+
|
|
34
|
+
while (i := i + 1) and p[i] is not None:
|
|
35
|
+
d = i
|
|
36
|
+
n = n * p[i]
|
|
37
|
+
|
|
38
|
+
# d dimensions and n leaves;
|
|
39
|
+
|
|
40
|
+
# A: list[int] = [None] * (n + 1) # type: ignore
|
|
41
|
+
# B: list[int] = [None] * (n + 1) # type: ignore
|
|
42
|
+
# count: list[int] = [None] * (n + 1) # type: ignore
|
|
43
|
+
# gapter: list[int] = [None] * (n + 1) # type: ignore
|
|
44
|
+
# gap: list[int] = [None] * (n * n + 1) # type: ignore
|
|
45
|
+
A: list[int] = [0] * (n + 1) # type: ignore
|
|
46
|
+
B: list[int] = [0] * (n + 1) # type: ignore
|
|
47
|
+
count: list[int] = [0] * (n + 1) # type: ignore
|
|
48
|
+
gapter: list[int] = [0] * (n + 1) # type: ignore
|
|
49
|
+
gap: list[int] = [0] * (n * n + 1) # type: ignore
|
|
50
|
+
|
|
51
|
+
# B[m] is the leaf below leaf m in the current folding,
|
|
52
|
+
# A[m] the leaf above. count[m] is the no. of sections in which
|
|
53
|
+
# there is a gap for the new leaf l below leaf m,
|
|
54
|
+
# gap[gapter[l - 1] + j] is the j-th (possible or actual) gap for leaf l,
|
|
55
|
+
# and later gap[gapter[l]] is the gap where leaf l is currently inserted;
|
|
56
|
+
|
|
57
|
+
P: list[int] = [0] * (d + 1) # type: ignore
|
|
58
|
+
C: list[list[int]] = [[0] * (n + 1) for dimension1 in range(d + 1)] # type: ignore
|
|
59
|
+
# D: list[list[list[int]]] = [[[None] * (n + 1) for dimension2 in range(n + 1)] for dimension1 in range(d + 1)] # type: ignore
|
|
60
|
+
D: list[list[list[int]]] = [[[0] * (n + 1) for dimension2 in range(n + 1)] for dimension1 in range(d + 1)]
|
|
61
|
+
|
|
62
|
+
P[0] = 1
|
|
63
|
+
for i in range(1, d + 1):
|
|
64
|
+
P[i] = P[i - 1] * p[i]
|
|
65
|
+
|
|
66
|
+
for i in range(1, d + 1):
|
|
67
|
+
for m in range(1, n + 1):
|
|
68
|
+
C[i][m] = ((m - 1) // P[i - 1]) - ((m - 1) // P[i]) * p[i] + 1
|
|
69
|
+
|
|
70
|
+
for i in range(1, d + 1):
|
|
71
|
+
for l in range(1, n + 1):
|
|
72
|
+
for m in range(1, l + 1):
|
|
73
|
+
D[i][l][m] = (0 if m == 0
|
|
74
|
+
else
|
|
75
|
+
((m if C[i][m] == 1
|
|
76
|
+
else m - P[i - 1])
|
|
77
|
+
if C[i][l] - C[i][m] == (C[i][l] - C[i][m]) // 2 * 2
|
|
78
|
+
else
|
|
79
|
+
(m if C[i][m] == p[i] or m + P[i - 1] > l
|
|
80
|
+
else m + P[i - 1])))
|
|
81
|
+
# P[i] = p[1] x ... x p[i], C[i][m] = i-th co-ordinate of leaf m,
|
|
82
|
+
# D[i][l][m] = leaf connected to m in section i when inserting l;
|
|
83
|
+
|
|
84
|
+
for m in range(n + 1):
|
|
85
|
+
count[m] = 0
|
|
86
|
+
|
|
87
|
+
A[0], B[0], g, l = 0, 0, 0, 0
|
|
88
|
+
|
|
89
|
+
state = 'entry'
|
|
90
|
+
while True:
|
|
91
|
+
if state == 'entry':
|
|
92
|
+
gapter[l] = g
|
|
93
|
+
l = l + 1
|
|
94
|
+
if l <= n:
|
|
95
|
+
state = 'down'
|
|
96
|
+
continue
|
|
97
|
+
else:
|
|
98
|
+
job(A, B)
|
|
99
|
+
state = 'up'
|
|
100
|
+
continue
|
|
101
|
+
|
|
102
|
+
elif state == 'down':
|
|
103
|
+
dd = 0
|
|
104
|
+
gg = gapter[l - 1]
|
|
105
|
+
g = gg
|
|
106
|
+
for i in range(1, d + 1):
|
|
107
|
+
if D[i][l][l] == l:
|
|
108
|
+
dd = dd + 1
|
|
109
|
+
else:
|
|
110
|
+
m = D[i][l][l]
|
|
111
|
+
while m != l:
|
|
112
|
+
gap[gg] = m
|
|
113
|
+
if count[m] == 0:
|
|
114
|
+
gg = gg + 1
|
|
115
|
+
count[m] = count[m] + 1
|
|
116
|
+
m = D[i][l][B[m]]
|
|
117
|
+
|
|
118
|
+
if dd == d:
|
|
119
|
+
for m in range(l):
|
|
120
|
+
gap[gg] = m
|
|
121
|
+
gg = gg + 1
|
|
122
|
+
|
|
123
|
+
for j in range(g, gg):
|
|
124
|
+
gap[g] = gap[j]
|
|
125
|
+
if count[gap[j]] == d - dd:
|
|
126
|
+
g = g + 1
|
|
127
|
+
count[gap[j]] = 0
|
|
128
|
+
state = 'along'
|
|
129
|
+
continue
|
|
130
|
+
|
|
131
|
+
elif state == 'along':
|
|
132
|
+
if g == gapter[l - 1]:
|
|
133
|
+
state = 'up'
|
|
134
|
+
continue
|
|
135
|
+
g = g - 1
|
|
136
|
+
A[l] = gap[g]
|
|
137
|
+
B[l] = B[A[l]]
|
|
138
|
+
B[A[l]] = l
|
|
139
|
+
A[B[l]] = l
|
|
140
|
+
state = 'entry'
|
|
141
|
+
continue
|
|
142
|
+
|
|
143
|
+
elif state == 'up':
|
|
144
|
+
l = l - 1
|
|
145
|
+
B[A[l]] = B[l]
|
|
146
|
+
A[B[l]] = A[l]
|
|
147
|
+
if l > 0:
|
|
148
|
+
state = 'along'
|
|
149
|
+
continue
|
|
150
|
+
else:
|
|
151
|
+
break
|
|
152
|
+
|
|
153
|
+
return G #if job.__closure__ else None
|