Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (4.81 KB)

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

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

    
8
void printGraph(Graph &g) {
9
    typename boost::graph_traits<Graph>::out_edge_iterator out_i, out_ie;
10
    Viter v_i, v_ie;
11

    
12
    Vertex v;
13
    Edge e;
14

    
15
    boost::tie(v_i, v_ie) = boost::vertices(g);
16
    for (v_i; v_i != v_ie; ++v_i) {
17
        v = *v_i;
18
        boost::tie(out_i, out_ie) = boost::out_edges(v, g);
19

    
20
        for (out_i; out_i != out_ie; ++out_i) {
21
            e = *out_i;
22
            Vertex src = boost::source(e, g);
23
            Vertex targ = boost::target(e, g);
24
            std::cout << "(" << g[src].id << ","
25
            << g[targ].id << ") ";
26
        }
27
        cout << endl;
28
    }
29
}
30

    
31
/* outops namespace */
32
namespace outops {
33
    std::ostream& operator<<(std::ostream& os, const Graph& g)
34
    {
35
        os <<   "Graph has: \n"
36
                "---------- " << boost::num_vertices(g) << " vertices\n"
37
                "---------- " << boost::num_edges(g) << " edges\n";
38

    
39
        std::set<std::string> verticesSet;
40
        BGL_FORALL_VERTICES_T(v, g, Graph) verticesSet.insert(g[v].id);
41

    
42
        std::vector<std::string> edgesVec;
43
        std::vector<double> costsVec;
44
        BGL_FORALL_EDGES_T(e, g, Graph) {
45
            std::string s = "(" + g[e.m_source].id + ", " + g[e.m_target].id + ") - " + std::to_string(g[e].cost);
46
            edgesVec.push_back(s);
47
            costsVec.push_back(g[e].cost);
48
        }
49

    
50
        using namespace boost::spirit::karma;
51
        os <<   "Vertices:\n"
52
                "  ";
53
        os << format("(" << auto_ % ", " << ") ", verticesSet);
54
        os << "\n";
55

    
56
        os <<   "Edges:\n";
57
        os << format("  " << (auto_ % "\n  ") << eol, edgesVec);
58
        os << "\n";
59

    
60
        return os;
61
    }
62

    
63
    std::ostream& operator<<(std::ostream& os, std::pair<const Graph&, const VertexIndexMap&> p) {
64
        // ERROR: wrong output.
65
        // I think it's because of copy constructor.
66
        // Check out shell_output/w14
67

    
68
        // Calling example:
69
        // outops::operator<<(cout, std::pair<const Graph&, const VertexIndexMap&>(g_, v_index_map_));
70
        Graph g = p.first;
71
        VertexIndexMap v_index_map = p.second;
72

    
73
        std::list<std::string> outputs;
74
        BGL_FORALL_VERTICES_T(v, g, Graph) {
75
            int index = boost::get(v_index_map, v);
76
            cout << g[v].id << ": " << v << endl;
77
            string vertex_id = g[v].id;
78
            outputs.push_back(vertex_id + ": " + std::to_string(index));
79
        }
80

    
81
        using namespace boost::spirit::karma;
82
        os << "Vertex Index Map:\n";
83
        os << "[\n";
84
        os << format("  " << (auto_ % "\n  ") << "]\n", outputs);
85

    
86
        return os;
87
    }
88

    
89
    std::ostream& operator<<(std::ostream& os, const std::map<string, int>& m)
90
    {
91
        // similar to printhelper::for_map()
92
        os << "cout << std::map\n";
93
        std::map<string, int>::const_iterator iter;
94
        for (iter = m.begin(); iter != m.end(); ++iter) {
95
            os << (*iter).first << ": " << (*iter).second << endl;
96
        }
97
        os << endl;
98

    
99
        return os;
100
    }
101

    
102
    std::ostream& operator<<(std::ostream& os, const vector< vector< int> >& data) {
103
        cout << "cout << vector<vector<int> >\n";
104
        int row_size = data.size();
105
        int col_size = 0;
106
        if (row_size > 0) {
107
            col_size = data[0].size();
108
        }
109
        for (int i = 0; i < row_size; ++i) {
110
            for (int j = 0; j < col_size; ++j) {
111
                os << data[i][j] << " ";
112
            }
113
            os << endl;
114
        }
115
        return os;
116
    }
117
}
118

    
119
/* graphext namespace */
120
namespace graphext {
121
    void id_of_all_vertices(const Graph& g, std::set<std::string>& r) {
122
        BGL_FORALL_VERTICES_T(v, g, Graph) {
123
            r.insert(g[v].id);
124
        }
125
    }
126

    
127
    void print_v_index_std_map(const Graph& g, const VertexIndexStdMap& v_index_std_map) {
128
        std::list<std::string> outputs;
129

    
130
        VertexIndexStdMap::const_iterator iter;
131
        for (iter = v_index_std_map.begin(); iter != v_index_std_map.end(); ++iter) {
132
            outputs.push_back(std::to_string(iter->second));
133
            // outputs.push_back(std::to_string(&(iter->first)) + ": " + std::to_string(iter->second));
134
        }
135

    
136
        using namespace boost::spirit::karma;
137
        cout << "Vertex Index Std Map:\n";
138
        cout << format("[\n  " << (auto_ % "\n  ") << "\n]\n", outputs);
139
    }
140

    
141
    void print_v_index_map(const Graph& g, const VertexIndexMap& v_index_map) {
142
        cout << "Vertex Index Map:\n";
143

    
144
        std::list<std::string> outputs;
145
        BGL_FORALL_VERTICES_T(v, g, Graph) {
146
            int index = boost::get(v_index_map, v);
147
            std::string vertex_id = g[v].id;
148
            cout << v << endl;
149
            outputs.push_back(vertex_id + ": " + std::to_string(index));
150
        }
151

    
152
        using namespace boost::spirit::karma;
153
        cout << format("[\n  " << (auto_ % "\n  ") << "\n]\n", outputs);
154
    }
155
}