{"id":8321,"date":"2024-02-29T22:06:00","date_gmt":"2024-02-29T21:06:00","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=8321"},"modified":"2024-03-05T21:25:19","modified_gmt":"2024-03-05T20:25:19","slug":"typescript-timeline-problem","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/typescript-timeline-problem\/","title":{"rendered":"Typescript timeline problem"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"tsx\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">interface ITimeDetail {\n    key: string;\n    time: Date;\n}\n\nconst details: ITimeDetail[] = [\n    { key: \"A\", time: new Date(\"2024-02-29T08:00:00\") },\n    { key: \"B\", time: new Date(\"2024-02-29T09:00:00\") },\n    { key: \"A\", time: new Date(\"2024-02-29T09:30:00\") },\n    { key: \"C\", time: new Date(\"2024-02-29T10:00:00\") },\n    { key: \"E\", time: new Date(\"2024-02-29T09:45:00\") },\n    { key: \"B\", time: new Date(\"2024-02-29T10:01:00\") },\n    { key: \"D\", time: new Date(\"2024-02-29T10:15:00\") },\n    { key: \"E\", time: new Date(\"2024-02-29T10:30:00\") },\n    { key: \"D\", time: new Date(\"2024-02-29T10:45:00\") },\n    { key: \"C\", time: new Date(\"2024-02-29T11:30:00\") }\n];\n\nfunction getLaneManager() {\n    const allLanes = new Set&lt;number>();\n    const laneQueue: number[] = [];\n    const keyToLaneMap = new Map&lt;string, number>();\n    const laneToKeyMap = new Map&lt;number, string>();\n    let i = 0;\n    \n    function take(key: string) {\n        if (keyToLaneMap.has(key)) return;\n        let lane = laneQueue.pop();\n        if (lane === undefined) {\n            lane = i;\n            allLanes.add(i);\n            i += 1;\n        }\n        keyToLaneMap.set(key, lane);\n        laneToKeyMap.set(lane, key);\n    }\n    function release(key: string) {\n        const lane = keyToLaneMap.get(key);\n        if (lane !== undefined &amp;&amp; allLanes.has(lane)) {\n            laneQueue.push(lane);\n            keyToLaneMap.delete(key);\n            laneToKeyMap.delete(lane);\n        }\n    }\n    const has = (key: string) => keyToLaneMap.has(key);\n    const getLanes = () => Array.from(allLanes).map(x => laneToKeyMap.get(x));\n    return { take, has, release, getLanes };\n}\n\nfunction getDateTimeStr(date: Date) {\n    return new Date(date).toISOString().slice(0, 16).split('T').join(\" \");\n}\n\nconst laneManager = getLaneManager();\nconst rows = [];\ndetails.sort((a, b) => a.time.getTime() - b.time.getTime());\nfor (const d of details) {\n    const key = d.key;\n\n    const wasActive = laneManager.has(key);\n    if (!wasActive)\n        laneManager.take(key);\n\n    const desc = laneManager.getLanes().map(x => {\n        if (x === key)\n            return wasActive ?  \"x\" :  \"o\";\n        return !x ? \" \" : \"|\";\n    }).join(\" \");\n\n    const desc2 = laneManager.getLanes().map(x => x === undefined ? \" \" : `${x}`.padStart(1, \" \")).join(\" \");\n\n    rows.push([key, getDateTimeStr(d.time), desc, desc2]);\n\n    if (wasActive)\n        laneManager.release(key)\n}\n\nfor (const row of rows) \n    console.log(row.join(\"\\t\"))<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"plain_text\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">A       2024-02-29 07:00        o       A\nB       2024-02-29 08:00        | o     A B\nA       2024-02-29 08:30        x |     A B\nE       2024-02-29 08:45        o |     E B\nC       2024-02-29 09:00        | | o   E B C\nB       2024-02-29 09:01        | x |   E B C\nD       2024-02-29 09:15        | o |   E D C\nE       2024-02-29 09:30        x | |   E D C\nD       2024-02-29 09:45          x |     D C\nC       2024-02-29 10:30            x       C<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Output<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8321","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8321","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=8321"}],"version-history":[{"count":9,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8321\/revisions"}],"predecessor-version":[{"id":8366,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8321\/revisions\/8366"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=8321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=8321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=8321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}