]> granicus.if.org Git - graphviz/commitdiff
sgd_graph: use 'size_t' for 'sources' and 'targets'
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Wed, 22 Dec 2021 04:24:06 +0000 (20:24 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Tue, 28 Dec 2021 00:27:34 +0000 (16:27 -0800)
Following on from 1c42bb82cd73cb45d3abee845c21819add00d982, this uses a more
appropriate type for these members, permitting collections that exceed 2³² - 1
and avoiding sign conversion issues.

lib/neatogen/dijkstra.c
lib/neatogen/sgd.c
lib/neatogen/sgd.h

index eb851501bbea65247da6d5f8fb0833e59b2bf389..93177f44dee95fec48c1c22954eb48c1436e2435 100644 (file)
@@ -375,8 +375,9 @@ int dijkstra_sgd(graph_sgd *graph, int source, term_sgd *terms) {
         dists[i] = FLT_MAX;
     }
     dists[source] = 0;
-    for (int i = graph->sources[source]; i < graph->sources[source + 1]; i++) {
-        int target = graph->targets[i];
+    for (size_t i = graph->sources[source]; i < graph->sources[source + 1];
+         i++) {
+        size_t target = graph->targets[i];
         dists[target] = graph->weights[i];
     }
     assert(graph->n <= INT_MAX);
@@ -397,11 +398,12 @@ int dijkstra_sgd(graph_sgd *graph, int source, term_sgd *terms) {
             terms[offset].w = 1 / (d*d);
             offset++;
         }
-        for (int i = graph->sources[closest]; i < graph->sources[closest + 1];
+        for (size_t i = graph->sources[closest]; i < graph->sources[closest + 1];
              i++) {
-            int target = graph->targets[i];
+            size_t target = graph->targets[i];
             float weight = graph->weights[i];
-            increaseKey_f(&h, target, d+weight, indices, dists);
+            assert(target <= (size_t)INT_MAX);
+            increaseKey_f(&h, (int)target, d+weight, indices, dists);
         }
     }
     freeHeap(&h);
index 7ec0411af118ff1d9b24efb817834e782cfcf5f0..03fbf04466337c9fdc97ae4b1a2b20f1272a55ec 100644 (file)
@@ -51,26 +51,26 @@ static graph_sgd * extract_adjacency(graph_t *G, int model) {
         }
     }
     graph_sgd *graph = N_NEW(1, graph_sgd);
-    graph->sources = N_NEW(n_nodes+1, int);
+    graph->sources = N_NEW(n_nodes + 1, size_t);
     bitarray_resize_or_exit(&graph->pinneds, n_nodes);
-    graph->targets = N_NEW(n_edges, int);
+    graph->targets = N_NEW(n_edges, size_t);
     graph->weights = N_NEW(n_edges, float);
 
     graph->n = n_nodes;
     assert(n_edges <= INT_MAX);
-    graph->sources[graph->n] = (int)n_edges; // to make looping nice
+    graph->sources[graph->n] = n_edges; // to make looping nice
 
     n_nodes = 0, n_edges = 0;
     for (np = agfstnode(G); np; np = agnxtnode(G,np)) {
         assert(n_edges <= INT_MAX);
-        graph->sources[n_nodes] = (int)n_edges;
+        graph->sources[n_nodes] = n_edges;
         bitarray_set(graph->pinneds, n_nodes, isFixed(np));
         for (ep = agfstedge(G, np); ep; ep = agnxtedge(G, ep, np)) {
             if (agtail(ep) == aghead(ep)) { // ignore self-loops and double edges
                 continue;
             }
             node_t *target = (agtail(ep) == np) ? aghead(ep) : agtail(ep); // in case edge is reversed
-            graph->targets[n_edges] = ND_id(target);
+            graph->targets[n_edges] = (size_t)ND_id(target);
             graph->weights[n_edges] = ED_dist(ep);
             assert(graph->weights[n_edges] > 0);
             n_edges++;
@@ -79,8 +79,8 @@ static graph_sgd * extract_adjacency(graph_t *G, int model) {
     }
     assert(n_nodes == graph->n);
     assert(n_edges <= INT_MAX);
-    assert((int)n_edges == graph->sources[graph->n]);
-    graph->sources[n_nodes] = (int)n_edges;
+    assert(n_edges == graph->sources[graph->n]);
+    graph->sources[n_nodes] = n_edges;
 
     if (model == MODEL_SHORTPATH) {
         // do nothing
@@ -94,21 +94,21 @@ static graph_sgd * extract_adjacency(graph_t *G, int model) {
             neighbours_j[i] = false;
         }
         for (size_t i = 0; i < graph->n; i++) {
-            int x;
             int deg_i = 0;
-            for (x=graph->sources[i]; x<graph->sources[i+1]; x++) {
-                int j = graph->targets[x];
+            for (size_t x = graph->sources[i]; x < graph->sources[i + 1]; x++) {
+                size_t j = graph->targets[x];
                 if (neighbours_i[j] == false) { // ignore multiedges
                     neighbours_i[j] = true; // set up sort of hashset
                     deg_i++;
                 }
             }
-            for (x=graph->sources[i]; x<graph->sources[i+1]; x++) {
-                int j = graph->targets[x];
-                int y, intersect = 0;
+            for (size_t x = graph->sources[i]; x < graph->sources[i + 1]; x++) {
+                size_t j = graph->targets[x];
+                int intersect = 0;
                 int deg_j = 0;
-                for (y=graph->sources[j]; y<graph->sources[j+1]; y++) {
-                    int k = graph->targets[y];
+                for (size_t y = graph->sources[j]; y < graph->sources[j + 1];
+                     y++) {
+                    size_t k = graph->targets[y];
                     if (neighbours_j[k] == false) { // ignore multiedges
                         neighbours_j[k] = true; // set up sort of hashset
                         deg_j++;
@@ -119,13 +119,14 @@ static graph_sgd * extract_adjacency(graph_t *G, int model) {
                 }
                 graph->weights[x] = deg_i + deg_j - (2*intersect);
                 assert(graph->weights[x] > 0);
-                for (y=graph->sources[j]; y<graph->sources[j+1]; y++) {
-                    int k = graph->targets[y];
+                for (size_t y = graph->sources[j]; y < graph->sources[j + 1];
+                     y++) {
+                    size_t k = graph->targets[y];
                     neighbours_j[k] = false; // reset sort of hashset
                 }
             }
-            for (x=graph->sources[i]; x<graph->sources[i+1]; x++) {
-                int j = graph->targets[x];
+            for (size_t x = graph->sources[i]; x < graph->sources[i + 1]; x++) {
+                size_t j = graph->targets[x];
                 neighbours_i[j] = false; // reset sort of hashset
             }
         }
index 52f353a8a171504b2c1e13c99425712d759e8c2a..aabb80a5b12fdbf96c98f8296d2699d68ae3c6e1 100644 (file)
@@ -14,10 +14,10 @@ typedef struct term_sgd {
 
 typedef struct graph_sgd {
     size_t n; // number of nodes
-    int *sources; // index of first edge in *targets for each node (length n+1)
+    size_t *sources; // index of first edge in *targets for each node (length n+1)
     bitarray_t pinneds; // whether a node is fixed or not
 
-    int *targets; // index of targets for each node (length sources[n])
+    size_t *targets; // index of targets for each node (length sources[n])
     float *weights; // weights of edges (length sources[n])
 } graph_sgd;