Spaces:
Sleeping
Sleeping
File size: 1,255 Bytes
daea7f9 |
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 |
#include "hash_table.h"
HashTable::HashTable(size_t numPlanes, size_t dimension) : numPlanes(numPlanes), dimension(dimension)
{
generateRandomHyperplanes();
}
void HashTable::generateRandomHyperplanes()
{
randomHyperplanes.resize(numPlanes, vector<double>(dimension));
std::random_device rd;
std::mt19937 gen(rd());
std::normal_distribution<double> dist(0.0, 1.0);
for (size_t i = 0; i < numPlanes; ++i)
{
double norm = 0.0;
for (size_t j = 0; j < dimension; ++j)
{
randomHyperplanes[i][j] = dist(gen);
norm += randomHyperplanes[i][j] * randomHyperplanes[i][j];
}
norm = std::sqrt(norm);
for (size_t j = 0; j < dimension; ++j)
randomHyperplanes[i][j] /= norm;
}
}
size_t HashTable::hashFunction(const vector<double> &featureVector)
{
size_t hashValue = 0;
for (size_t i = 0; i < numPlanes; ++i)
{
double dotProduct = 0;
for (size_t j = 0; j < dimension; ++j)
{
dotProduct += featureVector[j] * randomHyperplanes[i][j];
}
if (dotProduct > 0)
{
hashValue |= (1 << i);
}
}
return hashValue;
} |