Graph::operator==

master
dmatetelki 8 years ago
parent d54c349b2c
commit f4b80c3ec9

@ -62,7 +62,8 @@ public:
Graph(const std::vector<V>& vertex_list);
Graph(std::initializer_list<Edge> edge_list);
Graph(const std::vector<Edge>& edge_list);
bool operator==(const Graph& o) const { return m_vertices == o.m_vertices; }
bool operator==(const Graph& o) const;
bool operator!=(const Graph& o) const { return !(*this == o); }
void addVertex(const_reference data);
void removeVertex(const_reference data);
@ -204,6 +205,30 @@ inline Graph<V>::Graph(const std::vector<Edge>& edge_list)
addEdge(e.source, e.destination);
}
template <typename V>
inline bool Graph<V>::operator==(const Graph& o) const
{
if (numberOfVertices(*this) != numberOfVertices(o))
return false;
for (const auto& v : m_vertices) {
const auto& o_v(o.neighboursOf(v.first));
if (v.second.size() != o_v.size())
return false;
// compare 2 unordered vectors of unique elements...very costy
/// @note should vector be replaced with set?
auto v_copy(v.second);
auto o_v_copy(o_v);
sort(v_copy.begin(), v_copy.end());
sort(o_v_copy.begin(), o_v_copy.end());
if (v_copy != o_v_copy)
return false;
}
return true;
}
template <typename V>
inline void Graph<V>::addVertex(const_reference data)
{

Loading…
Cancel
Save