Statistics
| Branch: | Revision:

root / custompackages / graph-parser / src / graph_manager.cpp @ 437fd680

History | View | Annotate | Download (4.38 KB)

1
#include "graph_manager.h"
2

    
3
// CONSTRUCTOR
4
GraphManager::GraphManager() {
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\n*******COPY CONSTRUCTOR******\n\n";
11
    g_ = other.g_;
12
    ResetVerticesAndEdgesIndexMap();
13
}
14

    
15
GraphManager& GraphManager::operator=(GraphManager& rhs) {
16
    cout << "\n\n*******ASSIGNMENT OPERATOR******\n\n";
17
    g_ = rhs.g_;
18
    ResetVerticesAndEdgesIndexMap();
19

    
20
    return *this;
21
}
22

    
23
// GETTERS
24
const VertexIndexPMap& GraphManager::v_index_pmap() const {
25
    return v_index_pmap_;
26
}
27

    
28
const EdgeIndexPMap& GraphManager::e_index_pmap() const {
29
    return e_index_pmap_;
30
}
31
NameToIntMap const& GraphManager::v_id_index_map() const {
32
    return v_id_index_map_;
33
}
34

    
35
// UPDATE GRAPHS
36
void GraphManager::AddEdge(VertexProperties vp1, VertexProperties vp2, EdgeProperties ep) {
37
    // cout << "add edge GM " << vp1.label << " - " << vp2.label << endl;
38

    
39
    string s1 = vp1.id;
40
    string s2 = vp2.id;
41
    Vertex v1;
42
    Vertex v2;
43

    
44
    try {
45
        v1 = get_vertex_from_id(s1);
46
    }
47
    catch (exception& e) {
48
        v1 = boost::add_vertex(vp1, g_);
49
        v_id_vertex_map_[s1] = v1;
50
        update_v_index_pmap(v1);
51
    }
52
    try {
53
        v2 = get_vertex_from_id(s2);
54
    }
55
    catch (exception& e) {
56
        v2 = boost::add_vertex(vp2, g_);
57
        v_id_vertex_map_[s2] = v2;
58
        update_v_index_pmap(v2);
59
    }
60

    
61
    Edge e;
62
    bool inserted;
63
    boost::tie(e, inserted) = boost::add_edge(v1, v2, ep, g_);
64
    update_e_index_pmap(e);
65
}
66

    
67
void GraphManager::ResetVerticesAndEdgesIndexMap() {
68
    reset_v_index_pmap();
69
    reset_v_id_vertex_map();
70
    // The line below must be called after reset_v_index_pmap()
71
    reset_v_id_index_map();
72

    
73
    reset_e_index_pmap();
74
}
75

    
76
// HELPERS
77
bool GraphManager::vertex_existed(string s) {
78
    std::map<std::string, Vertex>::iterator it;
79
    it = v_id_vertex_map_.find(s);
80
    return (it != v_id_vertex_map_.end());
81
}
82

    
83
const Vertex& GraphManager::get_vertex_from_id(string s) {
84
    if (vertex_existed(s)) {
85
        return v_id_vertex_map_[s];
86
    }
87
    else {
88
        throw std::runtime_error("Vertex not found\n");
89
    }
90
}
91

    
92
int GraphManager::get_index_from_id(string s) {
93
    if (vertex_existed(s)) {
94
        return v_id_index_map_[s];
95
    }
96
    else {
97
        throw std::runtime_error("Vertex with id " + s + " is not found\n");
98
    }
99
}
100

    
101
// OUTPUTTING THE RESULT
102
void GraphManager::print() {
103
    cout << "\nGraph Manager:\n";
104
    outops::operator<<(cout, g_);
105
    print_v_index_pmap();
106
    print_e_index_pmap();
107

    
108
    cout << "v_id_index_map:\n";
109
    outops::operator<< <int>(cout, v_id_index_map_);
110
}
111

    
112
void GraphManager::print_v_index_pmap() {
113
    graphext::print_v_index_pmap(g_, v_index_pmap_);
114
}
115

    
116
void GraphManager::print_e_index_pmap() {
117
    graphext::print_e_index_pmap(g_, e_index_pmap_);
118
}
119

    
120
std::ostream& operator<<(std::ostream& os, const GraphManager& gm) {
121
    cout << "\nGraph Manager: " << endl;
122
    outops::operator<<(cout, gm.g_);
123

    
124
    cout << "v_id_index_map:\n";
125
    outops::operator<< <int>(cout, gm.v_id_index_map());
126
    return os;
127
}
128

    
129
// Private Functions
130
void GraphManager::reset_v_id_vertex_map() {
131
    v_id_vertex_map_ = NameVertexMap();
132
    BGL_FORALL_VERTICES(v, g_, Graph) {
133
        string id = g_[v].id;
134
        v_id_vertex_map_[id] = v;
135
    }
136
}
137

    
138
void GraphManager::reset_v_id_index_map() {
139
    v_id_index_map_ = NameToIntMap();
140
    BGL_FORALL_VERTICES(v, g_, Graph) {
141
        int index = boost::get(v_index_pmap_, v);
142
        string name = g_[v].id;
143
        v_id_index_map_[name] = index;
144
    }
145
}
146

    
147
void GraphManager::reset_v_index_pmap() {
148
    v_index_std_map_ = VertexIndexStdMap();
149
    v_index_pmap_ = VertexIndexPMap(v_index_std_map_);
150
    int i = 0;
151
    BGL_FORALL_VERTICES(v, g_, Graph) {
152
        boost::put(v_index_pmap_, v, i);
153
        ++i;
154
    }
155
}
156

    
157
void GraphManager::reset_e_index_pmap() {
158
    e_index_std_map_ = EdgeIndexStdMap();
159
    e_index_pmap_ = EdgeIndexPMap(e_index_std_map_);
160
    int i = 0;
161
    BGL_FORALL_EDGES(e, g_, Graph) {
162
        boost::put(e_index_pmap_, e, i);
163
        ++i;
164
    }
165
}
166

    
167
void GraphManager::update_v_index_pmap(Vertex new_vertex) {
168
    // NOTE: this function might not perform correctly for vecS
169
    int index = boost::num_vertices(g_);
170
    v_index_std_map_[new_vertex] = index - 1;
171
}
172

    
173
void GraphManager::update_e_index_pmap(Edge new_edge) {
174
    int index = boost::num_edges(g_);
175
    e_index_std_map_[new_edge] = index - 1;
176
}