-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtil.h
More file actions
202 lines (158 loc) · 3.49 KB
/
Util.h
File metadata and controls
202 lines (158 loc) · 3.49 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
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
#ifndef UTIL_H_
#define UTIL_H_
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <ctime>
#include <cmath>
#include <vector>
#include <string>
#include <stdint.h> // for uint32_t
#include <limits.h> // for CHAR_BIT
#include <assert.h>
#define NDEBUG
using namespace std;
extern uint64_t RAND_A;
extern uint64_t RAND_B;
int EDistance(const char* s1, const char* s2, int s1len, int s2len);
bool EDcheck(char* s1, char* s2, int s1len, int s2len, int tau);
unsigned long long MyRand64();
void SetRand();
void PrintBinary(uint32_t num);
inline int min3(const int a, const int b, const int c){
int m = a;
if (m > b) m = b;
if (m > c) m = c;
return m;
}
inline uint32_t rotl32 (uint32_t n, unsigned int c){
return (n<<c) | (n>>(32-c));
}
inline uint32_t rotr32 (uint32_t n, unsigned int c){
const unsigned int mask = (CHAR_BIT*sizeof(n)-1);
#ifndef Release
assert ( (c<=mask) &&"rotate by type width or more");
#endif
c &= mask; // avoid undef behaviour with NDEBUG. 0 overhead for most types / compilers
return (n>>c) | (n<<( (-c)&mask ));
}
inline uint64_t rotl64 (uint64_t n, unsigned int c){
return (n<<c) | (n>>(64-c));
}
inline uint64_t rotr64 (uint64_t n, unsigned int c){
const unsigned int mask = (CHAR_BIT*sizeof(n)-1);
#ifndef Release
assert ( (c<=mask) &&"rotate by type width or more");
#endif
c &= mask; // avoid undef behaviour with NDEBUG. 0 overhead for most types / compilers
return (n>>c) | (n<<( (-c)&mask ));
}
inline uint32_t Hash32(unsigned int x){
return x*(uint32_t)(2654435761)+(uint32_t)(3435961857);
}
inline uint64_t Hash64(unsigned int x){
return x*RAND_A+RAND_B;
}
class Int{
public:
int num;
Int(){
num=0;
}
};
string extractFilename(const char* filename);
void quit();
bool PrimeTest(unsigned long long p);
template<class T>
void VectorPreprocessing(vector<T>& v, T u){
if(v.size()<2)
return ;
int p1, p2;
sort(v.begin(), v.end());
p1=0;
p2=1;
while(v[0]==u){
v.erase(v.begin());
}
while(p2<v.size()){
if(v[p2]==u || v[p1]==v[p2]){
v.erase(v.begin()+p2);
continue;
}
p1++;
p2++;
}
}
template<class T>
int IntersectionSize(const vector<T>& v1, const vector<T>& v2){
int i=0, j=0;
int count=0;
while(i<v1.size()&&j<v2.size()){
if(v1[i]==v2[j]){
count++;
i++;
j++;
continue;
}
if(v1[i]<v2[j]){
i++;
continue;
}
if(v1[i]>v2[j]){
j++;
continue;
}
}
return count;
}
template<class T>
int IntersectionSize(const T* v1, const T* v2, int s1, int s2, T u){
int i=upper_bound(v1, v1+s1, u)-v1;
int j=lower_bound(v2, v2+s2, v1[i])-v2;
int count=0;
while(i<s1&&j<s2){
if(v1[i]==v2[j]){
count++;
i++;
j++;
continue;
}
if(v1[i]<v2[j]){
i++;
continue;
}
if(v1[i]>v2[j]){
j++;
continue;
}
}
return count;
}
template<class T>
inline bool VectorEq(const vector<T>& v1, const vector<T>& v2){
if(v1.size()!=v2.size())
return false;
for(int i=0; i<v1.size(); i++){
if(v1[i]!=v2[i])
return false;
}
return true;
}
template<class T>
bool IsIntersect(const vector<T>& v1, const vector<T>& v2){
int i=0, j=0;
while(i<v1.size()&&j<v2.size()){
if(v1[i]==v2[j]){
return true;
}
if(v1[i]<v2[j]){
i++;
}
if(v1[i]>v2[j]){
j++;
}
}
return false;
}
#endif