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,10 +1,12 @@
import { getInst, getType } from "../primitives/dynamic.js";
import { SymbolBool, SymbolBottom, SymbolByte, SymbolChar, SymbolDouble, SymbolDynamic, SymbolInt, SymbolUUID, SymbolType, SymbolUnit, SymbolOrdering } from "../primitives/primitive_types.js";
import { UNBOUND_SYMBOLS } from "../primitives/typevars.js";
import { symbolDictIterator } from "../structures/dict.types.js";
import { symbolSetIterator } from "../structures/set.types.js";
import { symbolDict, symbolFunction, symbolList, symbolProduct, symbolSet, symbolSum } from "../structures/type_constructors.js";
import { prettyT } from "../util/pretty.js";
import { compareBools, compareDoubles, compareOrderings, compareStrings, compareSymbols, compareUnits } from "./primitives.js";
import { compareDicts, compareFunctions, compareLists, compareProducts, compareSets, compareSums } from "./structures.js";
import { compareDictIterators, compareDicts, compareFunctions, compareLists, compareProducts, compareSetIterators, compareSets, compareSums } from "./structures.js";
import { compareTypes } from "./type.js";
export const compareDynamic = x => y =>
@ -28,12 +30,14 @@ const typeSymbolToCmp = new Map([
[SymbolOrdering, compareOrderings],
// these functions take extra comparison callbacks:
[symbolFunction, compareFunctions],
[symbolSum , compareSums ],
[symbolProduct , compareProducts ],
[symbolList , compareLists ],
[symbolSet , compareSets ],
[symbolDict , compareDicts ],
[symbolFunction , compareFunctions ],
[symbolSum , compareSums ],
[symbolProduct , compareProducts ],
[symbolList , compareLists ],
[symbolSet , compareSets ],
[symbolDict , compareDicts ],
[symbolSetIterator , compareSetIterators ],
[symbolDictIterator, compareDictIterators],
// even though we cannot compare typevar instances,
// we still need to give a function or shit will break

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)
};