Last active
December 29, 2015 03:29
-
-
Save nubunto/7607922 to your computer and use it in GitHub Desktop.
Priority Queue implemented in JavaScript. It is a closure, that returns an object, with push, shift, and length. It's first argument is a function that sorts the queue as you add elements to it. It allows successive method calls.
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
| var PriorityQueue = function (fn) { | |
| var queue = []; | |
| return { | |
| push: function (a) { | |
| queue.push(a); | |
| queue.sort(fn); | |
| return this; | |
| }, | |
| shift: function () { | |
| return queue.shift(); | |
| }, | |
| length: function () { | |
| return queue.length; | |
| } | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment