Skip to content
Snippets Groups Projects
Commit 3a475616 authored by David Rosca's avatar David Rosca
Browse files

AdjacencyList representation: fix compare

parent d6a2ab3e
No related branches found
No related tags found
No related merge requests found
...@@ -164,11 +164,37 @@ int AdjacencyListDirectedGraph::compare(IDirectedGraph *other) const ...@@ -164,11 +164,37 @@ int AdjacencyListDirectedGraph::compare(IDirectedGraph *other) const
{ {
AdjacencyListDirectedGraph *o = static_cast<AdjacencyListDirectedGraph*>(other); AdjacencyListDirectedGraph *o = static_cast<AdjacencyListDirectedGraph*>(other);
   
auto first = std::tie(edges, adj); std::compare<decltype(edges)> comp;
auto second = std::tie(o->edges, o->adj); int res = comp(edges, o->edges);
if (res != 0) {
return res;
}
if (adj.size() < o->adj.size()) {
return -1;
}
if (adj.size() > o->adj.size()) {
return 1;
}
for (auto i : adj) {
auto lst1 = i.second;
auto lst2 = o->adj.at(i.first);
std::set<Node> adj1;
std::set<Node> adj2;
std::copy(lst1.begin(), lst1.end(), std::inserter(adj1, adj1.begin()));
std::copy(lst2.begin(), lst2.end(), std::inserter(adj2, adj2.begin()));
std::compare<std::set<Node>> comp;
int res = comp(adj1, adj2);
if (res != 0) {
return res;
}
}
   
std::compare<decltype(first)> comp; return 0;
return comp(first, second);
} }
   
} // namespace graph } // namespace graph
...@@ -164,11 +164,37 @@ int AdjacencyListUndirectedGraph::compare(IUndirectedGraph *other) const ...@@ -164,11 +164,37 @@ int AdjacencyListUndirectedGraph::compare(IUndirectedGraph *other) const
{ {
AdjacencyListUndirectedGraph *o = static_cast<AdjacencyListUndirectedGraph*>(other); AdjacencyListUndirectedGraph *o = static_cast<AdjacencyListUndirectedGraph*>(other);
   
auto first = std::tie(edges, adj); std::compare<decltype(edges)> comp;
auto second = std::tie(o->edges, o->adj); int res = comp(edges, o->edges);
if (res != 0) {
return res;
}
if (adj.size() < o->adj.size()) {
return -1;
}
if (adj.size() > o->adj.size()) {
return 1;
}
for (auto i : adj) {
auto lst1 = i.second;
auto lst2 = o->adj.at(i.first);
std::set<Node> adj1;
std::set<Node> adj2;
std::copy(lst1.begin(), lst1.end(), std::inserter(adj1, adj1.begin()));
std::copy(lst2.begin(), lst2.end(), std::inserter(adj2, adj2.begin()));
std::compare<std::set<Node>> comp;
int res = comp(adj1, adj2);
if (res != 0) {
return res;
}
}
   
std::compare<decltype(first)> comp; return 0;
return comp(first, second);
} }
   
} // namespace graph } // namespace graph
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment