Statistics
| Branch: | Revision:

root / custompackages / graph-parser / src / utility.h @ f9e3a1f0

History | View | Annotate | Download (3.74 KB)

1
//
2
// Created by quynh on 12/15/15.
3
// All the generic functions are defined in utility.tpp - 27/01/2015
4
//
5

    
6
#ifndef GRAPH_PARSER_UTILITY_H
7
#define GRAPH_PARSER_UTILITY_H
8

    
9
#include <iostream>
10
#include <fstream>
11
#include <sstream>
12
#include <boost/graph/graph_traits.hpp>
13
#include <boost/graph/undirected_graph.hpp>
14
#include <boost/spirit/include/karma.hpp>
15
#include <boost/graph/iteration_macros.hpp>
16
#include <boost/graph/breadth_first_search.hpp>
17
#include "common.h"
18

    
19
namespace outops {
20
    std::ostream& operator<<(std::ostream& os, const Graph& g);
21

    
22
    // I have to use pair to add more than one argument for cout operator<<
23
    std::ostream& operator<<(std::ostream& os, std::pair<const Graph&, const VertexIndexPMap&> p);
24

    
25
    // For set
26
    template <typename T> std::ostream& operator<<(std::ostream& os, const std::set<T>& data);
27

    
28
    // For vector
29
    template <typename T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& data);
30

    
31
    // For map
32
    template <typename T> std::ostream& operator<<(std::ostream& os, const std::map<string, T>& data);
33

    
34
    // For 2-D vector
35
    std::ostream& operator<<(std::ostream& os, const vector< vector< int> >& data);
36
}
37

    
38
namespace printhelper {
39
    template <typename T1, typename T2> void for_map(const std::map<T1, T2> m);
40
}
41

    
42
// non-member functions operating on Graph datatype.
43
template < typename TimeMap, typename T > class bfs_time_visitor : public boost::default_bfs_visitor {
44
public:
45
    bfs_time_visitor(TimeMap tmap, T & t):m_timemap(tmap), m_time(t) { }
46
    template < typename Vertex, typename Graph >
47
    void discover_vertex(Vertex u, const Graph & g) const
48
    {
49
        boost::put(m_timemap, u, m_time++);
50
    }
51
    TimeMap m_timemap;
52
    T & m_time;
53
};
54

    
55
namespace graphext {
56
    void id_of_all_vertices(const Graph& g, std::set<std::string>& r);
57

    
58
    template <typename Container>
59
    void id_of_some_vertices(const Graph& g, const Container& container, std::set<std::string>& r);
60

    
61
    bool is_connected(const Graph& g, const VertexIndexPMap& v_index_pmap);
62

    
63
    void print_edge(const Graph& g, const Edge& e);
64

    
65
    void print_v_index_std_map(const Graph& g, const VertexIndexStdMap& v_index_std_map);
66
    void print_v_index_pmap(const Graph& g, const VertexIndexPMap& v_index_pmap);
67
    void print_e_index_pmap(const Graph& g, const EdgeIndexPMap& e_index_pmap);
68

    
69
    void write_betweenness_centrality(Graph const& g, std::vector<double> v_centrality_vec, string filepath);
70
}
71

    
72
namespace setops {
73
    template <typename T> std::set<T> operator-(const std::set<T>& a, const std::set<T>& b);
74
    template <typename T> std::set<T> operator/(const std::set<T>& a, const std::set<T>& b);
75
}
76

    
77
namespace stdhelper {
78
    template <typename T1, typename T2> bool exists(const std::map<T1, T2>& m, const T1& key);
79
    template <typename T> bool exists(const std::set<T>& s, const T& element);
80

    
81
    template <typename T>
82
    std::string to_string(T value);
83
}
84

    
85
namespace helper {
86
    // I do not want to use boost::filesystem, due to additional library must be included
87
    void get_file_name_and_extension(string s, string& name, string& ext);
88
}
89

    
90
template <typename Pair>
91
struct second_equal_to
92
    : std::unary_function<const Pair&, bool>
93
{
94
    second_equal_to(const typename Pair::second_type& value)
95
        : value_(value) { }
96

    
97
    bool operator()(const Pair& p) const
98
    {
99
        // I quick-hacked to make the Pair work for second argument with built-in type, such as int.
100
        // return p.second == *value_;
101
        return p.second == value_;
102
    }
103

    
104
private:
105
    typename Pair::second_type value_;
106
};
107

    
108
template <typename T1, typename T2>
109
struct less_second {
110
    typedef pair<T1, T2> type;
111
    bool operator ()(type const& a, type const& b) const {
112
        return a.second < b.second;
113
    }
114
};
115

    
116
#include "utility.tpp"
117

    
118
#endif //GRAPH_PARSER_UTILITY_H
119