Refactor (1)

This commit is contained in:
2025-09-19 18:54:12 +09:00
committed by cannorin
parent 6b15542c40
commit c3b1bf39a4
26 changed files with 1684 additions and 105 deletions

View File

@@ -39,3 +39,28 @@ export function permutations<T>(arr: readonly T[]): T[][] {
}
return result;
}
export function maximal<T>(
xs: readonly T[],
preorder: (x: T, y: T) => boolean,
): T[] {
const res: T[] = [];
outer: for (const x of xs) {
for (let i = 0; i < res.length; ) {
const y = res[i] as T;
const xLeY = preorder(x, y);
const yLeX = preorder(y, x);
if (xLeY && !yLeX) {
continue outer;
}
if (yLeX && !xLeY) {
res.splice(i, 1);
continue;
}
i++;
}
res.push(x);
}
return res;
}