-
Notifications
You must be signed in to change notification settings - Fork 0
/
LSH.c
157 lines (131 loc) · 3.43 KB
/
LSH.c
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
#include <stdio.h>
#include <stdlib.h>
#include "LSH.h"
#include "hashTable.h"
#include "random.h"
#include "list.h"
/* struct for each G function */
typedef struct hashMap_t {
hashTable tables;
void* GConfiguration;
} hashMap;
/* struct for LSH */
struct LSH_t {
hashMap* tables;
int L;
int k;
};
extern dataIF data;
void BuildLSH(LSH** lshptr, int L, int k) {
int i,j;
*lshptr = malloc(sizeof(LSH));
LSH* lsh = *lshptr;
lsh->L = L;
lsh->k = k;
lsh->tables = malloc(L * sizeof(hashMap));
value value;
for (i = 0; i < L; i++) {
//initialize struct for each G function depending on the metric
lsh->tables[i].GConfiguration = data.initStruct();
//initialize hash
InitHashTable(&(lsh->tables[i].tables), data.tableSize,
sizeof(value), data.print, data.distance, data.G,
NULL);
//insert each value in hash table
for(j=0; j<GetDataSize(); j++){
GetIthData(j,&value);
InsertHashTable(lsh->tables[i].tables, &value,
lsh->tables[i].GConfiguration);
//value = GetNextData();
}
}
}
double SearchANN(LSH* lsh, value query, value** result) {
int i;
int retrieved = 0;
double cur_distance = 0.0;
double distance = 10000.0;
value* lValueN = NULL;
for (i = 0; i < lsh->L; i++) {
//gets bucket where the value is
List bucketList = GetListHashTable(lsh->tables[i].tables, &query,
lsh->tables[i].GConfiguration);
value* lValue = GetFirst(bucketList);
// algorithm from theory
while (lValue != NULL) {
retrieved++;
if (retrieved > 5 * lsh->L) {
*result = lValueN;
return distance;
}
cur_distance = data.distance(lValue, &query);
if (cur_distance < distance) {
lValueN = lValue;
distance = cur_distance;
}
lValue = GetNext(bucketList);
}
}
*result = lValueN;
return distance;
}
void SearchRange(LSH* lsh, value query, double range, List neighbours) {
int i;
for (i = 0; i < lsh->L; i++) {
//gets bucket where the value is
List bucketList = GetListHashTable(lsh->tables[i].tables,
&query, lsh->tables[i].GConfiguration);
value* lValue = GetFirst(bucketList);
//get all neighbours in range an place them in a list
while (lValue != NULL) {
if (data.inRange(lValue, &query, range)) {
InsertValueList(neighbours, lValue);
}
lValue = GetNext(bucketList);
}
}
}
int MarkPointsOf(LSH* lsh,int cluster, double range) {
int i;
int pointsN=0;
value query;
GetIthCentroid(cluster,&query);
for (i = 0; i < lsh->L; i++) {
//gets bucket where the value is
List bucketList = GetListHashTable(lsh->tables[i].tables,
&query, lsh->tables[i].GConfiguration);
value* lValue = GetFirstBarrier(bucketList);
//get all neighbours in range an place them in a list
while (lValue != NULL) {
double dist = data.distance(lValue, &query);
// printf("%f\n", dist);
// printf("%p\n", lValue);
if ( dist < range) {
int assigned=AssignLSH(lValue, dist, cluster, range);
// InsertValueList(neighbours, lValue);
if( assigned){
//AddToBarrier(bucketList);
pointsN++;
}
}
lValue = GetNextBarrier(bucketList);
}
}
return pointsN;
}
void InitBarrierLSH(LSH* lsh){
int i=0;
for(i=0;i<lsh->L;i++){
InitBarrierHash(lsh->tables[i].tables);
}
}
void DestroyLSH(LSH** lshptr) {
int i;
LSH *lsh = *lshptr;
for(i=0; i<lsh->L; i++){
data.destroyStruct(&(lsh->tables[i].GConfiguration));
DestroyHashTable(&(lsh->tables[i].tables));
}
free(lsh->tables);
free(*lshptr);
}