Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function Request. All less than a value. #7

Open
jvonmitchell opened this issue May 27, 2014 · 1 comment
Open

Function Request. All less than a value. #7

jvonmitchell opened this issue May 27, 2014 · 1 comment

Comments

@jvonmitchell
Copy link

Hi,

It would make this library really useful if we could get a list of items less than a certain value or if there were a way to extrapolate that data without mutating the heap.

Here is a function I made which you are welcome to use.

Heap.prototype.peekTo = function(value,cmp) {
var c;
if(cmp===null) {
cmp=this.cmp;
}
for(c=0;c<this.nodes.length;++c) {
if(cmp(value,this.nodes[c]) <= 0) {
break;
}
}
return this.nodes.slice(0,c);
}

There is an option to provide a compare function in case we need to compare something like {someValue:....} and value without creating a new instance of the objects used in the heap.

@JAForbes
Copy link

I'm fairly sure the underlying nodes array is exposed as heap.nodes.

So you could use normal array functions like every to do any inspection you would like:

var greaterThan = function(value, node){

  if(typeof node == "undefined") return greaterThan.bind(null, value)

  return node > value
}

heap.nodes
  .every(greaterThan(5)) //=> ( true || false )

You could substitute greaterThan for any function you wish.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants