-
Notifications
You must be signed in to change notification settings - Fork 3
/
LSH.cpp
63 lines (51 loc) · 1.38 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
#include <iostream>
#include "LSH.h"
/* Author: Chen Luo and Anshumali Shrivastava
* COPYRIGHT PROTECTION
* Free for research use.
* For commercial use, contact: RICE UNIVERSITY INVENTION & PATENT or the Author.
*/
using namespace std;
LSH::LSH(int K, int L, int rangebits) {
_K = K;
_L = L;
_rangebits = rangebits;
_bucket = new int[_L * (1 << _rangebits)]();
int totsize = _L * (1 << _rangebits);
}
void LSH::add(int *hashes, int id) {
int index = 0;
#pragma omp parallel for
for (int i = 0; i < _L; i++) {
int index = 0;
for (int j = 0; j < _K; j++) {
index = (index << 1) + hashes[_K * i + j];
}
_bucket[i * (1 << _rangebits) + index] = _bucket[i * (1 << _rangebits) + index] + 1;
}
}
int *LSH::retrieve(int *hashes) {
int *retreived = new int[_L];
//std::unordered_map<int, int> m;
int count = 0;
#pragma omp parallel for
for (int i = 0; i < _L; i++) {
int index = 0;
for (int j = 0; j < _K; j++) {
index = (index << 1) + hashes[_K * i + j];
}
retreived[i] = _bucket[i * (1 << _rangebits) + index];
}
return retreived;
}
double LSH::retrieve_mean(int *hashes) {
int sum = 0;
int *retrivd = retrieve(hashes);
for (int i = 0; i < _L; i++) {
sum += retrivd[i];
}
return sum / (double) _L;
}
LSH::~LSH() {
delete[]_bucket;
}