From b079c2003052d8ca003da39132531e6ee2ea89f3 Mon Sep 17 00:00:00 2001 From: David Rosca <roscadav@fit.cvut.cz> Date: Tue, 24 Nov 2015 10:34:04 +0100 Subject: [PATCH] Graphs: Add embedding to Undirected and Directed Graphs --- alib2data/src/graph/directed/DirectedGraph.cpp | 17 +++++++++++++++-- alib2data/src/graph/directed/DirectedGraph.h | 5 +++++ .../src/graph/undirected/UndirectedGraph.cpp | 17 +++++++++++++++-- .../src/graph/undirected/UndirectedGraph.h | 5 +++++ 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/alib2data/src/graph/directed/DirectedGraph.cpp b/alib2data/src/graph/directed/DirectedGraph.cpp index 12ae66f3ba..7822b83c71 100644 --- a/alib2data/src/graph/directed/DirectedGraph.cpp +++ b/alib2data/src/graph/directed/DirectedGraph.cpp @@ -38,6 +38,7 @@ DirectedGraph::DirectedGraph(const DirectedGraph &other) : representation(other.representation) , nodeValues(other.nodeValues) , edgeValues(other.edgeValues) + , embedding(other.embedding) { init(); impl->copy(other.impl); @@ -48,6 +49,7 @@ DirectedGraph::DirectedGraph(DirectedGraph &&other) noexcept , impl(other.impl) , nodeValues(std::move(other.nodeValues)) , edgeValues(std::move(other.edgeValues)) + , embedding(std::move(other.embedding)) { other.impl = 0; } @@ -68,6 +70,7 @@ DirectedGraph &DirectedGraph::operator=(DirectedGraph &&other) noexcept std::swap(this->impl, other.impl); std::swap(this->nodeValues, other.nodeValues); std::swap(this->edgeValues, other.edgeValues); + std::swap(this->embedding, other.embedding); return *this; } @@ -192,6 +195,16 @@ void DirectedGraph::setEdgeValue(const DirectedEdge &edge, int value) edgeValues.insert({edge, value}); } +std::unordered_map < Node, std::vector < Node > > DirectedGraph::getEmbedding ( ) const +{ + return embedding; +} + +void DirectedGraph::setEmbedding ( const std::unordered_map < Node, std::vector< Node > > embedding ) +{ + this->embedding = embedding; +} + void DirectedGraph::operator>>(std::ostream &out) const { out << "(DirectedGraph " << static_cast<std::string>(*impl) << ")"; @@ -203,8 +216,8 @@ int DirectedGraph::compare(const DirectedGraph &other) const return static_cast<int>(representation) - static_cast<int>(other.representation); } - auto first = std::tie(nodeValues, edgeValues); - auto second = std::tie(other.nodeValues, other.edgeValues); + auto first = std::tie(nodeValues, edgeValues, embedding); + auto second = std::tie(other.nodeValues, other.edgeValues, other.embedding); std::compare<decltype(first)> comp; int res = comp(first, second); diff --git a/alib2data/src/graph/directed/DirectedGraph.h b/alib2data/src/graph/directed/DirectedGraph.h index c828f99ac0..7f1a4efe80 100644 --- a/alib2data/src/graph/directed/DirectedGraph.h +++ b/alib2data/src/graph/directed/DirectedGraph.h @@ -9,6 +9,7 @@ #define DIRECTED_GRAPH_H_ #include <unordered_map> +#include <vector> #include <set> #include "../GraphBase.h" @@ -62,6 +63,9 @@ public: int getEdgeValue ( const DirectedEdge & edge ) const; void setEdgeValue ( const DirectedEdge & edge, int value ); + std::unordered_map < Node, std::vector < Node > > getEmbedding ( ) const; + void setEmbedding ( const std::unordered_map < Node, std::vector< Node > > embedding ); + int compare ( const ObjectBase & other ) const override { if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); @@ -88,6 +92,7 @@ private: std::unordered_map < Node, int > nodeValues; std::unordered_map < DirectedEdge, int > edgeValues; + std::unordered_map < Node, std::vector < Node > > embedding; friend class GraphToXMLComposer; friend class GraphToStringComposer; diff --git a/alib2data/src/graph/undirected/UndirectedGraph.cpp b/alib2data/src/graph/undirected/UndirectedGraph.cpp index 3a0ebc79bb..c2f773c37e 100644 --- a/alib2data/src/graph/undirected/UndirectedGraph.cpp +++ b/alib2data/src/graph/undirected/UndirectedGraph.cpp @@ -38,6 +38,7 @@ UndirectedGraph::UndirectedGraph(const UndirectedGraph &other) : representation(other.representation) , nodeValues(other.nodeValues) , edgeValues(other.edgeValues) + , embedding(other.embedding) { init(); impl->copy(other.impl); @@ -48,6 +49,7 @@ UndirectedGraph::UndirectedGraph(UndirectedGraph &&other) noexcept , impl(other.impl) , nodeValues(std::move(other.nodeValues)) , edgeValues(std::move(other.edgeValues)) + , embedding(std::move(other.embedding)) { other.impl = 0; } @@ -68,6 +70,7 @@ UndirectedGraph &UndirectedGraph::operator=(UndirectedGraph &&other) noexcept std::swap(this->impl, other.impl); std::swap(this->nodeValues, other.nodeValues); std::swap(this->edgeValues, other.edgeValues); + std::swap(this->embedding, other.embedding); return *this; } @@ -192,6 +195,16 @@ void UndirectedGraph::setEdgeValue(const UndirectedEdge &edge, int value) edgeValues.insert({edge, value}); } +std::unordered_map < Node, std::vector < Node > > UndirectedGraph::getEmbedding ( ) const +{ + return embedding; +} + +void UndirectedGraph::setEmbedding ( const std::unordered_map < Node, std::vector< Node > > embedding ) +{ + this->embedding = embedding; +} + void UndirectedGraph::operator>>(std::ostream &out) const { out << "(UndirectedGraph " << static_cast<std::string>(*impl) << ")"; @@ -203,8 +216,8 @@ int UndirectedGraph::compare(const UndirectedGraph &other) const return static_cast<int>(representation) - static_cast<int>(other.representation); } - auto first = std::tie(nodeValues, edgeValues); - auto second = std::tie(other.nodeValues, other.edgeValues); + auto first = std::tie(nodeValues, edgeValues, embedding); + auto second = std::tie(other.nodeValues, other.edgeValues, other.embedding); std::compare<decltype(first)> comp; int res = comp(first, second); diff --git a/alib2data/src/graph/undirected/UndirectedGraph.h b/alib2data/src/graph/undirected/UndirectedGraph.h index 422b8fdd34..5a3d688ba3 100644 --- a/alib2data/src/graph/undirected/UndirectedGraph.h +++ b/alib2data/src/graph/undirected/UndirectedGraph.h @@ -9,6 +9,7 @@ #define UNDIRECTED_GRAPH_H_ #include <unordered_map> +#include <vector> #include <set> #include "../GraphBase.h" @@ -62,6 +63,9 @@ public: int getEdgeValue ( const UndirectedEdge & edge ) const; void setEdgeValue ( const UndirectedEdge & edge, int value ); + std::unordered_map < Node, std::vector < Node > > getEmbedding ( ) const; + void setEmbedding ( const std::unordered_map < Node, std::vector< Node > > embedding ); + int compare ( const ObjectBase & other ) const override { if ( std::type_index ( typeid ( * this ) ) == std::type_index ( typeid ( other ) ) ) return this->compare ( ( decltype ( * this ) )other ); @@ -88,6 +92,7 @@ private: std::unordered_map < Node, int > nodeValues; std::unordered_map < UndirectedEdge, int > edgeValues; + std::unordered_map < Node, std::vector < Node > > embedding; friend class GraphToXMLComposer; friend class GraphToStringComposer; -- GitLab