Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2.22 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
namespace outops {
32
    std::ostream& operator<<(std::ostream& os, const Graph& g)
33
    {
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, const std::set<std::string>& s) {
64
    //      can't make it work with a generic function
65
    //     ** std::ostream& opeartor<<(std::ostream& os, const Container<std::string>& s)
66

    
67
    //     using namespace boost::spirit::karma;
68
    //     os << format("(" << (auto_ % "\n  ") << ")", s);
69
    //     os << endl;
70
    // }
71
}
72

    
73
namespace graphext {
74
    void id_of_vertices(const Graph& g, std::set<std::string>& r) {
75
        BGL_FORALL_VERTICES_T(v, g, Graph) {
76
            r.insert(g[v].id);
77
        }
78
    }
79
}