-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_func.cpp
More file actions
85 lines (78 loc) · 2.24 KB
/
hash_func.cpp
File metadata and controls
85 lines (78 loc) · 2.24 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
//
// Created by kosti on 10/21/2018.
//
#include "hash_func.h"
#include <climits>
#include <random>
#include "constants.h"
#include "data_point.h"
hash_func::hash_func(int dimension, int k,std::string func){
std::random_device rd; // assume unsigned int is 32 bits
std::mt19937_64 generator(rd()); // seeded with 256 bits of entropy from random_device
std::normal_distribution<double> n_distribution(0.0,1.0);
std::uniform_real_distribution<double> uint_distW(0.0,(float) const_lsh::w);
std::vector<double> temp(dimension);
this->func=func;
for(int i=0 ; i < k ; i++ ) {
this->t.push_back(uint_distW(generator));
for(int j=0 ; j < dimension ; j++ ){
temp[j]=n_distribution(generator);
}
v.push_back(temp);
}
}
hash_func::~hash_func(){
for (int i=0;i<this->v.size();i++)
this->v[i].clear();
this->t.clear();
this->v.clear();
}
void hash_func::clean(){
for (int i=0;i<this->v.size();i++)
this->v[i].clear();
this->t.clear();
this->v.clear();
}
value_point<int> hash_func::hash_value(data_point<int>& k,int &f,int size,int table_size,std::vector<int> r){
value_point<int> point;
if (this->func=="euclidean"){
long int sum=0;
double h=0.0;
for(int j=0; j<size; j++){
for (int i=0; i<k.point.size(); i++){
h+=this->v[j][i]*k.point[i];
}
h=h+this->t[j];
h=h/(double)const_lsh::w;
int h1=(int)h;
h1=((h1%2)+2)%2;
point.point.push_back(h1);
sum+= (int) pow(2.0,double(size-1-j))*h1;
h=0.0;
}
f=int(sum);
point.p=&k;
return point;
}else {
long int sum=0;
double h=0.0;
for(int j=0; j<size; j++){
for (int i=0; i<k.point.size(); i++){
h+=this->v[j][i]*k.point[i];
}
int coin=0;
if (h>=0){
coin=1;
point.point.push_back(1);
}
else{
point.point.push_back(0);
}
sum+= (int) pow(2.0,double(size-1-j))*coin;
h=0.0;
}
f=sum;
point.p=&k;
return point;
}
}