Skip to content

Instantly share code, notes, and snippets.

@jackbergus
Last active January 2, 2016 02:29
Show Gist options
  • Save jackbergus/8237502 to your computer and use it in GitHub Desktop.
Save jackbergus/8237502 to your computer and use it in GitHub Desktop.
A simple wrapping over Boost Graph Library
/*
* Vertex.cpp
* This file is part of socialsim
*
* Copyright (C) 2014 - Giacomo Bergami
*
* socialsim is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* socialsim is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with socialsim. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <utility>
#include <boost/graph/directed_graph.hpp>
#include <boost/graph/graphml.hpp>
class Vertex;
/**
* Vertex:
* this class defines the basic properties of the graph vertex
*/
class VProp
{
public:
std::string name;
unsigned int id;
Vertex *self;
VProp() : name{""}, id{0}, self{nullptr} {};
VProp(std::string nome,unsigned int idv) : name{nome}, id{idv}, self{nullptr} {};
VProp(const VProp& vp) {
name = vp.name;
id = vp.id;
self = vp.self;
}
};
class Edge;
/**
* EProp:
* This class defines the edge properties
*/
class EProp
{
public:
std::string name;
unsigned int id;
Edge* self;
EProp() : name{""}, id{0}, self{nullptr} {};
EProp(std::string nome,unsigned int ide) : name{nome}, id{ide}, self{nullptr} {};
EProp(const EProp& ep) {
name = ep.name;
id = ep.id;
self = ep.self;
}
};
typedef boost::adjacency_list<boost::listS, boost::listS, boost::directedS,VProp, EProp> graph_t;
typedef boost::graph_traits<graph_t>::vertex_descriptor vertex_descriptor;
typedef boost::graph_traits<graph_t>::edge_descriptor edge_descriptor;
class Graph;
/**
* Vertex:
* This class defines the abstraction of a graph vertex, with its properties
* and the gboost identifier
*/
class Vertex {
private:
VProp* vp;
vertex_descriptor vd;
Graph* g;
public:
Vertex(VProp* prop, vertex_descriptor desc,Graph* grap) {
vp = prop;
vp->self = this;
vd = desc;
g = grap;
}
void setName(std::string new_name);
std::string getName();
vertex_descriptor get_gboost_vertex() {
return vd;
}
void remove();
};
/**
* Edge:
* This class defines the abstraction of a graph edge, with its properties
* and the gboost identifier
*/
class Edge {
private:
EProp* vp;
edge_descriptor vd;
Graph* g;
public:
Edge(EProp* prop, edge_descriptor desc,Graph* grap) {
vp = prop;
vp->self = this;
vd = desc;
g = grap;
}
void setName(std::string new_name);
std::string getName();
edge_descriptor get_gboost_edge() {
return vd;
}
void remove();
};
class Graph {
graph_t g;
unsigned int v_id = 0;
unsigned int e_id = 0;
public:
Vertex addVertex(std::string label) {
VProp vp{label,v_id++};
vertex_descriptor v0 = boost::add_vertex(g);
g[v0] = vp;
Vertex v{&vp,v0,this}; //updates vp pointer to vertex
return v;
}
Edge addEdge(Vertex src, Vertex dst, std::string label) {
EProp ep{label,e_id++};
edge_descriptor e0 = boost::add_edge(src.get_gboost_vertex(),dst.get_gboost_vertex(),ep,g).first;
Edge e{&ep,e0,this};
return e;
}
void setName(std::string label, Vertex* v) {
g[v->get_gboost_vertex()].name = label;
}
void setName(std::string label, Edge* v) {
g[v->get_gboost_edge()].name = label;
}
void remove(Vertex* v) {
if (!v) return;
boost::remove_vertex(v->get_gboost_vertex(),g);
v->remove();
}
void remove(Edge* v) {
if (!v) return;
boost::remove_edge(v->get_gboost_edge(),g);
v->remove();
}
graph_t get_gboost_graph() {
return g;
}
};
void Vertex::setName(std::string new_name) {
if ((!vp)||(!g)) return;
g->setName(new_name,this);
}
std::string Vertex::getName() {
if ((!vp)||(!g)) { std::string e{}; return e; }
graph_t gr = g->get_gboost_graph();
return (gr)[(vd)].name;
}
void Edge::setName(std::string new_name) {
if ((!vp)||(!g)) return;
g->setName(new_name,this);
}
std::string Edge::getName() {
if ((!vp)||(!g)) { std::string e{}; return e; }
graph_t gr = g->get_gboost_graph();
return (gr)[(vd)].name;
}
void Vertex::remove() {
if ((!vp)||(!g)) {
return;
}
Graph* tmp = g;
vp = nullptr;
g = nullptr;
tmp->remove(this);
}
void Edge::remove() {
if ((!vp)||(!g)) {
return;
}
Graph* tmp = g;
vp = nullptr;
g = nullptr;
tmp->remove(this);
}
int main() {
Graph h;
Vertex v = h.addVertex("ciao mondo");
Vertex u = h.addVertex("mondo vario");
v.setName("toccata");
auto vd = v.get_gboost_vertex();
auto g = h.get_gboost_graph();
std::cout << g[vd].name <<std::endl;
std::cout << v.getName() <<std::endl;
//v.remove();
//std::cout << v.getName() <<std::endl;
Edge e1 = h.addEdge(v,u,"link");
e1.setName("edge");
e1.remove();
std::cout << e1.getName() << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment