-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhashFunction.cpp
217 lines (177 loc) · 6.4 KB
/
hashFunction.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
213
214
215
216
217
#include "hashFunction.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
//prototype gia thn hashFunction dn kanei kati
template <class T>
HashFunction<T>::HashFunction()
{
}
template <class T>
unsigned int HashFunction<T>::HashFunctionHash(T key) //prototype gia thn hashfunction
{
}
//---------------------------------------------------------
HashFunction<ClusterNode<Vector*>*>::HashFunction(int K, int Dimensions):kHashFunctions(K), dimensions(Dimensions)
{
gFunction = new HashFunctionCosine*[kHashFunctions]();
for(int i=0; i< kHashFunctions; i++)
{
gFunction[i] = new HashFunctionCosine(dimensions);
}
}
HashFunction<ClusterNode<Vector*>*>::~HashFunction()
{
for(int i =0; i < kHashFunctions; i++)
{
delete gFunction[i];
}
delete []gFunction;
}
unsigned int HashFunction<ClusterNode<Vector*>*>::HashFunctionHash(ClusterNode<Vector*>* key) //pairnoume thn timh ths g
{
std::bitset<32> fBit; //mexri 2^32 buckets
for(int i=0; i < kHashFunctions; i++) //gia kathe hi
{
if(gFunction[i]->value(key->getPoint())) //true
{
fBit[kHashFunctions-i-1] = 1; // h prwth h synartisi kathorizei to prwto bit tou bitset ara pairnoume to k-i-1 giati o pinakas ksekinaei apo k-1.
}
else
{
fBit[kHashFunctions-i-1] = 0;
}
}
return fBit.to_ulong();
}
//---------------------------------------------
HashFunction<ClusterNode<Hamming*>*>::HashFunction(int K, int noBts):kHashFunctions(K), noBits(noBts)
{
gFunction = new int[kHashFunctions];
for(int i =0; i < kHashFunctions; i++)
{
gFunction[i] = randomNumberInt(0,noBts-1); //times apo 0 mexri number of bits-1
}
}
HashFunction<ClusterNode<Hamming*>*>::~HashFunction()
{
delete []gFunction;
}
unsigned int HashFunction<ClusterNode<Hamming*>*>::HashFunctionHash(ClusterNode<Hamming*>* key) //pairnoume thn timh ths g
{
std::bitset<32> fBit; //2^32
std::bitset<64> inputBitset; //h thesi 63 afora to pio simantiko bit kai h 0 to ligotero simantiko
key->getPoint()->get_bitString(inputBitset);
for(int i=0;i<kHashFunctions;i++) //gia kathe hi
{
fBit[kHashFunctions-i-1]=inputBitset[noBits-gFunction[i]-1]; //ksekiname apo to pio simantiko bit gia to prwto h.
}
return fBit.to_ulong();
}
//------------------------------------------------
HashFunction<ClusterNode<EuclideanNode*>*>::HashFunction(int K, int Dimensions, int W, int noBuckets):kHashFunctions(K), dimensions(Dimensions), nBuckets(noBuckets)
{
gFunction = new HashFunctionEuclidean*[kHashFunctions]();
for(int i=0; i< kHashFunctions; i++)
{
gFunction[i] = new HashFunctionEuclidean(dimensions, W);
}
rVariables = new int[kHashFunctions];
for(int i=0; i< kHashFunctions;i++)
{
rVariables[i] = randomNumberInt(-32766, 32767);//enas tyxaios arithmos 16 bit
}
}
HashFunction<ClusterNode<EuclideanNode*>*>::~HashFunction()
{
for(int i =0; i < kHashFunctions; i++)
{
delete gFunction[i];
}
delete []gFunction;
delete []rVariables;
}
unsigned int HashFunction<ClusterNode<EuclideanNode*>*>::HashFunctionHash(ClusterNode<EuclideanNode*>* key)
{
return modulo(this->hashFunctionID(key), nBuckets);
}
int HashFunction<ClusterNode<EuclideanNode*>*>::hashFunctionID(ClusterNode<EuclideanNode*>* key)
{
int result =0;
for(int i=0; i < kHashFunctions; i++)
{
result += rVariables[i]*gFunction[i]->value(key->getPoint()->get_vector())%MAXINT; //eswteriko ginomeno mod M
}
return result%MAXINT;
}
//--------------------------------------------------------------------------------
HashFunction<ClusterNode<MatrixPoint*>*>::HashFunction(int K, List<ClusterNode<MatrixPoint*>*>* myInput): kHashFunctions(K)
{
int x1, x2;
double distx1x2, Median;
ClusterNode<MatrixPoint*>* x1M = NULL;
ClusterNode<MatrixPoint*>* x2M = NULL;
double* myArray = new double[myInput->get_begin()->get_data()->getPoint()->get_noItems()];
gFunction = new HashFunctionMatrix*[kHashFunctions]();
for(int i=0; i< kHashFunctions; i++) //dimiourgoume ta k hi
{
x1 = randomNumberInt(0, myInput->get_begin()->get_data()->getPoint()->get_noItems()-1); //ena tyxaio shmeio
while((x2 = randomNumberInt(0, myInput->get_begin()->get_data()->getPoint()->get_noItems()-1))==x1); //oxi ta idia shmeia giati h metaksy tous apostash pou einai mhden mpainei ston paranomasth
x1M = getPoint(myInput, x1); //vriskoume to random shmeio
x2M = getPoint(myInput, x2);
distx1x2 = x1M->getPoint()->get_distance(x2); //ypologisoume thn apostash metaksy tous
gFunction[i] = new HashFunctionMatrix(x1, x2, distx1x2);
int k=0;
for(Node<ClusterNode<MatrixPoint*>*>* j = myInput->get_begin(); j != NULL; j = j->get_next())//ypologizw ola thn timh tou h gia kathe x
{
myArray[k] = gFunction[i]->dCalculation(j->get_data()->getPoint());
k++;
}
Median = median(myArray, myInput->get_begin()->get_data()->getPoint()->get_noItems()); //vriskoume thn median
gFunction[i]->setMedian(Median);
}
delete[] myArray;
}
HashFunction<ClusterNode<MatrixPoint*>*>::~HashFunction()
{
for(int i =0; i < kHashFunctions; i++)
{
delete gFunction[i];
}
delete []gFunction;
}
unsigned int HashFunction<ClusterNode<MatrixPoint*>*>::HashFunctionHash(ClusterNode<MatrixPoint*>* key) //epistrefei thn g
{
std::bitset<32> fBit; //2^32
for(int i=0; i < kHashFunctions; i++)
{
if(gFunction[i]->value(key->getPoint())) //true
{
fBit[kHashFunctions-i-1] = 1;
}
else
{
fBit[kHashFunctions-i-1] = 0;
}
}
return fBit.to_ulong();
}
ClusterNode<MatrixPoint*>* HashFunction<ClusterNode<MatrixPoint*>*>::getPoint(List<ClusterNode<MatrixPoint*>*>* myList, int pos) //epistrefei to tyxaio simeio
{
for(Node<ClusterNode<MatrixPoint*>*>* i = myList->get_begin(); i != NULL; i = i->get_next())
{
if(pos == i->get_data()->getPoint()->get_pos())
{
return i->get_data();
}
}
return NULL;
}
unsigned int modulo(int x1, int x2)
{
return (x1%x2+x2)%x2;
}
template class HashFunction<ClusterNode<Vector*>*>;
template class HashFunction<ClusterNode<Hamming*>*>;
template class HashFunction<ClusterNode<EuclideanNode*>*>;
template class HashFunction<ClusterNode<MatrixPoint*>*>;