From 5447c0ac287aec69ce649f22ad926cd2c8b78e6c Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 24 Feb 2024 12:37:44 -0400 Subject: data: withSortedList: general cleanup Still magic. --- src/data/composite/data/withSortedList.js | 67 ++++++++++++++++--------------- 1 file changed, 34 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/data/composite/data/withSortedList.js b/src/data/composite/data/withSortedList.js index 1d9188f8..51309e19 100644 --- a/src/data/composite/data/withSortedList.js +++ b/src/data/composite/data/withSortedList.js @@ -54,19 +54,21 @@ export default templateCompositeFrom({ [input('list')]: list, [input('sort')]: sortFn, }) { - const symbols = - Array.from({length: list.length}, () => Symbol()); + const originalIndices = + Array.from({length: list.length}, (_, index) => index); - const equalSymbols = - new Map(); + const symbols = []; + const symbolToIndex = new Map(); + const indexToSymbol = new Map(); - const symbolToIndex = - new Map(Array.from(symbols, - (symbol, index) => [symbol, index])); + for (const index of originalIndices) { + const symbol = Symbol(); + symbols.push(symbol); + symbolToIndex.set(symbol, index); + indexToSymbol.set(index, symbol); + } - const indexToSymbol = - new Map(Array.from(symbols, - (symbol, index) => [index, symbol])); + const equalSymbols = new Map(); const assertEqual = (symbol1, symbol2) => { if (equalSymbols.has(symbol1)) { @@ -76,6 +78,9 @@ export default templateCompositeFrom({ } }; + const isEqual = (symbol1, symbol2) => + !!equalSymbols.get(symbol1)?.has(symbol2); + symbols.sort((symbol1, symbol2) => { const comparison = sortFn( @@ -96,33 +101,29 @@ export default templateCompositeFrom({ const sortedList = sortIndices.map(index => list[index]); + const push = (array, value) => { + array.push(value); + return array; + }; + const stableToUnstable = symbols - .map((current, index) => { - if (index === 0) { - return false; - } - - const previous = symbols[index - 1]; - return equalSymbols.get(previous)?.has(current); - }) - .reduce((accumulator, collapseEqual, index) => { - if (index === 0) { - return accumulator; - } - - const last = accumulator.at(-1); - if (collapseEqual) { - accumulator.push(last); - } else { - accumulator.push(last + 1); - } - - return accumulator; - }, [0]); + .map( + (current, index) => + (index === 0 + ? false + : isEqual(current, symbols[index - 1]))) + .reduce( + (accumulator, equalsPrevious, index) => + (index === 0 + ? accumulator + : (equalsPrevious + ? push(accumulator, accumulator.at(-1)) + : push(accumulator, accumulator.at(-1) + 1))), + [0]); const unstableSortIndices = - Array.from({length: list.length}, (_, index) => index) + originalIndices .map(index => indexToSymbol.get(index)) .map(symbol => symbols.indexOf(symbol)) .map(stable => stableToUnstable[stable]); -- cgit 1.3.0-6-gf8a5