Skip to content

Commit 66efaaa

Browse files
committed
Renamed variables
1 parent 0843e72 commit 66efaaa

File tree

4 files changed

+34
-33
lines changed

4 files changed

+34
-33
lines changed

CPU/computations.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef ITEMS_H
2-
#define ITEMS_H
1+
#ifndef COMPUTATIONS_H
2+
#define COMPUTATIONS_H
33

44
#include <vector>
55
#include <fstream>

Project Document.docx

2.06 KB
Binary file not shown.

hnsw_implementation/hnsw.cpp

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
#include <limits>
1313
using namespace std;
1414

15-
vector<int> HNSWGraph::searchLayer(Item& q, int entry_point, int ef, int lc) {
16-
if (lc < 0 || lc >= layerEdgeLists.size()) {
15+
vector<int> HNSWGraph::searchLayer(Item& query, int entry_point, int efficient, int layers) {
16+
if (layers < 0 || layers >= layerEdgeLists.size()) {
1717
cerr << "Error: Invalid layer index." << endl;
1818
exit(EXIT_FAILURE);
1919
}
2020
set<pair<double, int>> candidates;
2121
set<pair<double, int>> nearestNeighbors;
2222
unordered_set<int> isVisited;
2323

24-
double td = q.dist(items[entry_point]);
24+
double td = query.dist(items[entry_point]);
2525
candidates.insert(make_pair(td, entry_point));
2626
nearestNeighbors.insert(make_pair(td, entry_point));
2727
isVisited.insert(entry_point);
@@ -33,18 +33,18 @@ vector<int> HNSWGraph::searchLayer(Item& q, int entry_point, int ef, int lc) {
3333
if (ci->first > fi->first) {
3434
break;
3535
}
36-
for (int ed: layerEdgeLists[lc][nid]) {
36+
for (int ed: layerEdgeLists[layers][nid]) {
3737
if (isVisited.find(ed) != isVisited.end()) {
3838
continue;
3939
}
4040
fi = nearestNeighbors.end();
4141
fi--;
4242
isVisited.insert(ed);
43-
td = q.dist(items[ed]);
44-
if ((td < fi->first) || nearestNeighbors.size() < ef) {
43+
td = query.dist(items[ed]);
44+
if ((td < fi->first) || nearestNeighbors.size() < efficient) {
4545
candidates.insert(make_pair(td, ed));
4646
nearestNeighbors.insert(make_pair(td, ed));
47-
if (nearestNeighbors.size() > ef) {
47+
if (nearestNeighbors.size() > efficient) {
4848
nearestNeighbors.erase(fi);
4949
}
5050
}
@@ -55,7 +55,7 @@ vector<int> HNSWGraph::searchLayer(Item& q, int entry_point, int ef, int lc) {
5555
return results;
5656
}
5757

58-
vector<int> HNSWGraph::KNNSearch(Item& q, int K, int N) {
58+
vector<int> HNSWGraph::KNNSearch(Item& query, int K, int N) {
5959

6060
if (K <= 0 || std::ceil(K) != K || K > std::numeric_limits<int>::max()) {
6161
cerr << "Error: Invalid value of K for KNNSearch." << endl;
@@ -67,30 +67,31 @@ vector<int> HNSWGraph::KNNSearch(Item& q, int K, int N) {
6767
exit(EXIT_FAILURE);
6868
}
6969

70-
int maxLyer = layerEdgeLists.size() - 1;
70+
int maxLayer = layerEdgeLists.size() - 1;
7171
int entry_point = enterNode;
72-
for (int l = maxLyer; l >= 1; l--) {
73-
entry_point = searchLayer(q, entry_point, 1, l)[0];
72+
for (int l = maxLayer; l >= 1; l--) {
73+
entry_point = searchLayer(query, entry_point, 1, l)[0];
7474
}
75-
return searchLayer(q, entry_point, K, 0);
75+
return searchLayer(query, entry_point, K, 0);
7676
}
7777

78-
void HNSWGraph::addEdge(int st, int ed, int lc) {
79-
if (lc < 0 || lc >= layerEdgeLists.size()) {
78+
void HNSWGraph::addEdge(int st, int ed, int layers) {
79+
if (layers < 0 || layers >= layerEdgeLists.size()) {
8080
cerr << "Error: Invalid layer index." << endl;
8181
return;
8282
}
8383
if (st == ed) {
8484
return;
8585
}
86-
layerEdgeLists[lc][st].push_back(ed);
87-
layerEdgeLists[lc][ed].push_back(st);
86+
layerEdgeLists[layers][st].push_back(ed);
87+
layerEdgeLists[layers][ed].push_back(st);
8888
}
8989

90-
void HNSWGraph::Insert(Item& q) {
90+
void HNSWGraph::Insert(Item& query) {
9191
int nid = items.size();
92-
itemNum++; items.push_back(q);
93-
int maxLyer = layerEdgeLists.size() - 1;
92+
itemNum++;
93+
items.push_back(query);
94+
int maxLayer = layerEdgeLists.size() - 1;
9495
int l = 0;
9596
uniform_real_distribution<double> distribution(0.0,1.0);
9697
while(l < ml && (1.0 / ml <= distribution(generator))) {
@@ -104,20 +105,20 @@ void HNSWGraph::Insert(Item& q) {
104105
return;
105106
}
106107
int entry_point = enterNode;
107-
for (int i = maxLyer; i > l; i--) {
108-
entry_point = searchLayer(q, entry_point, 1, i)[0];
108+
for (int i = maxLayer; i > l; i--) {
109+
entry_point = searchLayer(query, entry_point, 1, i)[0];
109110
}
110111
#pragma omp parallel for
111-
for (int i = min(l, maxLyer); i >= 0; i--) {
112+
for (int i = min(l, maxLayer); i >= 0; i--) {
112113
int MM = l == 0 ? MMax0 : MMax;
113-
vector<int> neighbors = searchLayer(q, entry_point, efConstruction, i);
114-
vector<int> selectedNeighbors = vector<int>(neighbors.begin(), neighbors.begin()+min(int(neighbors.size()), M));
115-
for (size_t j = 0; j < selectedNeighbors.size(); j++) {
116-
int n = selectedNeighbors[j];
114+
vector<int> neighbours = searchLayer(query, entry_point, efficientConstruction, i);
115+
vector<int> selectedNeighbours = vector<int>(neighbours.begin(), neighbours.begin()+min(int(neighbours.size()), M));
116+
for (size_t j = 0; j < selectedNeighbours.size(); j++) {
117+
int n = selectedNeighbours[j];
117118
addEdge(n, nid, i);
118119
}
119-
for (size_t j = 0; j < selectedNeighbors.size(); j++) {
120-
int n = selectedNeighbors[j];
120+
for (size_t j = 0; j < selectedNeighbours.size(); j++) {
121+
int n = selectedNeighbours[j];
121122
if (layerEdgeLists[i][n].size() > MM) {
122123
vector<pair<double, int>> distPairs;
123124
for (int nn: layerEdgeLists[i][n]) {
@@ -130,7 +131,7 @@ void HNSWGraph::Insert(Item& q) {
130131
}
131132
}
132133
}
133-
entry_point = selectedNeighbors[0];
134+
entry_point = selectedNeighbours[0];
134135
}
135136
if (l == layerEdgeLists.size() - 1) {
136137
enterNode = nid;

hnsw_implementation/hnsw.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ using namespace std;
1111

1212

1313
struct HNSWGraph {
14-
HNSWGraph(int _M, int _MMax, int _MMax0, int _efConstruction, int _ml):M(_M),MMax(_MMax),MMax0(_MMax0),efConstruction(_efConstruction),ml(_ml){
14+
HNSWGraph(int _M, int _MMax, int _MMax0, int _efficientConstruction, int _ml):M(_M),MMax(_MMax),MMax0(_MMax0),efficientConstruction(_efficientConstruction),ml(_ml){
1515
layerEdgeLists.push_back(unordered_map<int, vector<int>>());
1616
}
1717
int M;
1818
int MMax;
1919
int MMax0;
20-
int efConstruction;
20+
int efficientConstruction;
2121
int ml;
2222
int itemNum;
2323
vector<Item> items;

0 commit comments

Comments
 (0)