Files
puzzles/loopgen.h
Simon Tatham e6cdd70df8 grid.c: allocate face/edge/dot arrays incrementally.
Previously, the 'faces', 'edges' and 'dots' arrays in a grid structure
were arrays of actual grid_face, grid_edge and grid_dot structures,
physically containing all the data about the grid. But they also
referred to each other by pointers, which meant that they were hard to
realloc larger (you'd have to go through and rewrite all the pointers
whenever the base of an array moved). And _that_ meant that every grid
type had to figure out a reasonably good upper bound on the number of
all those things it was going to need, so that it could allocate those
arrays the right size to begin with, and not have to grow them
painfully later.

For some grid types - particularly the new aperiodic ones - that was
actually a painful part of the job. So I think enough is enough:
counting up how much memory you're going to need before using it is a
mug's game, and we should instead realloc on the fly.

So now g->faces, g->edges and g->dots have an extra level of
indirection. Instead of being arrays of actual structs, they're arrays
of pointers, and each struct in them is individually allocated. Once a
grid_face has been allocated, it never moves again, even if the array
of pointers changes size.

The old code had a common idiom of recovering the array index of a
particular element by subtracting it from the array base, e.g. writing
'f - g->faces' or 'e - g->edges'. To make that lookup remain possible,
I've added an 'index' field to each sub-structure, which is
initialised at the point where we decide what array index it will live
at.

This should involve no functional change, but the code that previously
had to figure out max_faces and max_dots up front for each grid type
is now completely gone, and nobody has to solve those problems in
advance any more.
2023-07-07 18:17:02 +01:00

36 lines
1.3 KiB
C

/*
* loopgen.h: interface file for loop generation functions for grid.[ch].
*/
#ifndef PUZZLES_LOOPGEN_H
#define PUZZLES_LOOPGEN_H
#include "puzzles.h"
#include "grid.h"
enum face_colour { FACE_WHITE, FACE_GREY, FACE_BLACK };
/* face should be of type grid_face* here. */
#define FACE_COLOUR(face) \
( (face) == NULL ? FACE_BLACK : \
board[(face)->index] )
typedef int (*loopgen_bias_fn_t)(void *ctx, char *board, int face);
/* 'board' should be a char array whose length is the same as
* g->num_faces: this will be filled in with FACE_WHITE or FACE_BLACK
* after loop generation.
*
* If 'bias' is non-null, it should be a user-provided function which
* rates a half-finished board (i.e. may include some FACE_GREYs) for
* desirability; this will cause the loop generator to bias in favour
* of loops with a high return value from that function. The 'face'
* parameter to the bias function indicates which face of the grid has
* been modified since the last call; it is guaranteed that only one
* will have been (so that bias functions can work incrementally
* rather than re-scanning the whole grid on every call). */
extern void generate_loop(grid *g, char *board, random_state *rs,
loopgen_bias_fn_t bias, void *biasctx);
#endif