Statistics
| Branch: | Revision:

root / custompackages / graph-parser / src / graph_manager.cpp @ cb770240

History | View | Annotate | Download (4.26 KB)

1
#include "graph_manager.h"
2

    
3
GraphManager::GraphManager() {
4
    v_index_map_ = VertexIndexMap(v_index_std_map_);
5
    e_index_map_ = EdgeIndexMap(e_index_std_map_);
6
}
7

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

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

    
23
    return *this;
24
}
25

    
26
void GraphManager::AddEdge(VertexProperties vp1, VertexProperties vp2, EdgeProperties ep) {
27
    // cout << "add edge GM " << vp1.label << " - " << vp2.label << endl;
28

    
29
    string s1 = vp1.id;
30
    string s2 = vp2.id;
31
    Vertex v1;
32
    Vertex v2;
33

    
34
    try {
35
        v1 = get_vertex_from_id(s1);
36
    }
37
    catch (exception& e) {
38
        v1 = boost::add_vertex(vp1, g_);
39
        v_id_vertex_map_[s1] = v1;
40
        update_v_index_map(v1);
41
    }
42
    try {
43
        v2 = get_vertex_from_id(s2);
44
    }
45
    catch (exception& e) {
46
        v2 = boost::add_vertex(vp2, g_);
47
        v_id_vertex_map_[s2] = v2;
48
        update_v_index_map(v2);
49
    }
50

    
51
    Edge e;
52
    bool inserted;
53
    boost::tie(e, inserted) = boost::add_edge(v1, v2, ep, g_);
54
    update_e_index_map(e);
55
    // print_e_index_map();
56

    
57
    // Print the VertexIndexMap and EdgeIndexMap
58
    // print_v_index_map();
59
    // graphext::print_v_index_map(g_, v_index_map_);
60
}
61

    
62
bool GraphManager::vertex_existed(string s) {
63
    std::map<std::string, Vertex>::iterator it;
64
    it = v_id_vertex_map_.find(s);
65
    return (it != v_id_vertex_map_.end());
66
}
67

    
68
const Vertex& GraphManager::get_vertex_from_id(string s) {
69
    if (vertex_existed(s)) {
70
        return v_id_vertex_map_[s];
71
    }
72
    else {
73
        throw std::runtime_error("Vertex not found\n");
74
    }
75
}
76

    
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;
87
    }
88

    
89
    // Reset EdgeIndexMap
90
    i = 0;
91
    BGL_FORALL_EDGES(e, g_, Graph) {
92
        boost::put(e_index_map_, e, i);
93
        ++i;
94
    }
95
}
96

    
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
}
102

    
103
void GraphManager::print_v_index_map() {
104
    graphext::print_v_index_map(g_, v_index_map_);
105
}
106

    
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);
119
}
120

    
121
const VertexIndexMap& GraphManager::v_index_map() const {
122
    return v_index_map_;
123
}
124

    
125
const EdgeIndexMap& GraphManager::e_index_map() const {
126
    return e_index_map_;
127
}
128

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

    
137
void GraphManager::reset_v_index_map() {
138
    v_index_std_map_ = VertexIndexStdMap();
139
    v_index_map_ = VertexIndexMap(v_index_std_map_);
140
    int i = 0;
141
    BGL_FORALL_VERTICES(v, g_, Graph) {
142
        boost::put(v_index_map_, v, i);
143
        ++i;
144
    }
145
}
146

    
147
void GraphManager::reset_e_index_map() {
148
    e_index_std_map_ = EdgeIndexStdMap();
149
    e_index_map_ = EdgeIndexMap(e_index_std_map_);
150
    int i = 0;
151
    BGL_FORALL_EDGES(e, g_, Graph) {
152
        boost::put(e_index_map_, e, i);
153
        ++i;
154
    }
155
}
156

    
157
void GraphManager::update_v_index_map(Vertex new_vertex) {
158
    int index = boost::num_vertices(g_);
159
    v_index_std_map_[new_vertex] = index - 1;
160
}
161

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