mirror of
git://git.tartarus.org/simon/puzzles.git
synced 2025-04-20 23:51:29 -07:00
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.
This commit is contained in:
15
grid.h
15
grid.h
@ -33,6 +33,7 @@ typedef struct grid_edge grid_edge;
|
||||
typedef struct grid_dot grid_dot;
|
||||
|
||||
struct grid_face {
|
||||
int index; /* index in grid->faces[] where this face appears */
|
||||
int order; /* Number of edges, also the number of dots */
|
||||
grid_edge **edges; /* edges around this face */
|
||||
grid_dot **dots; /* corners of this face */
|
||||
@ -56,8 +57,10 @@ struct grid_face {
|
||||
struct grid_edge {
|
||||
grid_dot *dot1, *dot2;
|
||||
grid_face *face1, *face2; /* Use NULL for the infinite outside face */
|
||||
int index; /* index in grid->edges[] where this edge appears */
|
||||
};
|
||||
struct grid_dot {
|
||||
int index; /* index in grid->dots[] where this dot appears */
|
||||
int order;
|
||||
grid_edge **edges;
|
||||
grid_face **faces; /* A NULL grid_face* means infinite outside face */
|
||||
@ -69,11 +72,13 @@ struct grid_dot {
|
||||
int x, y;
|
||||
};
|
||||
typedef struct grid {
|
||||
/* These are (dynamically allocated) arrays of all the
|
||||
* faces, edges, dots that are in the grid. */
|
||||
int num_faces; grid_face *faces;
|
||||
int num_edges; grid_edge *edges;
|
||||
int num_dots; grid_dot *dots;
|
||||
/* Arrays of all the faces, edges, dots that are in the grid.
|
||||
* The arrays themselves are dynamically allocated, and so is each object
|
||||
* inside them. num_foo indicates the number of things actually stored,
|
||||
* and size_foo indicates the allocated size of the array. */
|
||||
int num_faces, size_faces; grid_face **faces;
|
||||
int num_edges, size_edges; grid_edge **edges;
|
||||
int num_dots, size_dots; grid_dot **dots;
|
||||
|
||||
/* Cache the bounding-box of the grid, so the drawing-code can quickly
|
||||
* figure out the proper scaling to draw onto a given area. */
|
||||
|
Reference in New Issue
Block a user