Class NeighborArray


  • public class NeighborArray
    extends java.lang.Object
    NeighborArray encodes the neighbors of a node and their mutual scores in the HNSW graph as a pair of growable arrays. Nodes are arranged in the sorted order of their scores in descending order (if scoresDescOrder is true), or in the ascending order of their scores (if scoresDescOrder is false)
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) int[] node  
      java.util.concurrent.locks.ReadWriteLock rwlock  
      (package private) float[] score  
      private boolean scoresDescOrder  
      private int size  
      private int sortedNodeSize  
    • Constructor Summary

      Constructors 
      Constructor Description
      NeighborArray​(int maxSize, boolean descOrder)  
    • Field Detail

      • scoresDescOrder

        private final boolean scoresDescOrder
      • size

        private int size
      • score

        float[] score
      • node

        int[] node
      • sortedNodeSize

        private int sortedNodeSize
      • rwlock

        public final java.util.concurrent.locks.ReadWriteLock rwlock
    • Constructor Detail

      • NeighborArray

        public NeighborArray​(int maxSize,
                             boolean descOrder)
    • Method Detail

      • addInOrder

        public void addInOrder​(int newNode,
                               float newScore)
        Add a new node to the NeighborArray. The new node must be worse than all previously stored nodes. This cannot be called after addOutOfOrder(int, float)
      • addOutOfOrder

        public void addOutOfOrder​(int newNode,
                                  float newScore)
        Add node and newScore but do not insert as sorted
      • sort

        public int[] sort​(RandomVectorScorer scorer)
                   throws java.io.IOException
        Sort the array according to scores, and return the sorted indexes of previous unsorted nodes (unchecked nodes)
        Returns:
        indexes of newly sorted (unchecked) nodes, in ascending order, or null if the array is already fully sorted
        Throws:
        java.io.IOException
      • insertSortedInternal

        private int insertSortedInternal​(RandomVectorScorer scorer)
                                  throws java.io.IOException
        insert the first unsorted node into its sorted position
        Throws:
        java.io.IOException
      • insertSorted

        void insertSorted​(int newNode,
                          float newScore)
                   throws java.io.IOException
        This method is for test only.
        Throws:
        java.io.IOException
      • size

        public int size()
      • node

        public int[] node()
        Direct access to the internal list of node ids; provided for efficient writing of the graph
      • score

        public float[] score()
      • clear

        public void clear()
      • removeLast

        public void removeLast()
      • removeIndex

        public void removeIndex​(int idx)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • ascSortFindRightMostInsertionPoint

        private int ascSortFindRightMostInsertionPoint​(float newScore,
                                                       int bound)
      • descSortFindRightMostInsertionPoint

        private int descSortFindRightMostInsertionPoint​(float newScore,
                                                        int bound)