Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the two loops variant of this min heap implementation, which is a slight optimization over the current logic #221

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ public final class NeighboursBruteForce implements NeighboursQuery {
public List<Pair<Integer, Double>> query(SGDVector point, int k) {
PriorityQueue<MutablePair> queue = new PriorityQueue<>(k);

for (int neighbor = 0; neighbor < data.length; neighbor++) {
for (int neighbor = 0; neighbor < data.length && neighbor < k; neighbor++) {
double distance = DistanceType.getDistance(point, data[neighbor], distanceType);
if (queue.size() < k) {
MutablePair newPair = new MutablePair(neighbor, distance);
queue.offer(newPair);
} else if (Double.compare(distance, queue.peek().value) < 0) {
MutablePair newPair = new MutablePair(neighbor, distance);
queue.offer(newPair);
}

for (int neighbor = k; neighbor < data.length; neighbor++) {
double distance = DistanceType.getDistance(point, data[neighbor], distanceType);
if (Double.compare(distance, queue.peek().value) < 0) {
MutablePair pair = queue.poll();
pair.index = neighbor;
pair.value = distance;
Expand Down