Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (4.51 KB)

1
//
2
// Created by quynh on 12/15/15.
3
//
4

    
5
#include "utility.h"
6
using namespace boost;
7

    
8
/* outops namespace */
9
namespace outops {
10
    std::ostream& operator<<(std::ostream& os, const Graph& g) {
11
        os <<   "Graph has: \n"
12
                "---------- " << boost::num_vertices(g) << " vertices\n"
13
                "---------- " << boost::num_edges(g) << " edges\n";
14

    
15
        std::vector<std::string> verticesVec;
16
        BGL_FORALL_VERTICES(v, g, Graph) verticesVec.push_back(g[v].id);
17

    
18
        std::vector<std::string> edgesVec;
19
        std::vector<double> costsVec;
20
        BGL_FORALL_EDGES(e, g, Graph) {
21
            std::string s = "(" + g[e.m_source].id + ", " + g[e.m_target].id + ") - " + std::to_string(g[e].cost);
22
            edgesVec.push_back(s);
23
            costsVec.push_back(g[e].cost);
24
        }
25

    
26
        using namespace boost::spirit::karma;
27
        os <<   "Vertices:\n"
28
                "  ";
29
        os << format("(" << auto_ % ", " << ") ", verticesVec);
30
        os << "\n";
31

    
32
        os <<   "Edges:\n";
33
        os << format("  " << (auto_ % "\n  ") << eol, edgesVec);
34
        os << "\n";
35

    
36
        return os;
37
    }
38

    
39
    std::ostream& operator<<(std::ostream& os, std::pair<const Graph&, const VertexIndexPMap&> p) {
40
        // ERROR: wrong output.
41
        // I think it's because of copy constructor.
42
        // Check out shell_output/w14
43

    
44
        // Calling example:
45
        // outops::operator<<(cout, std::pair<const Graph&, const VertexIndexPMap&>(g_, v_index_pmap_));
46
        Graph g = p.first;
47
        VertexIndexPMap v_index_map = p.second;
48

    
49
        std::list<std::string> outputs;
50
        BGL_FORALL_VERTICES_T(v, g, Graph) {
51
            int index = boost::get(v_index_map, v);
52
            cout << g[v].id << ": " << v << endl;
53
            string vertex_id = g[v].id;
54
            outputs.push_back(vertex_id + ": " + std::to_string(index));
55
        }
56

    
57
        using namespace boost::spirit::karma;
58
        os << "Vertex Index Map:\n";
59
        os << "[\n";
60
        os << format("  " << (auto_ % "\n  ") << "]\n", outputs);
61

    
62
        return os;
63
    }
64

    
65
    std::ostream& operator<<(std::ostream& os, const vector< vector< int> >& data) {
66
        cout << "cout << vector<vector<int> >\n";
67
        int row_size = data.size();
68
        int col_size = 0;
69
        if (row_size > 0) {
70
            col_size = data[0].size();
71
        }
72
        for (int i = 0; i < row_size; ++i) {
73
            for (int j = 0; j < col_size; ++j) {
74
                os << data[i][j] << " ";
75
            }
76
            os << endl;
77
        }
78
        return os;
79
    }
80
}
81

    
82
/* graphext namespace */
83
namespace graphext {
84
    void id_of_all_vertices(const Graph& g, std::set<std::string>& r) {
85
        BGL_FORALL_VERTICES_T(v, g, Graph) {
86
            r.insert(g[v].id);
87
        }
88
    }
89

    
90
    void print_v_index_std_map(const Graph& g, const VertexIndexStdMap& v_index_std_map) {
91
        std::list<std::string> outputs;
92

    
93
        VertexIndexStdMap::const_iterator iter;
94
        for (iter = v_index_std_map.begin(); iter != v_index_std_map.end(); ++iter) {
95
            outputs.push_back(std::to_string(iter->second));
96
            // outputs.push_back(std::to_string(&(iter->first)) + ": " + std::to_string(iter->second));
97
        }
98

    
99
        using namespace boost::spirit::karma;
100
        cout << "Vertex Index Std Map:\n";
101
        cout << format("[\n  " << (auto_ % "\n  ") << "\n]\n", outputs);
102
    }
103

    
104
    void print_v_index_pmap(const Graph& g, const VertexIndexPMap& v_index_pmap) {
105
        std::list<std::string> outputs;
106
        BGL_FORALL_VERTICES_T(v, g, Graph) {
107
            int index = boost::get(v_index_pmap, v);
108
            std::string vertex_id = g[v].id;
109
            // Uncomment to print the address of vertex v
110
            // cout << v << endl;
111
            outputs.push_back(vertex_id + ": " + std::to_string(index));
112
        }
113

    
114
        using namespace boost::spirit::karma;
115
        cout << "Vertex Index Map:\n";
116
        cout << format("[\n  " << (auto_ % "\n  ") << "\n]\n", outputs);
117
    }
118

    
119
    void print_e_index_pmap(const Graph& g, const EdgeIndexPMap& e_index_pmap) {
120
        std::list<std::string> outputs;
121
        BGL_FORALL_EDGES_T(e, g, Graph) {
122
            int index = boost::get(e_index_pmap, e);
123
            std::string source_id = g[boost::source(e, g)].id;
124
            std::string target_id = g[boost::target(e, g)].id;
125
            outputs.push_back("edge (" + source_id + ", " + target_id + ")" + ": " + std::to_string(index));
126
        }
127

    
128
        using namespace boost::spirit::karma;
129
        cout << "Edge Index Map:\n";
130
        cout << format("[\n  " << (auto_ % "\n  ") << "\n]\n", outputs);
131
    }
132
}