diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2024-02-24 12:51:39 -0400 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2024-02-24 13:35:52 -0400 |
commit | 6c2fee0ec06693d442dfdac3afd5a9a8fe6704b3 (patch) | |
tree | 2df68e26731507c125996ea5ec0b5f162f4ed97a /src/data/composite | |
parent | 52113ef4d1976c353bda1a0e5f187d8ba93468c7 (diff) |
data: withSortedList: build stable indices iteratively
Lets us build up symbolToStable simultaneously, which we use when computing unstableSortIndices, officially making it "not magic".
Diffstat (limited to 'src/data/composite')
-rw-r--r-- | src/data/composite/data/withSortedList.js | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/src/data/composite/data/withSortedList.js b/src/data/composite/data/withSortedList.js index 9ee0766b..eda050ab 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, }); }, |