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

use rust 2021 and fix clippy warnings #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "hnsw"
version = "0.11.1-alpha.0"
authors = ["Geordon Worley <[email protected]>"]
edition = "2018"
edition = "2021"
description = "Fast approximate nearest neighbors"
keywords = ["hamming", "distance", "nearest", "neighbor", "search"]
categories = ["algorithms", "data-structures", "science"]
Expand Down
2 changes: 1 addition & 1 deletion ensure_no_std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "ensure_no_std"
version = "0.1.0"
authors = ["Geordon Worley <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
hnsw = { path = ".." }
Expand Down
4 changes: 2 additions & 2 deletions examples/recall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ fn process<const M: usize, const M0: usize>(opt: &Opt) -> (Vec<f64>, Vec<f64>) {
};
opt.k
];
let stats = easybench::bench_env(dest, |mut dest| {
let stats = easybench::bench_env(dest, |dest| {
let mut refmut = state.borrow_mut();
let (searcher, query) = &mut *refmut;
let (ix, query_feature) = query.next().unwrap();
let correct_worst_distance = correct_worst_distances[ix];
// Go through all the features.
for &mut neighbor in hnsw.nearest(&query_feature, ef, searcher, &mut dest) {
for &mut neighbor in hnsw.nearest(&query_feature, ef, searcher, dest) {
// Any feature that is less than or equal to the worst real nearest neighbor distance is correct.
if Euclidean.distance(&search_space[neighbor.index], &query_feature)
<= correct_worst_distance
Expand Down
36 changes: 18 additions & 18 deletions src/hnsw/hnsw_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,20 @@ where
///
/// The `item` must be retrieved from [`HNSW::search_layer`].
pub fn feature(&self, item: usize) -> &T {
&self.features[item as usize]
&self.features[item]
}

/// Extract the feature from a particular level for a given item returned by [`HNSW::search_layer`].
pub fn layer_feature(&self, level: usize, item: usize) -> &T {
&self.features[self.layer_item_id(level, item) as usize]
&self.features[self.layer_item_id(level, item)]
}

/// Retrieve the item ID for a given layer item returned by [`HNSW::search_layer`].
pub fn layer_item_id(&self, level: usize, item: usize) -> usize {
if level == 0 {
item
} else {
self.layers[level][item as usize].zero_node
self.layers[level][item].zero_node
}
}

Expand Down Expand Up @@ -319,16 +319,16 @@ where
cap: usize,
) {
while let Some(Neighbor { index, .. }) = searcher.candidates.pop() {
for neighbor in layer[index as usize].neighbors() {
let neighbor_node = &layer[neighbor as usize];
for neighbor in layer[index].neighbors() {
let neighbor_node = &layer[neighbor];
// Don't visit previously visited things. We use the zero node to allow reusing the seen filter
// across all layers since zero nodes are consistent among all layers.
// TODO: Use Cuckoo Filter or Bloom Filter to speed this up/take less memory.
if searcher.seen.insert(neighbor_node.zero_node) {
// Compute the distance of this neighbor.
let distance = self
.metric
.distance(q, &self.features[neighbor_node.zero_node as usize]);
.distance(q, &self.features[neighbor_node.zero_node]);
// Attempt to insert into nearest queue.
let pos = searcher.nearest.partition_point(|n| n.distance <= distance);
if pos != cap {
Expand All @@ -339,7 +339,7 @@ where
}
// Either way, add the new item.
let candidate = Neighbor {
index: neighbor as usize,
index: neighbor,
distance,
};
searcher.nearest.insert(pos, candidate);
Expand All @@ -353,13 +353,13 @@ where
/// Greedily finds the approximate nearest neighbors to `q` in the zero layer.
fn search_zero_layer(&self, q: &T, searcher: &mut Searcher<Met::Unit>, cap: usize) {
while let Some(Neighbor { index, .. }) = searcher.candidates.pop() {
for neighbor in self.zero[index as usize].neighbors() {
for neighbor in self.zero[index].neighbors() {
// Don't visit previously visited things. We use the zero node to allow reusing the seen filter
// across all layers since zero nodes are consistent among all layers.
// TODO: Use Cuckoo Filter or Bloom Filter to speed this up/take less memory.
if searcher.seen.insert(neighbor) {
// Compute the distance of this neighbor.
let distance = self.metric.distance(q, &self.features[neighbor as usize]);
let distance = self.metric.distance(q, &self.features[neighbor]);
// Attempt to insert into nearest queue.
let pos = searcher.nearest.partition_point(|n| n.distance <= distance);
if pos != cap {
Expand All @@ -370,7 +370,7 @@ where
}
// Either way, add the new item.
let candidate = Neighbor {
index: neighbor as usize,
index: neighbor,
distance,
};
searcher.nearest.insert(pos, candidate);
Expand All @@ -392,7 +392,7 @@ where
let &Neighbor { index, distance } = searcher.nearest.first().unwrap();
searcher.nearest.clear();
// Update the node to the next layer.
let new_index = layer[index].next_node as usize;
let new_index = layer[index].next_node;
let candidate = Neighbor {
index: new_index,
distance,
Expand Down Expand Up @@ -427,7 +427,7 @@ where
/// Gets the entry point's feature.
fn entry_feature(&self) -> &T {
if let Some(last_layer) = self.layers.last() {
&self.features[last_layer[0].zero_node as usize]
&self.features[last_layer[0].zero_node]
} else {
&self.features[0]
}
Expand All @@ -446,11 +446,11 @@ where
let new_index = self.zero.len();
let mut neighbors: [usize; M0] = [!0; M0];
for (d, s) in neighbors.iter_mut().zip(nearest.iter()) {
*d = s.index as usize;
*d = s.index;
}
let node = NeighborNodes { neighbors };
for neighbor in node.neighbors() {
self.add_neighbor(q, new_index as usize, neighbor, layer);
self.add_neighbor(q, new_index, neighbor, layer);
}
self.zero.push(node);
} else {
Expand Down Expand Up @@ -498,9 +498,9 @@ where
// In this case we did find the first spot where the target was empty within the slice.
// Now we add the neighbor to this slot.
if layer == 0 {
self.zero[target_ix as usize].neighbors[empty_point] = node_ix;
self.zero[target_ix].neighbors[empty_point] = node_ix;
} else {
self.layers[layer - 1][target_ix as usize]
self.layers[layer - 1][target_ix]
.neighbors
.neighbors[empty_point] = node_ix;
}
Expand Down Expand Up @@ -534,9 +534,9 @@ where
// This is also different for the zero layer.
if self.metric.distance(q, target_feature) < worst_distance {
if layer == 0 {
self.zero[target_ix as usize].neighbors[worst_ix] = node_ix;
self.zero[target_ix].neighbors[worst_ix] = node_ix;
} else {
self.layers[layer - 1][target_ix as usize]
self.layers[layer - 1][target_ix]
.neighbors
.neighbors[worst_ix] = node_ix;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn linear_1_nn_inliers() {
.cloned()
.map(|mut feature| {
for bit in 0..128 {
let choice: bool = prng_bit_chooser.sample(&bernoulli);
let choice: bool = prng_bit_chooser.sample(bernoulli);
feature[bit / 8] ^= (choice as u8) << (bit % 8);
}
feature
Expand Down