Skip to content

Commit

Permalink
check in idea for heapify, run qheap benchmark twice
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasq committed Apr 24, 2021
1 parent f8c8cc4 commit a3e0008
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2013-2020 Andras Radics
Copyright 2013-2021 Andras Radics
andras at andrasq dot com

Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
9 changes: 7 additions & 2 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2017 Andras Radics
* Copyright (C) 2017-2021 Andras Radics
* Licensed under the Apache License, Version 2.0
*/

Expand Down Expand Up @@ -87,13 +87,18 @@ timeit.bench({
for (i = 128; i < 1280; i++) { b.push(rand(i)) ; b.pop() }
},
**/
qheap: function(){
var i, b = qheap();
for (i = 0; i < 128; i++) { b.insert(rand(i)) }
for (i = 128; i < 1280; i++) { b.insert(rand(i)) ; b.remove() }
},
fastpriorityqueue: function(){
var i, b = new fastpriorityqueue(comparAfter);
//var i, b = new fastpriorityqueue();
for (i = 0; i < 128; i++) { b.add(rand(i)) }
for (i = 128; i < 1280; i++) { b.add(rand(i)) ; b.poll() }
},
qheap: function(){
qheap2: function(){
var i, b = qheap();
for (i = 0; i < 128; i++) { b.insert(rand(i)) }
for (i = 128; i < 1280; i++) { b.insert(rand(i)) ; b.remove() }
Expand Down
21 changes: 20 additions & 1 deletion qheap.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Items are stored in a balanced binary tree packed into an array where
* node is at [i], left child is at [2*i], right at [2*i+1]. Root is at [1].
*
* Copyright (C) 2014-2020 Andras Radics
* Copyright (C) 2014-2021 Andras Radics
* Licensed under the Apache License, Version 2.0
*/

Expand Down Expand Up @@ -153,6 +153,25 @@ Heap.prototype.toArray = function toArray( limit ) {
return this._list.slice(1, limit);
}

// rehash all items on the heap
/**
Heap.prototype.heapify = function heapify( array, presorted ) {
if (array) {
array = this._list;
this._list = new Array(array.length + 1);
this.length = array.length;
if (presorted) for (var i = 1; i <= array.length; i++) array[i];
else for (var i = 0; i < array.length; i++) this.insert(array[i]);
}
else {
array = this._list;
this._list = new Array(this.length + 1);
this.length = 0;
for (var i = 1; i <= list.length; i++) this.insert(list[i]);
}
}
**/

Heap.prototype.copy = function copy( ) {
var ret = new Heap(this.options);
for (var i = 1; i <= this.length; i++) ret._list[i] = this._list[i];
Expand Down

0 comments on commit a3e0008

Please sign in to comment.