forked from rackingroll/mcmc_lsh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLSH.cpp
212 lines (180 loc) · 4.75 KB
/
LSH.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
#include <iostream>
#include <unordered_map>
#include "HashFunction.h"
//#include "DensifiedMinHash.h"
#include "LSH.h"
//#include <ppl.h>
#include <random>
#include <algorithm>
#include <climits>
//#pragma once
/* Author: Anshumali Shrivastava
* COPYRIGHT PROTECTION
* Free for research use.
* For commercial use, contact: RICE UNIVERSITY INVENTION & PATENT or the Author.
*/
// Edited by Chen Luo, remove the parrallel part of the code.
using namespace std;
//using namespace concurrency;
int LSH::_rangePow = 20;
int LSH::_thresh = 1; // The number of appeared times in the bucket
LSH::LSH(int K, int L)
{
_K = K;
_L = L;
//_range = 1 << 22;
LSH::_rangePow = K;
// Create a list of
_bucket = new Bucket*[L];
//#pragma omp parallel for
// Create L tables for Hashing
for (int i = 0; i < L; i++)
{
// Each _bucket is a
// _bucket[i] = new Bucket[1<<_K];
_bucket[i] = new Bucket[1<<_rangePow];
}
// _K* _L number of bits
rand1 = new int[_K*_L];
std::random_device rd;
std::mt19937 gen(rd());
// std::uniform_int_distribution<> dis(1, UINT_MAX);
std::uniform_int_distribution<> dis(1, INT_MAX);
//#pragma omp parallel for
for (int i = 0; i < _K*_L; i++)
{
rand1[i] = dis(gen);
if (rand1[i] % 2 == 0)
rand1[i]++;
}
}
void LSH::add(int *hashes, int id)
{
for (int i = 0; i < _L; i++)
{
unsigned int index = 0;
for (int j = 0; j < _K; j++)
{
unsigned int h = hashes[_K*i + j];
h *= rand1[_K*i + j];
h ^= h >> 13;
h ^= rand1[_K*i + j];
index += h*hashes[_K*i + j];
}
index = (index << 2) >> (32 - LSH::_rangePow);
_bucket[i][index].add(id);
}
}
// return uniqeRet
// uniqeRet[0] is the size of returned list
// uniqeRet[1] is id, I don't know what the hell is that!
int * LSH::retrieve(int *hashes)
{
// The element in the list (index) is the data point id, the value is the number its appeared.
std::unordered_map<int, int> m;
int count = 0;
for (int i = 0; i < _L; i++)
{
// Get the index of the current hash like above.
unsigned int index = 0;
for (int j = 0; j < _K; j++)
{
unsigned int h = hashes[_K*i + j];
h *= rand1[_K*i + j];
h ^= h >> 13;
h ^= rand1[_K*i + j];
index += h*hashes[_K*i + j];
}
index = (index << 2) >> (32 - LSH::_rangePow);
// Nothing in the bucket, go to next table
if (_bucket[i][index].getAll() == NULL)
{
continue;
}
// Iterate all the element in the corresponding bucket
for (size_t a = 0; a < _bucket[i][index].getSize(); a++)
{
if (_bucket[i][index].getAll()[a] == 0)
continue;
if (_bucket[i][index].getAll()[a] < 0)
continue;
// See if already has the datapoint _bucket[i][index].getAll()[a] in m, accumulate all the stuff here
if (m.find(_bucket[i][index].getAll()[a]) == m.end())
m[_bucket[i][index].getAll()[a]] = 1;
else
m[_bucket[i][index].getAll()[a]] += 1;
}
}
int *uniqeRet = new int[m.size()+2];
//uniqeRet[0] = m.size(); //reserved for size
uniqeRet[1] = -1; //reserveed for id
int ind = 2;
for (unordered_map<int,int>::const_iterator it = m.begin(); it != m.end(); ++it) {
int val = it->first;
if (m[val] >= LSH::_thresh)
{
uniqeRet[ind] = val;
ind++;
}
}
uniqeRet[0] = ind - 2; //reserved for size
return uniqeRet;
}
/*
returns an array with ret with 3 values
ret[0] is the sample returned
Sampling Probability = (1 - (1 - p^K)^ret[2])*(1/returnarray[1])
where p = (1 - 1/(Range))LSHCollprob(q,ret[0]) + 1/(Range)
Range = the range of hashtable which in our case is (1<<_rangePow)
*/
int * LSH::sample(int *hashes)
{
int * samplewithProb = new int[3];
samplewithProb[0] = -1;
samplewithProb[1] = -1;
samplewithProb[2] = -1;
std::vector<int> v;
for (int i = 0; i < _L; i++)
{
v.push_back(i);
//v[i] = i;
}
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(v.begin(), v.end(), g);
for (int i = 0; i < _L; i++)
{
int table = v[i];
unsigned int index = 0;
for (int j = 0; j < _K; j++)
{
unsigned int h = hashes[_K*table + j];
h *= rand1[_K*table + j];
h ^= h >> 13;
h ^= rand1[_K*table + j];
index += h*hashes[_K*table + j];
}
index = (index << 2) >> (32 - LSH::_rangePow);
if (_bucket[table][index].getAll() == NULL)
{
// Nothing returned
continue;
}
else{
int * retSamp = _bucket[table][index].sample();
samplewithProb[0] = retSamp[0]; // The sample
samplewithProb[1] = retSamp[1]; // The uniform distribution of the sample
samplewithProb[2] = i; // In which table it is.
return samplewithProb;
}
}
return samplewithProb;
}
LSH::~LSH()
{
for (size_t i = 0; i < _L; i++)
{
delete[] _bucket[i];
}
delete[] _bucket;
}