Class: Vines::Services::PriorityQueue

Inherits:
EM::Queue
  • Object
show all
Defined in:
lib/vines/services/priority_queue.rb

Overview

Behaves just like EM::Queue with the exception that items added to the queue are sorted and popped from the queue by their priority. The optional comparator block passed in the constructor determines element priority, with items sorted lowest to highest. If no block is provided, the elements’ natural ordering, via <=>, is used.

Examples:


q = PriorityQueue.new do |a, b|
  a[:priority] <=> b[:priority]
end
q.push({:priority => 3, :msg => 'three'})
q.push({:priority => 2, :msg => 'two'})
q.push({:priority => 1, :msg => 'one'})
3.times do
  q.pop {|item| puts item[:msg] }
end
#=> "one"
#   "two"
#   "three"

Defined Under Namespace

Classes: Heap

Instance Method Summary collapse

Constructor Details

#initialize(&comparator) ⇒ PriorityQueue

Returns a new instance of PriorityQueue.



28
29
30
31
# File 'lib/vines/services/priority_queue.rb', line 28

def initialize(&comparator)
  super
  @items = Heap.new(&comparator)
end