Class Containers::PriorityQueue

  1. lib/containers/priority_queue.rb
Parent: Object

A Priority Queue is a data structure that behaves like a queue except that elements have an associated priority. The next and pop methods return the item with the next highest priority.

Priority Queues are often used in graph problems, such as Dijkstra’s Algorithm for shortest path, and the A* search algorithm for shortest path.

This container is implemented using the Fibonacci heap included in the Collections library.

Methods

public class

  1. new

public instance

  1. clear
  2. delete
  3. empty?
  4. has_priority? priority
  5. length
  6. next
  7. next!
  8. pop
  9. push
  10. size

Included modules

  1. Enumerable

Public class methods

new (&block)

Create a new, empty PriorityQueue

[show source]
    # File lib/containers/priority_queue.rb, line 16
16:   def initialize(&block)
17:     # We default to a priority queue that returns the largest value
18:     block ||= lambda { |x, y| (x <=> y) == 1 }
19:     @heap = Containers::Heap.new(&block)
20:   end

Public instance methods

clear ()

Clears all the items in the queue.

[show source]
    # File lib/containers/priority_queue.rb, line 43
43:   def clear
44:     @heap.clear
45:   end
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
[show source]
     # File lib/containers/priority_queue.rb, line 109
109:   def delete(priority)
110:     @heap.delete(priority)
111:   end
empty? ()

Returns true if the queue is empty, false otherwise.

[show source]
    # File lib/containers/priority_queue.rb, line 48
48:   def empty?
49:     @heap.empty?
50:   end
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
[show source]
    # File lib/containers/priority_queue.rb, line 62
62:   def has_priority?(priority)
63:     @heap.has_key?(priority)
64:   end
length ()

Alias for size

next → object

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

q = Containers::PriorityQueue.new
q.push("Alaska", 50)
q.push("Delaware", 30)
q.push("Georgia", 35)
q.next          #=> "Alaska"
[show source]
    # File lib/containers/priority_queue.rb, line 76
76:   def next
77:     @heap.next
78:   end
next! ()

Alias for pop

pop → object

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

q = Containers::PriorityQueue.new
q.push("Alaska", 50)
q.push("Delaware", 30)
q.push("Georgia", 35)
q.pop         #=> "Alaska"
q.size        #=> 2
[show source]
    # File lib/containers/priority_queue.rb, line 91
91:   def pop
92:     @heap.pop
93:   end
push (object, priority)

Add an object to the queue with associated priority.

q = Containers::PriorityQueue.new
q.push("Alaska", 1)
q.pop #=> "Alaska"
[show source]
    # File lib/containers/priority_queue.rb, line 38
38:   def push(object, priority)    
39:     @heap.push(priority, object)
40:   end
size ()

Returns the number of elements in the queue.

q = Containers::PriorityQueue.new
q.size #=> 0
q.push("Alaska", 1)
q.size #=> 1
[show source]
    # File lib/containers/priority_queue.rb, line 28
28:   def size
29:     @heap.size
30:   end