-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.h
55 lines (47 loc) · 925 Bytes
/
helper.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
#ifndef _HELPER_H
#define _HELPER_H
#include "Object.h"
#include "String.h"
#define equals(T) int equals_##T(T a, T b) { return a == b; }
#define hash(T) long hash_##T(T a) { return (long)a; }
int equals_String(String a, String b) {
int size_a = *(int*)&a;
int size_b = *(int*)&b;
if (size_a != size_b)
return 0;
void * cur_this = this;
int flag = 1;
Object.load(&a);
while (size_a--) {
char c = a.at(size_a);
Object.load(&b);
if (c != b.at(size_a)) {
flag = 0;
break;
}
Object.load(&a);
}
this = cur_this;
return flag;
}
int hash_String(String s) {
void * cur_this = this;
long ans = 0;
long exp = 1;
long mask = (1L << 32) - 1;
Object.load(&s);
int size = s.size();
for (int i = 0; i < size; ++i) {
ans = (ans + s.at(i) * exp) & mask;
exp = (exp * 31) & mask;
}
this = cur_this;
return ans;
}
equals(int);
equals(long);
equals(ptr);
hash(int);
hash(long);
hash(ptr);
#endif