From 80ca33f781b710074b11ed68eae6fc2ec314ea84 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 24 Feb 2024 12:20:47 -0400 Subject: data: withSortedList: ensure unstableSortIndices maps from orig order This code is terrifying. --- src/data/composite/data/withSortedList.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/data/composite/data/withSortedList.js b/src/data/composite/data/withSortedList.js index a7942ab4..3a069cf7 100644 --- a/src/data/composite/data/withSortedList.js +++ b/src/data/composite/data/withSortedList.js @@ -60,10 +60,14 @@ export default templateCompositeFrom({ const equalSymbols = new Map(); - const indexMap = + const symbolToIndex = new Map(Array.from(symbols, (symbol, index) => [symbol, index])); + const indexToSymbol = + new Map(Array.from(symbols, + (symbol, index) => [index, symbol])); + const assertEqual = (symbol1, symbol2) => { if (equalSymbols.has(symbol1)) { equalSymbols.get(symbol1).add(symbol2); @@ -75,8 +79,8 @@ export default templateCompositeFrom({ symbols.sort((symbol1, symbol2) => { const comparison = sortFn( - list[indexMap.get(symbol1)], - list[indexMap.get(symbol2)]); + list[symbolToIndex.get(symbol1)], + list[symbolToIndex.get(symbol2)]); if (comparison === 0) { assertEqual(symbol1, symbol2); @@ -87,7 +91,7 @@ export default templateCompositeFrom({ }); const sortIndices = - symbols.map(symbol => indexMap.get(symbol)); + symbols.map(symbol => symbolToIndex.get(symbol)); const sortedList = sortIndices.map(index => list[index]); @@ -118,7 +122,10 @@ export default templateCompositeFrom({ }, [0]); const unstableSortIndices = - sortIndices.map(stable => stableToUnstable[stable]); + Array.from( + {length: list.length}, + (_, index) => + stableToUnstable[symbols.indexOf(indexToSymbol.get(index))]); return continuation({ ['#sortedList']: sortedList, -- cgit 1.3.0-6-gf8a5