Statistics
| Branch: | Revision:

root / custompackages / graph-parser / src / sub_component.cpp @ efed924d

History | View | Annotate | Download (2.23 KB)

1
//
2
// Created by quynh on 1/9/16.
3
//
4

    
5
#include "sub_component.h"
6

    
7

    
8
SubComponent::SubComponent() {
9
    // do nothing
10
}
11

    
12
SubComponent::SubComponent(StringSet art_points, Graph sub_graph) : art_points_(art_points), sub_graph_(sub_graph) {
13
// SubComponent::SubComponent(VertexVec aart_points, Graph asubGraph) {
14
    // art_points = aart_points;
15
    // subGraph = asubGraph;
16
    // Deep copy for art_points
17
    // cout << "ABC " << endl;
18
    // for (VertexVecIter vi = aart_points.begin(); vi != aart_points.end(); ++vi) {
19
    //     Vertex v = Vertex(*vi);
20
    //     this->art_points.insert(this->art_points.end(), v);
21
    //     cout << "   asubGraph " << asubGraph[*vi].name << endl;
22
    //     cout << "    subGraph " << subGraph[*vi].name << endl;
23
    // }
24

    
25

    
26
    // init();
27
}
28

    
29
StringSet SubComponent::art_points() const {
30
    return art_points_;
31
}
32

    
33
void SubComponent::set_art_points(StringSet& art_points) {
34
    art_points_ = art_points;
35
}
36

    
37
int SubComponent::num_vertices() {
38
    return boost::num_vertices(sub_graph_);
39
}
40

    
41
void SubComponent::AddEdge(Router r1, Router r2, Link l) {
42
    cout << "add edge " << r1.label << " - " << r2.label << endl;
43

    
44
    string s1 = r1.id;
45
    string s2 = r2.id;
46
    Vertex v1;
47
    Vertex v2;
48

    
49
    try {
50
        v1 = get_vertex_from_id(s1);
51
    }
52
    catch (exception& e) {
53
        v1 = boost::add_vertex(r1, sub_graph_);
54
        name_vertex_map_[s1] = v1;
55
    }
56
    try {
57
        v2 = get_vertex_from_id(s2);
58
    }
59
    catch (exception& e) {
60
        v2 = boost::add_vertex(r2, sub_graph_);
61
        name_vertex_map_[s2] = v2;
62
    }
63
    boost::add_edge(v1, v2, l, sub_graph_);
64
}
65

    
66
bool SubComponent::vertex_existed(string s) {
67
    std::map<std::string, Vertex>::iterator it;
68
    it = name_vertex_map_.find(s);
69
    return (it != name_vertex_map_.end());
70
}
71

    
72
const Vertex& SubComponent::get_vertex_from_id(string s) {
73
    if (vertex_existed(s)) {
74
        return name_vertex_map_[s];
75
    }
76
    else {
77
        throw std::runtime_error("Vertex not found\n");
78
    }
79
}
80

    
81
std::ostream& operator<<(std::ostream& os, const SubComponent& sc) {
82
    cout << "Sub-component" << endl;
83
    outops::operator<<(cout, sc.sub_graph());
84
    outops::operator<<(cout, sc.art_points());
85
    return os;
86
}
87

    
88
Graph const& SubComponent::sub_graph() const {
89
    return sub_graph_;
90
}
91