-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.h
120 lines (107 loc) · 3.32 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifndef UTIL
#define UTIL
#include <string>
#include <vector>
using namespace std;
class Utils {
public:
template <typename T>
static void printVector(const vector<T> & vec) {
if (vec.size() != 0) {
typename vector<T>::const_iterator it = vec.begin();
cout << *it;
++it;
for (; it != vec.end(); ++it) {
cout << " " << *it;
}
}
cout << endl;
return;
}
inline static void Tokenize(const string& str, vector<string>& tokens, const string& delimiters = " ") {
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first "non-delimiter".
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos) {
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
template <typename T>
inline static bool insertInOrder(vector<T> & sortedVector, const T & newElement) {
if ((sortedVector.size() == 0) || (newElement > sortedVector.back())) {
sortedVector.push_back(newElement);
return true;
}
if (newElement < sortedVector[0]) {
sortedVector.insert(sortedVector.begin(), newElement);
return true;
}
unsigned minPos = 0;
unsigned maxPos = sortedVector.size() - 1;
while ((maxPos - minPos) > 1) {
unsigned testPos = (minPos + maxPos) / 2;
T testVal = sortedVector[testPos];
if (newElement > testVal) {
minPos = testPos;
} else if (newElement < testVal) {
maxPos = testPos;
} else if (newElement == testVal) {
return false;
}
}
if ((newElement == sortedVector[minPos]) || (newElement == sortedVector[maxPos])) {
return false;
}
//cout << "Inserting " << newElement << "\t" << sortedVector[minPos] << "\t" << sortedVector[maxPos] << endl;
typename vector<T>::iterator it = sortedVector.begin();
it += maxPos;
//printVector(sortedVector);
sortedVector.insert(it, newElement);
//printVector(sortedVector);
return true;
}
template <typename T>
inline static bool eraseInOrder(vector<T> & sortedVector, const T & toErase) {
signed long minPos = 0;
signed long maxPos = sortedVector.size() - 1;
while (maxPos >= minPos) {
signed long testPos = (minPos + maxPos) / 2;
T testVal = sortedVector[testPos];
if (toErase > testVal) {
minPos = testPos + 1;
} else if (toErase < testVal) {
maxPos = testPos - 1;
} else {
typename vector<T>::iterator it = sortedVector.begin();
it += testPos;
sortedVector.erase(it);
return true;
}
}
return false;
}
template <typename T>
inline static bool elementExists(vector<T> & sortedVector, const T & toCheck) {
signed long minPos = 0;
signed long maxPos = sortedVector.size() - 1;
while (maxPos >= minPos) {
signed long testPos = (minPos + maxPos) / 2;
T testVal = sortedVector[testPos];
if (toCheck > testVal) {
minPos = testPos + 1;
} else if (toCheck < testVal) {
maxPos = testPos - 1;
} else {
return true;
}
}
return false;
}
};
#endif // UTIL