mayya-sharipova commented on code in PR #862: URL: https://github.com/apache/lucene/pull/862#discussion_r864407239
########## lucene/core/src/java/org/apache/lucene/util/hnsw/NeighborArray.java: ########## @@ -21,32 +21,64 @@ /** * NeighborArray encodes the neighbors of a node and their mutual scores in the HNSW graph as a pair - * of growable arrays. + * 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) * * @lucene.internal */ public class NeighborArray { - + private final boolean scoresDescOrder; private int size; float[] score; int[] node; - public NeighborArray(int maxSize) { + public NeighborArray(int maxSize, boolean descOrder) { node = new int[maxSize]; score = new float[maxSize]; + this.scoresDescOrder = descOrder; } + /** + * Add a new node to the NeighborArray. The new node must be worse than all previously stored + * nodes. + */ public void add(int newNode, float newScore) { if (size == node.length - 1) { node = ArrayUtil.grow(node, (size + 1) * 3 / 2); score = ArrayUtil.growExact(score, node.length); } + if (size > 0) { + float previousScore = score[size - 1]; + assert ((scoresDescOrder && (previousScore >= newScore)) + || (scoresDescOrder == false && (previousScore <= newScore))) + : "Nodes are added in the incorrect order!"; + } node[size] = newNode; score[size] = newScore; ++size; } + /** Add a new node to the NeighborArray into a correct sort position according to its score. */ + public void addAndSort(int newNode, float newScore) { + if (size == node.length - 1) { + node = ArrayUtil.grow(node, (size + 1) * 3 / 2); + score = ArrayUtil.growExact(score, node.length); + } + int insertionPoint = + scoresDescOrder + ? descSortFindRightMostInsertionPoint(newScore) + : ascSortFindRightMostInsertionPoint(newScore); + for (int i = size; i > insertionPoint; i--) { Review Comment: TIL: that we can use System.arraycopy for the reverse case as well. Addressed in: 160904904c94ffd4d194fc2509124c0e2eb9c44a ########## lucene/core/src/java/org/apache/lucene/util/hnsw/NeighborArray.java: ########## @@ -21,32 +21,64 @@ /** * NeighborArray encodes the neighbors of a node and their mutual scores in the HNSW graph as a pair - * of growable arrays. + * 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) * * @lucene.internal */ public class NeighborArray { - + private final boolean scoresDescOrder; private int size; float[] score; int[] node; - public NeighborArray(int maxSize) { + public NeighborArray(int maxSize, boolean descOrder) { node = new int[maxSize]; score = new float[maxSize]; + this.scoresDescOrder = descOrder; } + /** + * Add a new node to the NeighborArray. The new node must be worse than all previously stored + * nodes. + */ public void add(int newNode, float newScore) { if (size == node.length - 1) { node = ArrayUtil.grow(node, (size + 1) * 3 / 2); score = ArrayUtil.growExact(score, node.length); } + if (size > 0) { + float previousScore = score[size - 1]; + assert ((scoresDescOrder && (previousScore >= newScore)) + || (scoresDescOrder == false && (previousScore <= newScore))) + : "Nodes are added in the incorrect order!"; + } node[size] = newNode; score[size] = newScore; ++size; } + /** Add a new node to the NeighborArray into a correct sort position according to its score. */ + public void addAndSort(int newNode, float newScore) { Review Comment: Addressed in: 160904904c94ffd4d194fc2509124c0e2eb9c44a -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org