mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
lib/directed-graph: add findVertex() and minor refactoring
This commit is contained in:
parent
02e873e8ff
commit
33ba634d87
1 changed files with 16 additions and 2 deletions
|
@ -71,6 +71,7 @@ public:
|
|||
|
||||
using VertexIdentifier = Vertex::Identifier;
|
||||
using EdgeIdentifier = Edge::Identifier;
|
||||
using Path = std::list<EdgeIdentifier>;
|
||||
|
||||
DirectedGraph(const std::string& name = "DirectedGraph") :
|
||||
lastVertexId(0), lastEdgeId(0)
|
||||
|
@ -88,6 +89,18 @@ public:
|
|||
return vertices.at(vertexId);
|
||||
}
|
||||
|
||||
template<class UnaryPredicate>
|
||||
VertexIdentifier findVertex(UnaryPredicate p)
|
||||
{
|
||||
for(auto& [vertexId, vertex] : vertices) {
|
||||
if(p(vertex)) {
|
||||
return vertexId;
|
||||
}
|
||||
}
|
||||
|
||||
throw std::out_of_range("vertex not found");
|
||||
}
|
||||
|
||||
std::shared_ptr<EdgeType> getEdge(EdgeIdentifier edgeId) const
|
||||
{
|
||||
if(edgeId < 0 or edgeId >= lastEdgeId)
|
||||
|
@ -194,8 +207,9 @@ public:
|
|||
vertexGetEdges(VertexIdentifier vertexId) const
|
||||
{ return getVertex(vertexId)->edges; }
|
||||
|
||||
bool getPath(VertexIdentifier fromVertexId, VertexIdentifier toVertexId,
|
||||
std::list<EdgeIdentifier>& path)
|
||||
bool getPath(VertexIdentifier fromVertexId,
|
||||
VertexIdentifier toVertexId,
|
||||
Path& path)
|
||||
{
|
||||
if(fromVertexId == toVertexId) {
|
||||
// arrived at the destination
|
||||
|
|
Loading…
Add table
Reference in a new issue