@kstasi/jest-tolk
Version:
<p align="center"> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/tonkite/tonkite/main/assets/logo-dark.svg"> <img alt="tonkite logo" src="https://raw.githubusercontent.com/tonkite/tonkite/main/a
49 lines (48 loc) • 1.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createIntTree = createIntTree;
function createIntTree(start, bits, signed, fixed = false) {
const max = signed
? (BigInt(1) << BigInt(bits - 1)) - BigInt(1)
: (BigInt(1) << BigInt(bits)) - BigInt(1);
const min = signed ? -(BigInt(1) << BigInt(bits - 1)) : BigInt(0);
let lo = 0n;
let hi = start;
let curr = start;
function magnitudeGreater(lhs, rhs) {
if (lhs === BigInt(0)) {
return false;
}
return lhs > rhs !== lhs < BigInt(0);
}
function reposition() {
const interval = hi - lo;
const newMid = lo + interval / BigInt(2);
if (newMid === curr) {
return false;
}
curr = newMid;
return true;
}
function current() {
return curr;
}
function simplify() {
if (fixed || !magnitudeGreater(hi, lo)) {
return false;
}
hi = curr;
return reposition();
}
function complicate() {
if (fixed || !magnitudeGreater(hi, lo)) {
return false;
}
lo =
curr !== min && curr !== max
? curr + (hi < BigInt(0) ? BigInt(-1) : BigInt(1))
: curr;
return reposition();
}
return { current, simplify, complicate };
}