]> granicus.if.org Git - graphviz/commitdiff
ortho: use a dynamic collection for final intersected boxes
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Thu, 24 Nov 2022 18:26:09 +0000 (10:26 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Wed, 30 Nov 2022 04:15:50 +0000 (20:15 -0800)
This is a simplification that removes the need for over-allocation and then
later shrinking.

Gitlab: #56

lib/ortho/partition.c

index 66a200f80bf4157b94a1d855aee5f98dab3409ea..9814f21cef2ae227161a74930e3fe405fe72ae66 100644 (file)
@@ -690,8 +690,6 @@ partition (cell* cells, int ncells, int* nrects, boxf bb)
     int nsegs = 4*(ncells+1);
     segment_t* segs = gv_calloc(nsegs + 1, sizeof(segment_t));
     int* permute = gv_calloc(nsegs + 1, sizeof(int));
-    int cnt = 0;
-    boxf* rs;
     int ntraps = TRSIZE(nsegs);
     traps_t trs = {.length = ntraps,
                     .data = gv_calloc(ntraps, sizeof(trap_t))};
@@ -727,18 +725,19 @@ partition (cell* cells, int ncells, int* nrects, boxf bb)
     boxes_t vert_decomp = {0};
     monotonate_trapezoids(nsegs, segs, &trs, 1, &vert_decomp);
 
-    rs = gv_calloc(hor_decomp.size * vert_decomp.size, sizeof(boxf));
+    boxes_t rs = {0};
     for (size_t i = 0; i < vert_decomp.size; ++i)
-       for (size_t j = 0; j < hor_decomp.size; ++j)
-           if (rectIntersect(&rs[cnt], &vert_decomp.data[i], &hor_decomp.data[j]))
-               cnt++;
+       for (size_t j = 0; j < hor_decomp.size; ++j) {
+           boxf newbox = {{0}};
+           if (rectIntersect(&newbox, &vert_decomp.data[i], &hor_decomp.data[j]))
+               boxes_append(&rs, newbox);
+       }
 
-    rs = gv_recalloc(rs, hor_decomp.size * vert_decomp.size, cnt, sizeof(boxf));
     free (segs);
     free (permute);
     free(trs.data);
     boxes_free(&hor_decomp);
     boxes_free(&vert_decomp);
-    *nrects = cnt;
-    return rs;
+    *nrects = rs.size;
+    return rs.data;
 }