Statistics
| Branch: | Revision:

root / custompackages / graph-parser / src / graph_manager.cpp @ 162e1bda

History | View | Annotate | Download (4.53 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

    
42
    if (s1 != s2) { // do not add self-loop into the graph
43
        Vertex v1;
44
        Vertex v2;
45

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

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

    
70
void GraphManager::ResetVerticesAndEdgesIndexMap() {
71
    reset_v_index_pmap();
72
    reset_v_id_vertex_map();
73
    // The line below must be called after reset_v_index_pmap()
74
    reset_v_id_index_map();
75

    
76
    reset_e_index_pmap();
77
}
78

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

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

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

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

    
111
    cout << "v_id_index_map:\n";
112
    outops::operator<< <int>(cout, v_id_index_map_);
113
}
114

    
115
void GraphManager::print_v_index_pmap() {
116
    graphext::print_v_index_pmap(g_, v_index_pmap_);
117
}
118

    
119
void GraphManager::print_e_index_pmap() {
120
    graphext::print_e_index_pmap(g_, e_index_pmap_);
121
}
122

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

    
127
    cout << "v_id_index_map:\n";
128
    outops::operator<< <int>(cout, gm.v_id_index_map());
129
    return os;
130
}
131

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

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

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

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

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

    
176
void GraphManager::update_e_index_pmap(Edge new_edge) {
177
    int index = boost::num_edges(g_);
178
    e_index_std_map_[new_edge] = index - 1;
179
}