Class: Algorithms::Containers::PriorityQueue

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/containers/priority_queue.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ PriorityQueue

Create a new, empty PriorityQueue



18
19
20
21
22
# File 'lib/containers/priority_queue.rb', line 18

def initialize(&block)
  # We default to a priority queue that returns the largest value
  block ||= lambda { |x, y| (x <=> y) == 1 }
  @heap = Heap.new(&block)
end

Instance Method Details

#clearObject

Clears all the items in the queue.



45
46
47
# File 'lib/containers/priority_queue.rb', line 45

def clear
  @heap.clear
end

#delete(priority) ⇒ Object

call-seq:

delete(priority) -> object
delete(priority) -> nil

Delete an object with specified priority from the queue. If there are duplicates, an arbitrary object with that priority is deleted and returned. Returns nil if there are no objects with the priority.

q = PriorityQueue.new
q.push("Alaska", 50)
q.push("Delaware", 30)
q.delete(50)            #=> "Alaska"
q.delete(10)            #=> nil


111
112
113
# File 'lib/containers/priority_queue.rb', line 111

def delete(priority)
  @heap.delete(priority)
end

#empty?Boolean

Returns true if the queue is empty, false otherwise.

Returns:

  • (Boolean)


50
51
52
# File 'lib/containers/priority_queue.rb', line 50

def empty?
  @heap.empty?
end

#has_priority?(priority) ⇒ Boolean

call-seq:

has_priority? priority -> boolean

Return true if the priority is in the queue, false otherwise.

q = PriorityQueue.new
q.push("Alaska", 1)

q.has_priority?(1)    #=> true 
q.has_priority?(2)    #=> false

Returns:

  • (Boolean)


64
65
66
# File 'lib/containers/priority_queue.rb', line 64

def has_priority?(priority)
  @heap.has_key?(priority)
end

#nextObject

call-seq:

next -> object

Return the object with the next highest priority, but does not remove it

q = Algorithms::Containers::PriorityQueue.new
q.push("Alaska", 50)
q.push("Delaware", 30)
q.push("Georgia", 35)
q.next          #=> "Alaska"


78
79
80
# File 'lib/containers/priority_queue.rb', line 78

def next
  @heap.next
end

#popObject Also known as: next!

call-seq:

pop -> object

Return the object with the next highest priority and removes it from the queue

q = Algorithms::Containers::PriorityQueue.new
q.push("Alaska", 50)
q.push("Delaware", 30)
q.push("Georgia", 35)
q.pop         #=> "Alaska"
q.size        #=> 2


93
94
95
# File 'lib/containers/priority_queue.rb', line 93

def pop
  @heap.pop
end

#push(object, priority) ⇒ Object

Add an object to the queue with associated priority.

q = Algorithms::Containers::PriorityQueue.new
q.push("Alaska", 1)
q.pop #=> "Alaska"


40
41
42
# File 'lib/containers/priority_queue.rb', line 40

def push(object, priority)    
  @heap.push(priority, object)
end

#sizeObject Also known as: length

Returns the number of elements in the queue.

q = Algorithms::Containers::PriorityQueue.new
q.size #=> 0
q.push("Alaska", 1)
q.size #=> 1


30
31
32
# File 'lib/containers/priority_queue.rb', line 30

def size
  @heap.size
end