mirror of
git://git.tartarus.org/simon/puzzles.git
synced 2025-04-21 08:01:30 -07:00
Files

Having stated the principle in the previous commit, I should apply it consistently. A source file linked into the Puzzles library of common support code should not also define a main() under ifdef. This commit only goes as far as the _library_ support modules. It would be a much bigger job to do the same for all the actual _puzzles_ that have test main()s or standalone-solver main()s. And it's not necessary, because modifying one of those source files only triggers a rebuild of _one_ puzzle, not absolutely everything. (Not to mention that it's quite likely the puzzle and the test main() will need to be modified in conjunction anyway.) As in the previous commit, this has required exposing a few internal API functions as global, and maybe editing them a bit. In particular, the one-shot internal function that divvy_rectangle() loops on until it succeeds is now exposed as divvy_rectangle_attempt(), which means the test program doesn't have to condition a failure counter into the real function. I've thrown away penrose-vector-test completely, because that didn't look like a test program with any ongoing use at all - it was surely vestigial, while James was getting the vector representation up and running in the first place.
90 lines
2.3 KiB
C
90 lines
2.3 KiB
C
/*
|
|
* Implement arraysort() defined in puzzles.h.
|
|
*
|
|
* Strategy: heapsort.
|
|
*/
|
|
|
|
#include <stddef.h>
|
|
#include <string.h>
|
|
|
|
#include "puzzles.h"
|
|
|
|
static void memswap(void *av, void *bv, size_t size)
|
|
{
|
|
char t[4096];
|
|
char *a = (char *)av, *b = (char *)bv;
|
|
|
|
while (size > 0) {
|
|
size_t thissize = size < sizeof(t) ? size : sizeof(t);
|
|
|
|
memcpy(t, a, thissize);
|
|
memcpy(a, b, thissize);
|
|
memcpy(b, t, thissize);
|
|
|
|
size -= thissize;
|
|
a += thissize;
|
|
b += thissize;
|
|
}
|
|
}
|
|
|
|
#define PTR(i) ((char *)array + size * (i))
|
|
#define SWAP(i,j) memswap(PTR(i), PTR(j), size)
|
|
#define CMP(i,j) cmp(PTR(i), PTR(j), ctx)
|
|
|
|
#define LCHILD(i) (2*(i)+1)
|
|
#define RCHILD(i) (2*(i)+2)
|
|
#define PARENT(i) (((i)-1)/2)
|
|
|
|
static void downheap(void *array, size_t nmemb, size_t size,
|
|
arraysort_cmpfn_t cmp, void *ctx, size_t i)
|
|
{
|
|
while (LCHILD(i) < nmemb) {
|
|
/* Identify the smallest element out of i and its children. */
|
|
size_t j = i;
|
|
if (CMP(j, LCHILD(i)) < 0)
|
|
j = LCHILD(i);
|
|
if (RCHILD(i) < nmemb &&
|
|
CMP(j, RCHILD(i)) < 0)
|
|
j = RCHILD(i);
|
|
|
|
if (j == i)
|
|
return; /* smallest element is already where it should be */
|
|
|
|
SWAP(j, i);
|
|
i = j;
|
|
}
|
|
}
|
|
|
|
void arraysort_fn(void *array, size_t nmemb, size_t size,
|
|
arraysort_cmpfn_t cmp, void *ctx)
|
|
{
|
|
size_t i;
|
|
|
|
if (nmemb < 2)
|
|
return; /* trivial */
|
|
|
|
/*
|
|
* Stage 1: build the heap.
|
|
*
|
|
* Linear-time if we do it by downheaping the elements in
|
|
* decreasing order of index, instead of the more obvious approach
|
|
* of upheaping in increasing order. (Also, it means we don't need
|
|
* the upheap function at all.)
|
|
*
|
|
* We don't need to downheap anything in the second half of the
|
|
* array, because it can't have any children to swap with anyway.
|
|
*/
|
|
for (i = PARENT(nmemb-1) + 1; i-- > 0 ;)
|
|
downheap(array, nmemb, size, cmp, ctx, i);
|
|
|
|
/*
|
|
* Stage 2: dismantle the heap by repeatedly swapping the root
|
|
* element (at index 0) into the last position and then
|
|
* downheaping the new root.
|
|
*/
|
|
for (i = nmemb-1; i > 0; i--) {
|
|
SWAP(0, i);
|
|
downheap(array, i, size, cmp, ctx, 0);
|
|
}
|
|
}
|