Removed the id fields, now compute it from the memory address.
authorFrancois Fleuret <francois@fleuret.org>
Tue, 18 Dec 2012 07:28:10 +0000 (08:28 +0100)
committerFrancois Fleuret <francois@fleuret.org>
Tue, 18 Dec 2012 07:28:10 +0000 (08:28 +0100)
mtp_graph.cc

index 6537a7f..5371732 100644 (file)
@@ -30,11 +30,12 @@ using namespace std;
 
 class Edge {
 public:
-  int id, occupied;
+  int occupied;
   scalar_t length, positivized_length;
   Vertex *origin_vertex, *terminal_vertex;
 
-  // These are the links in the origin_vertex leaving edge list
+  // These fields are used for the linked list of a vertex's leaving
+  // edge list. We have to do insertions / deletions.
   Edge *next_leaving_edge, *pred_leaving_edge;
 
   inline void invert();
@@ -42,7 +43,6 @@ public:
 
 class Vertex {
 public:
-  int id;
   Edge *leaving_edges;
   scalar_t distance_from_source;
   Edge *pred_edge_toward_source;
@@ -109,14 +109,9 @@ MTPGraph::MTPGraph(int nb_vertices, int nb_edges,
   _source = &_vertices[source];
   _sink = &_vertices[sink];
 
-  for(int k = 0; k < _nb_vertices; k++) {
-    _vertices[k].id = k;
-  }
-
   for(int e = 0; e < nb_edges; e++) {
     _vertices[vertex_from[e]].add_leaving_edge(_edges + e);
     _edges[e].occupied = 0;
-    _edges[e].id = e;
     _edges[e].origin_vertex = _vertices + vertex_from[e];
     _edges[e].terminal_vertex = _vertices + vertex_to[e];
   }
@@ -139,9 +134,9 @@ MTPGraph::~MTPGraph() {
 void MTPGraph::print(ostream *os) {
   for(int k = 0; k < _nb_edges; k++) {
     Edge *e = _edges + k;
-    (*os) << e->origin_vertex->id
+    (*os) << e->origin_vertex - _vertices
          << " -> "
-         << e->terminal_vertex->id
+         << e->terminal_vertex - _vertices
          << " "
          << e->length;
     if(e->occupied) {
@@ -156,11 +151,14 @@ void MTPGraph::print_dot(ostream *os) {
   (*os) << "        rankdir=\"LR\";" << endl;
   (*os) << "        node [shape=circle,width=0.75,fixedsize=true];" << endl;
   (*os) << "        edge [color=gray,arrowhead=open]" << endl;
-  (*os) << "        " << _source->id << " [peripheries=2];" << endl;
-  (*os) << "        " << _sink->id << " [peripheries=2];" << endl;
+  (*os) << "        " << _source - _vertices << " [peripheries=2];" << endl;
+  (*os) << "        " << _sink - _vertices << " [peripheries=2];" << endl;
   for(int k = 0; k < _nb_edges; k++) {
     Edge *e = _edges + k;
-    (*os) << "        " << e->origin_vertex->id << " -> " << e->terminal_vertex->id
+    (*os) << "        "
+          << e->origin_vertex - _vertices
+          << " -> "
+          << e->terminal_vertex - _vertices
           << " [";
     if(e->occupied) {
       (*os) << "style=bold,color=black,";
@@ -376,13 +374,13 @@ int MTPGraph::retrieve_one_path(Edge *e, Path *path) {
   int l = 0, nb_occupied_next;
 
   if(path) {
-    path->nodes[l++] = e->origin_vertex->id;
+    path->nodes[l++] = e->origin_vertex - _vertices;
     path->length = e->length;
   } else l++;
 
   while(e->terminal_vertex != _sink) {
     if(path) {
-      path->nodes[l++] = e->terminal_vertex->id;
+      path->nodes[l++] = e->terminal_vertex - _vertices;
       path->length += e->length;
     } else l++;
 
@@ -407,7 +405,7 @@ int MTPGraph::retrieve_one_path(Edge *e, Path *path) {
   }
 
   if(path) {
-    path->nodes[l++] = e->terminal_vertex->id;
+    path->nodes[l++] = e->terminal_vertex - _vertices;
     path->length += e->length;
   } else l++;