Class: Rx::PriorityQueue
- Inherits:
-
Object
- Object
- Rx::PriorityQueue
- Defined in:
- lib/rx/internal/priority_queue.rb
Overview
Priority Queue implemented as a binary heap.
Defined Under Namespace
Classes: IndexedItem
Instance Method Summary collapse
- #delete(item) ⇒ Object
-
#initialize ⇒ PriorityQueue
constructor
A new instance of PriorityQueue.
- #length ⇒ Object
- #peek ⇒ Object
- #push(item) ⇒ Object
- #shift ⇒ Object
Constructor Details
#initialize ⇒ PriorityQueue
Returns a new instance of PriorityQueue.
7 8 9 10 |
# File 'lib/rx/internal/priority_queue.rb', line 7 def initialize @items = [] @mutex = Mutex.new end |
Instance Method Details
#delete(item) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rx/internal/priority_queue.rb', line 33 def delete(item) @mutex.synchronize do index = @items.index {|it| it.value == item } if index delete_at index true else false end end end |
#length ⇒ Object
45 46 47 |
# File 'lib/rx/internal/priority_queue.rb', line 45 def length @items.length end |
#peek ⇒ Object
12 13 14 15 16 |
# File 'lib/rx/internal/priority_queue.rb', line 12 def peek @mutex.synchronize do unsafe_peek end end |
#push(item) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/rx/internal/priority_queue.rb', line 26 def push(item) @mutex.synchronize do @items.push IndexedItem.new(item) percolate length - 1 end end |
#shift ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/rx/internal/priority_queue.rb', line 18 def shift @mutex.synchronize do result = unsafe_peek delete_at 0 result end end |