1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-08-17 20:11:50 +02:00

refactor: tail recursion

This commit is contained in:
Justin Lin
2022-03-20 12:18:38 +08:00
parent 8c57db2e06
commit f0bc7e5dc2

View File

@@ -83,12 +83,12 @@ function wf_collapse(wf, x, y) =
_wf_collapse(wf, x, y, weights_xy, len(weights_xy), threshold); _wf_collapse(wf, x, y, weights_xy, len(weights_xy), threshold);
function _wf_collapse(wf, x, y, states_weights, leng, threshold, i = 0) = function _wf_collapse(wf, x, y, states_weights, leng, threshold, i = 0) =
i == leng ? wf : threshold < 0 || i == leng ? wf :
let( let(
state_weight = states_weights[i], state_weight = states_weights[i],
t = threshold - state_weight[1] t = threshold - state_weight[1]
) )
t < 0 ? _oneStateAt(wf, x, y, state_weight[0]) : _wf_collapse(wf, x, y, states_weights, leng, t, i + 1); _wf_collapse(t < 0 ? _oneStateAt(wf, x, y, state_weight[0]) : wf, x, y, states_weights, leng, t, i + 1);
function _oneStateAt(wf, x, y, state) = _replaceStatesAt(wf, x, y, [state]); function _oneStateAt(wf, x, y, state) = _replaceStatesAt(wf, x, y, [state]);
@@ -196,13 +196,12 @@ function _doDirs(tm, stack, cx, cy, current_tiles, dirs, leng, i = 0) =
not_compatible_nbr_tiles = [ not_compatible_nbr_tiles = [
for(nbr_tile = nbr_tiles) for(nbr_tile = nbr_tiles)
if(not_compatible_nbr_tile(tm, current_tiles, nbr_tile, dir)) nbr_tile if(not_compatible_nbr_tile(tm, current_tiles, nbr_tile, dir)) nbr_tile
] ],
leng_not_compatible_nbr_tiles_0 = len(not_compatible_nbr_tiles) == 0
) )
len(not_compatible_nbr_tiles) == 0 ? _doDirs(tm, stack, cx, cy, current_tiles, dirs, leng, i + 1) : _doDirs(
let( leng_not_compatible_nbr_tiles_0 ? tm :
nstack = stack_push(stack, [nbrx, nbry]), let(nwf = wf_remove(wf, nbrx, nbry, not_compatible_nbr_tiles)) [
nwf = wf_remove(wf, nbrx, nbry, not_compatible_nbr_tiles),
ntm = [
tilemap_width(tm), tilemap_width(tm),
tilemap_height(tm), tilemap_height(tm),
tilemap_compatibilities(tm), tilemap_compatibilities(tm),
@@ -210,9 +209,10 @@ function _doDirs(tm, stack, cx, cy, current_tiles, dirs, leng, i = 0) =
assert(false, assert(false,
str("(", nbrx, ", ", nbry, ")", str("(", nbrx, ", ", nbry, ")",
" reaches a contradiction. Tiles have all been ruled out by your previous choices. Please try again.")) " reaches a contradiction. Tiles have all been ruled out by your previous choices. Please try again."))
] ],
) leng_not_compatible_nbr_tiles_0 ? stack : stack_push(stack, [nbrx, nbry]),
_doDirs(ntm, nstack, cx, cy, current_tiles, dirs, leng, i + 1); cx, cy, current_tiles, dirs, leng, i + 1
);
function tilemap_generate(tm) = function tilemap_generate(tm) =
let(wf = tilemap_wf(tm)) let(wf = tilemap_wf(tm))