Created
December 6, 2012 02:51
-
-
Save lambda-fairy/4221445 to your computer and use it in GitHub Desktop.
Inefficient priority queue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Extremely inefficient priority queue. | |
| * | |
| * In a real program this would probably be implemented as a heap, | |
| * but this is good enough for this demonstration. | |
| */ | |
| function PQueue(comparator) { | |
| this.comparator = comparator || function(x, y) { return x - y } | |
| this.array = [] | |
| } | |
| PQueue.prototype.add = function(item) { | |
| this.array.push(item) | |
| this.heapify() | |
| } | |
| PQueue.prototype.addAll = function(items) { | |
| for (var i = 0; i < items.length; ++i) { | |
| this.array.push(items[i]) | |
| } | |
| this.heapify() | |
| } | |
| PQueue.prototype.heapify = function() { | |
| // lol | |
| this.array.sort(this.comparator) | |
| } | |
| PQueue.prototype.size = function() { | |
| return this.array.length | |
| } | |
| PQueue.prototype.shift = function() { | |
| return this.array.shift() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment