Skip to content

Commit

Permalink
Valgrind 8B: HGEdge::written
Browse files Browse the repository at this point in the history
* ditch ~HGEdge; have HGedge::detach take care of deletions
* replace s_written, c_written & t_written arrays with single "written" array + bitmasks
  • Loading branch information
yakra committed Jul 10, 2021
1 parent 0822708 commit 029ce2d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 52 deletions.
45 changes: 13 additions & 32 deletions siteupdate/cplusplus/classes/GraphGeneration/HGEdge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@

HGEdge::HGEdge(HighwaySegment *s, HighwayGraph *graph, int numthreads)
{ // initial construction is based on a HighwaySegment
s_written = new bool[numthreads];
c_written = new bool[numthreads];
t_written = new bool[numthreads];
// deleted by ~HGEdge
// we only need to initialize the first element, for master graphs.
// the rest will get zeroed out for each subgraph set.
s_written[0] = 0;
c_written[0] = 0;
t_written[0] = 0;
vertex1 = s->waypoint1->hashpoint()->vertex;
vertex2 = s->waypoint2->hashpoint()->vertex;
format = simple | collapsed | traveled;
Expand All @@ -33,6 +24,11 @@ HGEdge::HGEdge(HighwaySegment *s, HighwayGraph *graph, int numthreads)
{ delete this;
return;
}
written = new char[numthreads];
// deleted by HGEdge::detach
// we only need to initialize the first element, for master graphs.
// the rest will get zeroed out for each subgraph set.
written[0] = 0;
segment_name = s->segment_name();
vertex1->incident_s_edges.push_back(this);
vertex2->incident_s_edges.push_back(this);
Expand All @@ -56,11 +52,9 @@ HGEdge::HGEdge(HighwaySegment *s, HighwayGraph *graph, int numthreads)

HGEdge::HGEdge(HGVertex *vertex, unsigned char fmt_mask, int numthreads)
{ // build by collapsing two existing edges around a common hidden vertex
c_written = new bool[numthreads];
t_written = new bool[numthreads];
// deleted by ~HGEdge
c_written[0] = 0;
t_written[0] = 0;
written = new char[numthreads];
// deleted by HGEdge::detach
written[0] = 0;
format = fmt_mask;
// we know there are exactly 2 incident edges, as we
// checked for that, and we will replace these two
Expand Down Expand Up @@ -132,8 +126,6 @@ HGEdge::HGEdge(HGVertex *vertex, unsigned char fmt_mask, int numthreads)
// replace edge references at our endpoints with ourself
edge1->detach(fmt_mask);
edge2->detach(fmt_mask);
if (!edge1->format) delete edge1;
if (!edge2->format) delete edge2;
if (fmt_mask & collapsed)
{ vertex1->incident_c_edges.push_back(this);
vertex2->incident_c_edges.push_back(this);
Expand All @@ -144,6 +136,7 @@ HGEdge::HGEdge(HGVertex *vertex, unsigned char fmt_mask, int numthreads)
}
}

void HGEdge::detach() {return detach(format);}
void HGEdge::detach(unsigned char fmt_mask)
{ if (fmt_mask & simple)
{ for ( std::list<HGEdge*>::iterator e = vertex1->incident_s_edges.begin();
Expand Down Expand Up @@ -194,22 +187,10 @@ void HGEdge::detach(unsigned char fmt_mask)
}
}
format &= ~fmt_mask;
}

HGEdge::~HGEdge()
{ /*if (format)
{ std::cout << '~';
if (format & simple) std::cout << 's';
if (format & collapsed) std::cout << 'c';
std::cout << std::endl;
}//*/
detach(format);
segment_name.clear();
intermediate_points.clear();
route_names_and_systems.clear();
if (format & simple) delete[] s_written;
delete[] c_written;
delete[] t_written;
if (!format)
{ delete[] written;
delete this;
}
}

// compute an edge label, optionally resticted by systems
Expand Down
4 changes: 2 additions & 2 deletions siteupdate/cplusplus/classes/GraphGeneration/HGEdge.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class HGEdge
edge that can incorporate intermediate points.
*/
public:
bool *s_written, *c_written, *t_written; // simple, collapsed, traveled
char* written;
std::string segment_name;
HGVertex *vertex1, *vertex2;
std::list<HGVertex*> intermediate_points; // if more than 1, will go from vertex1 to vertex2
Expand All @@ -26,8 +26,8 @@ class HGEdge

HGEdge(HighwaySegment *, HighwayGraph *, int);
HGEdge(HGVertex *, unsigned char, int);
~HGEdge();

void detach();
void detach(unsigned char);
void write_label(std::ofstream&, std::list<HighwaySystem*> *);
void collapsed_tmg_line(std::ofstream&, char*, unsigned int, std::list<HighwaySystem*>*);
Expand Down
6 changes: 3 additions & 3 deletions siteupdate/cplusplus/classes/GraphGeneration/HGVertex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ HGVertex::HGVertex(Waypoint *wpt, const std::string *n, unsigned int numthreads)

HGVertex::~HGVertex()
{ //std::cout << "deleting vertex at " << first_waypoint->str() << std::endl;
while (incident_s_edges.size()) delete incident_s_edges.front();
while (incident_c_edges.size()) delete incident_c_edges.front();
while (incident_t_edges.size()) delete incident_t_edges.front();
while (incident_s_edges.size()) incident_s_edges.front()->detach();
while (incident_c_edges.size()) incident_c_edges.front()->detach();
while (incident_t_edges.size()) incident_t_edges.front()->detach();
delete[] s_vertex_num;
delete[] c_vertex_num;
delete[] t_vertex_num;
Expand Down
30 changes: 15 additions & 15 deletions siteupdate/cplusplus/classes/GraphGeneration/HighwayGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,17 @@ inline void HighwayGraph::matching_vertices_and_edges

// initialize *_written booleans
for (HGVertex *v : mvset)
{ for (HGEdge* e : v->incident_s_edges) e->s_written[threadnum] = 0;
for (HGEdge* e : v->incident_c_edges) e->c_written[threadnum] = 0;
for (HGEdge* e : v->incident_t_edges) e->t_written[threadnum] = 0;
{ for (HGEdge* e : v->incident_s_edges) e->written[threadnum] = 0;
for (HGEdge* e : v->incident_c_edges) e->written[threadnum] = 0;
for (HGEdge* e : v->incident_t_edges) e->written[threadnum] = 0;
}

// Compute sets of edges for subgraphs, optionally
// restricted by region or system or placeradius.
// Keep a count of collapsed & traveled vertices as we go.
for (HGVertex *v : mvset)
{ for (HGEdge *e : v->incident_s_edges)
if ((!g.placeradius || g.placeradius->contains_edge(e)) && !e->s_written[threadnum])
if ((!g.placeradius || g.placeradius->contains_edge(e)) && !(e->written[threadnum] & HGEdge::simple))
if (!g.regions || contains(*g.regions, e->segment->route->region))
{ bool system_match = !g.systems;
if (!system_match)
Expand All @@ -241,13 +241,13 @@ inline void HighwayGraph::matching_vertices_and_edges
}
if (system_match)
{ mse.push_back(e);
e->s_written[threadnum] = 1;
e->written[threadnum] |= HGEdge::simple;
}
}
if (v->visibility < 1) continue;
tv_count++;
for (HGEdge *e : v->incident_t_edges)
if ((!g.placeradius || g.placeradius->contains_edge(e)) && !e->t_written[threadnum])
if ((!g.placeradius || g.placeradius->contains_edge(e)) && !(e->written[threadnum] & HGEdge::traveled))
if (!g.regions || contains(*g.regions, e->segment->route->region))
{ bool system_match = !g.systems;
if (!system_match)
Expand All @@ -258,7 +258,7 @@ inline void HighwayGraph::matching_vertices_and_edges
}
if (system_match)
{ mte.push_back(e);
e->t_written[threadnum] = 1;
e->written[threadnum] |= HGEdge::traveled;
for (TravelerList *t : e->segment->clinched_by)
if (!t->in_subgraph[threadnum])
{ traveler_lists.push_back(t);
Expand All @@ -269,7 +269,7 @@ inline void HighwayGraph::matching_vertices_and_edges
if (v->visibility < 2) continue;
cv_count++;
for (HGEdge *e : v->incident_c_edges)
if ((!g.placeradius || g.placeradius->contains_edge(e)) && !e->c_written[threadnum])
if ((!g.placeradius || g.placeradius->contains_edge(e)) && !(e->written[threadnum] & HGEdge::collapsed))
if (!g.regions || contains(*g.regions, e->segment->route->region))
{ bool system_match = !g.systems;
if (!system_match)
Expand All @@ -280,7 +280,7 @@ inline void HighwayGraph::matching_vertices_and_edges
}
if (system_match)
{ mce.push_back(e);
e->c_written[threadnum] = 1;
e->written[threadnum] |= HGEdge::collapsed;
}
}
}
Expand Down Expand Up @@ -338,8 +338,8 @@ void HighwayGraph::write_master_graphs_tmg()
// now edges, only write if not already written
for (HGVertex* v : vertices)
{ for (HGEdge *e : v->incident_s_edges)
if (!e->s_written[0])
{ e->s_written[0] = 1;
if (!(e->written[0] & HGEdge::simple))
{ e->written[0] |= HGEdge::simple;
simplefile << e->vertex1->s_vertex_num[0] << ' ' << e->vertex2->s_vertex_num[0] << ' ';
e->write_label(simplefile, 0);
simplefile << '\n';
Expand All @@ -349,15 +349,15 @@ void HighwayGraph::write_master_graphs_tmg()
{ char fstr[57];
// in traveled graph,
for (HGEdge *e : v->incident_t_edges)
if (!e->t_written[0])
{ e->t_written[0] = 1;
if (!(e->written[0] & HGEdge::traveled))
{ e->written[0] |= HGEdge::traveled;
e->traveled_tmg_line(travelfile, fstr, 0, 0, &TravelerList::allusers);
}
if (v->visibility == 2)
{ // and in collapsed graph
for (HGEdge *e : v->incident_c_edges)
if (!e->c_written[0])
{ e->c_written[0] = 1;
if (!(e->written[0] & HGEdge::collapsed))
{ e->written[0] |= HGEdge::collapsed;
e->collapsed_tmg_line(collapfile, fstr, 0, 0);
}
}
Expand Down

0 comments on commit 029ce2d

Please sign in to comment.