]> granicus.if.org Git - graphviz/commitdiff
fdpgen: replace inline object list with generic list
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Wed, 28 Dec 2022 23:03:47 +0000 (15:03 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 1 Jan 2023 00:38:32 +0000 (16:38 -0800)
lib/fdpgen/clusteredges.c

index 06509eda034b8bbed5b8824a6516fb6a517048e7..7b56450672ccc0ffc049eedd8f60908b199c1394 100644 (file)
@@ -21,6 +21,7 @@
 #include "config.h"
 #include <assert.h>
 #include <cgraph/alloc.h>
+#include <cgraph/list.h>
 #include <fdpgen/clusteredges.h>
 #include <fdpgen/fdp.h>
 #include <limits.h>
 #include <pack/pack.h>
 #include <stdbool.h>
 
-typedef struct {
-    size_t cnt;
-    size_t sz;
-    Ppoly_t **obs;
-} objlist;
-
-/* addObj:
- * Add an object to the list. The array is increased if necessary.
- */
-#define INIT_SZ 100
+DEFINE_LIST(objlist, Ppoly_t*)
 
 #if defined(DEBUG) && DEBUG > 1
 static void dumpObj(Ppoly_t * p)
@@ -52,39 +44,13 @@ static void dumpObj(Ppoly_t * p)
     fputs("\n", stderr);
 }
 
