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

Clippy fixes #34

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
38 changes: 16 additions & 22 deletions src/hnsw/hnsw_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,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 @@ -322,11 +322,11 @@ where
) {
while let Some(Neighbor { index, .. }) = searcher.candidates.pop() {
for neighbor in match layer {
Layer::NonZero(layer) => layer[index as usize].get_neighbors(),
Layer::Zero => self.zero[index as usize].get_neighbors(),
Layer::NonZero(layer) => layer[index].get_neighbors(),
Layer::Zero => self.zero[index].get_neighbors(),
} {
let node_to_visit = match layer {
Layer::NonZero(layer) => layer[neighbor as usize].zero_node,
Layer::NonZero(layer) => layer[neighbor].zero_node,
Layer::Zero => neighbor,
};

Expand All @@ -335,9 +335,7 @@ where
// TODO: Use Cuckoo Filter or Bloom Filter to speed this up/take less memory.
if searcher.seen.insert(node_to_visit) {
// Compute the distance of this neighbor.
let distance = self
.metric
.distance(q, &self.features[node_to_visit as usize]);
let distance = self.metric.distance(q, &self.features[node_to_visit]);
// Attempt to insert into nearest queue.
let pos = searcher.nearest.partition_point(|n| n.distance <= distance);
if pos != cap {
Expand All @@ -348,7 +346,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 @@ -375,7 +373,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 @@ -410,7 +408,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 @@ -429,11 +427,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.get_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 @@ -481,11 +479,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]
.neighbors
.neighbors[empty_point] = node_ix;
self.layers[layer - 1][target_ix].neighbors.neighbors[empty_point] = node_ix;
}
} else {
// Otherwise, we need to find the worst neighbor currently.
Expand Down Expand Up @@ -517,11 +513,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]
.neighbors
.neighbors[worst_ix] = node_ix;
self.layers[layer - 1][target_ix].neighbors.neighbors[worst_ix] = node_ix;
}
}
}
Expand Down