-
Notifications
You must be signed in to change notification settings - Fork 0
/
hopscotch.h
executable file
·171 lines (147 loc) · 3.98 KB
/
hopscotch.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
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
#ifndef HOPSCOTCH_H
#define HOPSCOTCH_H
#include<iostream>
#include<pthread.h>
#define mmix(h,k) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; }
#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
#define MSB (((uint64_t)1) << 63)
using namespace std;
//10% -> 83886080
//20% -> 41943040
//40% -> 20971520
//60% -> 13981013
class Hopscotch {
private:
static const int HOP_RANGE = 32;//32;//1024;//32;
static const int ADD_RANGE = 256;//1024*4;//256;
static const int MAX_SEGMENTS = 13981013;//118857;//1024*1024*16-1;//13981013;
//1024*1024*2-1;//1048576;//13981013;//102*1024*8;////// Including neighbourhodd for last hash location
static const int MAX_TRIES = 2;
int BUSY=0;
int size=MAX_SEGMENTS + ADD_RANGE;
struct Bucket {
unsigned int volatile _hop_info;
int volatile _key;
char* volatile _address;
unsigned int volatile _lock;
unsigned int volatile _timestamp;
pthread_mutex_t lock_mutex;
pthread_cond_t lock_cv;
Bucket(){// 构造函数
_hop_info = 0;
_lock = 0;
_key = NULL;
_timestamp = 0;
pthread_mutex_init(&lock_mutex,NULL);
pthread_cond_init(&lock_cv, NULL);
}
void lock(){
pthread_mutex_lock(&lock_mutex);
while(1){
if (_lock==0){
_lock =1;
pthread_mutex_unlock(&lock_mutex);
break;
}
pthread_cond_wait(&lock_cv, &lock_mutex);
}
}
void unlock(){
pthread_mutex_lock(&lock_mutex);
_lock = 0;
pthread_cond_signal(&lock_cv);
pthread_mutex_unlock(&lock_mutex);
}
};
Bucket* segments_arys;
public:
static uint64_t qt_hashword(uint64_t key){ /*{{{*/
uint32_t a, b, c;
const union {
uint64_t key;
uint8_t b[sizeof(uint64_t)];
} k = {
key
};
a = b = c = 0x32533d0c + sizeof(uint64_t); // an arbitrary value, randomly selected
c += 47;
b += k.b[7] << 24;
b += k.b[6] << 16;
b += k.b[5] << 8;
b += k.b[4];
a += k.b[3] << 24;
a += k.b[2] << 16;
a += k.b[1] << 8;
a += k.b[0];
c ^= b;
c -= rot(b, 14);
a ^= c;
a -= rot(c, 11);
b ^= a;
b -= rot(a, 25);
c ^= b;
c -= rot(b, 16);
a ^= c;
a -= rot(c, 4);
b ^= a;
b -= rot(a, 14);
c ^= b;
c -= rot(b, 24);
return ((uint64_t)c + (((uint64_t)b) << 32)) & (~MSB);
}
int newhashf(int key){
int num = 0x27d4eb2d; // a prime or an odd constant
key = (key ^ 61) ^ (key >> 16);
key = key + (key << 3);
key = key ^ (key >> 4);
key = key * num;
key = key ^ (key >> 15);
return key % size;
}
unsigned int MurmurHash2A (const int key, int len){
return newhashf(key);
const unsigned int m = 0x5bd1e995;
const int r = 24;
unsigned int l = len;
const unsigned char * data = (const unsigned char *)key;
unsigned int h = seed;
while(len >= 4)
{
unsigned int k = 0;//*(unsigned int*)data;
mmix(h,k);
data += 4;
len -= 4;
}
unsigned int t = 0;
switch(len)
{
case 3: t ^= data[2] << 16; break;
case 2: t ^= data[1] << 8; break;
case 1: t ^= data[0]; break;
};
mmix(h,t);
mmix(h,l);
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h % (size-1);
}
void pr(){
for(int i=0;i<20;i++){
cout<<segments_arys[i]._key<<" "<<segments_arys[i]._hop_info<<endl;
}
}
Hopscotch();
Hopscotch(int Hsize);
~Hopscotch();
bool contains(int key);
bool add(int key, const char *data);
bool remove(int key);
unsigned int seed;
void find_closer_bucket(Bucket**,int*,int &);
void resize();
int getsize();
int find(int key);
char* returnchar(int id);
};
#endif // HOPSCOTCH_H