-static void dumpObjlist(objlist * l)
-{
-    for (size_t i = 0; i < l->cnt; i++) {
-       dumpObj(l->obs[i]);
+static void dumpObjlist(const objlist_t *l) {
+    for (size_t i = 0; i < objlist_size(l); i++) {
+       dumpObj(objlist_get(l, i));
     }
 }
 #endif
 
-static void addObj(objlist * l, Ppoly_t * obj)
-{
-    if (l->sz == l->cnt) {
-       if (l->obs) {
-           l->obs = gv_recalloc(l->obs, l->sz, l->sz * 2, sizeof(Ppoly_t*));
-           l->sz *= 2;
-       } else {
-           l->obs = gv_calloc(INIT_SZ, sizeof(Ppoly_t*));
-           l->sz = INIT_SZ;
-       }
-    }
-    l->obs[l->cnt++] = obj;
-}
-
-/* freeObjlist:
- * Release memory.
- */
-static void freeObjlist(objlist * l)
-{
-    if (l) {
-       free(l->obs);
-       free(l);
-    }
-}
-
 /* makeClustObs:
  * Create an obstacle corresponding to a cluster's bbox.
  */
@@ -137,21 +103,20 @@ static Ppoly_t *makeClustObs(graph_t * g, expand_t* pm)
  * Return the list.
  */
 static void
-addGraphObjs(objlist * l, graph_t * g, void *tex, void *hex, expand_t* pm)
-{
+addGraphObjs(objlist_t *l, graph_t *g, void *tex, void *hex, expand_t *pm) {
     node_t *n;
     graph_t *sg;
     int i;
 
     for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
        if (PARENT(n) == g && n != tex && n != hex && !IS_CLUST_NODE(n)) {
-           addObj(l, makeObstacle(n, pm, false));
+           objlist_append(l, makeObstacle(n, pm, false));
        }
     }
     for (i = 1; i <= GD_n_cluster(g); i++) {
        sg = GD_clust(g)[i];
        if (sg != tex && sg != hex) {
-           addObj(l, makeClustObs(sg, pm));
+           objlist_append(l, makeClustObs(sg, pm));
        }
     }
 }
@@ -163,7 +128,7 @@ addGraphObjs(objlist * l, graph_t * g, void *tex, void *hex, expand_t* pm)
  * Return appended list, plus pass back last cluster processed in gp.
  */
 static void
-raiseLevel(objlist * l, int maxlvl, void *ex, int minlvl, graph_t ** gp,
+raiseLevel(objlist_t *l, int maxlvl, void *ex, int minlvl, graph_t **gp,
           expand_t* pm)
 {
     graph_t *g = *gp;
@@ -184,8 +149,7 @@ raiseLevel(objlist * l, int maxlvl, void *ex, int minlvl, graph_t ** gp,
  * Return the list.
  * Assume e is not a loop.
  */
-static objlist *objectList(edge_t * ep, expand_t* pm)
-{
+static objlist_t objectList(edge_t *ep, expand_t *pm) {
     node_t *h = aghead(ep);
     node_t *t = agtail(ep);
     graph_t *hg = PARENT(h);
@@ -194,7 +158,7 @@ static objlist *objectList(edge_t * ep, expand_t* pm)
     int tlevel;
     void *hex;                 /* Objects to be excluded from list */
     void *tex;
-    objlist *list = gv_alloc(sizeof(objlist));
+    objlist_t list = {0};
 
     /* If either endpoint is a cluster node, we move up one level */
     if (IS_CLUST_NODE(h)) {
@@ -211,25 +175,25 @@ static objlist *objectList(edge_t * ep, expand_t* pm)
     hlevel = LEVEL(hg);
     tlevel = LEVEL(tg);
     if (hlevel > tlevel) {
-       raiseLevel(list, hlevel, hex, tlevel, &hg, pm);
+       raiseLevel(&list, hlevel, hex, tlevel, &hg, pm);
        hex = hg;
        hg = GPARENT(hg);
     } else if (tlevel > hlevel) {
-       raiseLevel(list, tlevel, tex, hlevel, &tg, pm);
+       raiseLevel(&list, tlevel, tex, hlevel, &tg, pm);
        tex = tg;
        tg = GPARENT(tg);
     }
 
     /* hg and tg always have the same level */
     while (hg != tg) {
-       addGraphObjs(list, hg, NULL, hex, pm);
-       addGraphObjs(list, tg, tex, NULL, pm);
+       addGraphObjs(&list, hg, NULL, hex, pm);
+       addGraphObjs(&list, tg, tex, NULL, pm);
        hex = hg;
        hg = GPARENT(hg);
        tex = tg;
        tg = GPARENT(tg);
     }
-    addGraphObjs(list, tg, tex, hex, pm);
+    addGraphObjs(&list, tg, tex, hex, pm);
 
     return list;
 }
@@ -258,14 +222,14 @@ int compoundEdges(graph_t * g, expand_t* pm, int edgetype)
            if (n == head && ED_count(e)) {     /* self arc */
                makeSelfArcs(e, GD_nodesep(g));
            } else if (ED_count(e)) {
-               objlist *objl = objectList(e, pm);
-               assert(objl->cnt <= INT_MAX);
-               if (Plegal_arrangement(objl->obs, (int)objl->cnt)) {
-                   vconfig = Pobsopen(objl->obs, (int)objl->cnt);
+               objlist_t objl = objectList(e, pm);
+               assert(objlist_size(&objl) <= INT_MAX);
+               if (Plegal_arrangement(objlist_at(&objl, 0), (int)objlist_size(&objl))) {
+                   vconfig = Pobsopen(objlist_at(&objl, 0), (int)objlist_size(&objl));
                    if (!vconfig) {
                        agerr(AGWARN, "compoundEdges: could not construct obstacles - falling back to straight line edges\n");
                        rv = 1;
-                       freeObjlist(objl);
+                       objlist_free(&objl);
                        continue;
                    }
                }
@@ -281,7 +245,7 @@ int compoundEdges(graph_t * g, expand_t* pm, int edgetype)
                                margin.x, margin.y, pm->x, pm->y);
                        rv = 1;
                    }
-                   freeObjlist(objl);
+                   objlist_free(&objl);
                    continue;
                }
 
@@ -291,10 +255,10 @@ int compoundEdges(graph_t * g, expand_t* pm, int edgetype)
                 */
                for (e0 = e; e0; e0 = ED_to_virt(e0)) {
                    ED_path(e0) = getPath(e0, vconfig, 0);
-                   assert(objl->cnt <= INT_MAX);
-                   makeSpline(e0, objl->obs, (int)objl->cnt, false);
+                   assert(objlist_size(&objl) <= INT_MAX);
+                   makeSpline(e0, objlist_at(&objl, 0), (int)objlist_size(&objl), false);
                }
-               freeObjlist(objl);
+               objlist_free(&objl);
            }
        }
     }