Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (4.21 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::set<std::string> verticesSet;
16
        BGL_FORALL_VERTICES_T(v, g, Graph) verticesSet.insert(g[v].id);
17

    
18
        std::vector<std::string> edgesVec;
19
        std::vector<double> costsVec;
20
        BGL_FORALL_EDGES_T(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_ % ", " << ") ", verticesSet);
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 VertexIndexMap&> 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 VertexIndexMap&>(g_, v_index_map_));
46
        Graph g = p.first;
47
        VertexIndexMap 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 std::map<string, int>& m) {
66
        // similar to printhelper::for_map()
67
        os << "cout << std::map\n";
68
        std::map<string, int>::const_iterator iter;
69
        for (iter = m.begin(); iter != m.end(); ++iter) {
70
            os << (*iter).first << ": " << (*iter).second << endl;
71
        }
72
        os << endl;
73

    
74
        return os;
75
    }
76

    
77
    std::ostream& operator<<(std::ostream& os, const vector< vector< int> >& data) {
78
        cout << "cout << vector<vector<int> >\n";
79
        int row_size = data.size();
80
        int col_size = 0;
81
        if (row_size > 0) {
82
            col_size = data[0].size();
83
        }
84
        for (int i = 0; i < row_size; ++i) {
85
            for (int j = 0; j < col_size; ++j) {
86
                os << data[i][j] << " ";
87
            }
88
            os << endl;
89
        }
90
        return os;
91
    }
92
}
93

    
94
/* graphext namespace */
95
namespace graphext {
96
    void id_of_all_vertices(const Graph& g, std::set<std::string>& r) {
97
        BGL_FORALL_VERTICES_T(v, g, Graph) {
98
            r.insert(g[v].id);
99
        }
100
    }
101

    
102
    void print_v_index_std_map(const Graph& g, const VertexIndexStdMap& v_index_std_map) {
103
        std::list<std::string> outputs;
104

    
105
        VertexIndexStdMap::const_iterator iter;
106
        for (iter = v_index_std_map.begin(); iter != v_index_std_map.end(); ++iter) {
107
            outputs.push_back(std::to_string(iter->second));
108
            // outputs.push_back(std::to_string(&(iter->first)) + ": " + std::to_string(iter->second));
109
        }
110

    
111
        using namespace boost::spirit::karma;
112
        cout << "Vertex Index Std Map:\n";
113
        cout << format("[\n  " << (auto_ % "\n  ") << "\n]\n", outputs);
114
    }
115

    
116
    void print_v_index_map(const Graph& g, const VertexIndexMap& v_index_map) {
117
        cout << "Vertex Index Map:\n";
118

    
119
        std::list<std::string> outputs;
120
        BGL_FORALL_VERTICES_T(v, g, Graph) {
121
            int index = boost::get(v_index_map, v);
122
            std::string vertex_id = g[v].id;
123
            cout << v << endl;
124
            outputs.push_back(vertex_id + ": " + std::to_string(index));
125
        }
126

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