pub fn insert(&mut self, q: T, searcher: &mut Searcher<Met::Unit>) -> usize {
        // Get the level of this feature.
        let level = self.random_level();
        let mut cap = if level >= self.layers.len() {
            self.params.ef_construction
        } else {
            1
        };

        // If this is empty, none of this will work, so just add it manually.
        if self.is_empty() {
            // Add the zero node unconditionally.
            self.zero.push(NeighborNodes {
                neighbors: [!0; M0],
            });
            self.features.push(q);

            // Add all the layers its in.
            while self.layers.len() < level {
                // It's always index 0 with no neighbors since its the first feature.
                let node = Node {
                    zero_node: 0,
                    next_node: 0,
                    neighbors: NeighborNodes { neighbors: [!0; M] },
                };
                self.layers.push(vec![node]);
            }
            return 0;
        }

        self.initialize_searcher(&q, searcher);

        // Find the entry point on the level it was created by searching normally until its level.
        for ix in (level..self.layers.len()).rev() {
            // Perform an ANN search on this layer like normal.
            self.search_single_layer(&q, searcher, &self.layers[ix], cap);
            // Then lower the search only after we create the node.
            self.lower_search(&self.layers[ix], searcher);
            cap = if ix == level {
                self.params.ef_construction
            } else {
                1
            };
        }

        // Then start from its level and connect it to its nearest neighbors.
        for ix in (0..core::cmp::min(level, self.layers.len())).rev() {
            // Perform an ANN search on this layer like normal.
            self.search_single_layer(&q, searcher, &self.layers[ix], cap);
            // Then use the results of that search on this layer to connect the nodes.
            self.create_node(&q, &searcher.nearest, ix + 1);
            // Then lower the search only after we create the node.
            self.lower_search(&self.layers[ix], searcher);
            cap = self.params.ef_construction;
        }

        // Also search and connect the node to the zero layer.
        self.search_zero_layer(&q, searcher, cap);
        self.create_node(&q, &searcher.nearest, 0);
        // Add the feature to the zero layer.
        self.features.push(q);

        // Add all level vectors needed to be able to add this level.
        let zero_node = self.zero.len() - 1;
        while self.layers.len() < level {
            let node = Node {
                zero_node,
                next_node: self.layers.last().map(|l| l.len() - 1).unwrap_or(zero_node),
                neighbors: NeighborNodes { neighbors: [!0; M] },
            };
            self.layers.push(vec![node]);
        }
        zero_node
    }
/// Inserts a feature into the HNSW.
Inserts a feature into the HNSW.
Inserts a feature into the HNSW .
pub fn insert(&mut self, q: T, searcher: &mut Searcher<Met::Unit>) -> usize {
        // Get the level of this feature.
        let level = self.random_level();
        let mut cap = if level >= self.layers.len() {
            self.params.ef_construction
        } else {
            1
        };
        // If this is empty, none of this will work, so just add it manually.
        if self.is_empty() {
            // Add the zero node unconditionally.
            self.zero.push(NeighborNodes {
                neighbors: [!0; M0],
            });
            self.features.push(q);
            // Add all the layers its in.
            while self.layers.len() < level {
                // It's always index 0 with no neighbors since its the first feature.
                let node = Node {
                    zero_node: 0,
                    next_node: 0,
                    neighbors: NeighborNodes { neighbors: [!0; M] },
                };
                self.layers.push(vec![node]);
            }
            return 0;
        }
        self.initialize_searcher(&q, searcher);
        // Find the entry point on the level it was created by searching normally until its level.
        for ix in (level..self.layers.len()).rev() {
            // Perform an ANN search on this layer like normal.
            self.search_single_layer(&q, searcher, &self.layers[ix], cap);
            // Then lower the search only after we create the node.
            self.lower_search(&self.layers[ix], searcher);
            cap = if ix == level {
                self.params.ef_construction
            } else {
                1
            };
        }
        // Then start from its level and connect it to its nearest neighbors.
        for ix in (0..core::cmp::min(level, self.layers.len())).rev() {
            // Perform an ANN search on this layer like normal.
            self.search_single_layer(&q, searcher, &self.layers[ix], cap);
            // Then use the results of that search on this layer to connect the nodes.
            self.create_node(&q, &searcher.nearest, ix + 1);
            // Then lower the search only after we create the node.
            self.lower_search(&self.layers[ix], searcher);
            cap = self.params.ef_construction;
        }
        // Also search and connect the node to the zero layer.
        self.search_zero_layer(&q, searcher, cap);
        self.create_node(&q, &searcher.nearest, 0);
        // Add the feature to the zero layer.
        self.features.push(q);
        // Add all level vectors needed to be able to add this level.
        let zero_node = self.zero.len() - 1;
        while self.layers.len() < level {
            let node = Node {
                zero_node,
                next_node: self.layers.last().map(|l| l.len() - 1).unwrap_or(zero_node),
                neighbors: NeighborNodes { neighbors: [!0; M] },
            };
            self.layers.push(vec![node]);
        }
        zero_node
    }
