UNPKG

hnsw-lite

Version:

A lightweight HNSW implementation for nearest neighbor search.

33 lines 1.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Node = void 0; class Node { constructor(id, vector, maxEdges, layer) { this.neighbors = []; // Tuple of node and distance this.id = id; this.vector = vector; this.maxEdges = maxEdges; this.layer = layer; } addNeighbor(node, distance) { if (this.id === node.id) { return; // Skip adding self as neighbor } this.neighbors.push([node, distance]); this.neighbors.sort((a, b) => a[1] - b[1]); // Sort by distance if (this.neighbors.length > this.maxEdges) { this.neighbors.pop(); // Enforce maxEdges limit } } removeNeighbor(target) { this.neighbors = this.neighbors.filter(([node]) => node !== target); } clearNeighbors() { this.neighbors.forEach(([node]) => { node.removeNeighbor(this); }); this.neighbors = []; } } exports.Node = Node; //# sourceMappingURL=node.js.map