Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (3 KB)

1
namespace outops
2
{
3
    template <typename T>
4
    std::ostream& operator<<(std::ostream& os, const std::set<T>& data) {
5
        using namespace boost::spirit::karma;
6
        os << format("(" << (auto_ % "\n  ") << ")", data);
7
        os << endl;
8
    }
9

    
10
    template <typename T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& data) {
11
        os << "cout << std::vector<T>\n";
12

    
13
        using namespace boost::spirit::karma;
14
        os << format("(" << (auto_ % "\n  ") << ")", data);
15
        os << endl;
16
    }
17

    
18
    template <typename T>
19
    std::ostream& operator<<(std::ostream& os, const std::map<string, T>& data) {
20
        os << "cout << std::map<string, T>\n";
21
        typename std::map<string, T>::const_iterator iter;
22
        for (iter = data.begin(); iter != data.end(); ++iter) {
23
            os << (*iter).first << ": " << (*iter).second << endl;
24
        }
25
        os << endl;
26
    }
27
}
28

    
29
namespace printhelper {
30
    template <typename T1, typename T2>
31
    void for_map(const std::map<T1, T2> m) {
32
        // similar to cout<< in namespace outops
33
        cout << "printhelper - for_map\n";
34
        typename std::map<T1, T2>::const_iterator iter;
35
        for (iter = m.begin(); iter != m.end(); ++iter) {
36
            cout << (*iter).first << ": " << (*iter).second;
37
        }
38
    }
39
}
40
namespace graphext {
41
    template <typename Container>
42
    void id_of_some_vertices(const Graph& g, const Container& container, std::set<std::string>& r) {
43
        /*
44
        ** Find id for a vec
45
        */
46
        for (typename Container::const_iterator ci = container.begin(); ci != container.end(); ++ci) {
47
            r.insert(g[*ci].id);
48
        }
49
    }
50
}
51

    
52
namespace setops {
53
    // From http://stackoverflow.com/questions/8175933/to-compare-two-boost-graph-having-same-vertices
54
    template <typename T> std::set<T> operator-(const std::set<T>& a, const std::set<T>& b) {
55
        std::set<T> r;
56
        std::set_difference(
57
                a.begin(), a.end(),
58
                b.begin(), b.end(),
59
                std::inserter(r, r.end()));
60

    
61
        return r;
62
    }
63

    
64
    template <typename T> std::set<T> operator/(const std::set<T>& a, const std::set<T>& b) {
65
        std::set<T> r;
66
        std::set_intersection(
67
                a.begin(), a.end(),
68
                b.begin(), b.end(),
69
                std::inserter(r, r.end()));
70

    
71
        return r;
72
    }
73
}
74

    
75
namespace stdhelper {
76
    template <typename T1, typename T2>
77
    bool exists(const std::map<T1, T2>& c, const T1& key) {
78
        return (c.count(key) > 0);
79
    }
80

    
81
    template <typename T>
82
    bool exists(const std::set<T>& c, const T& key) {
83
        return (c.count(key) > 0);
84
    }
85

    
86
    template <typename T>
87
    std::string to_string(T value)
88
    {
89
        // From http://stackoverflow.com/questions/12975341/to-string-is-not-a-member-of-std-says-so-g
90

    
91
        //create an output string stream
92
        std::ostringstream os ;
93

    
94
        //throw the value into the string stream
95
        os << value ;
96

    
97
        //convert the string stream into a string and return
98
        return os.str() ;
99
    }
100
}
101