| 1234567891011121314151617181920 |
- export function visitPartialOrdering<T>(elements: T[], smallerThan: (T) => T[], visitCallback: (T) => void) {
- const visitable = new Set(elements);
- const remaining = new Set(elements);
- while (remaining.size > 0) {
- let found = false;
- for (const elem of remaining) {
- if (smallerThan(elem).every(e => !visitable.has(e) || !remaining.has(e))) {
- visitCallback(elem);
- remaining.delete(elem);
- found = true;
- break;
- }
- }
- if (!found) {
- throw new Error("Could not find a smallest element - not a partial ordering?");
- }
- }
- }
|