-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
97 lines (89 loc) · 2.16 KB
/
util.h
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
* util.h
*
* Created on: Sep 27, 2019
* Author: menright
*/
#ifndef UTIL_H_
#define UTIL_H_
#include <string>
#include <list>
#include <algorithm>
template <typename T, typename UnaryOp, typename T2=std::string, typename C2=std::list<T2>>
C2 mapf(const T b, const T e, UnaryOp f)
{
C2 out;
std::transform(b, e, back_inserter(out), f);
return out;
}
template <typename C1, typename UnaryOp, typename T2=std::string, typename C2=C1>
C2 mapf(const C1 in, UnaryOp f)
{
C2 out;
std::transform(in.begin(), in.end(), back_inserter(out), f);
return out;
}
template <typename T, typename UnaryOp, typename T2=std::string, typename C2=std::list<T2>>
C2 mapf(const T* b, const T*e, UnaryOp f)
{
C2 out;
std::transform(b, e, back_inserter(out), f);
return out;
}
template <typename T, typename UnaryOp, typename T2=std::string, typename T3=unsigned, typename C2=std::list<T2>>
C2 mapf(const T* a, const T3& n, UnaryOp f)
{
C2 out;
std::transform(a, a+n, back_inserter(out), f);
return out;
}
template <typename T, typename UnaryOp, typename T2=std::string, typename T3=unsigned, typename C2=std::list<T2>>
C2 mapfl(const T* a, const T3& n, UnaryOp f)
{
C2 out;
for (T3 i=0; i!=n; ++i) {
out.push_back(f(a[i],i));
}
return out;
}
template <typename BinaryOp>
std::string fold(std::list<std::string>& strings, std::string base, BinaryOp op)
{
std::list<std::string>::const_iterator ps = strings.begin();
std::string out = op(base,*ps++);
while (ps != strings.end()) {
out = op(out,*ps++);
}
return out;
}
template <typename C1, typename C2, typename BinaryOp>
C2 fold(C1& strings, C2 base, BinaryOp op)
{
typename C1::const_iterator ps = strings.begin();
C2 out = op(base,*ps++);
while (ps != strings.end()) {
out = op(out,*ps++);
}
return out;
}
inline std::string join(const std::list<std::string>& strs, int joint) {
std::string r;
auto p = strs.begin();
r = *p++;
for (; p!=strs.end(); ++p) {
r+= std::string(1,joint);
r+= *p;
}
return r;
}
template <typename C> std::string join(const C& ner, int joint) {
std::string rv;
auto i = ner.begin();
rv += *i++;
while (i != ner.end()) {
rv += joint;
rv += *i++;
}
return rv;
}
#endif /* UTIL_H_ */