]> granicus.if.org Git - graphviz/commitdiff
dijkstra_bounded: replace boolean array 'node_in_neighborhood' with a bit array
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Wed, 12 Jan 2022 03:21:47 +0000 (19:21 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Wed, 12 Jan 2022 15:50:35 +0000 (07:50 -0800)
This is more memory efficient. This also undoes the retention and reuse of this
array making its live range more obvious to the compiler.

lib/neatogen/dijkstra.c

index 93177f44dee95fec48c1c22954eb48c1436e2435..404024a2ede4edfb69902b517492dcc103bac03c 100644 (file)
@@ -185,8 +185,6 @@ dijkstra_bounded(int vertex, vtx_data * graph, int n, DistType * dist,
 {
     int num_visited_nodes;
     int i;
-    static boolean *node_in_neighborhood = NULL;
-    static int size = 0;
     Queue Q;
     heap H;
     int closestVertex, neighbor;
@@ -201,15 +199,10 @@ dijkstra_bounded(int vertex, vtx_data * graph, int n, DistType * dist,
     }
     num_visited_nodes =
        bfs_bounded(vertex, graph, n, dist, &Q, bound, visited_nodes);
-    if (size < n) {
-       node_in_neighborhood = realloc(node_in_neighborhood, n * sizeof(boolean));
-       for (i = size; i < n; i++) {
-           node_in_neighborhood[i] = FALSE;
-       }
-       size = n;
-    }
+    bitarray_t node_in_neighborhood = {0};
+    bitarray_resize_or_exit(&node_in_neighborhood, n);
     for (i = 0; i < num_visited_nodes; i++) {
-       node_in_neighborhood[visited_nodes[i]] = TRUE;
+       bitarray_set(node_in_neighborhood, visited_nodes[i], true);
     }
 
     int *index = gcalloc(n, sizeof(int));
@@ -226,7 +219,7 @@ dijkstra_bounded(int vertex, vtx_data * graph, int n, DistType * dist,
 
     while (num_found < num_visited_nodes
           && extractMax(&H, &closestVertex, index, dist)) {
-       if (node_in_neighborhood[closestVertex]) {
+       if (bitarray_get(node_in_neighborhood, closestVertex)) {
            num_found++;
        }
        closestDist = dist[closestVertex];
@@ -239,10 +232,7 @@ dijkstra_bounded(int vertex, vtx_data * graph, int n, DistType * dist,
        }
     }
 
-    /* restore initial false-status of 'node_in_neighborhood' */
-    for (i = 0; i < num_visited_nodes; i++) {
-       node_in_neighborhood[visited_nodes[i]] = FALSE;
-    }
+    bitarray_reset(&node_in_neighborhood);
     freeHeap(&H);
     free(index);
     freeQueue(&Q);