Skip to content

Instantly share code, notes, and snippets.

@semencov
Last active August 2, 2019 11:15
Show Gist options
  • Save semencov/a9429e7fec5a6b09d715 to your computer and use it in GitHub Desktop.
Save semencov/a9429e7fec5a6b09d715 to your computer and use it in GitHub Desktop.
sortedIndex = require('lodash/sortedIndex')
module.exports = class PriorityQueue
constructor: () ->
@heap = []
@length = 0
push: (item, priority=0) ->
data =
data: item,
priority: priority
index = sortedIndex @heap, data, "priority"
@heap.splice index, 0, data
return @length = @heap.length
pop: () ->
item = @heap.pop()
@length = @heap.length
if item then item.data else undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment