-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.cpp
220 lines (194 loc) · 5.68 KB
/
utils.cpp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* utils.cpp
*
* Created on: 29.08.2016
* Author: kruppa
*/
#include <cctype>
#include <cassert>
#include <cstring>
#include <sys/time.h>
#include <sys/resource.h>
#include "utils.h"
using std::endl;
using std::cout;
using std::string;
using std::vector;
vector<string> split(const string &s, char delim, vector<string> &elems) {
std::stringstream ss(s);
string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
vector<string> split2(const string &s, char delim, char delim2, vector<string> &elems) {
std::stringstream ss(s);
string item;
while (std::getline(ss, item, delim)) {
vector<string> elems2 = split(item, delim2);
elems.insert(elems.end(), elems2.begin(), elems2.end());
}
return elems;
}
vector<string> split2(const string &s, char delim, char delim2) {
vector<string> elems;
split2(s, delim, delim2, elems);
return elems;
}
string <rim(string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
string &rtrim(string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
string &trim(string &s) {
return ltrim(rtrim(s));
}
bool containsAlpha(const string &s)
{
for (string::const_iterator it = s.begin(); it != s.end(); it++) {
if (isalpha(*it))
return true;
}
return false;
}
template <typename element_type>
void print_histogram(const element_type *values, const size_t len)
{
size_t count = 1;
if (len == 0)
return;
element_type last = values[0];
for (int i = 1; i <= len; i++) {
if (i < len && values[i] == last) {
count++;
} else {
if (count == 1)
cout << " " << last;
else
cout << " " << last << "*" << count;
if (i < len)
last = values[i];
count = 1;
}
}
}
template
void print_histogram<int>(const int *values, const size_t len);
string::iterator
skip_brackets(string::iterator start, string::iterator end)
{
string::iterator it = start;
/* For empty input string, or if first character is not an open bracket,
* return start iterator. If the first character is an open bracket,
* return an iterator pointing to the matching closing bracket.
* If there is no matching closing bracket, return end iterator. */
if (it == end)
return it;
if (*it != '(')
return it;
size_t nr_open_brackets = 1;
it++;
while (nr_open_brackets > 0 && it != end) {
if (*it == '(') nr_open_brackets++;
if (*it == ')') nr_open_brackets--;
if (nr_open_brackets > 0)
it++;
}
return it;
}
vector<string>
split_sum(string::iterator start, string::iterator end)
{
string::iterator token_start;
bool had_text = false;
vector<string> tokens;
for (string::iterator it = start; it != end; it++) {
if (!had_text)
token_start = it;
/* Skip brackets */
const string::iterator it2 = skip_brackets(it, end);
if (it2 != it) {
assert (it2 != end); /* Make sure brackets were balanced */
had_text = true;
it = it2;
} else if ((*it == '+' || *it == '-') && !had_text) {
// Consume any sign
had_text = true;
} else if (*it == '+') { // Implies had_text == true
// cout << "Pushing token " << string(token_start, it) << endl;
tokens.push_back(string(token_start, it));
had_text = false;
} else if (*it == '-') { // Implies had_text == true
// This case occurs for example in "[A-B]". Here we push the
// token "A" and let the new token begin at "-", so that the
// second token will become "-B" and the "-" sign takes a
// unary role.
// cout << "Pushing token " << string(token_start, it) << endl;
tokens.push_back(string(token_start, it));
token_start = it;
had_text = true;
} else {
had_text = true;
}
}
if (had_text) {
// cout << "Pushing token " << string(token_start, end) << endl;
tokens.push_back(string(token_start, end));
}
return tokens;
}
// Parses a string integer, detects hex or decimal and basic expressions e.g. 3-5
int64_t getVal(string val) {
val = trim(val);
if (val.substr(1, 4) == "word")
val = val.substr(5);
size_t loc = val.find_first_of("+-\0", 1);
if (loc != std::string::npos)
return getVal(val.substr(0, loc)) + getVal(val.substr(loc));
loc = val.find('x');
if (loc != std::string::npos)
return std::stol(val.c_str(), NULL, 16);
return std::stol(val.c_str(), NULL, 10);
}
char *
readLine(char * &lineBuf, size_t &lineBufSize, FILE *inputFile)
{
size_t lineLen = 0;
assert(lineBufSize > 0);
do {
if (fgets(lineBuf + lineLen, lineBufSize - lineLen, inputFile) == NULL)
{
return NULL;
}
lineLen += strnlen(lineBuf + lineLen, lineBufSize - lineLen);
if (lineLen == lineBufSize - 1 && lineBuf[lineLen - 1] != '\n') {
const size_t newLineBufSize = 2 * lineBufSize;
lineBuf = (char *) realloc(lineBuf, newLineBufSize);
if (lineBuf == NULL) {
perror("Realloc lineBuf failed: ");
return NULL;
}
lineBufSize = newLineBufSize;
continue;
} else {
break;
}
} while(1);
return lineBuf;
}
long int
get_nivcsw()
{
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
return usage.ru_nivcsw;
}