interface ITimeDetail { key: string; time: Date; } const details: ITimeDetail[] = [ { key: "A", time: new Date("2024-02-29T08:00:00") }, { key: "B", time: new Date("2024-02-29T09:00:00") }, { key: "A", time: new Date("2024-02-29T09:30:00") }, { key: "C", time: new Date("2024-02-29T10:00:00") }, { key: "E", time: new Date("2024-02-29T09:45:00") }, { key: "B", time: new Date("2024-02-29T10:01:00") }, { key: "D", time: new Date("2024-02-29T10:15:00") }, { key: "E", time: new Date("2024-02-29T10:30:00") }, { key: "D", time: new Date("2024-02-29T10:45:00") }, { key: "C", time: new Date("2024-02-29T11:30:00") } ]; function getLaneManager() { const allLanes = new Set<number>(); const laneQueue: number[] = []; const keyToLaneMap = new Map<string, number>(); const laneToKeyMap = new Map<number, string>(); let i = 0; function take(key: string) { if (keyToLaneMap.has(key)) return; let lane = laneQueue.pop(); if (lane === undefined) { lane = i; allLanes.add(i); i += 1; } keyToLaneMap.set(key, lane); laneToKeyMap.set(lane, key); } function release(key: string) { const lane = keyToLaneMap.get(key); if (lane !== undefined && allLanes.has(lane)) { laneQueue.push(lane); keyToLaneMap.delete(key); laneToKeyMap.delete(lane); } } const has = (key: string) => keyToLaneMap.has(key); const getLanes = () => Array.from(allLanes).map(x => laneToKeyMap.get(x)); return { take, has, release, getLanes }; } function getDateTimeStr(date: Date) { return new Date(date).toISOString().slice(0, 16).split('T').join(" "); } const laneManager = getLaneManager(); const rows = []; details.sort((a, b) => a.time.getTime() - b.time.getTime()); for (const d of details) { const key = d.key; const wasActive = laneManager.has(key); if (!wasActive) laneManager.take(key); const desc = laneManager.getLanes().map(x => { if (x === key) return wasActive ? "x" : "o"; return !x ? " " : "|"; }).join(" "); const desc2 = laneManager.getLanes().map(x => x === undefined ? " " : `${x}`.padStart(1, " ")).join(" "); rows.push([key, getDateTimeStr(d.time), desc, desc2]); if (wasActive) laneManager.release(key) } for (const row of rows) console.log(row.join("\t"))
Output
A 2024-02-29 07:00 o A B 2024-02-29 08:00 | o A B A 2024-02-29 08:30 x | A B E 2024-02-29 08:45 o | E B C 2024-02-29 09:00 | | o E B C B 2024-02-29 09:01 | x | E B C D 2024-02-29 09:15 | o | E D C E 2024-02-29 09:30 x | | E D C D 2024-02-29 09:45 x | D C C 2024-02-29 10:30 x C
832120cookie-checkTypescript timeline problem