pub fn insert ( & mut self , q : T , searcher : & mut Searcher < Met :: Unit > ) -> usize { // Get the level of this feature. let level = self . random_level ( ) ; let mut cap = if level >= self . layers . len ( ) { self . params . ef_construction } else { 1 } ; // If this is empty, none of this will work, so just add it manually. if self . is_empty ( ) { // Add the zero node unconditionally. self . zero . push ( NeighborNodes { neighbors : [ ! 0 ; M0 ] , } ) ; self . features . push ( q ) ; // Add all the layers its in. while self . layers . len ( ) < level { // It's always index 0 with no neighbors since its the first feature. let node = Node { zero_node : 0 , next_node : 0 , neighbors : NeighborNodes { neighbors : [ ! 0 ; M ] } , } ; self . layers . push ( vec ! [ node ] ) ; } return 0 ; } self . initialize_searcher ( & q , searcher ) ; // Find the entry point on the level it was created by searching normally until its level. for ix in ( level .. self . layers . len ( ) ) . rev ( ) { // Perform an ANN search on this layer like normal. self . search_single_layer ( & q , searcher , & self . layers [ ix ] , cap ) ; // Then lower the search only after we create the node. self . lower_search ( & self . layers [ ix ] , searcher ) ; cap = if ix == level { self . params . ef_construction } else { 1 } ; } // Then start from its level and connect it to its nearest neighbors. for ix in ( 0 .. core :: cmp :: min ( level , self . layers . len ( ) ) ) . rev ( ) { // Perform an ANN search on this layer like normal. self . search_single_layer ( & q , searcher , & self . layers [ ix ] , cap ) ; // Then use the results of that search on this layer to connect the nodes. self . create_node ( & q , & searcher . nearest , ix + 1 ) ; // Then lower the search only after we create the node. self . lower_search ( & self . layers [ ix ] , searcher ) ; cap = self . params . ef_construction ; } // Also search and connect the node to the zero layer. self . search_zero_layer ( & q , searcher , cap ) ; self . create_node ( & q , & searcher . nearest , 0 ) ; // Add the feature to the zero layer. self . features . push ( q ) ; // Add all level vectors needed to be able to add this level. let zero_node = self . zero . len ( ) - 1 ; while self . layers . len ( ) < level { let node = Node { zero_node , next_node : self . layers . last ( ) . map ( | l | l . len ( ) - 1 ) . unwrap_or ( zero_node ) , neighbors : NeighborNodes { neighbors : [ ! 0 ; M ] } , } ; self . layers . push ( vec ! [ node ] ) ; } zero_node }
Inserts a feature into the HNSW.
Inserts a feature into the HNSW .
&mut self
q
T
searcher
&mut Searcher<Met::Unit>
&mut self
q
T
searcher
&mut Searcher<Met::Unit>