annotate src/layout/algorithm.ts @ 122:2e8fa3fec0c8

support joining subjects into wider rows
author drewp@bigasterisk.com
date Sun, 20 Mar 2022 14:12:03 -0700
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
122
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
1 import Immutable from "immutable";
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
2
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
3 export function addToValues<K, V>(
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
4 imap: Immutable.Map<K, V[]>,
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
5 key: K,
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
6 newValue: V): Immutable.Map<K, V[]> {
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
7 let cur = imap.get(key, undefined);
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
8 let ret = imap;
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
9 if (cur === undefined) {
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
10 cur = [];
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
11 ret = imap.set(key, cur);
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
12 }
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
13 cur.push(newValue);
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
14 return ret;
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
15 }
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
16
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
17 export function multiColumnSort<T>(
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
18 elems: T[],
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
19 makeSortCols: (elem: T, index: number) => Array<number | string>): T[] {
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
20 const tagged = elems.map((p, i) => {
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
21 return {
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
22 sort: makeSortCols(p, i),
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
23 val: p
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
24 };
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
25 });
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
26 tagged.sort((e1, e2) => {
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
27 let index = 0;
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
28 for (let k1 of e1.sort) {
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
29 const k2 = e2.sort[index];
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
30 if (!Immutable.is(k1, k2)) {
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
31 if (typeof k1 === "number") {
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
32 if (typeof k2 === "number") {
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
33 return k1 - k2;
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
34 } else {
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
35 throw new Error(`${k1} vs ${k2}`);
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
36 }
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
37 } else {
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
38 return k1.localeCompare(k2 as string);
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
39 }
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
40 }
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
41 index++;
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
42 }
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
43 return 0;
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
44 });
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
45 return tagged.map((e) => e.val);
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents:
diff changeset
46 }