From: Matthew Fernandez Date: Wed, 22 Dec 2021 04:24:06 +0000 (-0800) Subject: sgd_graph: use 'size_t' for 'sources' and 'targets' X-Git-Tag: 3.0.0~100^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9209ebcb8d61c604ff162010d45846c1de44a62e;p=graphviz sgd_graph: use 'size_t' for 'sources' and 'targets' Following on from 1c42bb82cd73cb45d3abee845c21819add00d982, this uses a more appropriate type for these members, permitting collections that exceed 2³² - 1 and avoiding sign conversion issues. --- diff --git a/lib/neatogen/dijkstra.c b/lib/neatogen/dijkstra.c index eb851501b..93177f44d 100644 --- a/lib/neatogen/dijkstra.c +++ b/lib/neatogen/dijkstra.c @@ -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); diff --git a/lib/neatogen/sgd.c b/lib/neatogen/sgd.c index 7ec0411af..03fbf0446 100644 --- a/lib/neatogen/sgd.c +++ b/lib/neatogen/sgd.c @@ -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]; xsources[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]; xsources[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]; ysources[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]; ysources[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]; xsources[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 } } diff --git a/lib/neatogen/sgd.h b/lib/neatogen/sgd.h index 52f353a8a..aabb80a5b 100644 --- a/lib/neatogen/sgd.h +++ b/lib/neatogen/sgd.h @@ -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;