-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
121 lines (103 loc) · 2.08 KB
/
Copy pathutil.cpp
File metadata and controls
121 lines (103 loc) · 2.08 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "util.h"
std::vector<int> range(unsigned lim){
std::vector<int> r(lim);
std::iota(begin(r),end(r),0);
return r;
}
std::vector<int> range(int start,int lim){
assert(lim>=start);
std::vector<int> r(lim-start);
std::iota(begin(r),end(r),start);
return r;
}
void write_file(std::string filename,std::string data){
std::ofstream f(filename);
assert(f.good());
f<<data;
}
int min(int a,unsigned b){
assert(b<INT_MAX);
return std::min(a,(int)b);
}
char as_hex_digit(int i){
assert(i>=0);
assert(i<16);
if(i<=9) return '0'+i;
return 'a'+(i-10);
}
std::string as_hex(int i){
assert(i>=0);
assert(i<=255);
return std::string()+as_hex_digit(i>>4)+as_hex_digit(i&0xf);
}
int rerange(int min_a,int max_a,int min_b,int max_b,int value){
auto f=(value-min_a)/(max_a-min_a+0.0);
auto out=min_b+f*(max_b-min_b);
return out;
}
int sum(std::pair<int,int> p){
return p.first+p.second;
}
std::string first_word(std::string const& s){
auto f=s.find(' ');
if(f==std::string::npos) return s;
return s.substr(0,f);
}
std::string join(std::vector<std::string> a){
std::stringstream ss;
for(auto elem:a) ss<<elem;
return ss.str();
}
std::vector<bool> bools(){
std::vector<bool> r;
r|=0;
r|=1;
return r;
}
std::vector<std::string> split(std::string const& s,char target){
std::vector<std::string> r;
std::stringstream ss;
for(auto c:s){
if(c==target){
r|=ss.str();
ss.str("");
}else{
ss<<c;
}
}
if(ss.str().size()){
r|=ss.str();
}
return r;
}
int atoi(std::string const& s){
return atoi(s.c_str());
}
std::vector<std::string> args(int argc,char **argv){
std::vector<std::string> r;
for(int i=0;i<argc;i++){
r|=argv[i];
}
return r;
}
size_t sum(std::multiset<bool> const& m){
return FILTER(id,m).size();
}
float mean(std::multiset<bool> const& m){
assert(m.size());
return (0.0+sum(m))/m.size();
}
std::string pop(std::vector<std::string>& v){
//warning! this is O(n) and modifies its argument
assert(v.size());
auto r=v[0];
v.erase(v.begin());
//PRINT(v);
return r;
}
void indent_by(unsigned int i){
for(auto _:range(i)){
(void)_;
std::cout<<"\t";
}
}