Simplified compute_dp_ranks.
authorFrancois Fleuret <francois@fleuret.org>
Sat, 29 Dec 2012 22:05:59 +0000 (23:05 +0100)
committerFrancois Fleuret <francois@fleuret.org>
Sat, 29 Dec 2012 22:05:59 +0000 (23:05 +0100)
mtp_graph.cc

index 7cd7e52..80f9f05 100644 (file)
@@ -205,9 +205,9 @@ void MTPGraph::compute_dp_ranks() {
   // set the distance_from_source fields to this value.
 
   int *nb_predecessors = new int[_nb_vertices];
-  int *without_predecessor = new int[_nb_vertices];
-  int *new_without_predecessor = new int[_nb_vertices];
-  int nb_without_predecessor, new_nb_without_predecessor;
+  Vertex **bunch = new Vertex *[_nb_vertices];
+
+  Vertex **already_processed = bunch, **front = bunch, **fresh = bunch;
 
   for(int k = 0; k < _nb_vertices; k++) {
     nb_predecessors[k] = 0;
@@ -221,44 +221,38 @@ void MTPGraph::compute_dp_ranks() {
     }
   }
 
-  nb_without_predecessor = 0;
   for(int k = 0; k < _nb_vertices; k++) {
     if(nb_predecessors[k] == 0) {
-      without_predecessor[nb_without_predecessor++] = k;
+      *(front++) = _vertices + k;
     }
   }
 
   scalar_t rank = 1;
-  while(nb_without_predecessor > 0) {
-    new_nb_without_predecessor = 0;
-    for(int l = 0; l < nb_without_predecessor; l++) {
-      v = &_vertices[without_predecessor[l]];
+  while(already_processed < front) {
+    fresh = front;
+    while(already_processed < front) {
+      v = *(already_processed++);
       v->distance_from_source = rank;
       for(e = v->leaving_edge_list_root; e; e = e->next_leaving_edge) {
         tv = int(e->terminal_vertex - _vertices);
         nb_predecessors[tv]--;
         ASSERT(nb_predecessors[tv] >= 0);
         if(nb_predecessors[tv] == 0) {
-          new_without_predecessor[new_nb_without_predecessor++] = tv;
+          *(fresh++) = e->terminal_vertex;
         }
       }
     }
-
-    swap(without_predecessor, new_without_predecessor);
-    nb_without_predecessor = new_nb_without_predecessor;
+    front = fresh;
     rank++;
   }
 
-  for(int k = 0; k < _nb_vertices; k++) {
-    if(nb_predecessors[k] > 0) {
-      cerr << __FILE__ << ": The graph is not a DAG." << endl;
-      abort();
-    }
+  if(already_processed < bunch + _nb_vertices) {
+    cerr << __FILE__ << ": The graph is not a DAG." << endl;
+    abort();
   }
 
   delete[] nb_predecessors;
-  delete[] without_predecessor;
-  delete[] new_without_predecessor;
+  delete[] bunch;
 }
 
 //////////////////////////////////////////////////////////////////////