]> granicus.if.org Git - graphviz/commitdiff
ortho: push trapezoids allocation into 'construct_trapezoids'
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Thu, 24 Nov 2022 18:52:55 +0000 (10:52 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Wed, 30 Nov 2022 04:15:50 +0000 (20:15 -0800)
This makes it more obvious to readers and the compiler that the value of this
on-entry to `construct_trapezoids` is unimportant and the value does not need to
be retained between the two `construct_trapezoids` calls in `partition`.

lib/ortho/partition.c
lib/ortho/trap.h
lib/ortho/trapezoid.c

index aa972d2439a2bce7f000531596684bb3bc330f6d..2c7cc00ff871c84f4674a4c619e81d20640db8fd 100644 (file)
@@ -12,6 +12,7 @@
 #include <common/boxes.h>
 #include <cgraph/alloc.h>
 #include <cgraph/bitarray.h>
+#include <cgraph/prisize_t.h>
 #include <ortho/partition.h>
 #include <ortho/trap.h>
 #include <math.h>
@@ -690,12 +691,6 @@ partition (cell* cells, int ncells, int* nrects, boxf bb)
     segment_t* segs = gv_calloc(nsegs + 1, sizeof(segment_t));
     int* permute = gv_calloc(nsegs + 1, sizeof(int));
 
-    // First trapezoid is reserved as a sentinel. We will append later
-    // trapezoids by expanding this on-demand.
-    traps_t trs = {.length = 1, .data = gv_calloc(1, sizeof(trap_t))};
-
-    int nt;
-
     if (DEBUG) {
        fprintf (stderr, "cells = %d segs = %d traps = dynamic\n", ncells, nsegs);
     }
@@ -710,25 +705,23 @@ partition (cell* cells, int ncells, int* nrects, boxf bb)
     }
     srand48(173);
     generateRandomOrdering (nsegs, permute);
-    nt = construct_trapezoids(nsegs, segs, permute, &trs);
+    traps_t hor_traps = construct_trapezoids(nsegs, segs, permute);
     if (DEBUG) {
-       fprintf (stderr, "hor traps = %d\n", nt);
+       fprintf (stderr, "hor traps = %" PRISIZE_T "\n", hor_traps.length);
     }
     boxes_t hor_decomp = {0};
-    monotonate_trapezoids(nsegs, segs, &trs, 0, &hor_decomp);
-
-    // reset trapezoid collection
-    free(trs.data);
-    trs = (traps_t){.length = 1, .data = gv_calloc(1, sizeof(trap_t))};
+    monotonate_trapezoids(nsegs, segs, &hor_traps, 0, &hor_decomp);
+    free(hor_traps.data);
 
     genSegments (cells, ncells, bb, segs, 1);
     generateRandomOrdering (nsegs, permute);
-    nt = construct_trapezoids(nsegs, segs, permute, &trs);
+    traps_t ver_traps = construct_trapezoids(nsegs, segs, permute);
     if (DEBUG) {
-       fprintf (stderr, "ver traps = %d\n", nt);
+       fprintf (stderr, "ver traps = %" PRISIZE_T "\n", ver_traps.length);
     }
     boxes_t vert_decomp = {0};
-    monotonate_trapezoids(nsegs, segs, &trs, 1, &vert_decomp);
+    monotonate_trapezoids(nsegs, segs, &ver_traps, 1, &vert_decomp);
+    free(ver_traps.data);
 
     boxes_t rs = {0};
     for (size_t i = 0; i < vert_decomp.size; ++i)
@@ -740,7 +733,6 @@ partition (cell* cells, int ncells, int* nrects, boxf bb)
 
     free (segs);
     free (permute);
-    free(trs.data);
     boxes_free(&hor_decomp);
     boxes_free(&vert_decomp);
     *nrects = rs.size;
index 7378a26d3f7cec8a12b276605dab149cdb10e8cf..4317410439562511eb27312fc605cbf1b501d233 100644 (file)
@@ -83,4 +83,4 @@ static inline int dfp_cmp(double f1, double f2) {
 #define _greater_than(v0, v1) \
   (((v0)->y > (v1)->y + C_EPS) ? true : (((v0)->y < (v1)->y - C_EPS) ? false : ((v0)->x > (v1)->x)))
 
-extern int construct_trapezoids(int, segment_t*, int*, traps_t *tr);
+extern traps_t construct_trapezoids(int, segment_t*, int*);
index 631b9405f92b8dd857353de8f5e9ab8677eff9df..d5ff19f9b443289bc9591dc8dd8e0233a49e299a 100644 (file)
@@ -1005,8 +1005,7 @@ static int math_N(int n, int h)
 }
 
 /* Main routine to perform trapezoidation */
-int
-construct_trapezoids(int nseg, segment_t *seg, int *permute, traps_t* tr) {
+traps_t construct_trapezoids(int nseg, segment_t *seg, int *permute) {
     int i;
     int root, h;
     int segi = 1;
@@ -1015,26 +1014,30 @@ construct_trapezoids(int nseg, segment_t *seg, int *permute, traps_t* tr) {
     // sentinel.
     qnodes_t qs = {.length = 1, .data = gv_calloc(1, sizeof(qnode_t))};
 
+    // First trapezoid is reserved as a sentinel. We will append later
+    // trapezoids by expanding this on-demand.
+    traps_t tr = {.length = 1, .data = gv_calloc(1, sizeof(trap_t))};
+
   /* Add the first segment and get the query structure and trapezoid */
   /* list initialised */
 
-    root = init_query_structure(permute[segi++], seg, tr, &qs);
+    root = init_query_structure(permute[segi++], seg, &tr, &qs);
 
     for (i = 1; i <= nseg; i++)
        seg[i].root0 = seg[i].root1 = root;
 
     for (h = 1; h <= math_logstar_n(nseg); h++) {
        for (i = math_N(nseg, h -1) + 1; i <= math_N(nseg, h); i++)
-           add_segment(permute[segi++], seg, tr, &qs);
+           add_segment(permute[segi++], seg, &tr, &qs);
 
       /* Find a new root for each of the segment endpoints */
        for (i = 1; i <= nseg; i++)
-           find_new_roots(i, seg, tr, &qs);
+           find_new_roots(i, seg, &tr, &qs);
     }
 
     for (i = math_N(nseg, math_logstar_n(nseg)) + 1; i <= nseg; i++)
-       add_segment(permute[segi++], seg, tr, &qs);
+       add_segment(permute[segi++], seg, &tr, &qs);
 
     free(qs.data);
-    return tr->length;
+    return tr;
 }