]> granicus.if.org Git - graphviz/commitdiff
ortho: allocate qnodes on-demand
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Thu, 24 Nov 2022 17:21:01 +0000 (09:21 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Wed, 30 Nov 2022 04:15:47 +0000 (20:15 -0800)
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

lib/ortho/trapezoid.c

index 37001898223af942454a0fcfce740d21da30e8a1..de472f3e476f7daac6af57dd19aee064d54fdbd6 100644 (file)
@@ -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 */