root / custompackages / graph-parser / src / graph_manager.cpp @ d5b7a27f
History | View | Annotate | Download (4.94 KB)
1 | cb770240 | Quynh PX Nguyen | #include "graph_manager.h" |
---|---|---|---|
2 | |||
3 | 437fd680 | Quynh PX Nguyen | // CONSTRUCTOR
|
4 | d5b7a27f | Quynh PX Nguyen | GraphManager::GraphManager(bool weighted_graph) : weighted_graph_(weighted_graph) {
|
5 | 437fd680 | Quynh PX Nguyen | v_index_pmap_ = VertexIndexPMap(v_index_std_map_); |
6 | e_index_pmap_ = EdgeIndexPMap(e_index_std_map_); |
||
7 | cb770240 | Quynh PX Nguyen | } |
8 | |||
9 | GraphManager::GraphManager(const GraphManager& other) {
|
||
10 | d5b7a27f | Quynh PX Nguyen | // cout << "\n*******COPY CONSTRUCTOR******\n";
|
11 | cb770240 | Quynh PX Nguyen | g_ = other.g_; |
12 | d5b7a27f | Quynh PX Nguyen | weighted_graph_ = other.weighted_graph_; |
13 | 437fd680 | Quynh PX Nguyen | ResetVerticesAndEdgesIndexMap(); |
14 | cb770240 | Quynh PX Nguyen | } |
15 | |||
16 | d5b7a27f | Quynh PX Nguyen | GraphManager& GraphManager::operator=(const GraphManager& rhs) { |
17 | // cout << "\n*******ASSIGNMENT OPERATOR******\n";
|
||
18 | cb770240 | Quynh PX Nguyen | g_ = rhs.g_; |
19 | d5b7a27f | Quynh PX Nguyen | weighted_graph_ = rhs.weighted_graph_; |
20 | 437fd680 | Quynh PX Nguyen | ResetVerticesAndEdgesIndexMap(); |
21 | cb770240 | Quynh PX Nguyen | |
22 | return *this; |
||
23 | } |
||
24 | |||
25 | 437fd680 | Quynh PX Nguyen | // 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 | d5b7a27f | Quynh PX Nguyen | bool GraphManager::weighted_graph() const { |
38 | return weighted_graph_;
|
||
39 | } |
||
40 | |||
41 | 437fd680 | Quynh PX Nguyen | // UPDATE GRAPHS
|
42 | cb770240 | Quynh PX Nguyen | 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 | 162e1bda | Quynh PX Nguyen | 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 | cb770240 | Quynh PX Nguyen | } |
74 | 437fd680 | Quynh PX Nguyen | } |
75 | cb770240 | Quynh PX Nguyen | |
76 | 437fd680 | Quynh PX Nguyen | 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 | cb770240 | Quynh PX Nguyen | } |
84 | |||
85 | 437fd680 | Quynh PX Nguyen | // HELPERS
|
86 | cb770240 | Quynh PX Nguyen | 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 | 437fd680 | Quynh PX Nguyen | int GraphManager::get_index_from_id(string s) { |
102 | if (vertex_existed(s)) {
|
||
103 | return v_id_index_map_[s];
|
||
104 | cb770240 | Quynh PX Nguyen | } |
105 | 437fd680 | Quynh PX Nguyen | else {
|
106 | throw std::runtime_error("Vertex with id " + s + " is not found\n"); |
||
107 | cb770240 | Quynh PX Nguyen | } |
108 | } |
||
109 | |||
110 | 437fd680 | Quynh PX Nguyen | // OUTPUTTING THE RESULT
|
111 | void GraphManager::print() {
|
||
112 | cout << "\nGraph Manager:\n";
|
||
113 | outops::operator<<(cout, g_);
|
||
114 | e263e3c7 | Quynh PX Nguyen | |
115 | cout << "Is graph connected?\n";
|
||
116 | bool connected = graphext::is_connected(g_, v_index_pmap_);
|
||
117 | cout << "Connected = " << connected << endl;
|
||
118 | cb770240 | Quynh PX Nguyen | |
119 | d5b7a27f | Quynh PX Nguyen | cout << "Is this a weighted graph?\n";
|
120 | cout << "is weighted = " << weighted_graph_ << endl;
|
||
121 | |||
122 | 437fd680 | Quynh PX Nguyen | cout << "v_id_index_map:\n";
|
123 | outops::operator<< <int>(cout, v_id_index_map_); |
||
124 | cb770240 | Quynh PX Nguyen | } |
125 | |||
126 | 437fd680 | Quynh PX Nguyen | void GraphManager::print_v_index_pmap() {
|
127 | graphext::print_v_index_pmap(g_, v_index_pmap_); |
||
128 | cb770240 | Quynh PX Nguyen | } |
129 | |||
130 | 437fd680 | Quynh PX Nguyen | void GraphManager::print_e_index_pmap() {
|
131 | graphext::print_e_index_pmap(g_, e_index_pmap_); |
||
132 | cb770240 | Quynh PX Nguyen | } |
133 | |||
134 | 437fd680 | Quynh PX Nguyen | 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 | cb770240 | Quynh PX Nguyen | } |
142 | |||
143 | 437fd680 | Quynh PX Nguyen | // Private Functions
|
144 | cb770240 | Quynh PX Nguyen | 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 | 437fd680 | Quynh PX Nguyen | 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 | cb770240 | Quynh PX Nguyen | v_index_std_map_ = VertexIndexStdMap(); |
163 | 437fd680 | Quynh PX Nguyen | v_index_pmap_ = VertexIndexPMap(v_index_std_map_); |
164 | cb770240 | Quynh PX Nguyen | int i = 0; |
165 | BGL_FORALL_VERTICES(v, g_, Graph) { |
||
166 | 437fd680 | Quynh PX Nguyen | boost::put(v_index_pmap_, v, i); |
167 | cb770240 | Quynh PX Nguyen | ++i; |
168 | } |
||
169 | } |
||
170 | |||
171 | 437fd680 | Quynh PX Nguyen | void GraphManager::reset_e_index_pmap() {
|
172 | cb770240 | Quynh PX Nguyen | e_index_std_map_ = EdgeIndexStdMap(); |
173 | 437fd680 | Quynh PX Nguyen | e_index_pmap_ = EdgeIndexPMap(e_index_std_map_); |
174 | cb770240 | Quynh PX Nguyen | int i = 0; |
175 | BGL_FORALL_EDGES(e, g_, Graph) { |
||
176 | 437fd680 | Quynh PX Nguyen | boost::put(e_index_pmap_, e, i); |
177 | cb770240 | Quynh PX Nguyen | ++i; |
178 | } |
||
179 | } |
||
180 | |||
181 | 437fd680 | Quynh PX Nguyen | void GraphManager::update_v_index_pmap(Vertex new_vertex) {
|
182 | // NOTE: this function might not perform correctly for vecS
|
||
183 | cb770240 | Quynh PX Nguyen | int index = boost::num_vertices(g_);
|
184 | v_index_std_map_[new_vertex] = index - 1;
|
||
185 | } |
||
186 | |||
187 | 437fd680 | Quynh PX Nguyen | void GraphManager::update_e_index_pmap(Edge new_edge) {
|
188 | cb770240 | Quynh PX Nguyen | int index = boost::num_edges(g_);
|
189 | e_index_std_map_[new_edge] = index - 1;
|
||
190 | } |