]> granicus.if.org Git - graphviz/commitdiff
Agglomerative_Ink_Bundling_establish: replace 'cedges' with a 'std::vector'
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 15 Jan 2022 21:24:20 +0000 (13:24 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 16 Jan 2022 18:51:42 +0000 (10:51 -0800)
This simplifies the code as well as making it significantly easier for humans
and compilers to understand what is going on here.

lib/mingle/agglomerative_bundling.cpp

index a6382cc205716f09044086ae961c1285738d7c33..697d88a83dac1bb9fc323ed17b1b4ff748b5b514 100644 (file)
 #include <math.h>
 #include <time.h>
 #include <sparse/SparseMatrix.h>
-#include <sparse/vector.h>
 #include <mingle/edge_bundling.h>
 #include <mingle/ink.h>
 #include <mingle/agglomerative_bundling.h>
 #include <mingle/nearest_neighbor_graph.h>
 #include <string.h>
+#include <vector>
 
 #if OPENGL
 #include <gl.h>
@@ -92,15 +92,14 @@ static Agglomerative_Ink_Bundling Agglomerative_Ink_Bundling_establish(Agglomera
   double *inks = grid->inks, *cinks, inki, inkj;
   double gain, maxgain, minink, total_gain = 0;
   int *ip = NULL, *jp = NULL, ie;
-  Vector *cedges;/* a table listing the content of bundled edges in the coarsen grid.
+  std::vector<std::vector<int>> cedges;/* a table listing the content of bundled edges in the coarsen grid.
                    cedges[i] contain the list of origonal edges that make up the bundle i in the next level */
   double ink0, ink1, grand_total_ink = 0, grand_total_gain = 0;
   point_t meet1, meet2;
 
   if (Verbose > 1) fprintf(stderr,"level ===================== %d, n = %d\n",grid->level, n);
-  cedges = (Vector*)MALLOC(sizeof(Vector)*n);
+  cedges.resize(n);
   cinks = (double*)MALLOC(sizeof(double)*n);
-  for (i = 0; i < n; i++) cedges[i] = Vector_new(1, sizeof(int), NULL);
 
   if (grid->level > 0){
     ip = grid->R0->ia;
@@ -149,10 +148,10 @@ static Agglomerative_Ink_Bundling Agglomerative_Ink_Bundling_establish(Agglomera
        } else {
          ni = 1; pick[0] = i; 
        }
-       nj = Vector_get_length(cedges[jc]);
+       nj = cedges[jc].size();
        npicks = ni;
        for (k = 0; k < nj; k++) {
-         pick[npicks++] = *((int*) Vector_get(cedges[jc], k));
+         pick[npicks++] = cedges[jc][k];
        }
       }
 
@@ -191,10 +190,10 @@ static Agglomerative_Ink_Bundling Agglomerative_Ink_Bundling_establish(Agglomera
        if (ip){
          for (k = ip[jmax]; k < ip[jmax+1]; k++) {
            ie = jp[k];
-           Vector_add(cedges[nc], &ie);
+           cedges[nc].push_back(ie);
          }
        } else {
-         Vector_add(cedges[nc], &jmax);
+         cedges[nc].push_back(jmax);
        }
        jc = nc;
        nc++;
@@ -217,10 +216,10 @@ static Agglomerative_Ink_Bundling Agglomerative_Ink_Bundling_establish(Agglomera
     if (ip){
       for (k = ip[i]; k < ip[i+1]; k++) {
        ie = jp[k];
-       Vector_add(cedges[jc], &ie);
+       cedges[jc].push_back(ie);
       }
     } else {
-       Vector_add(cedges[jc], &i);
+       cedges[jc].push_back(i);
     }
     cinks[jc] = minink;
     grand_total_ink += minink;
@@ -228,8 +227,8 @@ static Agglomerative_Ink_Bundling Agglomerative_Ink_Bundling_establish(Agglomera
 
     if (Verbose && DEBUG){
       fprintf(stderr," coarse edge[%d]={",jc);
-      for (k = 0; k < Vector_get_length(cedges[jc]); k++) {
-       fprintf(stderr,"%d,", *((int*) Vector_get(cedges[jc], k)));
+      for (const int &cedge : cedges[jc]) {
+       fprintf(stderr,"%d,", cedge);
       }
       fprintf(stderr,"}, grand_total_gain=%f\n",grand_total_gain);
     }
@@ -289,8 +288,6 @@ static Agglomerative_Ink_Bundling Agglomerative_Ink_Bundling_establish(Agglomera
 
  RETURN:
   free(matching);
-  for (i = 0; i < n; i++) Vector_delete(cedges[i]);
-  free(cedges);
   free(mask);
   return grid;
 }