class PriorityQueue

Inefficient and poorly tested substitute for priority_queue gem.

The underlying data structure is a sorted list. Make it a min-heap, and this implementation is unobjectionable.

Public Class Methods

new() click to toggle source

Make an empty queue.

# File hack_priority_queue.rb, line 42
def initialize
    @q = []
end

Public Instance Methods

[](priority) click to toggle source

Return a new Entry in the queue, with only priority set.

# File hack_priority_queue.rb, line 47
def [](priority)
    entry = Entry.new(priority)
    i = @q.find_index {|e| e.priority >= priority}
    @q.insert(i == nil ? -1 : i, entry)
    return entry
end
shift() click to toggle source

Return highest-priority item, removing its entry from queue.

# File hack_priority_queue.rb, line 55
def shift
    @q.shift.item
end
size() click to toggle source

The number of entries in the queue.

# File hack_priority_queue.rb, line 60
def size
    @q.length
end