@jawis/shared-page-heap
Version:
Heap for concurrent programs.
83 lines (82 loc) • 2.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports._getJumpLength = exports._getRoot = exports._rightNode = exports._leftNode = void 0;
const _jab_1 = require("^jab");
/**
* Get the left subnode.
*
* - Returns false when there's no left subnode.
*
* notes
* - It's possible to determine the jump length from the node index.
* - The left subtree is always full, so it only depends on the jumplength.
*
*/
const _leftNode = (nodeIndex) => {
const jumpLength = (0, exports._getJumpLength)(nodeIndex);
//the bottom leaves never have a left subnode.
if (jumpLength === 0) {
return;
}
//the left subtree is always full, so the
return nodeIndex - jumpLength;
};
exports._leftNode = _leftNode;
/**
* Get the right subnode.
*
* - Returns null when there's no left subnode.
* - The JumpLength paremeter is only for double checking.
*
*
* notes
* - It's possible to determine the size of the right subtree.
* - If it's full it's simple to determine the right subnode. But if it's partial full
* the right subnode depends of the size of the right subtree.
* - The right subtree is full if the there is enough nodes in the data structure to fill
* it up. That's why the size of the data structure is enough to determine its size.
*/
const _rightNode = (nodeIndex, nodeCount) => {
if (nodeIndex > nodeCount) {
(0, _jab_1.err)("Index can't be larger than size.", { node: nodeIndex, nodeCount });
}
const maxJumpLength = (0, exports._getJumpLength)(nodeIndex);
//the bottom leaves never have a right subnode.
if (maxJumpLength === 0) {
return;
}
//Prevent `_getRoot` from throwing for this case.
if (nodeIndex + 1 === nodeCount) {
return;
}
//given the size of the right subtree, we can determine which node is root thereof.
// That will be the right subnode.
let actualJump = (0, exports._getRoot)(nodeCount - nodeIndex - 1) + 1;
//correction when right subtree is full.
if (actualJump > maxJumpLength) {
actualJump = maxJumpLength;
}
return nodeIndex + actualJump;
};
exports._rightNode = _rightNode;
/**
*
*/
const _getRoot = (nodeCount) => {
if (nodeCount <= 0) {
(0, _jab_1.err)("NodeCount must be positive, was: " + nodeCount);
}
return (0, _jab_1.preserveHeighestBit)(nodeCount) - 1;
};
exports._getRoot = _getRoot;
/**
* Get jump length of the given node. Assuming the subtree is full.
*/
const _getJumpLength = (nodeIndex) => {
if (nodeIndex < 0) {
(0, _jab_1.err)("Index must be non-negative, was: " + nodeIndex);
}
const n = (0, _jab_1.numberOfRightZeroBits)(nodeIndex + 1);
return n === 0 ? 0 : 1 << (n - 1);
};
exports._getJumpLength = _getJumpLength;