diff --git a/alib2data/src/graph/directed/AdjacencyListDirectedGraph.cpp b/alib2data/src/graph/directed/AdjacencyListDirectedGraph.cpp index be31ad52549ba415866d7d22f88305612add018a..183b78b7d9581d7c8ff1d41d3f93bbf952839d50 100644 --- a/alib2data/src/graph/directed/AdjacencyListDirectedGraph.cpp +++ b/alib2data/src/graph/directed/AdjacencyListDirectedGraph.cpp @@ -164,11 +164,37 @@ int AdjacencyListDirectedGraph::compare(IDirectedGraph *other) const { AdjacencyListDirectedGraph *o = static_cast<AdjacencyListDirectedGraph*>(other); - auto first = std::tie(edges, adj); - auto second = std::tie(o->edges, o->adj); + std::compare<decltype(edges)> comp; + 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 comp(first, second); + return 0; } } // namespace graph diff --git a/alib2data/src/graph/undirected/AdjacencyListUndirectedGraph.cpp b/alib2data/src/graph/undirected/AdjacencyListUndirectedGraph.cpp index 4f98050aac27073f7d85e91ebb51e75b73cbf66d..3d54453729e335a1978aa2023f39eb2f426b896e 100644 --- a/alib2data/src/graph/undirected/AdjacencyListUndirectedGraph.cpp +++ b/alib2data/src/graph/undirected/AdjacencyListUndirectedGraph.cpp @@ -164,11 +164,37 @@ int AdjacencyListUndirectedGraph::compare(IUndirectedGraph *other) const { AdjacencyListUndirectedGraph *o = static_cast<AdjacencyListUndirectedGraph*>(other); - auto first = std::tie(edges, adj); - auto second = std::tie(o->edges, o->adj); + std::compare<decltype(edges)> comp; + 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 comp(first, second); + return 0; } } // namespace graph