-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyLib.cpp
359 lines (320 loc) · 7.24 KB
/
MyLib.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/////////////////////////////////////////////////////////////////////////////////////
// File Name : MyLib.cpp
// Project Name: IRLAS
// Author : Huipeng Zhang ([email protected])
// Environment : Microsoft Visual C++ 6.0
// Description : some utility functions
// Time : 2005.9
// History :
// CopyRight : HIT-IRLab (c) 2001-2005, all rights reserved.
/////////////////////////////////////////////////////////////////////////////////////
#include "MyLib.h"
void replace_char_by_char(string &str, char c1, char c2)
{
string::size_type pos = 0;
for (; pos < str.size(); ++pos) {
if (str[pos] == c1) {
str[pos] = c2;
}
}
}
void split_bychars(const string& str, vector<string> & vec, const char *sep)
{ //assert(vec.empty());
vec.clear();
string::size_type pos1 = 0, pos2 = 0;
string word;
while((pos2 = str.find_first_of(sep, pos1)) != string::npos)
{
word = str.substr(pos1, pos2-pos1);
pos1 = pos2 + 1;
if(!word.empty())
vec.push_back(word);
}
word = str.substr(pos1);
if(!word.empty())
vec.push_back(word);
}
// remove the blanks at the begin and end of string
void clean_str(string &str)
{
string blank = " \t\r\n";
string::size_type pos1 = str.find_first_not_of(blank);
string::size_type pos2 = str.find_last_not_of(blank);
if (pos1 == string::npos) {
str = "";
} else {
str = str.substr(pos1, pos2-pos1+1);
}
}
bool my_getline(ifstream &inf, string &line)
{
if (!getline(inf, line)) return false;
int end = line.size() - 1;
while (end >= 0 && (line[end] == '\r' || line[end] == '\n')) {
line.erase(end--);
}
return true;
}
void str2uint_vec(const vector<string> &vecStr, vector<unsigned int> &vecInt)
{
vecInt.resize(vecStr.size());
int i = 0;
for (; i < vecStr.size(); ++i)
{
vecInt[i] = atoi(vecStr[i].c_str());
}
}
void str2int_vec(const vector<string> &vecStr, vector<int> &vecInt)
{
vecInt.resize(vecStr.size());
int i = 0;
for (; i < vecStr.size(); ++i)
{
vecInt[i] = atoi(vecStr[i].c_str());
}
}
void int2str_vec(const vector<int> &vecInt, vector<string> &vecStr)
{
vecStr.resize(vecInt.size());
int i = 0;
for (; i < vecInt.size(); ++i) {
ostringstream out;
out << vecInt[i];
vecStr[i] = out.str();
}
}
void join_bystr(const vector<string> &vec, string &str, const string &sep)
{
str = "";
if (vec.empty()) return;
str = vec[0];
int i = 1;
for(; i < vec.size(); ++i)
{
str += sep + vec[i];
}
}
void split_bystr(const string &str, vector<string> &vec, const string &sep)
{
vec.clear();
string::size_type pos1 = 0, pos2 = 0;
string word;
while((pos2 = str.find(sep, pos1)) != string::npos)
{
word = str.substr(pos1, pos2-pos1);
pos1 = pos2 + sep.size();
if(!word.empty()) vec.push_back(word);
}
word = str.substr(pos1);
if(!word.empty()) vec.push_back(word);
}
void split_pair_vector(const vector< pair<int, string> > &vecPair, vector<int> &vecInt, vector<string> &vecStr)
{
int i = 0;
vecInt.resize(vecPair.size());
vecStr.resize(vecPair.size());
for (; i < vecPair.size(); ++i) {
vecInt[i] = vecPair[i].first;
vecStr[i] = vecPair[i].second;
}
}
void split_bychar(const string& str, vector<string>& vec,
const char separator)
{
//assert(vec.empty());
vec.clear();
string::size_type pos1 = 0, pos2 = 0;
string word;
while((pos2 = str.find_first_of(separator, pos1)) != string::npos)
{
word = str.substr(pos1, pos2-pos1);
pos1 = pos2 + 1;
if(!word.empty())
vec.push_back(word);
}
word = str.substr(pos1);
if(!word.empty())
vec.push_back(word);
}
void string2pair(const string& str, pair<string, string>& pairStr, const char separator)
{
string::size_type pos = str.find_last_of(separator);
if (pos == string::npos) {
string tmp = str + "";
clean_str(tmp);
pairStr.first = tmp;
pairStr.second = "";
} else {
string tmp = str.substr(0, pos);
clean_str(tmp);
pairStr.first = tmp;
tmp = str.substr(pos+1);
clean_str(tmp);
pairStr.second = tmp;
}
}
void convert_to_pair(vector<string>& vecString,
vector< pair<string, string> >& vecPair)
{
assert(vecPair.empty());
int size = vecString.size();
string::size_type cur;
string strWord, strPos;
for(int i = 0; i < size; ++i)
{
cur = vecString[i].find('/');
if (cur == string::npos)
{
strWord = vecString[i].substr(0);
strPos = "";
}
else if (cur == vecString[i].size()-1)
{
strWord = vecString[i].substr(0, cur);
strPos = "";
}
else
{
strWord = vecString[i].substr(0, cur);
strPos = vecString[i].substr(cur+1);
}
vecPair.push_back(pair<string, string>(strWord, strPos));
}
}
void split_to_pair(const string& str, vector< pair<string, string> >& vecPair)
{
assert(vecPair.empty());
vector<string> vec;
split_bychar(str, vec);
convert_to_pair(vec, vecPair);
}
void chomp(string& str)
{
string white = " \t\n";
string::size_type pos1 = str.find_first_not_of(white);
string::size_type pos2 = str.find_last_not_of(white);
if (pos1 == string::npos || pos2 == string::npos)
{
str = "";
}
else
{
str = str.substr(pos1, pos2-pos1+1);
}
}
int common_substr_len(string str1, string str2)
{
string::size_type minLen;
if (str1.length() < str2.length())
{
minLen = str1.length();
}
else
{
minLen = str2.length();
str1.swap(str2); //make str1 the shorter string
}
string::size_type maxSubstrLen = 0;
string::size_type posBeg;
string::size_type substrLen;
string sub;
for (posBeg = 0; posBeg < minLen; posBeg++)
{
for (substrLen = minLen-posBeg; substrLen > 0; substrLen--)
{
sub = str1.substr(posBeg, substrLen);
if (str2.find(sub) != string::npos)
{
if (maxSubstrLen < substrLen)
{
maxSubstrLen = substrLen;
}
if (maxSubstrLen >= minLen-posBeg-1)
{
return maxSubstrLen;
}
}
}
}
return 0;
}
int get_char_index(string& str)
{
assert(str.size() == 2);
return ((unsigned char)str[0]-176)*94 + (unsigned char)str[1] - 161;
}
bool is_chinese_char(string& str)
{
if (str.size() != 2)
{
return false;
}
int index = ((unsigned char)str[0]-176)*94 + (unsigned char)str[1] - 161;
if (index >= 0 && index < 6768)
{
return true;
}
else
{
return false;
}
}
int find_GB_char(const string& str, string wideChar, int begPos)
{
assert(wideChar.size() == 2 && wideChar[0] < 0); //is a GB char
int strLen = str.size();
if (begPos >= strLen)
{
return -1;
}
string GBchar;
for (int i = begPos; i < strLen-1; i++)
{
if (str[i] < 0) //is a GB char
{
GBchar = str.substr(i, 2);
if (GBchar == wideChar)
return i;
else
i++;
}
}
return -1;
}
void split_by_separator(const string& str, vector<string>& vec, const string separator)
{
assert(vec.empty());
string::size_type pos1 = 0, pos2 = 0;
string word;
while((pos2 = find_GB_char(str, separator, pos1)) != -1)
{
word = str.substr(pos1, pos2-pos1);
pos1 = pos2 + separator.size();
if(!word.empty())
vec.push_back(word);
}
word = str.substr(pos1);
if(!word.empty())
vec.push_back(word);
}
//void compute_time()
//{
// clock_t tick = clock();
// double t = (double)tick / CLK_TCK;
// cout << endl << "The time used: " << t << " seconds." << endl;
//}
string word(string& word_pos)
{
return word_pos.substr(0, word_pos.find("/"));
}
bool is_ascii_string(string& word)
{
for (unsigned int i = 0; i < word.size(); i++)
{
if (word[i] < 0)
{
return false;
}
}
return true;
}