Skip to content

Commit

Permalink
fix: containment count to bigint to prepare for bags
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Jan 24, 2022
1 parent 5c9231d commit e12f321
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions packages/store/src/keys/merge-set-operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const { details: X } = assert;
* according to `fullOrder`, which should differ from `rankOrder` only
* by being more precise.
*
* This should be equivalent to resorting the entire `elements` array according to
* `fullOrder`. However, it optimizes for the case where these contiguous
* This should be equivalent to resorting the entire `elements` array according
* to `fullOrder`. However, it optimizes for the case where these contiguous
* runs that need to be resorted are either absent or small.
*
* @template T
Expand Down Expand Up @@ -72,7 +72,7 @@ const windowResort = (elements, rankCompare, fullCompare) => {
* @template T
* @param {T[]} xelements
* @param {T[]} yelements
* @returns {Iterable<[T,number,number]>}
* @returns {Iterable<[T,bigint,bigint]>}
*/
const merge = (xelements, yelements) => {
// This fullOrder contains history dependent state. It is specific
Expand Down Expand Up @@ -107,35 +107,35 @@ const merge = (xelements, yelements) => {
next: () => {
/** @type {boolean} */
let done = false;
/** @type {[T,number,number]} */
/** @type {[T,bigint,bigint]} */
let value;
if (xDone && yDone) {
done = true;
// @ts-ignore Because the terminating value does not matter
value = [null, 0, 0];
value = [null, 0n, 0n];
} else if (xDone) {
// only ys are left
value = [y, 0, 1];
value = [y, 0n, 1n];
nextY();
} else if (yDone) {
// only xs are left
value = [x, 1, 0];
value = [x, 1n, 0n];
nextX();
} else {
const comp = fullCompare(x, y);
if (comp === 0) {
// x and y are equivalent, so report both
value = [x, 1, 1];
value = [x, 1n, 1n];
nextX();
nextY();
} else if (comp < 0) {
// x is earlier, so report it
value = [x, 1, 0];
value = [x, 1n, 0n];
nextX();
} else {
// y is earlier, so report it
assert(comp > 0);
value = [y, 0, 1];
value = [y, 0n, 1n];
nextY();
}
}
Expand All @@ -149,7 +149,7 @@ harden(merge);

const iterIsSuperset = xyi => {
for (const [_m, xc, _yc] of xyi) {
if (xc === 0) {
if (xc === 0n) {
// something in y is not in x, so x is not a superset of y
return false;
}
Expand All @@ -159,7 +159,7 @@ const iterIsSuperset = xyi => {

const iterIsDisjoint = xyi => {
for (const [_m, xc, yc] of xyi) {
if (xc >= 1 && yc >= 1) {
if (xc >= 1n && yc >= 1n) {
// Something in both, so not disjoint
return false;
}
Expand All @@ -171,11 +171,11 @@ const iterCompare = xyi => {
let loneY = false;
let loneX = false;
for (const [_m, xc, yc] of xyi) {
if (xc === 0) {
if (xc === 0n) {
// something in y is not in x, so x is not a superset of y
loneY = true;
}
if (yc === 0) {
if (yc === 0n) {
// something in x is not in y, so y is not a superset of x
loneX = true;
}
Expand All @@ -196,10 +196,10 @@ const iterCompare = xyi => {
const iterUnion = xyi => {
const result = [];
for (const [m, xc, yc] of xyi) {
if (xc >= 0) {
if (xc >= 0n) {
result.push(m);
} else {
assert(yc >= 0);
assert(yc >= 0n);
// if x and y were both ready, then they were equivalent and
// above clause already took care of it. Otherwise push here.
result.push(m);
Expand All @@ -211,11 +211,11 @@ const iterUnion = xyi => {
const iterDisjointUnion = xyi => {
const result = [];
for (const [m, xc, yc] of xyi) {
assert(xc === 0 || yc === 0, X`Sets must not have common elements: ${m}`);
if (xc >= 1) {
assert(xc === 0n || yc === 0n, X`Sets must not have common elements: ${m}`);
if (xc >= 1n) {
result.push(m);
} else {
assert(yc >= 1);
assert(yc >= 1n);
result.push(m);
}
}
Expand All @@ -225,7 +225,7 @@ const iterDisjointUnion = xyi => {
const iterIntersection = xyi => {
const result = [];
for (const [m, xc, yc] of xyi) {
if (xc >= 1 && yc >= 1) {
if (xc >= 1n && yc >= 1n) {
// If they are both present, then they were equivalent
result.push(m);
}
Expand All @@ -236,8 +236,8 @@ const iterIntersection = xyi => {
const iterDisjointSubtract = xyi => {
const result = [];
for (const [m, xc, yc] of xyi) {
assert(xc >= 1, X`right element ${m} was not in left`);
if (yc === 0) {
assert(xc >= 1n, X`right element ${m} was not in left`);
if (yc === 0n) {
// the x was not in y
result.push(m);
}
Expand Down

0 comments on commit e12f321

Please sign in to comment.