NeighborArray (Lucene 10.5.0 core API)
Description: declaration: package: org.apache.lucene.util.hnsw, class: NeighborArray

Package org.apache.lucene.util.hnsw

# Class NeighborArray

java.lang.Object
org.apache.lucene.util.hnsw.NeighborArray

public class NeighborArray extends 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)

NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `NeighborArray(int maxSize, boolean descOrder)`
  `NeighborArray(int maxSize, boolean descOrder, LongConsumer onHeapMemoryUsageListener)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addAndEnsureDiversity(int newNode, float newScore, int nodeId, UpdateableRandomVectorScorer scorer)`
  In addition to `addOutOfOrder(int, float)`, this function will also remove the least-diverse node if the node array is full after insertion
  `void`
  `addInOrder(int newNode, float newScore)`
  Add a new node to the NeighborArray.
  `void`
  `addOutOfOrder(int newNode, float newScore)`
  Add node and newScore but do not insert as sorted
  `void`
  `clear()`
  `float`
  `getScores(int i)`
  Get the score at the given index
  `int`
  `maxSize()`
  `int[]`
  `nodes()`
  Direct access to the internal list of node ids; provided for efficient writing of the graph
  `int`
  `size()`
  `String`
  `toString()`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### NeighborArray

    public NeighborArray(int maxSize, boolean descOrder)

  - ### NeighborArray

    public NeighborArray(int maxSize, boolean descOrder, LongConsumer onHeapMemoryUsageListener)

- ## Method Details

  - ### 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

  - ### addAndEnsureDiversity

    public void addAndEnsureDiversity(int newNode, float newScore, int nodeId, UpdateableRandomVectorScorer scorer) throws IOException
    In addition to `addOutOfOrder(int, float)`, this function will also remove the least-diverse node if the node array is full after insertion

    In multi-threading environment, this method need to be locked as it will be called by multiple threads while other add method is only supposed to be called by one thread.

    Parameters:
      `nodeId` - node Id of the owner of this NeighbourArray
    Throws:
      `IOException`

  - ### size

    public int size()

  - ### nodes

    public int[] nodes()
    Direct access to the internal list of node ids; provided for efficient writing of the graph
    NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.

  - ### getScores

    public float getScores(int i)
    Get the score at the given index
    Parameters:
      `i` - index of the score to get
    Returns:
      the score at the given index

  - ### clear

    public void clear()

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `Object`

  - ### maxSize

    public int maxSize()