{"id":2222,"date":"2019-06-18T15:46:52","date_gmt":"2019-06-18T14:46:52","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=2222"},"modified":"2023-02-21T12:26:24","modified_gmt":"2023-02-21T11:26:24","slug":"typescript-caculate-gaps-in-ranges","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/typescript-caculate-gaps-in-ranges\/","title":{"rendered":"Typescript calculate gaps in ranges"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">interface IRange { min: number, max: number }\nconst range = (min:number, max: number) => ({min, max});\n\nfunction findGaps(parent: IRange, children: IRange[]): IRange[] {\n    children.sort((a, b) => a.min - b.min); \/\/ sort for deduce range values\n    let currentMin = parent.min; \/\/ absolute minimum\n    return children.reduce((gaps, curr, index, arr) => {            \n        const min = Math.max(currentMin, parent.min);\n        const max = Math.min(curr.min, parent.max);\n        if (max > min) {\n            gaps.push(range(min, max));\n        }            \n        currentMin = curr.max;\n        if  (index + 1 == arr.length &amp;&amp; currentMin &lt; parent.max) {\n            gaps.push(range(currentMin + 1, parent.max));\n        }\n        return gaps;\n    }, []);\n}\n\nconst gaps = findGaps(\n    range(0, 10), [\n        range(2,4),\n        range(6,9)\n    ]);\nconsole.log(\"gaps\", gaps);\n\/\/ 0: {min: 0, max: 2}\n\/\/ 1: {min: 4, max: 6}\n\/\/ 2: {min: 10, max: 10}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">C# (Integer) version<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">void Main()\n{\n\tvar parent = GapsHelper.GetRange(0, 50);\n\n\tvar children = new List&lt;IRange>() {\n\t\tGapsHelper.GetRange(5, 9),\n\t\tGapsHelper.GetRange(11, 15),\n\t\tGapsHelper.GetRange(15, 30),\n\t\tGapsHelper.GetRange(32, 40),\n\t};\n\tvar gaps2 = GapsHelper.FindGaps(parent, children);\n\tConsole.WriteLine(gaps2);\n\/*\n0 - 4 \n10 - 10 \n31 - 31 \n41 - 50 \n*\/\n}\n\npublic interface IRange\n{\n    int Min { get; set; }\n    int Max { get; set; }\n}\n\npublic class Range : IRange\n{\n    public int Min { get; set; }\n    public int Max { get; set; }\n}\n\npublic class GapsHelper\n{\n    public static IRange GetRange(int min, int max) => new Range { Min = min, Max = max };\n\n    public static IEnumerable&lt;IRange> FindGaps(IRange parent, List&lt;IRange> children)\n    {\n        children.Sort((a, b) => a.Min - b.Min); \/\/ sort for deduce range values\n        var currentMin = parent.Min - 1; \/\/ absolute minimum\n        var index = 0;\n        return children.Aggregate(new List&lt;IRange>(), (gaps, curr) => \/\/ List&lt;IRange> , Range\n        {\n            index++;\n\n            var min = Math.Max(currentMin + 1, parent.Min);\n            var max = Math.Min(curr.Min - 1, parent.Max);\n            if (max >= min)\n\t\tgaps.Add(GetRange(min, max));\n            \n            currentMin = curr.Max;\n            if (index == children.Count &amp;&amp; currentMin &lt; parent.Max)\n                gaps.Add(GetRange(currentMin + 1, parent.Max));\n            return gaps;\n        });\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C# (Integer) version<\/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":[6,4,1],"tags":[],"class_list":["post-2222","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2222","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=2222"}],"version-history":[{"count":10,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2222\/revisions"}],"predecessor-version":[{"id":7434,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2222\/revisions\/7434"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=2222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=2222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=2222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}