Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (2.99 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
    }
70

    
71
}
72

    
73
namespace outopserror {
74
    template <typename T>
75
    std::ostream& operator<<(std::ostream& os, const std::set<T>& s) {
76
        /* can't make it work with a generic function
77
        ** std::ostream& opeartor<<(std::ostream& os, const Container<std::string>& s)
78
        */
79
        using namespace boost::spirit::karma;
80
        os << format("( " << (auto_ % "\n  ") << ")", s);
81
    }
82
}
83

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

    
91
    // template <typename Container>
92
    // void id_of_vertices(const Graph& g, const Container& container, std::set<std::string>& r) {
93
    void id_of_vertices(const Graph& g, const VertexVec& container, std::set<std::string>& r) {
94
        /*
95
        ** Find id for a vec
96
        */
97
        for (VertexVec::const_iterator ci = container.begin(); ci != container.end(); ++ci) {
98
            r.insert(g[*ci].id);
99
        }
100
    }
101
}