« get me outta code hell

data: withSortedList: build stable indices iteratively - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data
diff options
context:
space:
mode:
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
commit6c2fee0ec06693d442dfdac3afd5a9a8fe6704b3 (patch)
tree2df68e26731507c125996ea5ec0b5f162f4ed97a /src/data
parent52113ef4d1976c353bda1a0e5f187d8ba93468c7 (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')
-rw-r--r--src/data/composite/data/withSortedList.js19
1 files changed, 12 insertions, 7 deletions
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,
         });
       },