root / custompackages / graph-parser / src / graph_manager.cpp @ d5b7a27f
History | View | Annotate | Download (4.94 KB)
1 |
#include "graph_manager.h" |
---|---|
2 |
|
3 |
// CONSTRUCTOR
|
4 |
GraphManager::GraphManager(bool weighted_graph) : weighted_graph_(weighted_graph) {
|
5 |
v_index_pmap_ = VertexIndexPMap(v_index_std_map_); |
6 |
e_index_pmap_ = EdgeIndexPMap(e_index_std_map_); |
7 |
} |
8 |
|
9 |
GraphManager::GraphManager(const GraphManager& other) {
|
10 |
// cout << "\n*******COPY CONSTRUCTOR******\n";
|
11 |
g_ = other.g_; |
12 |
weighted_graph_ = other.weighted_graph_; |
13 |
ResetVerticesAndEdgesIndexMap(); |
14 |
} |
15 |
|
16 |
GraphManager& GraphManager::operator=(const GraphManager& rhs) { |
17 |
// cout << "\n*******ASSIGNMENT OPERATOR******\n";
|
18 |
g_ = rhs.g_; |
19 |
weighted_graph_ = rhs.weighted_graph_; |
20 |
ResetVerticesAndEdgesIndexMap(); |
21 |
|
22 |
return *this; |
23 |
} |
24 |
|
25 |
// GETTERS
|
26 |
const VertexIndexPMap& GraphManager::v_index_pmap() const { |
27 |
return v_index_pmap_;
|
28 |
} |
29 |
|
30 |
const EdgeIndexPMap& GraphManager::e_index_pmap() const { |
31 |
return e_index_pmap_;
|
32 |
} |
33 |
NameToIntMap const& GraphManager::v_id_index_map() const { |
34 |
return v_id_index_map_;
|
35 |
} |
36 |
|
37 |
bool GraphManager::weighted_graph() const { |
38 |
return weighted_graph_;
|
39 |
} |
40 |
|
41 |
// UPDATE GRAPHS
|
42 |
void GraphManager::AddEdge(VertexProperties vp1, VertexProperties vp2, EdgeProperties ep) {
|
43 |
// cout << "add edge GM " << vp1.label << " - " << vp2.label << endl;
|
44 |
|
45 |
string s1 = vp1.id;
|
46 |
string s2 = vp2.id;
|
47 |
|
48 |
if (s1 != s2) { // do not add self-loop into the graph |
49 |
Vertex v1; |
50 |
Vertex v2; |
51 |
|
52 |
try {
|
53 |
v1 = get_vertex_from_id(s1); |
54 |
} |
55 |
catch (exception& e) {
|
56 |
v1 = boost::add_vertex(vp1, g_); |
57 |
v_id_vertex_map_[s1] = v1; |
58 |
update_v_index_pmap(v1); |
59 |
} |
60 |
try {
|
61 |
v2 = get_vertex_from_id(s2); |
62 |
} |
63 |
catch (exception& e) {
|
64 |
v2 = boost::add_vertex(vp2, g_); |
65 |
v_id_vertex_map_[s2] = v2; |
66 |
update_v_index_pmap(v2); |
67 |
} |
68 |
|
69 |
Edge e; |
70 |
bool inserted;
|
71 |
boost::tie(e, inserted) = boost::add_edge(v1, v2, ep, g_); |
72 |
update_e_index_pmap(e); |
73 |
} |
74 |
} |
75 |
|
76 |
void GraphManager::ResetVerticesAndEdgesIndexMap() {
|
77 |
reset_v_index_pmap(); |
78 |
reset_v_id_vertex_map(); |
79 |
// The line below must be called after reset_v_index_pmap()
|
80 |
reset_v_id_index_map(); |
81 |
|
82 |
reset_e_index_pmap(); |
83 |
} |
84 |
|
85 |
// HELPERS
|
86 |
bool GraphManager::vertex_existed(string s) { |
87 |
std::map<std::string, Vertex>::iterator it;
|
88 |
it = v_id_vertex_map_.find(s); |
89 |
return (it != v_id_vertex_map_.end());
|
90 |
} |
91 |
|
92 |
const Vertex& GraphManager::get_vertex_from_id(string s) { |
93 |
if (vertex_existed(s)) {
|
94 |
return v_id_vertex_map_[s];
|
95 |
} |
96 |
else {
|
97 |
throw std::runtime_error("Vertex not found\n"); |
98 |
} |
99 |
} |
100 |
|
101 |
int GraphManager::get_index_from_id(string s) { |
102 |
if (vertex_existed(s)) {
|
103 |
return v_id_index_map_[s];
|
104 |
} |
105 |
else {
|
106 |
throw std::runtime_error("Vertex with id " + s + " is not found\n"); |
107 |
} |
108 |
} |
109 |
|
110 |
// OUTPUTTING THE RESULT
|
111 |
void GraphManager::print() {
|
112 |
cout << "\nGraph Manager:\n";
|
113 |
outops::operator<<(cout, g_);
|
114 |
|
115 |
cout << "Is graph connected?\n";
|
116 |
bool connected = graphext::is_connected(g_, v_index_pmap_);
|
117 |
cout << "Connected = " << connected << endl;
|
118 |
|
119 |
cout << "Is this a weighted graph?\n";
|
120 |
cout << "is weighted = " << weighted_graph_ << endl;
|
121 |
|
122 |
cout << "v_id_index_map:\n";
|
123 |
outops::operator<< <int>(cout, v_id_index_map_); |
124 |
} |
125 |
|
126 |
void GraphManager::print_v_index_pmap() {
|
127 |
graphext::print_v_index_pmap(g_, v_index_pmap_); |
128 |
} |
129 |
|
130 |
void GraphManager::print_e_index_pmap() {
|
131 |
graphext::print_e_index_pmap(g_, e_index_pmap_); |
132 |
} |
133 |
|
134 |
std::ostream& operator<<(std::ostream& os, const GraphManager& gm) { |
135 |
cout << "\nGraph Manager: " << endl;
|
136 |
outops::operator<<(cout, gm.g_);
|
137 |
|
138 |
cout << "v_id_index_map:\n";
|
139 |
outops::operator<< <int>(cout, gm.v_id_index_map()); |
140 |
return os;
|
141 |
} |
142 |
|
143 |
// Private Functions
|
144 |
void GraphManager::reset_v_id_vertex_map() {
|
145 |
v_id_vertex_map_ = NameVertexMap(); |
146 |
BGL_FORALL_VERTICES(v, g_, Graph) { |
147 |
string id = g_[v].id;
|
148 |
v_id_vertex_map_[id] = v; |
149 |
} |
150 |
} |
151 |
|
152 |
void GraphManager::reset_v_id_index_map() {
|
153 |
v_id_index_map_ = NameToIntMap(); |
154 |
BGL_FORALL_VERTICES(v, g_, Graph) { |
155 |
int index = boost::get(v_index_pmap_, v);
|
156 |
string name = g_[v].id;
|
157 |
v_id_index_map_[name] = index; |
158 |
} |
159 |
} |
160 |
|
161 |
void GraphManager::reset_v_index_pmap() {
|
162 |
v_index_std_map_ = VertexIndexStdMap(); |
163 |
v_index_pmap_ = VertexIndexPMap(v_index_std_map_); |
164 |
int i = 0; |
165 |
BGL_FORALL_VERTICES(v, g_, Graph) { |
166 |
boost::put(v_index_pmap_, v, i); |
167 |
++i; |
168 |
} |
169 |
} |
170 |
|
171 |
void GraphManager::reset_e_index_pmap() {
|
172 |
e_index_std_map_ = EdgeIndexStdMap(); |
173 |
e_index_pmap_ = EdgeIndexPMap(e_index_std_map_); |
174 |
int i = 0; |
175 |
BGL_FORALL_EDGES(e, g_, Graph) { |
176 |
boost::put(e_index_pmap_, e, i); |
177 |
++i; |
178 |
} |
179 |
} |
180 |
|
181 |
void GraphManager::update_v_index_pmap(Vertex new_vertex) {
|
182 |
// NOTE: this function might not perform correctly for vecS
|
183 |
int index = boost::num_vertices(g_);
|
184 |
v_index_std_map_[new_vertex] = index - 1;
|
185 |
} |
186 |
|
187 |
void GraphManager::update_e_index_pmap(Edge new_edge) {
|
188 |
int index = boost::num_edges(g_);
|
189 |
e_index_std_map_[new_edge] = index - 1;
|
190 |
} |