diff --git a/graph.h b/graph.h index 5c6e147..130f32a 100644 --- a/graph.h +++ b/graph.h @@ -5,6 +5,8 @@ #include // directed, weighted +/// @todo weight type as param too? + template class Graph { @@ -52,16 +54,12 @@ public: // Lookup bool contains(const T& data) const; - std::vector vertices() const; - std::vector neighboursOf(const T& data) const; - std::vector edgesBetween(const T& source, const T& destination) const; - - std::string serialize() const; - + std::vector vertices() const; + std::vector neighboursOf(const T& data) const; + std::vector edgesBetween(const T& source, const T& destination) const; private: - typename std::vector::const_iterator find(const T& data) const; typename std::vector::iterator find(const T& data); @@ -249,41 +247,42 @@ bool Graph::contains(const T& data) const } template -std::vector Graph::vertices() const +std::vector Graph::vertices() const { - std::vector retval; + std::vector retval; std::for_each(m_vertices.begin(), m_vertices.end(), [&retval](const Vertex& v) - { retval.push_back(v.m_data); }); + { retval.push_back( const_cast(v.m_data)); }); return retval; } template -std::vector Graph::neighboursOf(const T& data) const +std::vector Graph::neighboursOf(const T& data) const { - typename std::vector retval; + typename std::vector retval; typename std::vector::const_iterator vertex_it = find(data); if (vertex_it == m_vertices.end()) return retval; std::for_each((*vertex_it).m_edges.begin(), (*vertex_it).m_edges.end(), [&retval](const Edge& e) - { retval.push_back(e.m_weight); }); + { retval.push_back( const_cast(e.m_destination)); }); return retval; } template -std::vector Graph::edgesBetween(const T& source, const T& destination) const +std::vector Graph::edgesBetween(const T& source, const T& destination) const { - std::vector retval; + std::vector retval; typename std::vector::const_iterator vertex_it = find(source); if (vertex_it == m_vertices.end()) return retval; std::for_each((*vertex_it).m_edges.begin(), (*vertex_it).m_edges.end(), [&retval, &destination](const Edge& e) - { if (e.m_destination == &destination) retval.push_back(e.m_weight); }); + { if (*(e.m_destination) == destination) + retval.push_back(e.m_weight); }); return retval; } @@ -294,7 +293,7 @@ typename std::vector::Vertex >::const_iterator Graph::find( { return std::find_if(m_vertices.begin(), m_vertices.end(), [&data](const Vertex& v) - { return v.m_data == &data; }); + { return *(v.m_data) == data; }); } template @@ -302,7 +301,7 @@ typename std::vector::Vertex >::iterator Graph::find(const { return std::find_if(m_vertices.begin(), m_vertices.end(), [&data](const Vertex& v) - { return v.m_data == &data; }); + { return *(v.m_data) == data; }); } diff --git a/main.cpp b/main.cpp index f806de4..903116f 100644 --- a/main.cpp +++ b/main.cpp @@ -21,20 +21,40 @@ int main() assert(g.addVertex(a) == false); assert(g.empty() == false); assert(g.numberOfVertices() == 1); + { + const std::vector v = g.vertices(); + assert(v.size() == 1); + assert(*(v[0]) == 2); + } assert(g.addEdge(a, b) == false); assert(g.addVertex(b) == true); assert(g.numberOfVertices() == 2); + { + const std::vector v = g.vertices(); + assert(v.size() == 2); + assert(*(v[0]) == 2); + assert(*(v[1]) == 5); + } assert(g.numberOfEdges() == 0); assert(g.addEdge(a, b) == true); assert(g.numberOfEdges() == 1); - - assert(g.edgesBetween(2, 5).size() == 1); + { + const std::vector eb = g.edgesBetween(2, 5); + assert(eb.size() == 1); + assert(eb[0] == 0); + } assert(g.edgesBetween(5, 2).size() == 0); assert(g.neighboursOf(2).size() == 1); + { + const std::vector n = g.neighboursOf(2); + assert(n.size() == 1); + assert(*(n[0]) == 5); + } + assert(g.neighboursOf(5).size() == 0); return 0;