From 6c2fee0ec06693d442dfdac3afd5a9a8fe6704b3 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Sat, 24 Feb 2024 12:51:39 -0400 Subject: data: withSortedList: build stable indices iteratively Lets us build up symbolToStable simultaneously, which we use when computing unstableSortIndices, officially making it "not magic". --- src/data/composite/data/withSortedList.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'src/data') diff --git a/src/data/composite/data/withSortedList.js b/src/data/composite/data/withSortedList.js index 9ee0766..eda050a 100644 --- a/src/data/composite/data/withSortedList.js +++ b/src/data/composite/data/withSortedList.js @@ -95,11 +95,16 @@ export default templateCompositeFrom({ return comparison; }); - const sortIndices = - symbols.map(symbol => symbolToIndex.get(symbol)); - - const sortedList = - sortIndices.map(index => list[index]); + const symbolToStable = new Map(); + const stableSortIndices = []; + const sortedList = []; + + for (const [stableIndex, symbol] of symbols.entries()) { + const sourceIndex = symbolToIndex.get(symbol); + stableSortIndices.push(sourceIndex); + symbolToStable.set(symbol, stableIndex); + sortedList.push(list[sourceIndex]); + } const push = (array, value) => { array.push(value); @@ -119,12 +124,12 @@ export default templateCompositeFrom({ const unstableSortIndices = originalIndices .map(index => indexToSymbol.get(index)) - .map(symbol => symbols.indexOf(symbol)) + .map(symbol => symbolToStable.get(symbol)) .map(stable => stableToUnstable[stable]); return continuation({ ['#sortedList']: sortedList, - ['#sortIndices']: sortIndices, + ['#sortIndices']: stableSortIndices, ['#unstableSortIndices']: unstableSortIndices, }); }, -- cgit 1.3.0-6-gf8a5