add comparison functions for SetIterator and DictIterator

This commit is contained in:
Joeri Exelmans 2025-05-09 16:51:29 +02:00
parent b0023afe8c
commit 1f2249e75a
5 changed files with 73 additions and 29 deletions

View file

@ -1,6 +1,6 @@
// Total ordering of composed types
import { compareDoubles, compareStrings } from "./primitives.js"
import { compareBools, compareDoubles, compareStrings } from "./primitives.js"
import { get, length as lengthLs } from "../structures/list.js";
import { read as readSet, length as lengthSet, first as firstSet } from "../structures/set.js";
import { read as readDict, length as lengthDict, first as firstDict } from "../structures/dict.js";
@ -93,3 +93,40 @@ export const compareDicts = compareKeys => compareValues => x => y => {
return iterate(firstDict(x))(firstDict(y));
})();
};
export const compareSetIterators = compareElems => x => y => {
// first compare the sets the iterators belong to,
// if the iterators belong to different sets, then they surely are not equal:
const ord = compareSets(compareElems)(x.tree)(y.tree);
if (ord !== 0) {
return ord; // sets differ
}
// sets are equal...
const xIsValid = x !== undefined && x.valid;
const yIsValid = y !== undefined && y.valid;
const ord2 = compareBools(xIsValid)(yIsValid);
if (ord2 !== 0) {
return ord2; // one is valid, the other is not
}
// both iterators are valid...
return compareElems(x.key)(y.key);
};
export const compareDictIterators = compareKeys => compareValues => x => y => {
// first compare the sets the iterators belong to,
// if the iterators belong to different sets, then they surely are not equal:
const ord = compareDicts(compareKeys)(compareValues)(x.tree)(y.tree);
if (ord !== 0) {
return ord; // sets differ
}
// sets are equal...
const xIsValid = x !== undefined && x.valid;
const yIsValid = y !== undefined && y.valid;
const ord2 = compareBools(xIsValid)(yIsValid);
if (ord2 !== 0) {
return ord2; // one is valid, the other is not
}
// both iterators are valid...
return compareKeys (x.key )(y.key )
|| compareValues(x.value)(y.value)
};