-
Notifications
You must be signed in to change notification settings - Fork 4
/
my_fun_stuffs.hpp
64 lines (54 loc) · 1.37 KB
/
my_fun_stuffs.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
template <
typename T,
typename F,
typename Rf = typename std::result_of<F(T)>::type,
typename R = Node<typename std::enable_if<!std::is_void<Rf>::value, Rf>::type>
>
R map(const Node<T> &node, const F &f)
{
R r(f(node.getValue()));
for( const auto &l : node.getLeafs() )
r.addLeaf(map(*l, f));
return r;
}
template <
typename T,
typename F,
typename Rf = typename std::result_of<F(T)>::type
>
typename std::enable_if<std::is_void<Rf>::value,void>::type
map(const Node<T> &node, const F &f)
{
f(node.getValue());
for( const auto &l : node.getLeafs() )
map(*l, f);
}
template < typename T, typename L >
void toSequencial( const Node<T> &n, L &seq )
{
auto itr = seq.begin();
map(n, [&](const T &e) {
seq.emplace(itr, e);
itr = seq.end();
});
}
template < typename T, typename F >
void mapOnce( const Node<T> &n, const F &f)
{
std::set<Node<T>*> uns;
mapOnce(n,f,uns);
}
template <
typename T,
typename F,
typename Rf = typename std::result_of<F(T)>::type
>
typename std::enable_if<std::is_void<Rf>::value,void>::type
mapOnce(const Node<T> &node, const F &f, std::set<Node<T>*> &unions)
{
if ( unions.insert((Node<T>*)&node).second == false )
return;
f(node.getValue());
for( auto &l : node.getLeafs() )
mapOnce(*l, f, unions);
}