Class: DataStructures::PriorityQueue

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/data_structures/priority_queue.rb

Defined Under Namespace

Classes: ObjectWithPriority

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ordering = DataStructures::BinaryHeap::HIGH_TO_LOW) ⇒ PriorityQueue

Returns a new instance of PriorityQueue.



11
12
13
# File 'lib/data_structures/priority_queue.rb', line 11

def initialize(ordering = DataStructures::BinaryHeap::HIGH_TO_LOW)
  @heap = DataStructures::BinaryHeap.new(ordering)
end

Class Method Details

.low_priorityObject



7
8
9
# File 'lib/data_structures/priority_queue.rb', line 7

def self.low_priority
  new(DataStructures::BinaryHeap::LOW_TO_HIGH)
end

Instance Method Details

#high_priority?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/data_structures/priority_queue.rb', line 15

def high_priority?
  @heap.max_heap?
end

#offer(object, priority) ⇒ Object



19
20
21
# File 'lib/data_structures/priority_queue.rb', line 19

def offer(object, priority)
  @heap.insert(ObjectWithPriority.new(object, priority))
end

#pollObject



23
24
25
# File 'lib/data_structures/priority_queue.rb', line 23

def poll
  @heap.remove.object
end