{"id":196,"date":"2020-05-05T23:21:16","date_gmt":"2020-05-05T15:21:16","guid":{"rendered":"https:\/\/blog.indeex.club\/?p=196"},"modified":"2020-08-18T22:22:54","modified_gmt":"2020-08-18T14:22:54","slug":"%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"https:\/\/blog.indeex.club\/index.php\/2020\/05\/05\/%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84\/","title":{"rendered":"\u6570\u636e\u7ed3\u6784"},"content":{"rendered":"<hr \/>\n<p>\u6570\u636e\u7ed3\u6784\u7684\u8bb2\u89e3\u5f88\u5c11\uff0c\u800c\u4e14\u96f6\u96f6\u6563\u6563\u7684\uff0c\u628a\u4ee5\u524d\u4f7f\u7528\u5176\u4ed6ECMAScript\u5b9e\u73b0\u7684\u6574\u7406\u518d\u7528Typescript\u91cd\u65b0\u8bb0\u5f55\u4e0b\u3002<\/p>\n<h3>\u6570\u7ec4<\/h3>\n<p>\u4e00\u5207\u4ece\u6570\u7ec4\u5f00\u59cb\uff1a<\/p>\n<p>\u7531\u4e8eECMAScript\u6bd4\u8f83\u7075\u6d3b\uff0c\u6570\u7ec4\u4e5f\u4e00\u6837\uff0c\u8bed\u6cd5\u4e5f\u7b80\u5355\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">let arr: number[] = [];\nfor(let i: number = 0; i &lt; 1000000; i++) arr[i] = i ;\nlet startT: number = new Date().getTime();\nlet num: number = 500;\nwhile(num &gt; 0)\n{\n    arr.splice(0, 1);\n    num--;\n}\nconsole.log(\"time:\", new Date().getTime() - startT);\n<\/code><\/pre>\n<h3>\u5806\u6808<\/h3>\n<p>\u5806\u6808\u9075\u5faa\u5148\u8fdb\u540e\u51fa\u7684\u89c4\u5219\uff0c\u76f4\u63a5\u4f7f\u7528\u6570\u7ec4\u7684push()\u548cpop()\u5c31\u53ef\u4ee5\uff0c\u6240\u4ee5ECMAScript\u7684\u6570\u7ec4\u672c\u8eab\u5c31\u662f\u6700\u597d\u7684\u5806\u6808\u3002<\/p>\n<h3>\u961f\u5217<\/h3>\n<p>\u961f\u5217\u9075\u5faa\u5148\u8fdb\u5148\u51fa\uff0c\u7528\u6570\u7ec4\u505a\u961f\u5217\u5728\u6d4f\u89c8\u5668\u91cc\u5220\u9664\u6570\u7ec4\u5143\u7d20\u6bd4\u8f83\u6162\uff0c\u670d\u52a1\u5668\u7aef\u6ca1\u6709\u6d4b\u8bd5\u3002<\/p>\n<pre><code class=\"language-javascript line-numbers\">let startT:number = new Date().getTime();\nlet queue:number[] = [];\nfor(let i:number = 0 ; i&lt;100000 ;i++);\n{\n    queue.push(i);\n}\nfor(let i:number = 0 ; i&lt;100000 ;i++)\n{\n    queue.shift();\n}\nconsole.log(new Date().getTime() - startT);\n<\/code><\/pre>\n<p>\u53ef\u4ee5\u91c7\u7528\u53cc\u5411\u94fe\u8868\u65b9\u5f0f\uff0c\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5f53\u4f5c\u961f\u5217\u4e5f\u53ef\u4ee5\u5f53\u4f5c\u5806\u6808\uff0c\u6548\u7387\u6bd4\u76f4\u63a5\u64cd\u4f5c\u6570\u7ec4\u9ad8\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">let startT: number = new Date().getTime();\nlet queue:DLinkedList = new DLinkedList();\n\/\/...\nconsole.log(new Date().getTime() - startT);\n<\/code><\/pre>\n<h3>\u94fe\u8868<\/h3>\n<p>\u53cc\u5411\u94fe\u8868\uff0c\u5220\u9664\u6570\u636e\u6bd4\u8f83\u5feb\uff0c\u5355\u94fe\u8868\u7701\u5185\u5b58\uff08\u4e00\u70b9\u70b9\u5185\u5b58\u4e5f\u662f\u5185\u5b58\uff09\uff0c\u5220\u9664\u4f1a\u6162\u5f88\u591a\u3002\u4f46\u968f\u7740ECMAScript 2015\u7684\u666e\u53ca\uff0c\u76f4\u63a5\u4f7f\u7528Iterator\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-javascript line-numbers\">let l: DLinkedList = new DLinkedList();\nl.push(1);\nl.push(2);\nl.push(3);\nconsole.log(l,l.head,l.tail);\nl.pop();\nconsole.log(l,l.head,l.tail);\nl.unshift(0);\nconsole.log(l,l.head,l.tail);\nl.shift();\nconsole.log(l,l.head,l.tail);\nl.push(3);\nconsole.log(l,l.head,l.tail);\n\nlet it:DListIterator = l.getIterator();\nfor(it.start() ; it.hasNext(); it.next())\n{\n    console.log(it.node.data);\n}\nit.start();\nit.next();\nl.remove(it);\nl.remove(it);\nconsole.log(l,l.head,l.tail);\nl.insert(it,2);\nconsole.log(l,l.head,l.tail);\nl.insert(it,3);\nconsole.log(l,l.head,l.tail);\n<\/code><\/pre>\n<h3>\u54c8\u5e0c\u8868<\/h3>\n<p>\u54c8\u5e0c\u8868\u7c7b\u4f3cECMAScript\u7684Object\u548cMap\uff0c\u4f46ECMAScript\u7684Map\u66f4\u9002\u5408\u505a\u54c8\u5e0c\u8868\u3002 \u5728ECMAScript\u4e2d\uff0cObject\u548cMap\u90fd\u662f\u52a8\u6001\u7684\uff0c\u8d4b\u503c\u5c31 obj.abc = 123 \uff0cmap.set(&#8220;abc&#8221;, 123)\uff0c\u5220\u9664\u5c31 delete obj.abc\uff0cmap.delete(&#8220;abc&#8221;):<\/p>\n<pre><code class=\"language-javascript line-numbers\">\/\/Object\nlet o: object = {};\no[\"key\"] = obj;\ndelete o[\"key\"];\n\n\/\/Map\nlet m: any = new Map();\nm.set(\"key\", map);\nm.delete(\"key\");\n<\/code><\/pre>\n<h3>\u4e8c\u53c9\u6811<\/h3>\n<p>\u6570\u7ec4\u548c\u94fe\u8868\u90fd\u53eb\u7ebf\u6027\u7ed3\u6784\uff0c\u800c\u6811\u662f\u5178\u578b\u7684\u975e\u7ebf\u6027\u6570\u636e\u7ed3\u6784\uff0c\u4e0b\u9762\u662f\u666e\u901a\u6811\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">let tree:Tree = new Tree(0);\nlet itr:TreeIterator = tree.getIterator();\nitr.appendChild(1);\nitr.appendChild(2);\n\n\/\/      0\n\/\/     \/ \\\n\/\/    1   2\n\nitr.down();\nitr.appendChild(3);\n\n\/\/         0\n\/\/        \/ \\\n\/\/       1   2\n\/\/      \/\n\/\/     3\n\nitr.up();\nitr.appendChild(4);\n\n\/\/          0\n\/\/        \/ | \\\n\/\/       1  2  4\n\/\/      \/\n\/\/     3\n\nitr.childEnd();\nitr.down();\nitr.prependChild(5);\nitr.prependChild(6);\n\n\/\/          0\n\/\/        \/ | \\\n\/\/       1  2  4\n\/\/      \/      \/\\\n\/\/     3      6  5\n\nconsole.log(tree.dump());\n<\/code><\/pre>\n<p>\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u6811\u5f62\u83dc\u5355\u601d\u8def\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">\/**\n* \u524d\u5e8f\u904d\u5386\n*\/\npublic preorder(Node:Tree, Process:function):void\n{\n    Process(Node)\n    let itr:DListIterator = Node.children.getIterator()\n    while(itr.hasNext())\n    {\n        preorder(Tree(itr.node.data),Process)\n        itr.next();\n    }\n}\n\n\/**\n* \u540e\u5e8f\u904d\u5386\n*\/\npublic postorder(Node:Tree,Process:function):void\n{\n    let itr:DListIterator =  Node.children.getIterator()\n    while(itr.hasNext())\n    {\n        postorder(Tree(itr.node.data),Process)\n    }\n    Process(Node)\n}\n<\/code><\/pre>\n<p>\u4f18\u5148\u961f\u5217\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u961f\u5217\uff0c\u5b83\u4e0d\u662f\u5148\u5165\u5148\u51fa\uff0c\u800c\u662f\u5165\u961f\u7684\u5143\u7d20\u4f1a\u81ea\u52a8\u6309\u4f60\u6307\u5b9a\u987a\u5e8f\u6392\u5217\uff0c\u5148\u51fa\u961f\u7684\u6c38\u8fdc\u662f\u4f60\u6307\u5b9a\u7684\u987a\u5e8f\u91cc\u6392\u6700\u524d\u9762\u7684\u3002<\/p>\n<p>\u5f53\u7136\u8fd9\u4e2a\u4f18\u5148\u961f\u5217\u53ef\u4ee5\u7528\u6570\u7ec4\u5b9e\u73b0\uff0c\u6bcf\u6b21\u5165\u961f\u5c31push\uff0c\u7136\u540e\u6392\u4e00\u6b21\u5e8f\uff0c\u51fa\u961fpop\u5c31\u53ef\u4ee5\u4e86\uff0c\u4f46\u662f\u8fd9\u6837\u6548\u7387\u4e0d\u4f73\uff0c\u4e00\u822c\u4f18\u5148\u961f\u5217\u4f1a\u4f7f\u7528\u4e8c\u53c9\u5806\u53bb\u5b9e\u73b0\u3002<\/p>\n<p>\u4e8c\u53c9\u6811\u662f\u53ea\u67092\u4e2a\u5b50\u8282\u70b9\u7684\u6811\uff0c\u4e8c\u53c9\u5806\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u4e8c\u53c9\u6811\uff0c\u4ed6\u7684\u6bcf\u4e2a\u7236\u8282\u70b9\u90fd\u6bd4\u5b50\u8282\u70b9\u5927\u3002<\/p>\n<p>\u6bd4\u5982A*\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">let priorityQueue:Heap =new Heap(); \npriorityQueue.enqueue(2);\npriorityQueue.enqueue(6);\npriorityQueue.enqueue(5);\npriorityQueue.enqueue(8);\npriorityQueue.enqueue(7);\npriorityQueue.enqueue(4);\npriorityQueue.enqueue(1);\npriorityQueue.enqueue(3);\npriorityQueue.enqueue(9);\n\nconsole.log(priorityQueue);  \/\/9,8,5,7,6,4,1,2,3   \nconsole.log(priorityQueue.dequeue());  \/\/9 \nconsole.log(priorityQueue.dequeue());  \/\/8\nconsole.log(priorityQueue.dequeue());  \/\/7\nconsole.log(priorityQueue.dequeue());  \/\/6\nconsole.log(priorityQueue.dequeue());  \/\/5\nconsole.log(priorityQueue.dequeue());  \/\/4\nconsole.log(priorityQueue.dequeue());  \/\/3\nconsole.log(priorityQueue.dequeue());  \/\/2\nconsole.log(priorityQueue.dequeue());  \/\/1\n\n\/\/\u9006\u5e8f\u6392\u5217\nlet f:function = function(a:int,b:int):int{return b-a};\nlet p:Heap =new Heap(f);\np.enqueue(2);\np.enqueue(6);\np.enqueue(5);\np.enqueue(8);\np.enqueue(7);\np.enqueue(4);\np.enqueue(1);\np.enqueue(3);\np.enqueue(9);\n\nconsole.log(p.dequeue()); \/\/1\nconsole.log(p.dequeue()); \/\/2\nconsole.log(p.dequeue()); \/\/3\nconsole.log(p.dequeue()); \/\/4\nconsole.log(p.dequeue()); \/\/5\nconsole.log(p.dequeue()); \/\/6\nconsole.log(p.dequeue()); \/\/7\nconsole.log(p.dequeue()); \/\/8\nconsole.log(p.dequeue()); \/\/9\n<\/code><\/pre>\n<p>Heap\u5185\u90e8\u4e5f\u662f\u6570\u7ec4\u5b58\u50a8\uff0c\u5b83\u7684\u7b97\u6cd5\u6709\u4e24\u4e2a\u5173\u952e:<\/p>\n<p>1) \u5982\u4f55\u7528\u6570\u7ec4\u50a8\u5b58\u4e8c\u53c9\u6811\u7ed3\u6784<\/p>\n<p>\u6bd4\u5982\u539f\u6811\u72b6\u7ed3\u6784\u662f\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">\/\/          4\n\/\/         \/ \\\n\/\/        3   0\n\/\/       \/\\\n\/\/      1  2\n<\/code><\/pre>\n<p>\u4f1a\u5b58\u6210\uff1a[4,3,0,1,2]<\/p>\n<p>2) \u5982\u4f55\u6c38\u8fdc\u4fdd\u6301parent\u6bd4child\u5927<\/p>\n<h3>\u56db\u53c9\u6811<\/h3>\n<p>\u56db\u53c9\u6811\u901a\u5e38\u7528\u4e8e2d\u6e38\u620f\u78b0\u649e\u68c0\u6d4b\u3002\u5982\u679c\u662f\u5c0f\u7403\uff0c\u7528\u534a\u5f84\u7b97\u51fa\u6765\u5c31\u53ef\u4ee5\uff0c\u4f46\u5982\u679c\u78b0\u4e0a\u590d\u6742\u7684\u56fe\u5f62\u7528\u4f4d\u56fe\u78b0\u649e\u68c0\u6d4b\uff0c\u6d4f\u89c8\u5668\u5f88\u96be\u627f\u53d7\u3002<\/p>\n<p>\u56db\u53c9\u6811\u7684\u601d\u60f3\u5c31\u662f\u5148\u5c4f\u5e55\u5206\u5272\uff0c\u7136\u540e\u8fc7\u6ee4\u51fa\u79bb\u81ea\u5df1\u5f88\u8fd1\u7684\uff0c\u6709\u53ef\u80fd\u4ea7\u751f\u78b0\u649e\u7684\u8fdb\u884c\u78b0\u649e\u68c0\u6d4b\u3002<\/p>\n<p>\u4e0d\u8fc7\u7ecf\u8fc7\u7814\u7a76\uff0c\u81ea\u5df1\u5b9e\u73b0\u7684\u56db\u53c9\u6811\u4f1a\u6709\u6f0f\u68c0\uff0c\u8fd9\u91cc\u5c31\u7565\u8fc7\u3002<\/p>\n<h3>\u56fe<\/h3>\n<p>\u56fe\u7531\u8282\u70b9node\u548c\u6307\u9488arc\u7ec4\u6210\uff0c\u56fe\u7684\u904d\u5386\u4e0e\u6811\u5dee\u4e0d\u591a\uff0c\u5206\u4e3a\u5e7f\u5ea6\u4f18\u5148\u904d\u5386\u548c\u6df1\u5ea6\u4f18\u5148\u904d\u5386\u3002<\/p>\n<p>\u56fe\u5728\u6e38\u620f\u4e2d\u901a\u5e38\u7528\u4f5c\u4fdd\u5b58\u5730\u56fe\uff0c\u533a\u5757\u5176\u5b9e\u5c31\u662f\u7b80\u5316\u4e86\u6307\u9488\u4e4b\u540e\u7684\u56fe\uff0c\u6240\u4ee5\u4ed6\u4eec\u7684\u7406\u8bba\u662f\u76f8\u901a\u7684\uff1a<\/p>\n<pre><code class=\"language-javascript line-numbers\">class Astar\n{\n    \/**\n    *   \u8ddd\u79bb\u542f\u53d1\u51fd\u6570\n    *\/\n    public static distance(startNode:GraphNode,endNode:GraphNode):number\n    {\n        return  Math.sqrt( (endNode.data.x - startNode.data.x)*(endNode.data.x - startNode.data.x) + (endNode.data.y - startNode.data.y)*(endNode.data.y - startNode.data.y))\n    }\n    \/**\n    *  \u66fc\u54c8\u987f\u542f\u53d1\u51fd\u6570\n    *\/\n    public static manhattan(startNode:GraphNode,endNode:GraphNode):number\n    {\n        return  Math.abs(endNode.data.x - startNode.data.x)+Math.abs(endNode.data.y - endNode.data.y);\n    }\n    public static find(graph:Graph , startIndex:int,endIndex:int):array\n    {\n        for each (let n:AstarNode in graph.nodes)\n        {\n            n.f = n.g = n.f = 0\n            n.parent = null\n        }\n        let open:Heap = new Heap(function(a:AstarNode,b:AstarNode):number{return b.f - a.f});\n\n        let close:array = []\n        AstarNode(graph.getNode(startIndex)).f = 0\n        open.enqueue(graph.getNode(startIndex));\n        while(open.heap.length&gt;0)\n        {\n            let cur:AstarNode = AstarNode(open.dequeue())\n            if(cur==graph.getNode(endIndex))return  Astar.getPath(cur)\n            close.push(cur)\n            for each(let i:GraphArc in cur.arcs)\n            {\n                let test:AstarNode = AstarNode(i.targetNode)\n                if(close.indexOf(test)&gt;=0)continue\uff1b\n                if(open.heap.indexOf(test)&lt;0)\n                {\n                    test.parent =cur\n                    test.g = cur.getArc(test).weight\n                    test.h = Astar.distance(test,graph.getNode(endIndex))\n                    test.f = test.g + test.h\n                    open.enqueue(test);\n                }else\n                {\n                    if( cur.g+ cur.getArc(test).weight &lt; test.g)\n                    {\n                        test.parent = cur\n                        test.g = cur.g+ cur.getArc(test).weight\n                        test.f = test.h+test.g\n                        open.modify(test,test);\n                    }\n                }\n            }\n        }\n        return []\n    }\n\n    public static getPath(node:AstarNode):array\n    {\n        let arr:array = [node]\n        while(node.parent)\n        {\n            arr.push(node.parent)\n            node = node.parent\n        }\n        return arr\n    }\n\n}\n<\/code><\/pre>\n<p>code enjoy!\ud83d\ude1c\ud83d\ude1c\ud83d\ude1c<\/p>\n<p>\u4f5c\u8005\uff1aindeex<\/p>\n<p>\u94fe\u63a5\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/indeex.club\/\">https:\/\/indeex.club<\/a><\/p>\n<p>\u8457\u4f5c\u6743\u5f52\u4f5c\u8005\u6240\u6709\u3002\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u8054\u7cfb\u4f5c\u8005\u83b7\u5f97\u6388\u6743\uff0c\u975e\u5546\u4e1a\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\u3002<\/p>\n<hr \/>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u636e\u7ed3\u6784\u7684\u8bb2\u89e3\u5f88\u5c11\uff0c\u800c\u4e14\u96f6\u96f6\u6563\u6563\u7684\uff0c\u628a\u4ee5\u524d\u4f7f\u7528\u5176\u4ed6ECMAScript\u5b9e\u73b0\u7684\u6574\u7406\u518d\u7528Typescri<a href=\"https:\/\/blog.indeex.club\/index.php\/2020\/05\/05\/%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84\/\" class=\"read-more\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[4],"_links":{"self":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/196"}],"collection":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/comments?post=196"}],"version-history":[{"count":1,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/196\/revisions"}],"predecessor-version":[{"id":197,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/posts\/196\/revisions\/197"}],"wp:attachment":[{"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/categories?post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.indeex.club\/index.php\/wp-json\/wp\/v2\/tags?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}