| OLD | NEW |
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 <!-- | 2 <!-- |
| 3 Copyright (c) 2012 The Chromium Authors. All rights reserved. | 3 Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 4 Use of this source code is governed by a BSD-style license that can be | 4 Use of this source code is governed by a BSD-style license that can be |
| 5 found in the LICENSE file. | 5 found in the LICENSE file. |
| 6 --> | 6 --> |
| 7 <link rel="import" href="/tracing/base/base.html"> | 7 <link rel="import" href="/tracing/base/base.html"> |
| 8 <script> | 8 <script> |
| 9 'use strict'; | 9 'use strict'; |
| 10 | 10 |
| 11 /** | 11 /** |
| 12 * @fileoverview Splay tree used by CodeMap. | 12 * @fileoverview Splay tree used by CodeMap. |
| 13 */ | 13 */ |
| 14 tr.exportTo('tr.e.importer.v8', function() { | 14 tr.exportTo('tr.e.importer.v8', function() { |
| 15 /** | 15 /** |
| 16 * Constructs a Splay tree. A splay tree is a self-balancing binary | 16 * Constructs a Splay tree. A splay tree is a self-balancing binary |
| 17 * search tree with the additional property that recently accessed | 17 * search tree with the additional property that recently accessed |
| 18 * elements are quick to access again. It performs basic operations | 18 * elements are quick to access again. It performs basic operations |
| 19 * such as insertion, look-up and removal in O(log(n)) amortized time. | 19 * such as insertion, look-up and removal in O(log(n)) amortized time. |
| 20 * | 20 * |
| 21 * @constructor | 21 * @constructor |
| 22 */ | 22 */ |
| 23 function SplayTree() { }; | 23 function SplayTree() { } |
| 24 | 24 |
| 25 /** | 25 /** |
| 26 * Pointer to the root node of the tree. | 26 * Pointer to the root node of the tree. |
| 27 * | 27 * |
| 28 * @type {SplayTree.Node} | 28 * @type {SplayTree.Node} |
| 29 * @private | 29 * @private |
| 30 */ | 30 */ |
| 31 SplayTree.prototype.root_ = null; | 31 SplayTree.prototype.root_ = null; |
| 32 | 32 |
| 33 /** | 33 /** |
| (...skipping 12 matching lines...) Expand all Loading... |
| 46 * @param {*} value Value to insert into the tree. | 46 * @param {*} value Value to insert into the tree. |
| 47 */ | 47 */ |
| 48 SplayTree.prototype.insert = function(key, value) { | 48 SplayTree.prototype.insert = function(key, value) { |
| 49 if (this.isEmpty()) { | 49 if (this.isEmpty()) { |
| 50 this.root_ = new SplayTree.Node(key, value); | 50 this.root_ = new SplayTree.Node(key, value); |
| 51 return; | 51 return; |
| 52 } | 52 } |
| 53 // Splay on the key to move the last node on the search path for | 53 // Splay on the key to move the last node on the search path for |
| 54 // the key to the root of the tree. | 54 // the key to the root of the tree. |
| 55 this.splay_(key); | 55 this.splay_(key); |
| 56 if (this.root_.key == key) { | 56 if (this.root_.key === key) { |
| 57 return; | 57 return; |
| 58 } | 58 } |
| 59 var node = new SplayTree.Node(key, value); | 59 var node = new SplayTree.Node(key, value); |
| 60 if (key > this.root_.key) { | 60 if (key > this.root_.key) { |
| 61 node.left = this.root_; | 61 node.left = this.root_; |
| 62 node.right = this.root_.right; | 62 node.right = this.root_.right; |
| 63 this.root_.right = null; | 63 this.root_.right = null; |
| 64 } else { | 64 } else { |
| 65 node.right = this.root_; | 65 node.right = this.root_; |
| 66 node.left = this.root_.left; | 66 node.left = this.root_.left; |
| 67 this.root_.left = null; | 67 this.root_.left = null; |
| 68 } | 68 } |
| 69 this.root_ = node; | 69 this.root_ = node; |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 | 72 |
| 73 /** | 73 /** |
| 74 * Removes a node with the specified key from the tree if the tree | 74 * Removes a node with the specified key from the tree if the tree |
| 75 * contains a node with this key. The removed node is returned. If the | 75 * contains a node with this key. The removed node is returned. If the |
| 76 * key is not found, an exception is thrown. | 76 * key is not found, an exception is thrown. |
| 77 * | 77 * |
| 78 * @param {number} key Key to find and remove from the tree. | 78 * @param {number} key Key to find and remove from the tree. |
| 79 * @return {SplayTree.Node} The removed node. | 79 * @return {SplayTree.Node} The removed node. |
| 80 */ | 80 */ |
| 81 SplayTree.prototype.remove = function(key) { | 81 SplayTree.prototype.remove = function(key) { |
| 82 if (this.isEmpty()) { | 82 if (this.isEmpty()) { |
| 83 throw Error('Key not found: ' + key); | 83 throw Error('Key not found: ' + key); |
| 84 } | 84 } |
| 85 this.splay_(key); | 85 this.splay_(key); |
| 86 if (this.root_.key != key) { | 86 if (this.root_.key !== key) { |
| 87 throw Error('Key not found: ' + key); | 87 throw Error('Key not found: ' + key); |
| 88 } | 88 } |
| 89 var removed = this.root_; | 89 var removed = this.root_; |
| 90 if (!this.root_.left) { | 90 if (!this.root_.left) { |
| 91 this.root_ = this.root_.right; | 91 this.root_ = this.root_.right; |
| 92 } else { | 92 } else { |
| 93 var right = this.root_.right; | 93 var right = this.root_.right; |
| 94 this.root_ = this.root_.left; | 94 this.root_ = this.root_.left; |
| 95 // Splay to make sure that the new root has an empty right child. | 95 // Splay to make sure that the new root has an empty right child. |
| 96 this.splay_(key); | 96 this.splay_(key); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 108 * | 108 * |
| 109 * | 109 * |
| 110 * @param {number} key Key to find in the tree. | 110 * @param {number} key Key to find in the tree. |
| 111 * @return {SplayTree.Node} Node having the specified key. | 111 * @return {SplayTree.Node} Node having the specified key. |
| 112 */ | 112 */ |
| 113 SplayTree.prototype.find = function(key) { | 113 SplayTree.prototype.find = function(key) { |
| 114 if (this.isEmpty()) { | 114 if (this.isEmpty()) { |
| 115 return null; | 115 return null; |
| 116 } | 116 } |
| 117 this.splay_(key); | 117 this.splay_(key); |
| 118 return this.root_.key == key ? this.root_ : null; | 118 return this.root_.key === key ? this.root_ : null; |
| 119 }; | 119 }; |
| 120 | 120 |
| 121 /** | 121 /** |
| 122 * @return {SplayTree.Node} Node having the minimum key value. | 122 * @return {SplayTree.Node} Node having the minimum key value. |
| 123 */ | 123 */ |
| 124 SplayTree.prototype.findMin = function() { | 124 SplayTree.prototype.findMin = function() { |
| 125 if (this.isEmpty()) { | 125 if (this.isEmpty()) { |
| 126 return null; | 126 return null; |
| 127 } | 127 } |
| 128 var current = this.root_; | 128 var current = this.root_; |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 /** | 262 /** |
| 263 * Performs a preorder traversal of the tree. | 263 * Performs a preorder traversal of the tree. |
| 264 * | 264 * |
| 265 * @param {function(SplayTree.Node)} f Visitor function. | 265 * @param {function(SplayTree.Node)} f Visitor function. |
| 266 * @private | 266 * @private |
| 267 */ | 267 */ |
| 268 SplayTree.prototype.traverse_ = function(f) { | 268 SplayTree.prototype.traverse_ = function(f) { |
| 269 var nodesToVisit = [this.root_]; | 269 var nodesToVisit = [this.root_]; |
| 270 while (nodesToVisit.length > 0) { | 270 while (nodesToVisit.length > 0) { |
| 271 var node = nodesToVisit.shift(); | 271 var node = nodesToVisit.shift(); |
| 272 if (node == null) { | 272 if (node === null) { |
| 273 continue; | 273 continue; |
| 274 } | 274 } |
| 275 f(node); | 275 f(node); |
| 276 nodesToVisit.push(node.left); | 276 nodesToVisit.push(node.left); |
| 277 nodesToVisit.push(node.right); | 277 nodesToVisit.push(node.right); |
| 278 } | 278 } |
| 279 }; | 279 }; |
| 280 | 280 |
| 281 /** | 281 /** |
| 282 * Constructs a Splay tree node. | 282 * Constructs a Splay tree node. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 297 /** | 297 /** |
| 298 * @type {SplayTree.Node} | 298 * @type {SplayTree.Node} |
| 299 */ | 299 */ |
| 300 SplayTree.Node.prototype.right = null; | 300 SplayTree.Node.prototype.right = null; |
| 301 | 301 |
| 302 return { | 302 return { |
| 303 SplayTree: SplayTree | 303 SplayTree: SplayTree |
| 304 }; | 304 }; |
| 305 }); | 305 }); |
| 306 </script> | 306 </script> |
| OLD | NEW |