fn add_neighbor(&mut self, q: &T, node_ix: usize, target_ix: usize, layer: usize) {
        // Get the feature for the target and get the neighbor slice for the target.
        // This is different for the zero layer.
        let (target_feature, target_neighbors) = if layer == 0 {
            (
                &self.features[target_ix],
                &self.zero[target_ix].neighbors[..],
            )
        } else {
            let target = &self.layers[layer - 1][target_ix];
            (
                &self.features[target.zero_node],
                &target.neighbors.neighbors[..],
            )
        };

        // Check if there is a point where the target has empty neighbor slots and add it there in that case.
        let empty_point = target_neighbors.partition_point(|&n| n != !0);
        if empty_point != target_neighbors.len() {
            // 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;
            } else {
                self.layers[layer - 1][target_ix as usize]
                    .neighbors
                    .neighbors[empty_point] = node_ix;
            }
        } else {
            // Otherwise, we need to find the worst neighbor currently.
            let (worst_ix, worst_distance) = target_neighbors
                .iter()
                .enumerate()
                .filter_map(|(ix, &n)| {
                    // Compute the distance to be higher than possible if the neighbor is not filled yet so its always filled.
                    if n == !0 {
                        None
                    } else {
                        // Compute the distance. The feature is looked up differently for the zero layer.
                        let distance = self.metric.distance(
                            target_feature,
                            &self.features[if layer == 0 {
                                n
                            } else {
                                self.layers[layer - 1][n].zero_node
                            }],
                        );
                        Some((ix, distance))
                    }
                })
                // This was done instead of max_by_key because min_by_key takes the first equally bad element.
                .min_by_key(|&(_, distance)| core::cmp::Reverse(distance))
                .unwrap();

            // If this is better than the worst, insert it in the worst's place.
            // 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;
                } else {
                    self.layers[layer - 1][target_ix as usize]
                        .neighbors
                        .neighbors[worst_ix] = node_ix;
                }
            }
        }
    }
/// Attempts to add a neighbor to a target node.
Attempts to add a neighbor to a target node.
Attempts to add a neighbor to a target node .
fn add_neighbor(&mut self, q: &T, node_ix: usize, target_ix: usize, layer: usize) {
        // Get the feature for the target and get the neighbor slice for the target.
        // This is different for the zero layer.
        let (target_feature, target_neighbors) = if layer == 0 {
            (
                &self.features[target_ix],
                &self.zero[target_ix].neighbors[..],
            )
        } else {
            let target = &self.layers[layer - 1][target_ix];
            (
                &self.features[target.zero_node],
                &target.neighbors.neighbors[..],
            )
        };
        // Check if there is a point where the target has empty neighbor slots and add it there in that case.
        let empty_point = target_neighbors.partition_point(|&n| n != !0);
        if empty_point != target_neighbors.len() {
            // 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;
            } else {
                self.layers[layer - 1][target_ix as usize]
                    .neighbors
                    .neighbors[empty_point] = node_ix;
            }
        } else {
            // Otherwise, we need to find the worst neighbor currently.
            let (worst_ix, worst_distance) = target_neighbors
                .iter()
                .enumerate()
                .filter_map(|(ix, &n)| {
                    // Compute the distance to be higher than possible if the neighbor is not filled yet so its always filled.
                    if n == !0 {
                        None
                    } else {
                        // Compute the distance. The feature is looked up differently for the zero layer.
                        let distance = self.metric.distance(
                            target_feature,
                            &self.features[if layer == 0 {
                                n
                            } else {
                                self.layers[layer - 1][n].zero_node
                            }],
                        );
                        Some((ix, distance))
                    }
                })
                // This was done instead of max_by_key because min_by_key takes the first equally bad element.
                .min_by_key(|&(_, distance)| core::cmp::Reverse(distance))
                .unwrap();
            // If this is better than the worst, insert it in the worst's place.
            // 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;
                } else {
                    self.layers[layer - 1][target_ix as usize]
                        .neighbors
                        .neighbors[worst_ix] = node_ix;
                }
            }
        }
    }
fn add_neighbor ( & mut self , q : & T , node_ix : usize , target_ix : usize , layer : usize ) { // Get the feature for the target and get the neighbor slice for the target. // This is different for the zero layer. let ( target_feature , target_neighbors ) = if layer == 0 { ( & self . features [ target_ix ] , & self . zero [ target_ix ] . neighbors [ .. ] , ) } else { let target = & self . layers [ layer - 1 ] [ target_ix ] ; ( & self . features [ target . zero_node ] , & target . neighbors . neighbors [ .. ] , ) } ; // Check if there is a point where the target has empty neighbor slots and add it there in that case. let empty_point = target_neighbors . partition_point ( | & n | n != ! 0 ) ; if empty_point != target_neighbors . len ( ) { // 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 ; } else { self . layers [ layer - 1 ] [ target_ix as usize ] . neighbors . neighbors [ empty_point ] = node_ix ; } } else { // Otherwise, we need to find the worst neighbor currently. let ( worst_ix , worst_distance ) = target_neighbors . iter ( ) . enumerate ( ) . filter_map ( | ( ix , & n ) | { // Compute the distance to be higher than possible if the neighbor is not filled yet so its always filled. if n == ! 0 { None } else { // Compute the distance. The feature is looked up differently for the zero layer. let distance = self . metric . distance ( target_feature , & self . features [ if layer == 0 { n } else { self . layers [ layer - 1 ] [ n ] . zero_node } ] , ) ; Some ( ( ix , distance ) ) } } ) // This was done instead of max_by_key because min_by_key takes the first equally bad element. . min_by_key ( | & ( _ , distance ) | core :: cmp :: Reverse ( distance ) ) . unwrap ( ) ; // If this is better than the worst, insert it in the worst's place. // 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 ; } else { self . layers [ layer - 1 ] [ target_ix as usize ] . neighbors . neighbors [ worst_ix ] = node_ix ; } } } }
Attempts to add a neighbor to a target node.
Attempts to add a neighbor to a target node .
&mut self
q
&T
node_ix
usize
target_ix
usize
layer
usize
&mut self
q
&T
node_ix
usize
target_ix
usize
layer
usize