Class: Containers::PriorityQueue

### Home Classes Methods

### In Files

- lib/containers/priority_queue.rb

### Parent

Object

### Methods

- ::new
- #clear
- #delete
- #empty?
- #has_priority?
- #length
- #next
- #next!
- #pop
- #push
- #size

### Included Modules

- Enumerable

### Files

- Makefile
- Makefile
- Makefile
- Makefile
- Makefile

### Class/Module Index

Quicksearch

- Containers
- Containers::CBst
- Containers::CDeque
- Containers::CRBTreeMap
- Containers::CSplayTreeMap
- Containers::Heap
- Containers::KDTree
- Containers::MaxHeap
- Containers::MinHeap
- Containers::PriorityQueue
- Containers::Queue
- Containers::RubyDeque
- Containers::RubyDeque
- Containers::RubyRBTreeMap
- Containers::RubyRBTreeMap
- Containers::RubySplayTreeMap
- Containers::RubySplayTreeMap
- Containers::Stack
- Containers::SuffixArray
- Containers::Trie
- Object
- Object::Deque
- Object::RBTreeMap
- Object::SplayTreeMap
- Algorithms
- Algorithms::Search
- Algorithms::String

No matching classes.

# Containers::PriorityQueue

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.

### Public Class Methods

new(&block) click to toggle source

Create a new, empty PriorityQueue

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

### Public Instance Methods

clear() click to toggle source

Clears all the items in the queue.

    # File lib/containers/priority_queue.rb, line 43
    def clear
      @heap.clear
    end

delete(priority) → object click to toggle source
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

    # File lib/containers/priority_queue.rb, line 109
    def delete(priority)
      @heap.delete(priority)
    end

empty?() click to toggle source

Returns true if the queue is empty, false otherwise.

    # File lib/containers/priority_queue.rb, line 48
    def empty?
      @heap.empty?
    end

has_priority? priority → boolean click to toggle source

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

    # File lib/containers/priority_queue.rb, line 62
    def has_priority?(priority)
      @heap.has_key?(priority)
    end

length() click to toggle source
Alias for: size
next → object click to toggle source

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"

    # File lib/containers/priority_queue.rb, line 76
    def next
      @heap.next
    end

next!() click to toggle source
Alias for: pop
pop → object click to toggle source

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

    # File lib/containers/priority_queue.rb, line 91
    def pop
      @heap.pop
    end

Also aliased as: next!
push(object, priority) click to toggle source

Add an object to the queue with associated priority.

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

    # File lib/containers/priority_queue.rb, line 38
    def push(object, priority)
      @heap.push(priority, object)
    end

size() click to toggle source

Returns the number of elements in the queue.

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

    # File lib/containers/priority_queue.rb, line 28
    def size
      @heap.size
    end

Also aliased as: length

[Validate]

Generated with the Darkfish Rdoc Generator 2.