Comparing version 1.1.0 to 1.2.0
@@ -0,1 +1,5 @@ | ||
##### 1.2.0 - 29 April 2016 | ||
Switched to Rollup. | ||
##### 1.1.0 - 10 July 2015 | ||
@@ -2,0 +6,0 @@ |
325
dist/yabh.js
@@ -1,207 +0,146 @@ | ||
/*! | ||
* yabh | ||
* @version 1.1.0 - Homepage <http://jmdobry.github.io/yabh/> | ||
* @author Jason Dobry <jason.dobry@gmail.com> | ||
* @copyright (c) 2015 Jason Dobry | ||
* @license MIT <https://github.com/jmdobry/yabh/blob/master/LICENSE> | ||
* | ||
* @overview Yet another Binary Heap. | ||
*/ | ||
(function webpackUniversalModuleDefinition(root, factory) { | ||
if(typeof exports === 'object' && typeof module === 'object') | ||
module.exports = factory(); | ||
else if(typeof define === 'function' && define.amd) | ||
define("yabh", factory); | ||
else if(typeof exports === 'object') | ||
exports["BinaryHeap"] = factory(); | ||
else | ||
root["BinaryHeap"] = factory(); | ||
})(this, function() { | ||
return /******/ (function(modules) { // webpackBootstrap | ||
/******/ // The module cache | ||
/******/ var installedModules = {}; | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define('yabh', factory) : | ||
(global.BinaryHeap = factory()); | ||
}(this, function () { 'use strict'; | ||
/******/ // The require function | ||
/******/ function __webpack_require__(moduleId) { | ||
/** | ||
* @method bubbleUp | ||
* @param {array} heap The heap. | ||
* @param {function} weightFunc The weight function. | ||
* @param {number} n The index of the element to bubble up. | ||
*/ | ||
var bubbleUp = function bubbleUp(heap, weightFunc, n) { | ||
var element = heap[n]; | ||
var weight = weightFunc(element); | ||
// When at 0, an element can not go up any further. | ||
while (n > 0) { | ||
// Compute the parent element's index, and fetch it. | ||
var parentN = Math.floor((n + 1) / 2) - 1; | ||
var parent = heap[parentN]; | ||
// If the parent has a lesser weight, things are in order and we | ||
// are done. | ||
if (weight >= weightFunc(parent)) { | ||
break; | ||
} else { | ||
heap[parentN] = element; | ||
heap[n] = parent; | ||
n = parentN; | ||
} | ||
} | ||
}; | ||
/******/ // Check if module is in cache | ||
/******/ if(installedModules[moduleId]) | ||
/******/ return installedModules[moduleId].exports; | ||
/** | ||
* @method bubbleDown | ||
* @param {array} heap The heap. | ||
* @param {function} weightFunc The weight function. | ||
* @param {number} n The index of the element to sink down. | ||
*/ | ||
var bubbleDown = function bubbleDown(heap, weightFunc, n) { | ||
var length = heap.length; | ||
var node = heap[n]; | ||
var nodeWeight = weightFunc(node); | ||
/******/ // Create a new module (and put it into the cache) | ||
/******/ var module = installedModules[moduleId] = { | ||
/******/ exports: {}, | ||
/******/ id: moduleId, | ||
/******/ loaded: false | ||
/******/ }; | ||
while (true) { | ||
var child2N = (n + 1) * 2; | ||
var child1N = child2N - 1; | ||
var swap = null; | ||
if (child1N < length) { | ||
var child1 = heap[child1N]; | ||
var child1Weight = weightFunc(child1); | ||
// If the score is less than our node's, we need to swap. | ||
if (child1Weight < nodeWeight) { | ||
swap = child1N; | ||
} | ||
} | ||
// Do the same checks for the other child. | ||
if (child2N < length) { | ||
var child2 = heap[child2N]; | ||
var child2Weight = weightFunc(child2); | ||
if (child2Weight < (swap === null ? nodeWeight : weightFunc(heap[child1N]))) { | ||
swap = child2N; | ||
} | ||
} | ||
/******/ // Execute the module function | ||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); | ||
if (swap === null) { | ||
break; | ||
} else { | ||
heap[n] = heap[swap]; | ||
heap[swap] = node; | ||
n = swap; | ||
} | ||
} | ||
}; | ||
/******/ // Flag the module as loaded | ||
/******/ module.loaded = true; | ||
function BinaryHeap(weightFunc, compareFunc) { | ||
if (!weightFunc) { | ||
weightFunc = function weightFunc(x) { | ||
return x; | ||
}; | ||
} | ||
if (!compareFunc) { | ||
compareFunc = function compareFunc(x, y) { | ||
return x === y; | ||
}; | ||
} | ||
if (typeof weightFunc !== 'function') { | ||
throw new Error('BinaryHeap([weightFunc][, compareFunc]): "weightFunc" must be a function!'); | ||
} | ||
if (typeof compareFunc !== 'function') { | ||
throw new Error('BinaryHeap([weightFunc][, compareFunc]): "compareFunc" must be a function!'); | ||
} | ||
this.weightFunc = weightFunc; | ||
this.compareFunc = compareFunc; | ||
this.heap = []; | ||
} | ||
/******/ // Return the exports of the module | ||
/******/ return module.exports; | ||
/******/ } | ||
var BHProto = BinaryHeap.prototype; | ||
BHProto.push = function (node) { | ||
this.heap.push(node); | ||
bubbleUp(this.heap, this.weightFunc, this.heap.length - 1); | ||
}; | ||
/******/ // expose the modules object (__webpack_modules__) | ||
/******/ __webpack_require__.m = modules; | ||
BHProto.peek = function () { | ||
return this.heap[0]; | ||
}; | ||
/******/ // expose the module cache | ||
/******/ __webpack_require__.c = installedModules; | ||
BHProto.pop = function () { | ||
var front = this.heap[0]; | ||
var end = this.heap.pop(); | ||
if (this.heap.length > 0) { | ||
this.heap[0] = end; | ||
bubbleDown(this.heap, this.weightFunc, 0); | ||
} | ||
return front; | ||
}; | ||
/******/ // __webpack_public_path__ | ||
/******/ __webpack_require__.p = ""; | ||
BHProto.remove = function (node) { | ||
var length = this.heap.length; | ||
for (var i = 0; i < length; i++) { | ||
if (this.compareFunc(this.heap[i], node)) { | ||
var removed = this.heap[i]; | ||
var end = this.heap.pop(); | ||
if (i !== length - 1) { | ||
this.heap[i] = end; | ||
bubbleUp(this.heap, this.weightFunc, i); | ||
bubbleDown(this.heap, this.weightFunc, i); | ||
} | ||
return removed; | ||
} | ||
} | ||
return null; | ||
}; | ||
/******/ // Load entry module and return exports | ||
/******/ return __webpack_require__(0); | ||
/******/ }) | ||
/************************************************************************/ | ||
/******/ ([ | ||
/* 0 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
BHProto.removeAll = function () { | ||
this.heap = []; | ||
}; | ||
/** | ||
* @method bubbleUp | ||
* @param {array} heap The heap. | ||
* @param {function} weightFunc The weight function. | ||
* @param {number} n The index of the element to bubble up. | ||
*/ | ||
function bubbleUp(heap, weightFunc, n) { | ||
var element = heap[n]; | ||
var weight = weightFunc(element); | ||
// When at 0, an element can not go up any further. | ||
while (n > 0) { | ||
// Compute the parent element's index, and fetch it. | ||
var parentN = Math.floor((n + 1) / 2) - 1; | ||
var _parent = heap[parentN]; | ||
// If the parent has a lesser weight, things are in order and we | ||
// are done. | ||
if (weight >= weightFunc(_parent)) { | ||
break; | ||
} else { | ||
heap[parentN] = element; | ||
heap[n] = _parent; | ||
n = parentN; | ||
} | ||
} | ||
} | ||
BHProto.size = function () { | ||
return this.heap.length; | ||
}; | ||
/** | ||
* @method bubbleDown | ||
* @param {array} heap The heap. | ||
* @param {function} weightFunc The weight function. | ||
* @param {number} n The index of the element to sink down. | ||
*/ | ||
var bubbleDown = function bubbleDown(heap, weightFunc, n) { | ||
var length = heap.length; | ||
var node = heap[n]; | ||
var nodeWeight = weightFunc(node); | ||
return BinaryHeap; | ||
while (true) { | ||
var child2N = (n + 1) * 2, | ||
child1N = child2N - 1; | ||
var swap = null; | ||
if (child1N < length) { | ||
var child1 = heap[child1N], | ||
child1Weight = weightFunc(child1); | ||
// If the score is less than our node's, we need to swap. | ||
if (child1Weight < nodeWeight) { | ||
swap = child1N; | ||
} | ||
} | ||
// Do the same checks for the other child. | ||
if (child2N < length) { | ||
var child2 = heap[child2N], | ||
child2Weight = weightFunc(child2); | ||
if (child2Weight < (swap === null ? nodeWeight : weightFunc(heap[child1N]))) { | ||
swap = child2N; | ||
} | ||
} | ||
if (swap === null) { | ||
break; | ||
} else { | ||
heap[n] = heap[swap]; | ||
heap[swap] = node; | ||
n = swap; | ||
} | ||
} | ||
}; | ||
function BinaryHeap(weightFunc, compareFunc) { | ||
if (!weightFunc) { | ||
weightFunc = function (x) { | ||
return x; | ||
}; | ||
} | ||
if (!compareFunc) { | ||
compareFunc = function (x, y) { | ||
return x === y; | ||
}; | ||
} | ||
if (typeof weightFunc !== 'function') { | ||
throw new Error('BinaryHeap([weightFunc][, compareFunc]): "weightFunc" must be a function!'); | ||
} | ||
if (typeof compareFunc !== 'function') { | ||
throw new Error('BinaryHeap([weightFunc][, compareFunc]): "compareFunc" must be a function!'); | ||
} | ||
this.weightFunc = weightFunc; | ||
this.compareFunc = compareFunc; | ||
this.heap = []; | ||
} | ||
var BHProto = BinaryHeap.prototype; | ||
BHProto.push = function (node) { | ||
this.heap.push(node); | ||
bubbleUp(this.heap, this.weightFunc, this.heap.length - 1); | ||
}; | ||
BHProto.peek = function () { | ||
return this.heap[0]; | ||
}; | ||
BHProto.pop = function () { | ||
var front = this.heap[0]; | ||
var end = this.heap.pop(); | ||
if (this.heap.length > 0) { | ||
this.heap[0] = end; | ||
bubbleDown(this.heap, this.weightFunc, 0); | ||
} | ||
return front; | ||
}; | ||
BHProto.remove = function (node) { | ||
var length = this.heap.length; | ||
for (var i = 0; i < length; i++) { | ||
if (this.compareFunc(this.heap[i], node)) { | ||
var removed = this.heap[i]; | ||
var end = this.heap.pop(); | ||
if (i !== length - 1) { | ||
this.heap[i] = end; | ||
bubbleUp(this.heap, this.weightFunc, i); | ||
bubbleDown(this.heap, this.weightFunc, i); | ||
} | ||
return removed; | ||
} | ||
} | ||
return null; | ||
}; | ||
BHProto.removeAll = function () { | ||
this.heap = []; | ||
}; | ||
BHProto.size = function () { | ||
return this.heap.length; | ||
}; | ||
module.exports = BinaryHeap; | ||
/***/ } | ||
/******/ ]) | ||
}); | ||
; | ||
})); | ||
//# sourceMappingURL=yabh.js.map |
@@ -1,12 +0,2 @@ | ||
/** | ||
* @author Jason Dobry <jason.dobry@gmail.com> | ||
* @file yabh.min.js | ||
* @version 1.1.0 - Homepage <http://jmdobry.github.io/yabh/> | ||
* @copyright (c) 2015 Jason Dobry | ||
* @license MIT <https://github.com/jmdobry/yabh/blob/master/LICENSE> | ||
* | ||
* @overview Yet another Binary Heap. | ||
*/ | ||
!function(a,b){"object"==typeof exports&&"object"==typeof module?module.exports=b():"function"==typeof define&&define.amd?define("yabh",b):"object"==typeof exports?exports.BinaryHeap=b():a.BinaryHeap=b()}(this,function(){return function(a){function b(d){if(c[d])return c[d].exports;var e=c[d]={exports:{},id:d,loaded:!1};return a[d].call(e.exports,e,e.exports,b),e.loaded=!0,e.exports}var c={};return b.m=a,b.c=c,b.p="",b(0)}([function(a,b,c){function d(a,b,c){for(var d=a[c],e=b(d);c>0;){var f=Math.floor((c+1)/2)-1,g=a[f];if(e>=b(g))break;a[f]=d,a[c]=g,c=f}}function e(a,b){if(a||(a=function(a){return a}),b||(b=function(a,b){return a===b}),"function"!=typeof a)throw new Error('BinaryHeap([weightFunc][, compareFunc]): "weightFunc" must be a function!');if("function"!=typeof b)throw new Error('BinaryHeap([weightFunc][, compareFunc]): "compareFunc" must be a function!');this.weightFunc=a,this.compareFunc=b,this.heap=[]}var f=function(a,b,c){for(var d=a.length,e=a[c],f=b(e);;){var g=2*(c+1),h=g-1,i=null;if(d>h){var j=a[h],k=b(j);f>k&&(i=h)}if(d>g){var l=a[g],m=b(l);m<(null===i?f:b(a[h]))&&(i=g)}if(null===i)break;a[c]=a[i],a[i]=e,c=i}},g=e.prototype;g.push=function(a){this.heap.push(a),d(this.heap,this.weightFunc,this.heap.length-1)},g.peek=function(){return this.heap[0]},g.pop=function(){var a=this.heap[0],b=this.heap.pop();return this.heap.length>0&&(this.heap[0]=b,f(this.heap,this.weightFunc,0)),a},g.remove=function(a){for(var b=this.heap.length,c=0;b>c;c++)if(this.compareFunc(this.heap[c],a)){var e=this.heap[c],g=this.heap.pop();return c!==b-1&&(this.heap[c]=g,d(this.heap,this.weightFunc,c),f(this.heap,this.weightFunc,c)),e}return null},g.removeAll=function(){this.heap=[]},g.size=function(){return this.heap.length},a.exports=e}])}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define("yabh",e):t.BinaryHeap=e()}(this,function(){"use strict";function t(t,e){if(t||(t=function(t){return t}),e||(e=function(t,e){return t===e}),"function"!=typeof t)throw new Error('BinaryHeap([weightFunc][, compareFunc]): "weightFunc" must be a function!');if("function"!=typeof e)throw new Error('BinaryHeap([weightFunc][, compareFunc]): "compareFunc" must be a function!');this.weightFunc=t,this.compareFunc=e,this.heap=[]}var e=function(t,e,n){for(var i=t[n],h=e(i);n>0;){var r=Math.floor((n+1)/2)-1,u=t[r];if(h>=e(u))break;t[r]=i,t[n]=u,n=r}},n=function(t,e,n){for(var i=t.length,h=t[n],r=e(h);;){var u=2*(n+1),o=u-1,a=null;if(i>o){var p=t[o],c=e(p);r>c&&(a=o)}if(i>u){var f=t[u],s=e(f);s<(null===a?r:e(t[o]))&&(a=u)}if(null===a)break;t[n]=t[a],t[a]=h,n=a}},i=t.prototype;return i.push=function(t){this.heap.push(t),e(this.heap,this.weightFunc,this.heap.length-1)},i.peek=function(){return this.heap[0]},i.pop=function(){var t=this.heap[0],e=this.heap.pop();return this.heap.length>0&&(this.heap[0]=e,n(this.heap,this.weightFunc,0)),t},i.remove=function(t){for(var i=this.heap.length,h=0;i>h;h++)if(this.compareFunc(this.heap[h],t)){var r=this.heap[h],u=this.heap.pop();return h!==i-1&&(this.heap[h]=u,e(this.heap,this.weightFunc,h),n(this.heap,this.weightFunc,h)),r}return null},i.removeAll=function(){this.heap=[]},i.size=function(){return this.heap.length},t}); | ||
//# sourceMappingURL=yabh.min.map |
{ | ||
"name": "yabh", | ||
"description": "Yet another Binary Heap.", | ||
"version": "1.1.0", | ||
"homepage": "http://jmdobry.github.io/yabh/", | ||
"version": "1.2.0", | ||
"homepage": "https://github.com/jmdobry/yabh", | ||
"repository": { | ||
@@ -15,9 +15,8 @@ "type": "git", | ||
}, | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "https://github.com/jmdobry/yabh/blob/master/LICENSE" | ||
} | ||
"license": "MIT", | ||
"main": "./dist/yabh.js", | ||
"files": [ | ||
"dist/", | ||
"src/" | ||
], | ||
"main": "./dist/yabh.js", | ||
"keywords": [ | ||
@@ -29,30 +28,43 @@ "priority", | ||
], | ||
"standard": { | ||
"parser": "babel-eslint", | ||
"globals": [ | ||
"beforeEach", | ||
"describe", | ||
"it" | ||
] | ||
}, | ||
"babel": { | ||
"presets": [ | ||
"es2015" | ||
] | ||
}, | ||
"scripts": { | ||
"lint": "standard src/ test/ *.js", | ||
"bundle": "rollup src/index.js -c -o dist/yabh.js -m dist/yabh.js.map -f umd", | ||
"min": "uglifyjs dist/yabh.js -o dist/yabh.min.js --source-map dist/yabh.min.map --source-map-url yabh.min.map -v -m -c --screw-ie8", | ||
"build": "npm run lint && npm run bundle && npm run min", | ||
"cover": "nyc --require babel-core/register --cache mocha --recursive -t 20000 -R dot mocha.start.js test/*.js", | ||
"karma": "karma start", | ||
"test": "npm run build && npm run cover && npm run karma", | ||
"ci": "npm test && nyc report --reporter=lcov | codecov" | ||
}, | ||
"devDependencies": { | ||
"babel-core": "5.6.17", | ||
"babel-loader": "5.3.1", | ||
"grunt": "0.4.5", | ||
"grunt-contrib-clean": "0.6.0", | ||
"grunt-contrib-uglify": "0.9.1", | ||
"grunt-karma": "0.11.2", | ||
"grunt-karma-coveralls": "2.5.3", | ||
"grunt-mocha-test": "0.12.7", | ||
"grunt-webpack": "1.0.11", | ||
"jit-grunt": "0.9.1", | ||
"jshint": "2.8.0", | ||
"jshint-loader": "0.8.3", | ||
"karma": "0.12.37", | ||
"babel-core": "6.7.7", | ||
"babel-eslint": "6.0.4", | ||
"babel-preset-es2015-rollup": "1.1.1", | ||
"chai": "3.5.0", | ||
"codecov": "1.0.1", | ||
"karma": "0.13.22", | ||
"karma-chai": "0.1.0", | ||
"karma-chrome-launcher": "0.2.0", | ||
"karma-coverage": "0.4.2", | ||
"karma-firefox-launcher": "0.1.6", | ||
"karma-mocha": "0.2.0", | ||
"karma-phantomjs-launcher": "0.2.0", | ||
"karma-script-launcher": "0.1.0", | ||
"karma-spec-reporter": "0.0.19", | ||
"time-grunt": "1.2.1", | ||
"webpack-dev-server": "1.10.1" | ||
}, | ||
"scripts": { | ||
"test": "grunt test" | ||
"karma-mocha": "0.2.2", | ||
"karma-phantomjs-launcher": "1.0.0", | ||
"mocha": "2.4.5", | ||
"nyc": "6.4.0", | ||
"phantomjs-prebuilt": "2.1.7", | ||
"rollup": "0.26.1", | ||
"rollup-plugin-babel": "2.4.0", | ||
"standard": "6.0.8", | ||
"uglify-js": "2.6.2" | ||
} | ||
} |
@@ -1,16 +0,7 @@ | ||
## yabh [![bower version](https://img.shields.io/bower/v/yabh.svg?style=flat-square)](https://www.npmjs.org/package/yabh) [![npm version](https://img.shields.io/npm/v/yabh.svg?style=flat-square)](https://www.npmjs.org/package/yabh) [![Circle CI](https://img.shields.io/circleci/project/jmdobry/yabh/master.svg?style=flat-square)](https://circleci.com/gh/jmdobry/yabh/tree/master) [![npm downloads](https://img.shields.io/npm/dm/yabh.svg?style=flat-square)](https://www.npmjs.org/package/yabh) [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/jmdobry/yabh/blob/master/LICENSE) | ||
# yabh | ||
[![bower version](https://img.shields.io/bower/v/yabh.svg?style=flat-square)](https://www.npmjs.org/package/yabh) [![npm version](https://img.shields.io/npm/v/yabh.svg?style=flat-square)](https://www.npmjs.org/package/yabh) [![Circle CI](https://img.shields.io/circleci/project/jmdobry/yabh/master.svg?style=flat-square)](https://circleci.com/gh/jmdobry/yabh/tree/master) [![npm downloads](https://img.shields.io/npm/dm/yabh.svg?style=flat-square)](https://www.npmjs.org/package/yabh) [![codecov](https://img.shields.io/codecov/c/github/jmdobry/yabh.svg)](https://codecov.io/gh/jmdobry/yabh) [![Codacy](https://img.shields.io/codacy/2fb5b2126ab9480883548804d729ebf2.svg?style=flat-square)](https://www.codacy.com/public/jasondobry/yabh/dashboard) | ||
Yet another Binary Heap. | ||
__Latest Release:__ [![Latest Release](https://img.shields.io/github/release/jmdobry/yabh.svg?style=flat-square)](https://github.com/jmdobry/yabh/releases) | ||
__Status:__ | ||
[![Dependency Status](https://img.shields.io/gemnasium/jmdobry/yabh.svg?style=flat-square)](https://gemnasium.com/jmdobry/yabh) [![Coverage Status](https://img.shields.io/coveralls/jmdobry/yabh/master.svg?style=flat-square)](https://coveralls.io/r/jmdobry/yabh?branch=master) [![Codacy](https://img.shields.io/codacy/2fb5b2126ab9480883548804d729ebf2.svg?style=flat-square)](https://www.codacy.com/public/jasondobry/yabh/dashboard) | ||
__Supported Platforms:__ | ||
[![node version](https://img.shields.io/badge/Node-0.10%2B-green.svg?style=flat-square)](https://github.com/jmdobry/yabh) [![browsers](https://img.shields.io/badge/Browser-Chrome%2CFirefox%2CSafari%2COpera%2CIE%209%2B%2CiOS%20Safari%207.1%2B%2CAndroid%20Browser%202.3%2B-green.svg?style=flat-square)](https://github.com/jmdobry/yabh) | ||
### Quick Start | ||
@@ -132,3 +123,3 @@ `bower install --save yabh` or `npm install --save yabh`. | ||
Copyright (c) 2015 Jason Dobry | ||
Copyright (c) 2015-2016 Jason Dobry | ||
@@ -135,0 +126,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
120
src/index.js
@@ -7,18 +7,18 @@ /** | ||
*/ | ||
function bubbleUp(heap, weightFunc, n) { | ||
let element = heap[n]; | ||
let weight = weightFunc(element); | ||
const bubbleUp = function (heap, weightFunc, n) { | ||
const element = heap[n] | ||
const weight = weightFunc(element) | ||
// When at 0, an element can not go up any further. | ||
while (n > 0) { | ||
// Compute the parent element's index, and fetch it. | ||
let parentN = Math.floor((n + 1) / 2) - 1; | ||
let parent = heap[parentN]; | ||
let parentN = Math.floor((n + 1) / 2) - 1 | ||
let parent = heap[parentN] | ||
// If the parent has a lesser weight, things are in order and we | ||
// are done. | ||
if (weight >= weightFunc(parent)) { | ||
break; | ||
break | ||
} else { | ||
heap[parentN] = element; | ||
heap[n] = parent; | ||
n = parentN; | ||
heap[parentN] = element | ||
heap[n] = parent | ||
n = parentN | ||
} | ||
@@ -34,17 +34,17 @@ } | ||
*/ | ||
let bubbleDown = (heap, weightFunc, n) => { | ||
var length = heap.length; | ||
let node = heap[n]; | ||
let nodeWeight = weightFunc(node); | ||
const bubbleDown = function (heap, weightFunc, n) { | ||
var length = heap.length | ||
let node = heap[n] | ||
let nodeWeight = weightFunc(node) | ||
while (true) { | ||
let child2N = (n + 1) * 2, | ||
child1N = child2N - 1; | ||
let child2N = (n + 1) * 2 | ||
let child1N = child2N - 1 | ||
let swap = null; | ||
if (child1N < length) { | ||
let child1 = heap[child1N], | ||
child1Weight = weightFunc(child1); | ||
let child1 = heap[child1N] | ||
let child1Weight = weightFunc(child1) | ||
// If the score is less than our node's, we need to swap. | ||
if (child1Weight < nodeWeight) { | ||
swap = child1N; | ||
swap = child1N | ||
} | ||
@@ -54,6 +54,6 @@ } | ||
if (child2N < length) { | ||
let child2 = heap[child2N], | ||
child2Weight = weightFunc(child2); | ||
let child2 = heap[child2N] | ||
let child2Weight = weightFunc(child2) | ||
if (child2Weight < (swap === null ? nodeWeight : weightFunc(heap[child1N]))) { | ||
swap = child2N; | ||
swap = child2N | ||
} | ||
@@ -63,75 +63,75 @@ } | ||
if (swap === null) { | ||
break; | ||
break | ||
} else { | ||
heap[n] = heap[swap]; | ||
heap[swap] = node; | ||
n = swap; | ||
heap[n] = heap[swap] | ||
heap[swap] = node | ||
n = swap | ||
} | ||
} | ||
}; | ||
} | ||
function BinaryHeap(weightFunc, compareFunc) { | ||
function BinaryHeap (weightFunc, compareFunc) { | ||
if (!weightFunc) { | ||
weightFunc = x => x; | ||
weightFunc = function (x) { return x } | ||
} | ||
if (!compareFunc) { | ||
compareFunc = (x, y) => x === y; | ||
compareFunc = function (x, y) { return x === y } | ||
} | ||
if (typeof weightFunc !== 'function') { | ||
throw new Error('BinaryHeap([weightFunc][, compareFunc]): "weightFunc" must be a function!'); | ||
throw new Error('BinaryHeap([weightFunc][, compareFunc]): "weightFunc" must be a function!') | ||
} | ||
if (typeof compareFunc !== 'function') { | ||
throw new Error('BinaryHeap([weightFunc][, compareFunc]): "compareFunc" must be a function!'); | ||
throw new Error('BinaryHeap([weightFunc][, compareFunc]): "compareFunc" must be a function!') | ||
} | ||
this.weightFunc = weightFunc; | ||
this.compareFunc = compareFunc; | ||
this.heap = []; | ||
this.weightFunc = weightFunc | ||
this.compareFunc = compareFunc | ||
this.heap = [] | ||
} | ||
let BHProto = BinaryHeap.prototype; | ||
let BHProto = BinaryHeap.prototype | ||
BHProto.push = function (node) { | ||
this.heap.push(node); | ||
bubbleUp(this.heap, this.weightFunc, this.heap.length - 1); | ||
}; | ||
this.heap.push(node) | ||
bubbleUp(this.heap, this.weightFunc, this.heap.length - 1) | ||
} | ||
BHProto.peek = function () { | ||
return this.heap[0]; | ||
}; | ||
return this.heap[0] | ||
} | ||
BHProto.pop = function () { | ||
let front = this.heap[0]; | ||
let end = this.heap.pop(); | ||
let front = this.heap[0] | ||
let end = this.heap.pop() | ||
if (this.heap.length > 0) { | ||
this.heap[0] = end; | ||
bubbleDown(this.heap, this.weightFunc, 0); | ||
this.heap[0] = end | ||
bubbleDown(this.heap, this.weightFunc, 0) | ||
} | ||
return front; | ||
}; | ||
return front | ||
} | ||
BHProto.remove = function (node) { | ||
var length = this.heap.length; | ||
var length = this.heap.length | ||
for (let i = 0; i < length; i++) { | ||
if (this.compareFunc(this.heap[i], node)) { | ||
let removed = this.heap[i]; | ||
let end = this.heap.pop(); | ||
let removed = this.heap[i] | ||
let end = this.heap.pop() | ||
if (i !== length - 1) { | ||
this.heap[i] = end; | ||
bubbleUp(this.heap, this.weightFunc, i); | ||
bubbleDown(this.heap, this.weightFunc, i); | ||
this.heap[i] = end | ||
bubbleUp(this.heap, this.weightFunc, i) | ||
bubbleDown(this.heap, this.weightFunc, i) | ||
} | ||
return removed; | ||
return removed | ||
} | ||
} | ||
return null; | ||
}; | ||
return null | ||
} | ||
BHProto.removeAll = function () { | ||
this.heap = []; | ||
}; | ||
this.heap = [] | ||
} | ||
BHProto.size = function () { | ||
return this.heap.length; | ||
}; | ||
return this.heap.length | ||
} | ||
module.exports = BinaryHeap; | ||
export default BinaryHeap |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16
25043
9
258
143
1