From 3a4756168bac259b66f7b0bd84dbdd2fadd95f70 Mon Sep 17 00:00:00 2001
From: David Rosca <roscadav@fit.cvut.cz>
Date: Sun, 22 Mar 2015 14:39:27 +0100
Subject: [PATCH] AdjacencyList representation: fix compare

---
 .../directed/AdjacencyListDirectedGraph.cpp   | 34 ++++++++++++++++---
 .../AdjacencyListUndirectedGraph.cpp          | 34 ++++++++++++++++---
 2 files changed, 60 insertions(+), 8 deletions(-)

diff --git a/alib2data/src/graph/directed/AdjacencyListDirectedGraph.cpp b/alib2data/src/graph/directed/AdjacencyListDirectedGraph.cpp
index be31ad5254..183b78b7d9 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 4f98050aac..3d54453729 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
-- 
GitLab