From: Matthew Fernandez Date: Thu, 24 Nov 2022 17:21:01 +0000 (-0800) Subject: ortho: allocate qnodes on-demand X-Git-Tag: 7.0.4~2^2~9 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5939edcfc9a5d767692cf9955bd0823493d70f8e;p=graphviz ortho: allocate qnodes on-demand The number of elements in this array is no longer calculated upfront; we simply expand the array as needed. This removes one blocker to removing calculation of the number of trapezoids upfront. As noted in the comment in this commit, the first element (index 0) is reserved. I do not know the motivation for this, but various places use index > 0 to decide if they are dealing with a real node. Gitlab: #56 --- diff --git a/lib/ortho/trapezoid.c b/lib/ortho/trapezoid.c index 370018982..de472f3e4 100644 --- a/lib/ortho/trapezoid.c +++ b/lib/ortho/trapezoid.c @@ -60,18 +60,13 @@ typedef struct { qnode_t *data; } qnodes_t; -static int q_idx; static int tr_idx; /* Return a new node to be added into the query tree */ static int newnode(qnodes_t *qs) { - if (q_idx < qs->length) - return q_idx++; - else { - fprintf(stderr, "newnode: Query-table overflow\n"); - assert(0); - return -1; - } + qs->data = gv_recalloc(qs->data, qs->length, qs->length + 1, sizeof(qnode_t)); + ++qs->length; + return qs->length - 1; } /* Return a free trapezoid */ @@ -1026,9 +1021,11 @@ construct_trapezoids(int nseg, segment_t *seg, int *permute, traps_t* tr) { int root, h; int segi = 1; - qnodes_t qs = {.length = 2 * tr->length, - .data = gv_calloc(2 * tr->length, sizeof(qnode_t))}; - q_idx = tr_idx = 1; + // We will append later nodes by expanding this on-demand. First node is a + // sentinel. + qnodes_t qs = {.length = 1, .data = gv_calloc(1, sizeof(qnode_t))}; + + tr_idx = 1; memset(tr->data, 0, tr->length * sizeof(trap_t)); /* Add the first segment and get the query structure and trapezoid */