-
Notifications
You must be signed in to change notification settings - Fork 10
/
lsh.h
134 lines (111 loc) · 4.51 KB
/
lsh.h
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
// slash - a locality sensitive hashing library.
// Copyright (c) 2013 Utkan Güngördü <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef SLASH_LSH_H
#define SLASH_LSH_H
#include <assert.h>
#include <stdint.h>
#include <vector>
#include <google/sparse_hash_map>
#include "types.h"
#include "querycontext.h"
namespace slash {
template <class FeatureVector>
class bin : public
google::sparse_hash_map<HashType, std::vector<const FeatureVector*> > {
};
template <class FeatureVector>
class hashCache : public
google::sparse_hash_map<const FeatureVector*, HashType*, FeatureVector, FeatureVector> {
};
// Class lsh implements Locality-Sensitive Hashing algorithm.
// A. Gionis, P. Indyk and R. Motwani, ``Similarity Search in High Dimensions via Hashing'',
// Proc. 25th International Conference on Very Large Data Bases, VLDB1999, pp.518-529, 1999.
template <class FeatureVector, class Hasher>
class LSH {
public:
// d, k and L refer to the variables found in the LSH literature:
//
// d is the dimension of the feature space.
// k is the number of elementary hash functions (h) to be concataneted to obtain a reliable enough hash function (g). LSH queries becomes more selective with increasing k, due to the reduced the probability of collision.
// L is the number of "copies" of the bins (with a different random matrices). Increasing L will increase the number of points the should be scanned linearly during query.
// cacheHashes enables caching of hashes, which speeds up queries at the expense of extra memory. It also reduces the strain on memory allocator.
LSH(int d, int k, int L, Hasher *hasher) : d(d), k(k), l(L), hasher(hasher) {
this->bins = new bin<FeatureVector>[this->l];
}
~LSH() {
for(auto &item: this->cache) {
delete [] item.second;
}
delete [] this->bins;
}
// Hashes given points from the feature space, making them avaiable
// for queries.
// A FeatureVector must not be inserted more than once.
void Insert(const std::vector<FeatureVector> &points) {
size_t nPoints = points.size();
for (size_t j = 0; j < nPoints; j++) {
const FeatureVector& p = points[j];
assert(this->cache[&p] == nullptr);
/* if (this->cache[&p] != nullptr) {
continue;
} */
auto g = new HashType[this->l];
this->cache[&p] = g;
this->hasher->Hash(p, g);
for (size_t i = 0; i < (size_t)this->l; i++) {
this->bins[i][g[i]].push_back(&p);
}
}
for (size_t i = 0; i < (size_t)this->l; i++) {
for (auto &item: bins[i]) {
item.second.shrink_to_fit();
}
}
}
// Returns nearest neighbors of p; at most limit entries.
// Runs in sublinear time O(n^ρ). The exponent ρ depends on the hashing function,
// and the parameters d, k, L.
// p must be Insert'ed before the Query.
std::vector<FeatureVector> Query(const FeatureVector &p, int limit, size_t *linearSearchSize = nullptr) {
QueryContext<FeatureVector> c(limit+1);
auto g = this->cache[&p];
if (g == nullptr) {
return c.Neighbors();
}
for (size_t i = 0; i < (size_t)this->l; i++) {
auto &v = bins[i][g[i]];
size_t vSize = v.size();
if (linearSearchSize != nullptr) {
*linearSearchSize += vSize;
}
for (size_t j = 0; j < vSize; j++) {
auto &q = *v[j];
c.Insert(q, p.Similarity(q), q.NCopies());
}
}
c.shrink();
return c.Neighbors();
}
private:
int d; // the dimension of the feature space.
int k; // number of elementary hash functions (h) to be concataneted to obtain a reliable enough hash function (g). LSH queries becomes more selective with increasing k, due to the reduced the probability of collision.
int l; // number of "copies" of the bins (with a different random matrices). Increasing L will increase the number of points the should be scanned linearly during query.
Hasher *hasher;
bin<FeatureVector> *bins; // bins[bin][hash] gives the FeatureVector that is hashed to hash in the bin bins[bin].
hashCache<FeatureVector> cache;
};
};
#endif // SLASH_LSH_H