dope2/lib/versioning/merge.js

73 lines
2 KiB
JavaScript

import { emptySet, add, length, forEach, union, has } from "../structures/set.js";
import { getDepth, Slot, Value, Write } from "./value_slot.js";
import { compareSlot } from "./compare.js";
import { makeTypeParser } from "../parser/type_parser.js";
import { newDynamic } from "../primitives/dynamic.js";
const emptySetOfSlots = emptySet(compareSlot);
export const findLCA = slotA => slotB => {
if (compareSlot(slotA)(slotB) === 0) {
return slotA;
}
if (getDepth(slotA) > getDepth(slotB)) {
// we can assume that slotA.variant === "write"
return findLCA(slotA.value.slot)(slotB);
}
else {
// we can assume that slotB.variant === "write"
return findLCA(slotA)(slotB.value.slot);
}
};
export const merge = slotA => slotB => {
const lca = findLCA(slotA)(slotB);
if (compareSlot(lca)(slotA) === 0) {
return add(emptySetOfSlots)(slotB);
}
if (compareSlot(lca)(slotB) === 0) {
return add(emptySetOfSlots)(slotA);
}
const setWithA = add(emptySetOfSlots)(slotA);
const setWithAandB = add(setWithA)(slotB);
return setWithAandB;
};
export const mergeN = slots => {
let toDelete = emptySetOfSlots;
forEach(slots)(slotA => {
forEach(slots)(slotB => {
// compare all non-identical pairs
if (compareSlot(slotA)(slotB) !== 0) {
const m = merge(slotA)(slotB);
if (length(m) === 1) {
// if in the pair, one is an ancestor of the other,
// only keep the other
if (has(m)(slotA)) {
toDelete = add(toDelete)(slotB);
}
}
}
});
});
let result = emptySetOfSlots;
forEach(slots)(slot => {
if (!has(toDelete)(slot)) {
result = add(result)(slot);
}
})
return result;
};
const mkType = makeTypeParser({
extraPrimitives: [
["Value", Value],
["Slot" , Slot ],
["Write", Write],
],
});
export const ModuleMerge = [
["merge" , newDynamic(merge)(mkType("Slot -> Slot -> {Slot}"))],
["mergeN", newDynamic(merge)(mkType("{Slot} -> {Slot}" ))],
];