binaryheap-resizable
Advanced tools
Comparing version 0.0.6 to 0.0.7
121
index.js
/** | ||
* Created by root on 25/08/14. | ||
* Authored by: Roberto Sales <robertosalesc@dcc.ufba.br> (rsalesc) | ||
* Name: binaryheap-resizable | ||
* | ||
*/ | ||
var util = require('util'); | ||
var peek = require('peek'); | ||
@@ -13,9 +16,22 @@ function BinaryHeapR(capacity, predicate) { | ||
this._predicate = predicate || function(a, b){ return a > b}; | ||
if(capacity === undefined) throw new Error("initial capacity of binary heap must be passed"); | ||
this.data = new Array(capacity + 1); | ||
this.data[0] = null; | ||
this._predicate_method = predicate || function(a, b){ return a > b}; | ||
this._predicate_deep = null; | ||
this._predicate_deep_default = null; | ||
this.size = 0; | ||
if(capacity instanceof Array){ | ||
this.data = new Array(capacity.length + 1); | ||
this.data[0]= null; | ||
this.insert(capacity); | ||
}else if(typeof capacity === 'number'){ | ||
this.data = new Array(capacity + 1); | ||
this.data[0] = null; | ||
}else{ | ||
throw new Error("initial capacity of binary heap must be passed") | ||
} | ||
this.length = this._initialLength = capacity; | ||
this.size = 0; | ||
this._resizeFactor = 2; | ||
@@ -26,2 +42,14 @@ } | ||
BinaryHeapR.prototype._predicate = function(a, b){ | ||
if(typeof this._predicate_deep === 'string' && this._predicate_deep.length > 0){ | ||
a = peek(this._predicate_deep)(a) || this._predicate_deep_default; | ||
b = peek(this._predicate_deep)(b) || this._predicate_deep_default; | ||
} | ||
if(typeof a === 'undefined' || typeof b === 'undefined' || a == null || b == null){ | ||
throw new Error("predicate member values cannot be accessed"); | ||
return; | ||
} | ||
return this._predicate_method(a, b); | ||
}; | ||
BinaryHeapR.prototype.resizeFactor = function(value){ | ||
@@ -44,2 +72,11 @@ if(value === undefined || value == null) return this._resizeFactor; | ||
var i = 0; | ||
if(arguments.length < 1){ | ||
throw new Error("invalid arguments"); | ||
} | ||
// check if arguments[0] is an array | ||
if(arguments[0] instanceof Array){ | ||
arguments = arguments[0]; | ||
if(arguments.length < 1) return; | ||
} | ||
// check if buffer is about to be overflowed | ||
@@ -55,9 +92,13 @@ // expand it if its too small | ||
for(i = 0; i < arguments.length; i++){ | ||
this.data[++this.size] = arguments[i]; | ||
var child = this.size; | ||
var parent = child >> 1; | ||
while(child > 1 && this._predicate(this.data[child], this.data[parent])){ | ||
this.swap(child, parent); | ||
child = parent; | ||
parent = child >> 1; | ||
if(typeof arguments[i] !== 'undefined' && arguments[i] !== null){ | ||
this.data[++this.size] = arguments[i]; | ||
var child = this.size; | ||
var parent = child >> 1; | ||
while(child > 1 && this._predicate(this.data[child], this.data[parent])){ | ||
this.swap(child, parent); | ||
child = parent; | ||
parent = child >> 1; | ||
} | ||
}else{ | ||
throw new Error("null object cannot be inserted"); | ||
} | ||
@@ -70,3 +111,8 @@ } | ||
BinaryHeapR.prototype.pop = BinaryHeapR.prototype.pull = function(){ | ||
BinaryHeapR.prototype.peek = function(){ | ||
if(this.size == 0) return null; | ||
return this.data[1]; | ||
}; | ||
BinaryHeapR.prototype.pull = function(){ | ||
if(this.size === 0) return null; | ||
@@ -102,6 +148,51 @@ | ||
BinaryHeapR.prototype.predicate = function(fn){ | ||
if(!(fn instanceof Function)) return this._predicate; | ||
this._predicate = fn; | ||
var self = this; | ||
var setup = function(fn, path, def){ | ||
path = path || null; | ||
def = def || null; | ||
self._predicate_method = fn; | ||
self._predicate_deep = path; | ||
self._predicate_deep_default = def; | ||
self.reinsert(); | ||
return self; | ||
}; | ||
if(!(fn instanceof Function)){ // (path, def) for all | ||
return{ | ||
greater: function(path, def){ | ||
return setup(function(a, b){ return a > b}, path, def); | ||
}, | ||
lesser: function(path, def){ | ||
return setup(function(a, b){ return a < b}, path, def); | ||
} | ||
} | ||
} | ||
return{ | ||
value: function(path, def){ | ||
return setup(fn, path, def); | ||
} | ||
} | ||
}; | ||
BinaryHeapR.prototype.maxHeap = function(path, def){ | ||
return this.predicate().greater(path, def); | ||
}; | ||
BinaryHeapR.prototype.minHeap = function(path, def){ | ||
return this.predicate().lesser(path, def); | ||
}; | ||
BinaryHeapR.prototype.reinsert = function(){ | ||
var old = this.data; | ||
var old_size = this.size; | ||
this.data = new Array(old.length); | ||
this.data[0] = null; | ||
this.size = 0; | ||
for(var i = 1; i <= old_size; i++){ | ||
this.insert(old[i]); | ||
} | ||
}; | ||
BinaryHeapR.prototype.swap = function(a, b){ | ||
@@ -108,0 +199,0 @@ var tmp = this.data[a]; |
{ | ||
"author" : "Roberto Sales <robertosalesc@dcc.ufba.br>", | ||
"name": "binaryheap-resizable", | ||
"version": "0.0.6", | ||
"description": "binary heap implemented over a resizable circular buffer", | ||
"keywords": ["binary", "heap", "circular buffer", "tree"], | ||
"version": "0.0.7", | ||
"description": "binary heap implemented over a resizable array with multiple ways of handling predicates", | ||
"keywords": ["binary", "heap", "predicate", "tree", "resizable", "binary heap"], | ||
"scripts" : { | ||
"test": "./node_modules/.bin/mocha --reporter spec" | ||
}, | ||
"license": "MIT", | ||
@@ -12,7 +15,12 @@ "main": "index.js", | ||
"type": "git", | ||
"url": "http://github.com/skywalkerd/binaryheap-resizable.git" | ||
"url": "http://github.com/rsalesc/binaryheap-resizable.git" | ||
}, | ||
"dependencies": | ||
"dependencies":{ | ||
"peek": "~0.0.2" | ||
}, | ||
"devDependencies": | ||
{ | ||
"chai": "*", | ||
"mocha": "*" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
47355
17
330
1
147
1
2
2
+ Addedpeek@~0.0.2
+ Addedpeek@0.0.2(transitive)