mapFolding 0.3.12__py3-none-any.whl → 0.4.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.
Files changed (45) hide show
  1. mapFolding/__init__.py +40 -38
  2. mapFolding/basecamp.py +50 -50
  3. mapFolding/beDRY.py +336 -336
  4. mapFolding/oeis.py +262 -262
  5. mapFolding/reference/flattened.py +294 -293
  6. mapFolding/reference/hunterNumba.py +126 -126
  7. mapFolding/reference/irvineJavaPort.py +99 -99
  8. mapFolding/reference/jax.py +153 -153
  9. mapFolding/reference/lunnan.py +148 -148
  10. mapFolding/reference/lunnanNumpy.py +115 -115
  11. mapFolding/reference/lunnanWhile.py +114 -114
  12. mapFolding/reference/rotatedEntryPoint.py +183 -183
  13. mapFolding/reference/total_countPlus1vsPlusN.py +203 -203
  14. mapFolding/someAssemblyRequired/__init__.py +2 -1
  15. mapFolding/someAssemblyRequired/getLLVMforNoReason.py +12 -12
  16. mapFolding/someAssemblyRequired/makeJob.py +48 -48
  17. mapFolding/someAssemblyRequired/synthesizeModuleJAX.py +17 -17
  18. mapFolding/someAssemblyRequired/synthesizeNumba.py +343 -633
  19. mapFolding/someAssemblyRequired/synthesizeNumbaGeneralized.py +371 -0
  20. mapFolding/someAssemblyRequired/synthesizeNumbaJob.py +150 -0
  21. mapFolding/someAssemblyRequired/synthesizeNumbaModules.py +75 -0
  22. mapFolding/syntheticModules/__init__.py +0 -0
  23. mapFolding/syntheticModules/numba_countInitialize.py +3 -3
  24. mapFolding/syntheticModules/numba_countParallel.py +3 -3
  25. mapFolding/syntheticModules/numba_countSequential.py +3 -3
  26. mapFolding/syntheticModules/numba_doTheNeedful.py +6 -6
  27. mapFolding/theDao.py +165 -165
  28. mapFolding/theSSOT.py +176 -172
  29. mapFolding/theSSOTnumba.py +90 -74
  30. mapFolding-0.4.0.dist-info/METADATA +122 -0
  31. mapFolding-0.4.0.dist-info/RECORD +41 -0
  32. tests/conftest.py +238 -128
  33. tests/test_oeis.py +80 -80
  34. tests/test_other.py +137 -224
  35. tests/test_tasks.py +21 -21
  36. tests/test_types.py +2 -2
  37. mapFolding/someAssemblyRequired/synthesizeNumbaHardcoding.py +0 -188
  38. mapFolding-0.3.12.dist-info/METADATA +0 -155
  39. mapFolding-0.3.12.dist-info/RECORD +0 -40
  40. tests/conftest_tmpRegistry.py +0 -62
  41. tests/conftest_uniformTests.py +0 -53
  42. {mapFolding-0.3.12.dist-info → mapFolding-0.4.0.dist-info}/LICENSE +0 -0
  43. {mapFolding-0.3.12.dist-info → mapFolding-0.4.0.dist-info}/WHEEL +0 -0
  44. {mapFolding-0.3.12.dist-info → mapFolding-0.4.0.dist-info}/entry_points.txt +0 -0
  45. {mapFolding-0.3.12.dist-info → mapFolding-0.4.0.dist-info}/top_level.txt +0 -0
@@ -4,129 +4,129 @@ import numpy
4
4
 
5
5
  @numba.jit(cache=True, nopython=True, fastmath=True)
6
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)
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)
@@ -5,116 +5,116 @@ https://github.com/archmageirvine/joeis/blob/80e3e844b11f149704acbab520bc3a3a25a
5
5
  Citation: mapFolding/citations/jOEIS.bibtex
6
6
  """
7
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.
8
+ """
9
+ Compute the total number of foldings for a map with dimensions specified in p.
10
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).
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
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
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
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
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
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]
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
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
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
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]
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
60
 
61
- # Initialize variables for backtracking
62
- total_count = 0 # Total number of foldings
63
- g = 0 # Gap index
64
- l = 1 # Current leaf
61
+ # Initialize variables for backtracking
62
+ total_count = 0 # Total number of foldings
63
+ g = 0 # Gap index
64
+ l = 1 # Current leaf
65
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
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
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]]
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
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
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
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
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
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]
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
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
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
119
 
120
- return total_count
120
+ return total_count