Class: DS::IndexedPriorityQueue

Inherits:
PriorityQueue show all
Defined in:
lib/ds/queues/indexed_priority_queue.rb

Overview

Indexed Priority Queue

Instance Method Summary collapse

Methods inherited from PriorityQueue

#dequeue, #empty?, #enqueue, #length, min, #peek

Constructor Details

#initialize(*args) ⇒ IndexedPriorityQueue

Creates new IndexedPriorityQueue from args. This is special form of PriorityQueue where you can access any element and update its priority



10
11
12
13
14
15
16
17
18
# File 'lib/ds/queues/indexed_priority_queue.rb', line 10

def initialize(*args)
  @store = if block_given?
             IndexedBinaryHeap.new(*args) do |parent, child|
               yield parent.key, child.key
             end
           else
             IndexedBinaryHeap.new(*args) { |parent, child| parent.key >= child.key }
           end
end

Instance Method Details

#get(x) ⇒ Object



24
25
26
# File 'lib/ds/queues/indexed_priority_queue.rb', line 24

def get(x)
  store.get(x)
end

#include?(x) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/ds/queues/indexed_priority_queue.rb', line 28

def include?(x)
  store.include?(x)
end

#update(x, priority) ⇒ Object



20
21
22
# File 'lib/ds/queues/indexed_priority_queue.rb', line 20

def update(x, priority)
  store.change_priority(x, priority)
end