-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary_heap.js.html
179 lines (158 loc) · 6.04 KB
/
binary_heap.js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: binary_heap.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: binary_heap.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>"use strict";
/**
* An implementation of the [Binary Heap]{@link https://en.wikipedia.org/wiki/Binary_heap} data structure suitable for priority queues.
* @constructor
* @alias Splat.BinaryHeap
* @param {compareFunction} cmp A comparison function that determines how the heap is sorted.
*/
function BinaryHeap(cmp) {
/**
* The comparison function for sorting the heap.
* @member {compareFunction}
* @private
*/
this.cmp = cmp;
/**
* The list of elements in the heap.
* @member {Array}
* @private
*/
this.array = [];
/**
* The number of elements in the heap.
* @member {number}
* @readonly
*/
this.length = 0;
}
/**
* Calculate the index of a node's parent.
* @param {number} i The index of the child node
* @returns {number}
* @private
*/
BinaryHeap.prototype.parentIndex = function(i) {
return ((i - 1) / 2) |0;
};
/**
* Calculate the index of a parent's first child node.
* @param {number} i The index of the parent node
* @returns {number}
* @private
*/
BinaryHeap.prototype.firstChildIndex = function(i) {
return (2 * i) + 1;
};
/**
* Bubble a node up the heap, stopping when it's value should not be sorted before its parent's value.
* @param {number} pos The index of the node to bubble up.
* @private
*/
BinaryHeap.prototype.bubbleUp = function(pos) {
if (pos === 0) {
return;
}
var data = this.array[pos];
var parentIndex = this.parentIndex(pos);
var parent = this.array[parentIndex];
if (this.cmp(data, parent) < 0) {
this.array[parentIndex] = data;
this.array[pos] = parent;
this.bubbleUp(parentIndex);
}
};
/**
* Store a new node in the heap.
* @param {object} data The data to store
*/
BinaryHeap.prototype.insert = function(data) {
this.array.push(data);
this.length = this.array.length;
var pos = this.array.length - 1;
this.bubbleUp(pos);
};
/**
* Bubble a node down the heap, stopping when it's value should not be sorted after its parent's value.
* @param {number} pos The index of the node to bubble down.
* @private
*/
BinaryHeap.prototype.bubbleDown = function(pos) {
var left = this.firstChildIndex(pos);
var right = left + 1;
var largest = pos;
if (left < this.array.length && this.cmp(this.array[left], this.array[largest]) < 0) {
largest = left;
}
if (right < this.array.length && this.cmp(this.array[right], this.array[largest]) < 0) {
largest = right;
}
if (largest !== pos) {
var tmp = this.array[pos];
this.array[pos] = this.array[largest];
this.array[largest] = tmp;
this.bubbleDown(largest);
}
};
/**
* Remove the heap's root node, and return it. The root node is whatever comes first as determined by the {@link compareFunction}.
* @returns {data} The root node's data.
*/
BinaryHeap.prototype.deleteRoot = function() {
var root = this.array[0];
if (this.array.length <= 1) {
this.array = [];
this.length = 0;
return root;
}
this.array[0] = this.array.pop();
this.length = this.array.length;
this.bubbleDown(0);
return root;
};
/**
* Search for a node in the heap.
* @param {object} data The data to search for.
* @returns {number} The index of the data in the heap, or -1 if it is not found.
*/
BinaryHeap.prototype.indexOf = function(data) {
for (var i = 0; i < this.array.length; i++) {
if (this.array[i] === data) {
return i;
}
}
return -1;
};
module.exports = BinaryHeap;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-buffer.html">buffer</a></li><li><a href="module-KeyMap.html">KeyMap</a></li></ul><h3>Externals</h3><ul><li><a href="external-AudioContext.html">AudioContext</a></li><li><a href="external-canvas.html">canvas</a></li><li><a href="external-CanvasRenderingContext2D.html">CanvasRenderingContext2D</a></li><li><a href="external-image.html">image</a></li></ul><h3>Classes</h3><ul><li><a href="Accelerometer.html">Accelerometer</a></li><li><a href="Animation.html">Animation</a></li><li><a href="AnimationLoader.html">AnimationLoader</a></li><li><a href="FontLoader.html">FontLoader</a></li><li><a href="ImageLoader.html">ImageLoader</a></li><li><a href="Keyboard.html">Keyboard</a></li><li><a href="Mouse.html">Mouse</a></li><li><a href="SceneManager.html">SceneManager</a></li><li><a href="SoundLoader.html">SoundLoader</a></li><li><a href="Splat.AnimatedEntity.html">AnimatedEntity</a></li><li><a href="Splat.AStar.html">AStar</a></li><li><a href="Splat.BinaryHeap.html">BinaryHeap</a></li><li><a href="Splat.Camera.html">Camera</a></li><li><a href="Splat.Entity.html">Entity</a></li><li><a href="Splat.EntityBoxCamera.html">EntityBoxCamera</a></li><li><a href="Splat.Game.html">Game</a></li><li><a href="Splat.math.Random.html">Random</a></li><li><a href="Splat.NinePatch.html">NinePatch</a></li><li><a href="Splat.Scene.html">Scene</a></li><li><a href="Splat.Timer.html">Timer</a></li></ul><h3>Namespaces</h3><ul><li><a href="Splat.html">Splat</a></li><li><a href="Splat.ads.html">ads</a></li><li><a href="Splat.leaderboards.html">leaderboards</a></li><li><a href="Splat.math.html">math</a></li><li><a href="Splat.saveData.html">saveData</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.2</a> on Thu Sep 17 2015 23:22:42 GMT-0400 (EDT)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>