mapFolding 0.11.2__py3-none-any.whl → 0.11.3__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.
@@ -1,202 +0,0 @@
1
- from concurrent.futures import Future as ConcurrentFuture, ProcessPoolExecutor
2
- from copy import deepcopy
3
- from mapFolding import ComputationState
4
- from mapFolding import Array1DElephino, Array1DFoldsTotal, Array1DLeavesTotal, Array3D, DatatypeElephino, DatatypeFoldsTotal, DatatypeLeavesTotal
5
- from numba import jit
6
-
7
- def countInitialize(state: ComputationState) -> ComputationState:
8
- while state.gap1ndex == 0:
9
- if state.leaf1ndex <= 1 or state.leafBelow[0] == 1:
10
- state.dimensionsUnconstrained = state.dimensionsTotal
11
- state.gap1ndexCeiling = state.gapRangeStart[state.leaf1ndex - 1]
12
- state.indexDimension = 0
13
- while state.indexDimension < state.dimensionsTotal:
14
- state.leafConnectee = state.connectionGraph[state.indexDimension, state.leaf1ndex, state.leaf1ndex]
15
- if state.leafConnectee == state.leaf1ndex:
16
- state.dimensionsUnconstrained -= 1
17
- else:
18
- while state.leafConnectee != state.leaf1ndex:
19
- state.gapsWhere[state.gap1ndexCeiling] = state.leafConnectee
20
- if state.countDimensionsGapped[state.leafConnectee] == 0:
21
- state.gap1ndexCeiling += 1
22
- state.countDimensionsGapped[state.leafConnectee] += 1
23
- state.leafConnectee = state.connectionGraph[state.indexDimension, state.leaf1ndex, state.leafBelow[state.leafConnectee]]
24
- state.indexDimension += 1
25
- if not state.dimensionsUnconstrained:
26
- state.indexLeaf = 0
27
- while state.indexLeaf < state.leaf1ndex:
28
- state.gapsWhere[state.gap1ndexCeiling] = state.indexLeaf
29
- state.gap1ndexCeiling += 1
30
- state.indexLeaf += 1
31
- state.indexMiniGap = state.gap1ndex
32
- while state.indexMiniGap < state.gap1ndexCeiling:
33
- state.gapsWhere[state.gap1ndex] = state.gapsWhere[state.indexMiniGap]
34
- if state.countDimensionsGapped[state.gapsWhere[state.indexMiniGap]] == state.dimensionsUnconstrained:
35
- state.gap1ndex += 1
36
- state.countDimensionsGapped[state.gapsWhere[state.indexMiniGap]] = 0
37
- state.indexMiniGap += 1
38
- if state.leaf1ndex > 0:
39
- state.gap1ndex -= 1
40
- state.leafAbove[state.leaf1ndex] = state.gapsWhere[state.gap1ndex]
41
- state.leafBelow[state.leaf1ndex] = state.leafBelow[state.leafAbove[state.leaf1ndex]]
42
- state.leafBelow[state.leafAbove[state.leaf1ndex]] = state.leaf1ndex
43
- state.leafAbove[state.leafBelow[state.leaf1ndex]] = state.leaf1ndex
44
- state.gapRangeStart[state.leaf1ndex] = state.gap1ndex
45
- state.leaf1ndex += 1
46
- return state
47
-
48
- @jit(_nrt=True, boundscheck=False, cache=True, error_model='numpy', fastmath=True, forceinline=True, inline='always', looplift=False, no_cfunc_wrapper=False, no_cpython_wrapper=False, nopython=True, parallel=False)
49
- def countParallel(leavesTotal: DatatypeLeavesTotal, taskDivisions: DatatypeLeavesTotal, connectionGraph: Array3D, dimensionsTotal: DatatypeLeavesTotal, countDimensionsGapped: Array1DLeavesTotal, dimensionsUnconstrained: DatatypeLeavesTotal, gapRangeStart: Array1DElephino, gapsWhere: Array1DLeavesTotal, leafAbove: Array1DLeavesTotal, leafBelow: Array1DLeavesTotal, foldGroups: Array1DFoldsTotal, gap1ndex: DatatypeElephino, gap1ndexCeiling: DatatypeElephino, groupsOfFolds: DatatypeFoldsTotal, indexDimension: DatatypeLeavesTotal, indexMiniGap: DatatypeElephino, leaf1ndex: DatatypeLeavesTotal, leafConnectee: DatatypeLeavesTotal, taskIndex: DatatypeLeavesTotal) -> DatatypeFoldsTotal:
50
- while leaf1ndex > 0:
51
- if leaf1ndex <= 1 or leafBelow[0] == 1:
52
- if leaf1ndex > leavesTotal:
53
- groupsOfFolds += 1
54
- else:
55
- dimensionsUnconstrained = dimensionsTotal
56
- gap1ndexCeiling = gapRangeStart[leaf1ndex - 1]
57
- indexDimension = 0
58
- while indexDimension < dimensionsTotal:
59
- if connectionGraph[indexDimension, leaf1ndex, leaf1ndex] == leaf1ndex:
60
- dimensionsUnconstrained -= 1
61
- else:
62
- leafConnectee = connectionGraph[indexDimension, leaf1ndex, leaf1ndex]
63
- while leafConnectee != leaf1ndex:
64
- if leaf1ndex != taskDivisions or leafConnectee % taskDivisions == taskIndex:
65
- gapsWhere[gap1ndexCeiling] = leafConnectee
66
- if countDimensionsGapped[leafConnectee] == 0:
67
- gap1ndexCeiling += 1
68
- countDimensionsGapped[leafConnectee] += 1
69
- leafConnectee = connectionGraph[indexDimension, leaf1ndex, leafBelow[leafConnectee]]
70
- indexDimension += 1
71
- indexMiniGap = gap1ndex
72
- while indexMiniGap < gap1ndexCeiling:
73
- gapsWhere[gap1ndex] = gapsWhere[indexMiniGap]
74
- if countDimensionsGapped[gapsWhere[indexMiniGap]] == dimensionsUnconstrained:
75
- gap1ndex += 1
76
- countDimensionsGapped[gapsWhere[indexMiniGap]] = 0
77
- indexMiniGap += 1
78
- while leaf1ndex > 0 and gap1ndex == gapRangeStart[leaf1ndex - 1]:
79
- leaf1ndex -= 1
80
- leafBelow[leafAbove[leaf1ndex]] = leafBelow[leaf1ndex]
81
- leafAbove[leafBelow[leaf1ndex]] = leafAbove[leaf1ndex]
82
- if leaf1ndex > 0:
83
- gap1ndex -= 1
84
- leafAbove[leaf1ndex] = gapsWhere[gap1ndex]
85
- leafBelow[leaf1ndex] = leafBelow[leafAbove[leaf1ndex]]
86
- leafBelow[leafAbove[leaf1ndex]] = leaf1ndex
87
- leafAbove[leafBelow[leaf1ndex]] = leaf1ndex
88
- gapRangeStart[leaf1ndex] = gap1ndex
89
- leaf1ndex += 1
90
- foldGroups[taskIndex] = groupsOfFolds
91
- return groupsOfFolds
92
-
93
- @jit(_nrt=True, boundscheck=False, cache=True, error_model='numpy', fastmath=True, forceinline=True, inline='always', looplift=False, no_cfunc_wrapper=False, no_cpython_wrapper=False, nopython=True, parallel=False)
94
- def countSequential(mapShape: tuple[DatatypeLeavesTotal, ...], leavesTotal: DatatypeLeavesTotal, taskDivisions: DatatypeLeavesTotal, concurrencyLimit: DatatypeElephino, connectionGraph: Array3D, dimensionsTotal: DatatypeLeavesTotal, countDimensionsGapped: Array1DLeavesTotal, dimensionsUnconstrained: DatatypeLeavesTotal, gapRangeStart: Array1DElephino, gapsWhere: Array1DLeavesTotal, leafAbove: Array1DLeavesTotal, leafBelow: Array1DLeavesTotal, foldGroups: Array1DFoldsTotal, foldsTotal: DatatypeFoldsTotal, gap1ndex: DatatypeElephino, gap1ndexCeiling: DatatypeElephino, groupsOfFolds: DatatypeFoldsTotal, indexDimension: DatatypeLeavesTotal, indexLeaf: DatatypeLeavesTotal, indexMiniGap: DatatypeElephino, leaf1ndex: DatatypeLeavesTotal, leafConnectee: DatatypeLeavesTotal, taskIndex: DatatypeLeavesTotal) -> tuple[tuple[DatatypeLeavesTotal, ...], DatatypeLeavesTotal, DatatypeLeavesTotal, DatatypeElephino, Array3D, DatatypeLeavesTotal, Array1DLeavesTotal, DatatypeLeavesTotal, Array1DElephino, Array1DLeavesTotal, Array1DLeavesTotal, Array1DLeavesTotal, Array1DFoldsTotal, DatatypeFoldsTotal, DatatypeElephino, DatatypeElephino, DatatypeFoldsTotal, DatatypeLeavesTotal, DatatypeLeavesTotal, DatatypeElephino, DatatypeLeavesTotal, DatatypeLeavesTotal, DatatypeLeavesTotal]:
95
- while leaf1ndex > 0:
96
- if leaf1ndex <= 1 or leafBelow[0] == 1:
97
- if leaf1ndex > leavesTotal:
98
- groupsOfFolds += 1
99
- else:
100
- dimensionsUnconstrained = dimensionsTotal
101
- gap1ndexCeiling = gapRangeStart[leaf1ndex - 1]
102
- indexDimension = 0
103
- while indexDimension < dimensionsTotal:
104
- leafConnectee = connectionGraph[indexDimension, leaf1ndex, leaf1ndex]
105
- if leafConnectee == leaf1ndex:
106
- dimensionsUnconstrained -= 1
107
- else:
108
- while leafConnectee != leaf1ndex:
109
- gapsWhere[gap1ndexCeiling] = leafConnectee
110
- if countDimensionsGapped[leafConnectee] == 0:
111
- gap1ndexCeiling += 1
112
- countDimensionsGapped[leafConnectee] += 1
113
- leafConnectee = connectionGraph[indexDimension, leaf1ndex, leafBelow[leafConnectee]]
114
- indexDimension += 1
115
- indexMiniGap = gap1ndex
116
- while indexMiniGap < gap1ndexCeiling:
117
- gapsWhere[gap1ndex] = gapsWhere[indexMiniGap]
118
- if countDimensionsGapped[gapsWhere[indexMiniGap]] == dimensionsUnconstrained:
119
- gap1ndex += 1
120
- countDimensionsGapped[gapsWhere[indexMiniGap]] = 0
121
- indexMiniGap += 1
122
- while leaf1ndex > 0 and gap1ndex == gapRangeStart[leaf1ndex - 1]:
123
- leaf1ndex -= 1
124
- leafBelow[leafAbove[leaf1ndex]] = leafBelow[leaf1ndex]
125
- leafAbove[leafBelow[leaf1ndex]] = leafAbove[leaf1ndex]
126
- if leaf1ndex == 3 and groupsOfFolds:
127
- groupsOfFolds *= 2
128
- break
129
- if leaf1ndex > 0:
130
- gap1ndex -= 1
131
- leafAbove[leaf1ndex] = gapsWhere[gap1ndex]
132
- leafBelow[leaf1ndex] = leafBelow[leafAbove[leaf1ndex]]
133
- leafBelow[leafAbove[leaf1ndex]] = leaf1ndex
134
- leafAbove[leafBelow[leaf1ndex]] = leaf1ndex
135
- gapRangeStart[leaf1ndex] = gap1ndex
136
- leaf1ndex += 1
137
- foldGroups[taskIndex] = groupsOfFolds
138
- return (mapShape, leavesTotal, taskDivisions, concurrencyLimit, connectionGraph, dimensionsTotal, countDimensionsGapped, dimensionsUnconstrained, gapRangeStart, gapsWhere, leafAbove, leafBelow, foldGroups, foldsTotal, gap1ndex, gap1ndexCeiling, groupsOfFolds, indexDimension, indexLeaf, indexMiniGap, leaf1ndex, leafConnectee, taskIndex)
139
-
140
- def doTheNeedful(state: ComputationState) -> ComputationState:
141
- state = countInitialize(state)
142
- if state.taskDivisions > 0:
143
- dictionaryConcurrency: dict[int, ConcurrentFuture[ComputationState]] = {}
144
- stateParallel = deepcopy(state)
145
- with ProcessPoolExecutor(stateParallel.concurrencyLimit) as concurrencyManager:
146
- for indexSherpa in range(stateParallel.taskDivisions):
147
- state = deepcopy(stateParallel)
148
- state.taskIndex = indexSherpa
149
- mapShape: tuple[DatatypeLeavesTotal, ...] = state.mapShape
150
- leavesTotal: DatatypeLeavesTotal = state.leavesTotal
151
- taskDivisions: DatatypeLeavesTotal = state.taskDivisions
152
- concurrencyLimit: DatatypeElephino = state.concurrencyLimit
153
- connectionGraph: Array3D = state.connectionGraph
154
- dimensionsTotal: DatatypeLeavesTotal = state.dimensionsTotal
155
- countDimensionsGapped: Array1DLeavesTotal = state.countDimensionsGapped
156
- dimensionsUnconstrained: DatatypeLeavesTotal = state.dimensionsUnconstrained
157
- gapRangeStart: Array1DElephino = state.gapRangeStart
158
- gapsWhere: Array1DLeavesTotal = state.gapsWhere
159
- leafAbove: Array1DLeavesTotal = state.leafAbove
160
- leafBelow: Array1DLeavesTotal = state.leafBelow
161
- foldGroups: Array1DFoldsTotal = state.foldGroups
162
- foldsTotal: DatatypeFoldsTotal = state.foldsTotal
163
- gap1ndex: DatatypeElephino = state.gap1ndex
164
- gap1ndexCeiling: DatatypeElephino = state.gap1ndexCeiling
165
- groupsOfFolds: DatatypeFoldsTotal = state.groupsOfFolds
166
- indexDimension: DatatypeLeavesTotal = state.indexDimension
167
- indexLeaf: DatatypeLeavesTotal = state.indexLeaf
168
- indexMiniGap: DatatypeElephino = state.indexMiniGap
169
- leaf1ndex: DatatypeLeavesTotal = state.leaf1ndex
170
- leafConnectee: DatatypeLeavesTotal = state.leafConnectee
171
- taskIndex: DatatypeLeavesTotal = state.taskIndex
172
- dictionaryConcurrency[indexSherpa] = concurrencyManager.submit(countParallel, leavesTotal, taskDivisions, connectionGraph, dimensionsTotal, countDimensionsGapped, dimensionsUnconstrained, gapRangeStart, gapsWhere, leafAbove, leafBelow, foldGroups, gap1ndex, gap1ndexCeiling, groupsOfFolds, indexDimension, indexMiniGap, leaf1ndex, leafConnectee, taskIndex)
173
- for indexSherpa in range(stateParallel.taskDivisions):
174
- stateParallel.foldGroups[indexSherpa] = dictionaryConcurrency[indexSherpa].result()
175
- state = stateParallel
176
- else:
177
- mapShape: tuple[DatatypeLeavesTotal, ...] = state.mapShape
178
- leavesTotal: DatatypeLeavesTotal = state.leavesTotal
179
- taskDivisions: DatatypeLeavesTotal = state.taskDivisions
180
- concurrencyLimit: DatatypeElephino = state.concurrencyLimit
181
- connectionGraph: Array3D = state.connectionGraph
182
- dimensionsTotal: DatatypeLeavesTotal = state.dimensionsTotal
183
- countDimensionsGapped: Array1DLeavesTotal = state.countDimensionsGapped
184
- dimensionsUnconstrained: DatatypeLeavesTotal = state.dimensionsUnconstrained
185
- gapRangeStart: Array1DElephino = state.gapRangeStart
186
- gapsWhere: Array1DLeavesTotal = state.gapsWhere
187
- leafAbove: Array1DLeavesTotal = state.leafAbove
188
- leafBelow: Array1DLeavesTotal = state.leafBelow
189
- foldGroups: Array1DFoldsTotal = state.foldGroups
190
- foldsTotal: DatatypeFoldsTotal = state.foldsTotal
191
- gap1ndex: DatatypeElephino = state.gap1ndex
192
- gap1ndexCeiling: DatatypeElephino = state.gap1ndexCeiling
193
- groupsOfFolds: DatatypeFoldsTotal = state.groupsOfFolds
194
- indexDimension: DatatypeLeavesTotal = state.indexDimension
195
- indexLeaf: DatatypeLeavesTotal = state.indexLeaf
196
- indexMiniGap: DatatypeElephino = state.indexMiniGap
197
- leaf1ndex: DatatypeLeavesTotal = state.leaf1ndex
198
- leafConnectee: DatatypeLeavesTotal = state.leafConnectee
199
- taskIndex: DatatypeLeavesTotal = state.taskIndex
200
- mapShape, leavesTotal, taskDivisions, concurrencyLimit, connectionGraph, dimensionsTotal, countDimensionsGapped, dimensionsUnconstrained, gapRangeStart, gapsWhere, leafAbove, leafBelow, foldGroups, foldsTotal, gap1ndex, gap1ndexCeiling, groupsOfFolds, indexDimension, indexLeaf, indexMiniGap, leaf1ndex, leafConnectee, taskIndex = countSequential(mapShape, leavesTotal, taskDivisions, concurrencyLimit, connectionGraph, dimensionsTotal, countDimensionsGapped, dimensionsUnconstrained, gapRangeStart, gapsWhere, leafAbove, leafBelow, foldGroups, foldsTotal, gap1ndex, gap1ndexCeiling, groupsOfFolds, indexDimension, indexLeaf, indexMiniGap, leaf1ndex, leafConnectee, taskIndex)
201
- state = ComputationState(mapShape=mapShape, leavesTotal=leavesTotal, taskDivisions=taskDivisions, concurrencyLimit=concurrencyLimit, countDimensionsGapped=countDimensionsGapped, dimensionsUnconstrained=dimensionsUnconstrained, gapRangeStart=gapRangeStart, gapsWhere=gapsWhere, leafAbove=leafAbove, leafBelow=leafBelow, foldGroups=foldGroups, foldsTotal=foldsTotal, gap1ndex=gap1ndex, gap1ndexCeiling=gap1ndexCeiling, groupsOfFolds=groupsOfFolds, indexDimension=indexDimension, indexLeaf=indexLeaf, indexMiniGap=indexMiniGap, leaf1ndex=leaf1ndex, leafConnectee=leafConnectee, taskIndex=taskIndex)
202
- return state
mapFolding/theDao.py DELETED
@@ -1,243 +0,0 @@
1
- """
2
- Core computational algorithm for map folding counting and enumeration.
3
-
4
- This module implements the core algorithms for enumerating and counting the various ways a rectangular map can be
5
- folded. It uses a functional state-transformation approach, where each function performs a specific state mutation and
6
- returns the updated state. The module provides three main counting algorithms:
7
-
8
- 1. `countInitialize`: Sets up the initial state for computation.
9
- 2. `countSequential`: Processes the folding computation sequentially.
10
- 3. `countParallel`: Distributes the computation across multiple processes.
11
-
12
- All algorithms operate on a `ComputationState` object that tracks the folding process, including:
13
- - A "leaf" is a unit square in the map.
14
- - A "gap" is a potential position where a new leaf can be folded.
15
- - Connections track how leaves can connect above/below each other.
16
- - Leaves are enumerated starting from 1, not 0; hence, `leaf1ndex` not `leafIndex`.
17
-
18
- The `doTheNeedful` function is the main entry point that orchestrates the computation strategy based on task divisions and
19
- concurrency parameters.
20
- """
21
- from concurrent.futures import Future as ConcurrentFuture, ProcessPoolExecutor
22
- from copy import deepcopy
23
- from mapFolding.beDRY import ComputationState
24
- from multiprocessing import set_start_method as multiprocessing_set_start_method
25
-
26
- # When to use multiprocessing.set_start_method https://github.com/hunterhogan/mapFolding/issues/6
27
- if __name__ == '__main__':
28
- multiprocessing_set_start_method('spawn')
29
-
30
- def activeLeafConnectedToItself(state: ComputationState) -> bool:
31
- return state.leafConnectee == state.leaf1ndex
32
-
33
- def activeLeafGreaterThan0(state: ComputationState) -> bool:
34
- return state.leaf1ndex > 0
35
-
36
- def activeLeafGreaterThanLeavesTotal(state: ComputationState) -> bool:
37
- return state.leaf1ndex > state.leavesTotal
38
-
39
- def activeLeafIsTheFirstLeaf(state: ComputationState) -> bool:
40
- return state.leaf1ndex <= 1
41
-
42
- def allDimensionsAreUnconstrained(state: ComputationState) -> bool:
43
- return not state.dimensionsUnconstrained
44
-
45
- def countGaps(state: ComputationState) -> ComputationState:
46
- state.gapsWhere[state.gap1ndexCeiling] = state.leafConnectee
47
- if state.countDimensionsGapped[state.leafConnectee] == 0:
48
- state = incrementGap1ndexCeiling(state)
49
- state.countDimensionsGapped[state.leafConnectee] += 1
50
- return state
51
-
52
- def decrementDimensionsUnconstrained(state: ComputationState) -> ComputationState:
53
- state.dimensionsUnconstrained -= 1
54
- return state
55
-
56
- def dimensionsUnconstrainedCondition(state: ComputationState) -> bool:
57
- return state.connectionGraph[state.indexDimension, state.leaf1ndex, state.leaf1ndex] == state.leaf1ndex
58
-
59
- def filterCommonGaps(state: ComputationState) -> ComputationState:
60
- state.gapsWhere[state.gap1ndex] = state.gapsWhere[state.indexMiniGap]
61
- if state.countDimensionsGapped[state.gapsWhere[state.indexMiniGap]] == state.dimensionsUnconstrained:
62
- state = incrementActiveGap(state)
63
- state.countDimensionsGapped[state.gapsWhere[state.indexMiniGap]] = 0
64
- return state
65
-
66
- def incrementActiveGap(state: ComputationState) -> ComputationState:
67
- state.gap1ndex += 1
68
- return state
69
-
70
- def incrementGap1ndexCeiling(state: ComputationState) -> ComputationState:
71
- state.gap1ndexCeiling += 1
72
- return state
73
-
74
- def incrementIndexDimension(state: ComputationState) -> ComputationState:
75
- state.indexDimension += 1
76
- return state
77
-
78
- def incrementIndexMiniGap(state: ComputationState) -> ComputationState:
79
- state.indexMiniGap += 1
80
- return state
81
-
82
- def initializeIndexMiniGap(state: ComputationState) -> ComputationState:
83
- state.indexMiniGap = state.gap1ndex
84
- return state
85
-
86
- def initializeLeafConnectee(state: ComputationState) -> ComputationState:
87
- state.leafConnectee = state.connectionGraph[state.indexDimension, state.leaf1ndex, state.leaf1ndex]
88
- return state
89
-
90
- def initializeVariablesToFindGaps(state: ComputationState) -> ComputationState:
91
- state.dimensionsUnconstrained = state.dimensionsTotal
92
- state.gap1ndexCeiling = state.gapRangeStart[state.leaf1ndex - 1]
93
- state.indexDimension = 0
94
- return state
95
-
96
- def insertLeafAtGap(state: ComputationState) -> ComputationState:
97
- state.gap1ndex -= 1
98
- state.leafAbove[state.leaf1ndex] = state.gapsWhere[state.gap1ndex]
99
- state.leafBelow[state.leaf1ndex] = state.leafBelow[state.leafAbove[state.leaf1ndex]]
100
- state.leafBelow[state.leafAbove[state.leaf1ndex]] = state.leaf1ndex
101
- state.leafAbove[state.leafBelow[state.leaf1ndex]] = state.leaf1ndex
102
- state.gapRangeStart[state.leaf1ndex] = state.gap1ndex
103
- state.leaf1ndex += 1
104
- return state
105
-
106
- def insertUnconstrainedLeaf(state: ComputationState) -> ComputationState:
107
- state.indexLeaf = 0
108
- while state.indexLeaf < state.leaf1ndex:
109
- state.gapsWhere[state.gap1ndexCeiling] = state.indexLeaf
110
- state.gap1ndexCeiling += 1
111
- state.indexLeaf += 1
112
- return state
113
-
114
- def leafBelowSentinelIs1(state: ComputationState) -> bool:
115
- return state.leafBelow[0] == 1
116
-
117
- def loopingLeavesConnectedToActiveLeaf(state: ComputationState) -> bool:
118
- return state.leafConnectee != state.leaf1ndex
119
-
120
- def loopingToActiveGapCeiling(state: ComputationState) -> bool:
121
- return state.indexMiniGap < state.gap1ndexCeiling
122
-
123
- def loopUpToDimensionsTotal(state: ComputationState) -> bool:
124
- return state.indexDimension < state.dimensionsTotal
125
-
126
- def noGapsHere(state: ComputationState) -> bool:
127
- return (state.leaf1ndex > 0) and (state.gap1ndex == state.gapRangeStart[state.leaf1ndex - 1])
128
-
129
- def thereIsAnActiveLeaf(state: ComputationState) -> bool:
130
- return state.leaf1ndex > 0
131
-
132
- def thisIsMyTaskIndex(state: ComputationState) -> bool:
133
- return (state.leaf1ndex != state.taskDivisions) or (state.leafConnectee % state.taskDivisions == state.taskIndex)
134
-
135
- def undoLastLeafPlacement(state: ComputationState) -> ComputationState:
136
- state.leaf1ndex -= 1
137
- state.leafBelow[state.leafAbove[state.leaf1ndex]] = state.leafBelow[state.leaf1ndex]
138
- state.leafAbove[state.leafBelow[state.leaf1ndex]] = state.leafAbove[state.leaf1ndex]
139
- return state
140
-
141
- def updateLeafConnectee(state: ComputationState) -> ComputationState:
142
- state.leafConnectee = state.connectionGraph[state.indexDimension, state.leaf1ndex, state.leafBelow[state.leafConnectee]]
143
- return state
144
-
145
- def countInitialize(state: ComputationState) -> ComputationState:
146
- while state.gap1ndex == 0:
147
- if activeLeafIsTheFirstLeaf(state) or leafBelowSentinelIs1(state):
148
- state = initializeVariablesToFindGaps(state)
149
- while loopUpToDimensionsTotal(state):
150
- state = initializeLeafConnectee(state)
151
- if activeLeafConnectedToItself(state):
152
- state = decrementDimensionsUnconstrained(state)
153
- else:
154
- while loopingLeavesConnectedToActiveLeaf(state):
155
- state = countGaps(state)
156
- state = updateLeafConnectee(state)
157
- state = incrementIndexDimension(state)
158
- if allDimensionsAreUnconstrained(state):
159
- state = insertUnconstrainedLeaf(state)
160
- state = initializeIndexMiniGap(state)
161
- while loopingToActiveGapCeiling(state):
162
- state = filterCommonGaps(state)
163
- state = incrementIndexMiniGap(state)
164
- if thereIsAnActiveLeaf(state):
165
- state = insertLeafAtGap(state)
166
- return state
167
-
168
- def countParallel(state: ComputationState) -> ComputationState:
169
- while activeLeafGreaterThan0(state):
170
- if activeLeafIsTheFirstLeaf(state) or leafBelowSentinelIs1(state):
171
- if activeLeafGreaterThanLeavesTotal(state):
172
- state.groupsOfFolds += 1
173
- else:
174
- state = initializeVariablesToFindGaps(state)
175
- while loopUpToDimensionsTotal(state):
176
- if dimensionsUnconstrainedCondition(state):
177
- state = decrementDimensionsUnconstrained(state)
178
- else:
179
- state = initializeLeafConnectee(state)
180
- while loopingLeavesConnectedToActiveLeaf(state):
181
- if thisIsMyTaskIndex(state):
182
- state = countGaps(state)
183
- state = updateLeafConnectee(state)
184
- state = incrementIndexDimension(state)
185
- state = initializeIndexMiniGap(state)
186
- while loopingToActiveGapCeiling(state):
187
- state = filterCommonGaps(state)
188
- state = incrementIndexMiniGap(state)
189
- while noGapsHere(state):
190
- state = undoLastLeafPlacement(state)
191
- if thereIsAnActiveLeaf(state):
192
- state = insertLeafAtGap(state)
193
- state.foldGroups[state.taskIndex] = state.groupsOfFolds
194
- return state
195
-
196
- def countSequential(state: ComputationState) -> ComputationState:
197
- while activeLeafGreaterThan0(state):
198
- if activeLeafIsTheFirstLeaf(state) or leafBelowSentinelIs1(state):
199
- if activeLeafGreaterThanLeavesTotal(state):
200
- state.groupsOfFolds += 1
201
- else:
202
- state = initializeVariablesToFindGaps(state)
203
- while loopUpToDimensionsTotal(state):
204
- state = initializeLeafConnectee(state)
205
- if activeLeafConnectedToItself(state):
206
- state = decrementDimensionsUnconstrained(state)
207
- else:
208
- while loopingLeavesConnectedToActiveLeaf(state):
209
- state = countGaps(state)
210
- state = updateLeafConnectee(state)
211
- state = incrementIndexDimension(state)
212
- state = initializeIndexMiniGap(state)
213
- while loopingToActiveGapCeiling(state):
214
- state = filterCommonGaps(state)
215
- state = incrementIndexMiniGap(state)
216
- while noGapsHere(state):
217
- state = undoLastLeafPlacement(state)
218
- if state.leaf1ndex == 3 and state.groupsOfFolds:
219
- state.groupsOfFolds *= 2
220
- # print('break')
221
- break
222
- if thereIsAnActiveLeaf(state):
223
- state = insertLeafAtGap(state)
224
- state.foldGroups[state.taskIndex] = state.groupsOfFolds
225
- return state
226
-
227
- def doTheNeedful(state: ComputationState) -> ComputationState:
228
- state = countInitialize(state)
229
- if state.taskDivisions > 0:
230
- dictionaryConcurrency: dict[int, ConcurrentFuture[ComputationState]] = {}
231
- stateParallel = deepcopy(state)
232
- with ProcessPoolExecutor(stateParallel.concurrencyLimit) as concurrencyManager:
233
- for indexSherpa in range(stateParallel.taskDivisions):
234
- state = deepcopy(stateParallel)
235
- state.taskIndex = indexSherpa
236
- dictionaryConcurrency[indexSherpa] = concurrencyManager.submit(countParallel, state)
237
- for indexSherpa in range(stateParallel.taskDivisions):
238
- stateParallel.foldGroups[indexSherpa] = dictionaryConcurrency[indexSherpa].result().foldGroups[indexSherpa]
239
- state = stateParallel
240
- else:
241
- state = countSequential(state)
242
-
243
- return state