Revision 437fd680 custompackages/graph-parser/src/graph_manager.cpp

View differences:

custompackages/graph-parser/src/graph_manager.cpp
1 1
#include "graph_manager.h"
2 2

  
3
// CONSTRUCTOR
3 4
GraphManager::GraphManager() {
4
    v_index_map_ = VertexIndexMap(v_index_std_map_);
5
    e_index_map_ = EdgeIndexMap(e_index_std_map_);
5
    v_index_pmap_ = VertexIndexPMap(v_index_std_map_);
6
    e_index_pmap_ = EdgeIndexPMap(e_index_std_map_);
6 7
}
7 8

  
8 9
GraphManager::GraphManager(const GraphManager& other) {
9 10
    cout << "\n\n*******COPY CONSTRUCTOR******\n\n";
10 11
    g_ = other.g_;
11
    reset_v_id_vertex_map();
12
    reset_v_index_map();
13
    reset_e_index_map();
12
    ResetVerticesAndEdgesIndexMap();
14 13
}
15 14

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

  
23 20
    return *this;
24 21
}
25 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
26 36
void GraphManager::AddEdge(VertexProperties vp1, VertexProperties vp2, EdgeProperties ep) {
27 37
    // cout << "add edge GM " << vp1.label << " - " << vp2.label << endl;
28 38

  
......
37 47
    catch (exception& e) {
38 48
        v1 = boost::add_vertex(vp1, g_);
39 49
        v_id_vertex_map_[s1] = v1;
40
        update_v_index_map(v1);
50
        update_v_index_pmap(v1);
41 51
    }
42 52
    try {
43 53
        v2 = get_vertex_from_id(s2);
......
45 55
    catch (exception& e) {
46 56
        v2 = boost::add_vertex(vp2, g_);
47 57
        v_id_vertex_map_[s2] = v2;
48
        update_v_index_map(v2);
58
        update_v_index_pmap(v2);
49 59
    }
50 60

  
51 61
    Edge e;
52 62
    bool inserted;
53 63
    boost::tie(e, inserted) = boost::add_edge(v1, v2, ep, g_);
54
    update_e_index_map(e);
55
    // print_e_index_map();
64
    update_e_index_pmap(e);
65
}
56 66

  
57
    // Print the VertexIndexMap and EdgeIndexMap
58
    // print_v_index_map();
59
    // graphext::print_v_index_map(g_, v_index_map_);
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();
60 74
}
61 75

  
76
// HELPERS
62 77
bool GraphManager::vertex_existed(string s) {
63 78
    std::map<std::string, Vertex>::iterator it;
64 79
    it = v_id_vertex_map_.find(s);
......
74 89
    }
75 90
}
76 91

  
77
void GraphManager::ResetVerticesAndEdgesIndexMap() {
78
    v_index_std_map_.erase(v_index_std_map_.begin(), v_index_std_map_.end());
79
    e_index_std_map_.erase(e_index_std_map_.begin(), e_index_std_map_.end());
80
    int i;
81

  
82
    // Reset VertexIndexMap
83
    i = 0;
84
    BGL_FORALL_VERTICES(v, g_, Graph) {
85
        boost::put(v_index_map_, v, i);
86
        ++i;
92
int GraphManager::get_index_from_id(string s) {
93
    if (vertex_existed(s)) {
94
        return v_id_index_map_[s];
87 95
    }
88

  
89
    // Reset EdgeIndexMap
90
    i = 0;
91
    BGL_FORALL_EDGES(e, g_, Graph) {
92
        boost::put(e_index_map_, e, i);
93
        ++i;
96
    else {
97
        throw std::runtime_error("Vertex with id " + s + " is not found\n");
94 98
    }
95 99
}
96 100

  
97
std::ostream& operator<<(std::ostream& os, const GraphManager& gm) {
98
    cout << "Graph Manager: " << endl;
99
    outops::operator<<(cout, gm.g_);
100
    return os;
101
}
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();
102 107

  
103
void GraphManager::print_v_index_map() {
104
    graphext::print_v_index_map(g_, v_index_map_);
108
    cout << "v_id_index_map:\n";
109
    outops::operator<< <int>(cout, v_id_index_map_);
105 110
}
106 111

  
107
void GraphManager::print_e_index_map() {
108
    std::list<std::string> outputs;
109
    BGL_FORALL_EDGES_T(e, g_, Graph) {
110
        int index = boost::get(e_index_map_, e);
111
        std::string source_id = g_[boost::source(e, g_)].id;
112
        std::string target_id = g_[boost::target(e, g_)].id;
113
        outputs.push_back("edge (" + source_id + ", " + target_id + ")" + ": " + std::to_string(index));
114
    }
115

  
116
    using namespace boost::spirit::karma;
117
    cout << "Edge Index Map:\n";
118
    cout << format("[\n  " << (auto_ % "\n  ") << "\n]\n", outputs);
112
void GraphManager::print_v_index_pmap() {
113
    graphext::print_v_index_pmap(g_, v_index_pmap_);
119 114
}
120 115

  
121
const VertexIndexMap& GraphManager::v_index_map() const {
122
    return v_index_map_;
116
void GraphManager::print_e_index_pmap() {
117
    graphext::print_e_index_pmap(g_, e_index_pmap_);
123 118
}
124 119

  
125
const EdgeIndexMap& GraphManager::e_index_map() const {
126
    return e_index_map_;
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 127
}
128 128

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

  
137
void GraphManager::reset_v_index_map() {
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() {
138 148
    v_index_std_map_ = VertexIndexStdMap();
139
    v_index_map_ = VertexIndexMap(v_index_std_map_);
149
    v_index_pmap_ = VertexIndexPMap(v_index_std_map_);
140 150
    int i = 0;
141 151
    BGL_FORALL_VERTICES(v, g_, Graph) {
142
        boost::put(v_index_map_, v, i);
152
        boost::put(v_index_pmap_, v, i);
143 153
        ++i;
144 154
    }
145 155
}
146 156

  
147
void GraphManager::reset_e_index_map() {
157
void GraphManager::reset_e_index_pmap() {
148 158
    e_index_std_map_ = EdgeIndexStdMap();
149
    e_index_map_ = EdgeIndexMap(e_index_std_map_);
159
    e_index_pmap_ = EdgeIndexPMap(e_index_std_map_);
150 160
    int i = 0;
151 161
    BGL_FORALL_EDGES(e, g_, Graph) {
152
        boost::put(e_index_map_, e, i);
162
        boost::put(e_index_pmap_, e, i);
153 163
        ++i;
154 164
    }
155 165
}
156 166

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

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

Also available in: Unified diff