From 16f997d34c7b435d3fcf5774c700579e188b017f Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Thu, 20 Apr 2023 13:13:47 +0100 Subject: Stop putting dsfs in existing scratch int arrays. I'm going to work towards turning 'dsf' into an opaque type, so that I can improve its implementation without breaking clients. The first step is to deal manually with puzzles that currently reuse a single array of ints for multiple purposes, some of which are dsf and some are not. In divvy_rectangle_attempt, 'tmp' was used as an int array and later a dsf, and I've made a new 'tmpdsf' to be the latter. In Dominosa, solver->pc_scratch2 was sometimes a dsf, and now there's a new solver->dsf_scratch. In Map, parse_edge_list() needed a dsf internally and then never exported it; it expected to be passed an array of 2*w*h ints and used the second half as a dsf. Now it expects only w*h ints, and allocates its own dsf internally, freeing it again before returning. And in Tents, find_errors() was allocating a single block of 2*w*h ints and using the second half of it as a dsf, apparently just to save one malloc. Now we malloc and free the dsf separately. --- divvy.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'divvy.c') diff --git a/divvy.c b/divvy.c index 021956f..91379e4 100644 --- a/divvy.c +++ b/divvy.c @@ -262,7 +262,7 @@ static bool addremcommon(int w, int h, int x, int y, int *own, int val) */ int *divvy_rectangle_attempt(int w, int h, int k, random_state *rs) { - int *order, *queue, *tmp, *own, *sizes, *addable, *retdsf; + int *order, *queue, *tmp, *own, *sizes, *addable, *retdsf, *tmpdsf; bool *removable; int wh = w*h; int i, j, n, x, y, qhead, qtail; @@ -277,6 +277,7 @@ int *divvy_rectangle_attempt(int w, int h, int k, random_state *rs) queue = snewn(n, int); addable = snewn(wh*4, int); removable = snewn(wh, bool); + retdsf = tmpdsf = NULL; /* * Permute the grid squares into a random order, which will be @@ -619,18 +620,18 @@ int *divvy_rectangle_attempt(int w, int h, int k, random_state *rs) * the ominoes really are k-ominoes and we haven't * accidentally split one into two disconnected pieces. */ - dsf_init(tmp, wh); + tmpdsf = snew_dsf(wh); for (y = 0; y < h; y++) for (x = 0; x+1 < w; x++) if (own[y*w+x] == own[y*w+(x+1)]) - dsf_merge(tmp, y*w+x, y*w+(x+1)); + dsf_merge(tmpdsf, y*w+x, y*w+(x+1)); for (x = 0; x < w; x++) for (y = 0; y+1 < h; y++) if (own[y*w+x] == own[(y+1)*w+x]) - dsf_merge(tmp, y*w+x, (y+1)*w+x); + dsf_merge(tmpdsf, y*w+x, (y+1)*w+x); for (i = 0; i < wh; i++) { j = dsf_canonify(retdsf, i); - assert(dsf_canonify(tmp, j) == dsf_canonify(tmp, i)); + assert(dsf_canonify(tmpdsf, j) == dsf_canonify(tmpdsf, i)); } cleanup: @@ -640,6 +641,7 @@ int *divvy_rectangle_attempt(int w, int h, int k, random_state *rs) */ sfree(order); sfree(tmp); + sfree(tmpdsf); sfree(own); sfree(sizes); sfree(queue); -- cgit v1.1