Statistics
| Branch: | Revision:

root / custompackages / graph-parser / src / utility.cpp @ 162e1bda

History | View | Annotate | Download (5.66 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_edge(const Graph& g, const Edge& e) {
91
        string s = g[boost::source(e, g)].id;
92
        string t = g[boost::target(e, g)].id;
93

    
94
        printf("edge (%s, %s)", s.c_str(), t.c_str());
95
    }
96

    
97
    void print_v_index_std_map(const Graph& g, const VertexIndexStdMap& v_index_std_map) {
98
        std::list<std::string> outputs;
99

    
100
        VertexIndexStdMap::const_iterator iter;
101
        for (iter = v_index_std_map.begin(); iter != v_index_std_map.end(); ++iter) {
102
            outputs.push_back(std::to_string(iter->second));
103
            // outputs.push_back(std::to_string(&(iter->first)) + ": " + std::to_string(iter->second));
104
        }
105

    
106
        using namespace boost::spirit::karma;
107
        cout << "Vertex Index Std Map:\n";
108
        cout << format("[\n  " << (auto_ % "\n  ") << "\n]\n", outputs);
109
    }
110

    
111
    void print_v_index_pmap(const Graph& g, const VertexIndexPMap& v_index_pmap) {
112
        std::list<std::string> outputs;
113
        BGL_FORALL_VERTICES_T(v, g, Graph) {
114
            int index = boost::get(v_index_pmap, v);
115
            std::string vertex_id = g[v].id;
116
            // Uncomment to print the address of vertex v
117
            // cout << v << endl;
118
            outputs.push_back(vertex_id + ": " + std::to_string(index));
119
        }
120

    
121
        using namespace boost::spirit::karma;
122
        cout << "Vertex Index Map:\n";
123
        cout << format("[\n  " << (auto_ % "\n  ") << "\n]\n", outputs);
124
    }
125

    
126
    void print_e_index_pmap(const Graph& g, const EdgeIndexPMap& e_index_pmap) {
127
        std::list<std::string> outputs;
128
        BGL_FORALL_EDGES_T(e, g, Graph) {
129
            int index = boost::get(e_index_pmap, e);
130
            std::string source_id = g[boost::source(e, g)].id;
131
            std::string target_id = g[boost::target(e, g)].id;
132
            outputs.push_back("edge (" + source_id + ", " + target_id + ")" + ": " + std::to_string(index));
133
        }
134

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

    
140
    void write_betweenness_centrality(Graph const& g, std::vector<double> v_centrality_vec, string file_path) {
141
        cout << "XXX Writing to File";
142
        // string filepath = "../output/boost_" + fileSuffix + ".csv";
143
        ofstream out_file(file_path.c_str());
144

    
145
        Viter vi, ve;
146
        size_t i = 0;
147
        if (out_file.is_open()) {
148
            for (boost::tie(vi, ve) = boost::vertices(g); vi != ve; ++vi) {
149
                out_file << g[*vi].id << ", " << v_centrality_vec.at(i) << endl;
150
                ++i;
151
            }
152
        }
153
        out_file.close();
154

    
155
        cout << "Done Writing BC score to file " << file_path << endl;
156
    }
157
}
158

    
159
// GENERAL HELPERS
160
namespace helper {
161
    string get_file_name(const string& s) {
162
       char sep = '/';
163

    
164
    #ifdef _WIN32
165
       sep = '\\';
166
    #endif
167

    
168
       size_t i = s.rfind(sep, s.length());
169
       if (i != string::npos) {
170
          return(s.substr(i+1, s.length() - i));
171
       }
172

    
173
       return("");
174
    }
175
}