You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
612 B
27 lines
612 B
var xor = require('xor-distance')
|
|
|
|
module.exports = QueryTable
|
|
|
|
function QueryTable (target) {
|
|
if (!(this instanceof QueryTable)) return new QueryTable(target)
|
|
|
|
this.target = target
|
|
this.nodes = []
|
|
}
|
|
|
|
QueryTable.prototype.add = function (node) {
|
|
for (var i = 0; i < this.nodes.length; i++) {
|
|
if (this.nodes[i].id.equals(node.id)) return node
|
|
}
|
|
node.distance = xor(this.target, node.id)
|
|
this.nodes.push(node)
|
|
this.nodes.sort(function (a, b) {
|
|
return xor.compare(a.distance, b.distance)
|
|
})
|
|
|
|
return node
|
|
}
|
|
|
|
QueryTable.prototype.closest = function (n) {
|
|
return this.nodes.slice(0, n)
|
|
}
|
|
|