itns-sidechain/lib/utils/binary.js

109 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

2017-10-26 04:07:36 -07:00
/*!
2018-08-01 20:00:09 -07:00
* binary.js - binary search utils for hsd
2018-02-01 13:40:45 -08:00
* Copyright (c) 2017-2018, Christopher Jeffrey (MIT License).
2018-08-01 20:00:09 -07:00
* https://github.com/handshake-org/hsd
2017-10-26 04:07:36 -07:00
*/
'use strict';
/**
* Perform a binary search on a sorted array.
* @param {Array} items
* @param {Object} key
* @param {Function} compare
2024-09-24 17:51:55 +04:00
* @param {Boolean} [insert=false]
2017-10-26 04:07:36 -07:00
* @returns {Number} Index.
*/
exports.search = function search(items, key, compare, insert) {
let start = 0;
let end = items.length - 1;
while (start <= end) {
const pos = (start + end) >>> 1;
const cmp = compare(items[pos], key);
if (cmp === 0)
return pos;
if (cmp < 0)
start = pos + 1;
else
end = pos - 1;
}
if (!insert)
return -1;
return start;
};
/**
* Perform a binary insert on a sorted array.
* @param {Array} items
* @param {Object} item
* @param {Function} compare
2024-09-24 17:51:55 +04:00
* @param {Boolean} [uniq=false]
2017-10-26 04:07:36 -07:00
* @returns {Number} index
*/
exports.insert = function insert(items, item, compare, uniq) {
const i = exports.search(items, item, compare, true);
if (uniq && i < items.length) {
if (compare(items[i], item) === 0)
return -1;
}
if (i === 0)
items.unshift(item);
else if (i === items.length)
items.push(item);
else
items.splice(i, 0, item);
return i;
};
/**
* Perform a binary removal on a sorted array.
* @param {Array} items
* @param {Object} item
* @param {Function} compare
* @returns {Boolean}
*/
exports.remove = function remove(items, item, compare) {
const i = exports.search(items, item, compare, false);
if (i === -1)
return false;
splice(items, i);
2017-10-26 04:07:36 -07:00
return true;
};
/*
* Helpers
*/
2024-09-24 17:51:55 +04:00
/**
* @param {Array} list
* @param {Number} i
*/
function splice(list, i) {
if (i === 0) {
list.shift();
return;
}
let k = i + 1;
while (k < list.length)
list[i++] = list[k++];
list.pop();
}