class PriorityQueue
Public Class Methods
from_array(array)
click to toggle source
Calls superclass method
Heap::from_array
# File lib/data_structures/priority_queue.rb, line 4 def self.from_array(array) nodes = array.map { |arr| PriorityQueueNode.new(arr.first, arr.last) } heap = super(nodes) from_heap(heap) end
from_hash(hash)
click to toggle source
# File lib/data_structures/priority_queue.rb, line 10 def self.from_hash(hash) nodes = hash.map { |k, v| PriorityQueueNode.new(k, v) } heap = self.superclass.from_array(nodes) from_heap(heap) end
Private Class Methods
from_heap(heap)
click to toggle source
# File lib/data_structures/priority_queue.rb, line 50 def self.from_heap(heap) queue = PriorityQueue.new queue.instance_variable_set(:@store, heap.send(:store)) queue end
Public Instance Methods
find(data)
click to toggle source
# File lib/data_structures/priority_queue.rb, line 29 def find(data) return nil if @store.empty? @store.each { |node| return node if node.send(:data) == data } nil end
include?(data)
click to toggle source
# File lib/data_structures/priority_queue.rb, line 35 def include?(data) return nil if @store.empty? @store.each { |node| return true if node.send(:data) == data } false end
insert(data, priority)
click to toggle source
Calls superclass method
Heap#insert
# File lib/data_structures/priority_queue.rb, line 24 def insert(data, priority) node = PriorityQueueNode.new(data, priority) super(node) end
inspect()
click to toggle source
# File lib/data_structures/priority_queue.rb, line 20 def inspect "Priority Queue: head=#{self.peek || 'nil'}, length=#{self.length}" end
merge(other_queue)
click to toggle source
# File lib/data_structures/priority_queue.rb, line 41 def merge(other_queue) raise ArgumentError.new("May only merge with a Priority Queue. You passed a #{other_queue.class}.") unless other_queue.is_a?(PriorityQueue) array = self.send(:store) + other_queue.send(:store) heap = self.class.superclass.from_array(array) self.class.send(:from_heap, heap) end
to_s()
click to toggle source
# File lib/data_structures/priority_queue.rb, line 16 def to_s "Priority Queue: head=#{self.peek || 'nil'}, length=#{self.length}" end