Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (3.66 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 <boost/graph/graph_traits.hpp>
12
#include <boost/graph/undirected_graph.hpp>
13
#include <boost/spirit/include/karma.hpp>
14
#include <boost/graph/iteration_macros.hpp>
15
#include <boost/graph/breadth_first_search.hpp>
16
#include "common.h"
17

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
80
}
81

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

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

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

    
101
private:
102
    typename Pair::second_type value_;
103
};
104

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

    
113
#include "utility.tpp"
114

    
115
#endif //GRAPH_PARSER_UTILITY_H
116