« get me outta code hell

data: withSortedList: build unstableSortIndices iteratively - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-02-24 13:07:21 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-02-24 13:35:53 -0400
commit604105d430056f9e015612d27981efbca04a1d0f (patch)
tree92639496603814e3b5cd77f80cd33ce0cbe70620 /src
parent094b91c6f07fc97497018d5238d0afa33262e9d9 (diff)
data: withSortedList: build unstableSortIndices iteratively
Diffstat (limited to 'src')
-rw-r--r--src/data/composite/data/withSortedList.js27
1 files changed, 5 insertions, 22 deletions
diff --git a/src/data/composite/data/withSortedList.js b/src/data/composite/data/withSortedList.js
index fc0ed9e9..6b67f57e 100644
--- a/src/data/composite/data/withSortedList.js
+++ b/src/data/composite/data/withSortedList.js
@@ -59,13 +59,11 @@ export default templateCompositeFrom({
 
         const symbols = [];
         const symbolToIndex = new Map();
-        const indexToSymbol = new Map();
 
         for (const index of originalIndices) {
           const symbol = Symbol();
           symbols.push(symbol);
           symbolToIndex.set(symbol, index);
-          indexToSymbol.set(index, symbol);
         }
 
         const equalSymbols = new Map();
@@ -96,38 +94,23 @@ export default templateCompositeFrom({
         });
 
         const stableSortIndices = [];
+        const unstableSortIndices = [];
         const sortedList = [];
 
-        const symbolToStable = new Map();
-        const stableToUnstable = new Map();
+        let unstableIndex = 0;
 
         for (const [stableIndex, symbol] of symbols.entries()) {
           const sourceIndex = symbolToIndex.get(symbol);
           stableSortIndices.push(sourceIndex);
-          symbolToStable.set(symbol, stableIndex);
           sortedList.push(list[sourceIndex]);
 
-          if (stableIndex === 0) {
-            stableToUnstable.set(stableIndex, 0);
-            continue;
+          if (stableIndex > 0 && !isEqual(symbol, symbols[stableIndex - 1])) {
+            unstableIndex++;
           }
 
-          const previousUnstable = stableToUnstable.get(stableIndex - 1);
-
-          const newUnstable =
-            (isEqual(symbol, symbols[stableIndex - 1])
-              ? previousUnstable
-              : previousUnstable + 1);
-
-          stableToUnstable.set(stableIndex, newUnstable);
+          unstableSortIndices[sourceIndex] = unstableIndex;
         }
 
-        const unstableSortIndices =
-          originalIndices
-            .map(index => indexToSymbol.get(index))
-            .map(symbol => symbolToStable.get(symbol))
-            .map(stable => stableToUnstable.get(stable));
-
         return continuation({
           ['#sortedList']: sortedList,
           ['#sortIndices']: stableSortIndices,