|
1 | | -primitives.common.FibonacciHeap = function (isMaximum) { |
| 1 | +/** |
| 2 | + * Creates Fibonacci Heap structure |
| 3 | + * |
| 4 | + * @param {boolean} isMaximum Is maximum heap |
| 5 | + * @returns {FibonacciHeap} Returns new FibonacciHeap object |
| 6 | + */ |
| 7 | +primitives.common.FibonacciHeap = function (isMaximum) { |
2 | 8 | var root = null, |
3 | 9 | count = 0, |
4 | 10 | nodes = {}; |
5 | 11 |
|
| 12 | + /** |
| 13 | + * @typedef {Object} HeapResult |
| 14 | + * @property {string} key Key |
| 15 | + * @property {number} priority Priority |
| 16 | + * @property {Object} item Context object |
| 17 | + */ |
6 | 18 | function Result(node) { |
7 | 19 | this.key = node.key; |
8 | 20 | this.priority = node.priority; |
|
22 | 34 | this.right = null; |
23 | 35 | } |
24 | 36 |
|
| 37 | + /** |
| 38 | + * Validates internal structure consistency. |
| 39 | + * |
| 40 | + * @returns {boolean} Returns true if structure pass data consistency check. |
| 41 | + */ |
25 | 42 | function validate() { |
26 | 43 | var totalNodes = 0; |
27 | 44 | for (var key in nodes) { |
|
105 | 122 | } |
106 | 123 | } |
107 | 124 |
|
| 125 | + /** |
| 126 | + * Adds a new item into the heap |
| 127 | + * @param {string} key A key of the new element |
| 128 | + * @param {number} priority A priority of the new element |
| 129 | + * @param {object} item A context object of the new element |
| 130 | + */ |
108 | 131 | function add(key, priority, item) { |
109 | 132 | if (nodes.hasOwnProperty(key)) { |
110 | 133 | throw "Duplicate keys are not supported!"; |
|
145 | 168 | node.left = node.key; |
146 | 169 | } |
147 | 170 |
|
| 171 | + /** |
| 172 | + * Gets priority of element by key |
| 173 | + * @param {string} key The element key |
| 174 | + * @returns {number} Returns priority of the element |
| 175 | + */ |
148 | 176 | function getPriority(key) { |
149 | 177 | var result = null; |
150 | 178 | if (nodes.hasOwnProperty(key)) { |
|
153 | 181 | return result; |
154 | 182 | } |
155 | 183 |
|
| 184 | + /** |
| 185 | + * Returns heap root element |
| 186 | + * |
| 187 | + * @returns {HeapResult} Returns root element of the heap |
| 188 | + */ |
156 | 189 | function heapRoot() { |
157 | 190 | var result = null; |
158 | 191 | if (root != null) { |
|
161 | 194 | return result; |
162 | 195 | } |
163 | 196 |
|
| 197 | + /** |
| 198 | + * Returns heap root element with removal |
| 199 | + * |
| 200 | + * @returns {HeapResult} Returns root element of the heap |
| 201 | + */ |
164 | 202 | function extractRoot() { |
165 | 203 | var result = heapRoot(); |
166 | 204 | if (result != null) { |
|
240 | 278 | node2.parent = node1.key; |
241 | 279 | } |
242 | 280 |
|
| 281 | + /** |
| 282 | + * Sets priority of an element by key |
| 283 | + * @param {string} key The key of the element |
| 284 | + * @param {number} priority Priority |
| 285 | + */ |
243 | 286 | function setPriority(key, priority) { |
244 | 287 | var node = nodes[key]; |
245 | 288 | if (isMaximum ? node.priority > priority : node.priority < priority) { |
|
284 | 327 | } |
285 | 328 | } |
286 | 329 |
|
| 330 | + /** |
| 331 | + * Deletes heap element by key |
| 332 | + * @param {string} key The Key |
| 333 | + */ |
287 | 334 | function deleteKey(key) { |
288 | 335 | setPriority(key, isMaximum ? Infinity : -1); |
289 | 336 | extractRoot(); |
|
0 commit comments