Graybat  1.1
Graph Approach for Highly Generic Communication Schemes Based on Adaptive Topologies
BGL.hpp
1 #pragma once
2 
3 //STL
4 #include <vector>
5 #include <iostream>
6 #include <tuple>
7 #include <tuple>
8 #include <string>
9 #include <fstream> /* std::fstream */
10 #include <utility> /* std::pair, std::make_pair */
11 #include <memory> /* std::unique_ptr */
12 
13 // BOOSt
14 #include <boost/graph/graph_traits.hpp>
15 #include <boost/graph/adjacency_list.hpp>
16 #include <boost/graph/subgraph.hpp>
17 #include <boost/graph/graphviz.hpp>
18 #include <boost/bimap.hpp>
19 
20 
21 namespace graybat {
22 
23  namespace graphPolicy {
24 
26 
27  };
28 
29  /************************************************************************/
37  template <class T_VertexProperty = SimpleProperty, class T_EdgeProperty = SimpleProperty>
38  class BGL {
39 
40  private:
41  using GraphType = boost::adjacency_list<boost::vecS,
42  boost::vecS,
43  boost::bidirectionalS,
44  boost::property<boost::vertex_index_t, size_t, std::pair<graybat::graphPolicy::VertexID, T_VertexProperty> >,
45  boost::property<boost::edge_index_t, size_t, std::pair<graybat::graphPolicy::EdgeID, T_EdgeProperty> > >;
46 
47  using BGLGraph = boost::subgraph<GraphType>;
48  using BglVertexID = typename BGLGraph::vertex_descriptor;
49  using BglEdgeID = typename BGLGraph::edge_descriptor;
50  using VertexID = graybat::graphPolicy::VertexID;
51  using EdgeID = graybat::graphPolicy::EdgeID;
52  using GraphID = graybat::graphPolicy::GraphID;
53 
54  public:
55 
56  using VertexProperty = T_VertexProperty;
57  using EdgeProperty = T_EdgeProperty;
58  using VertexDescription = graybat::graphPolicy::VertexDescription<BGL>;
59  using EdgeDescription = graybat::graphPolicy::EdgeDescription<BGL>;
60  using GraphDescription = graybat::graphPolicy::GraphDescription<BGL>;
61  using InEdgeIter = typename boost::graph_traits<BGLGraph>::in_edge_iterator;
62  using OutEdgeIter = typename boost::graph_traits<BGLGraph>::out_edge_iterator;
63  using AdjacentVertexIter = typename boost::graph_traits<BGLGraph>::adjacency_iterator;
64  using AllVertexIter = typename boost::graph_traits<BGLGraph>::vertex_iterator;
65 
66  private:
67  // Member
68  std::shared_ptr<BGLGraph> graph;
69  std::vector<BGL<VertexProperty, EdgeProperty>> subGraphs;
70  boost::bimap<EdgeID, BglEdgeID> edgeIdBiMap;
71 
72  public:
73  GraphID id;
74 
81  BGL(GraphDescription graphDesc) :
82  id(0){
83 
84  std::vector<VertexDescription> vertices = graphDesc.first;
85  std::vector<EdgeDescription> edges = graphDesc.second;
86 
87  graph = std::make_shared<BGLGraph>(vertices.size());
88 
89  unsigned edgeCount = 0;
90 
91 
92  for(EdgeID edgeId = 0; edgeId < edges.size(); ++edgeId){
93  BglVertexID srcVertex = std::get<0>(edges[edgeId].first);
94  BglVertexID targetVertex = std::get<1>(edges[edgeId].first);
95  BglEdgeID bglEdgeId = boost::add_edge(srcVertex, targetVertex, (*graph)).first;
96  edgeIdBiMap.insert(typename boost::bimap<EdgeID, BglEdgeID>::value_type(edgeId, bglEdgeId));
97  setEdgeProperty(bglEdgeId, std::make_pair(edgeCount++, edges[edgeId].second));
98  }
99 
100  // Bind vertex_descriptor and VertexProperty;
101  for(VertexDescription &v : vertices){
102  setVertexProperty(v.first, std::make_pair(v.first, v.second));
103  }
104 
105  }
106 
107  /*******************************************************************
108  * GRAPH OPERATIONS
109  ******************************************************************/
110 
115  std::pair<AllVertexIter, AllVertexIter> getVertices(){
116  return boost::vertices((*graph));
117 
118  }
119 
124  std::pair<EdgeID, bool> getEdge(const VertexID source, const VertexID target){
125  std::pair<BglEdgeID, bool> bglEdgeId = boost::edge(source, target, *graph);
126  return std::make_pair(edgeIdBiMap.right.at(bglEdgeId.first), bglEdgeId.second);
127  }
128 
133  std::pair<AdjacentVertexIter, AdjacentVertexIter> getAdjacentVertices(const VertexID id){
134  return boost::adjacent_vertices(id, *graph);
135  }
136 
141  std::pair<OutEdgeIter, OutEdgeIter> getOutEdges(const VertexID id){
142  return boost::out_edges((*graph).global_to_local(id), (*graph));
143  }
144 
149  std::pair<InEdgeIter, InEdgeIter> getInEdges(const VertexID id){
150  return boost::in_edges((*graph).global_to_local(id), (*graph));
151  }
152 
158  BglVertexID getLocalID(VertexProperty vertex){
159  return (*graph).global_to_local(vertex.id);
160  }
161 
162  void setVertexProperty(BglVertexID vertex, VertexProperty value){
163  std::pair<VertexID, VertexProperty> propPair = (*graph)[vertex];
164  (*graph)[vertex] = std::make_pair<propPair.first, value>;
165  }
166 
167  void setVertexProperty(BglVertexID vertex, std::pair<VertexID, VertexProperty> propPair){
168  (*graph)[vertex] = propPair;
169  }
170 
171 
172  void setEdgeProperty(BglEdgeID edge, EdgeProperty value){
173  std::pair<EdgeID, EdgeProperty> propPair = (*graph)[edge];
174  (*graph)[edge] = std::make_pair<propPair.first, value>;
175  }
176 
177  void setEdgeProperty(BglEdgeID edge, std::pair<EdgeID, EdgeProperty> propPair){
178  (*graph)[edge] = propPair;
179  }
180 
185  std::pair<VertexID, VertexProperty>& getVertexProperty(const VertexID vertex){
186  return (*graph)[vertex];
187  }
188 
189 
194  std::pair<EdgeID, EdgeProperty>& getEdgeProperty(const BglEdgeID edge){
195  return (*graph)[edge];
196  }
197 
198  std::pair<EdgeID, EdgeProperty>& getEdgeProperty(const EdgeID edge){
199  return getEdgeProperty(edgeIdBiMap.left.at(edge));
200  }
201 
206  BglVertexID getEdgeTarget(const BglEdgeID edge){
207  return boost::target(edge, (*graph));
208  }
209 
210  VertexID getEdgeTarget(const EdgeID edge){
211  return getEdgeTarget(edgeIdBiMap.left.at(edge));
212  }
213 
218  BglVertexID getEdgeSource(const BglEdgeID edge){
219  return boost::source(edge, (*graph));
220  }
221 
222  VertexID getEdgeSource(const EdgeID edge){
223  return getEdgeSource(edgeIdBiMap.left.at(edge));
224  }
225 
226 
227  };
228 
229  } // namespace graphPolicy
230 
231 } // namespace graybat
std::pair< AdjacentVertexIter, AdjacentVertexIter > getAdjacentVertices(const VertexID id)
Returns all vertices, that are adjacent (connected) to vertex
Definition: BGL.hpp:133
BGL(GraphDescription graphDesc)
The graph has to be described by edges (source Vertex ==> target Vertex) and the vertices of this gra...
Definition: BGL.hpp:81
std::pair< VertexID, VertexProperty > & getVertexProperty(const VertexID vertex)
Returns the property of vertex.
Definition: BGL.hpp:185
std::pair< AllVertexIter, AllVertexIter > getVertices()
Returns all vertices of the graph.
Definition: BGL.hpp:115
BglVertexID getEdgeTarget(const BglEdgeID edge)
Return the vertex to which edge points to.
Definition: BGL.hpp:206
BglVertexID getLocalID(VertexProperty vertex)
Returns the local id of vertex in this graph.
Definition: BGL.hpp:158
std::pair< EdgeID, EdgeProperty > & getEdgeProperty(const BglEdgeID edge)
Return the property of edge.
Definition: BGL.hpp:194
std::pair< OutEdgeIter, OutEdgeIter > getOutEdges(const VertexID id)
Returns all outgoing edgpes of srcVertex paired with its target vertex.
Definition: BGL.hpp:141
BglVertexID getEdgeSource(const BglEdgeID edge)
Return the vertex to which edge points from.
Definition: BGL.hpp:218
A class to describe directed graphs.
Definition: BGL.hpp:38
std::pair< InEdgeIter, InEdgeIter > getInEdges(const VertexID id)
Returns all incoming edges to targetVertex paired with its source vertex.
Definition: BGL.hpp:149
std::pair< EdgeID, bool > getEdge(const VertexID source, const VertexID target)
Returns the edge between source and target vertex.
Definition: BGL.hpp:124
Definition: BiStar.hpp:8