Class: EventMachine::PriorityQueue
- Inherits:
-
Object
- Object
- EventMachine::PriorityQueue
- Includes:
- Enumerable
- Defined in:
- lib/em-priority-queue/version.rb,
lib/em-priority-queue/priority_queue.rb
Constant Summary collapse
- VERSION =
"0.0.3"
Instance Method Summary collapse
- #clear ⇒ Object
- #delete(pri) ⇒ Object
- #empty? ⇒ Boolean
- #has_priority?(priority) ⇒ Boolean
-
#initialize(&blk) ⇒ PriorityQueue
constructor
A new instance of PriorityQueue.
- #next ⇒ Object
- #pop(*a, &c) ⇒ Object
- #push(obj, pri) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(&blk) ⇒ PriorityQueue
Returns a new instance of PriorityQueue.
5 6 7 8 9 |
# File 'lib/em-priority-queue/priority_queue.rb', line 5 def initialize(&blk) blk ||= lambda { |x, y| (x <=> y) == 1 } @heap = Containers::Heap.new(&blk) @callbacks = [] end |
Instance Method Details
#clear ⇒ Object
22 23 24 |
# File 'lib/em-priority-queue/priority_queue.rb', line 22 def clear @heap.clear end |
#delete(pri) ⇒ Object
50 51 52 |
# File 'lib/em-priority-queue/priority_queue.rb', line 50 def delete(pri) @heap.delete(pri) end |
#empty? ⇒ Boolean
26 27 28 |
# File 'lib/em-priority-queue/priority_queue.rb', line 26 def empty? @heap.empty? end |
#has_priority?(priority) ⇒ Boolean
30 31 32 |
# File 'lib/em-priority-queue/priority_queue.rb', line 30 def has_priority?(priority) @heap.has_key?(priority) end |
#next ⇒ Object
34 35 36 |
# File 'lib/em-priority-queue/priority_queue.rb', line 34 def next @heap.next end |
#pop(*a, &c) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/em-priority-queue/priority_queue.rb', line 38 def pop(*a, &c) cb = EM::Callback(*a, &c) EM.schedule do if @heap.empty? @callbacks << cb else cb.call @heap.pop end end nil end |
#push(obj, pri) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/em-priority-queue/priority_queue.rb', line 15 def push(obj, pri) EM.schedule do @heap.push(pri, obj) @callbacks.shift.call(@heap.pop) until @heap.empty? || @callbacks.empty? end end |
#size ⇒ Object
11 12 13 |
# File 'lib/em-priority-queue/priority_queue.rb', line 11 def size @heap.size end |