summaryrefslogtreecommitdiff
path: root/lib/nlp_ruby/PriorityQueue.rb
blob: 22662b360ea8608daa184ccf517fbd3f8978cb2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# FIXME dags
# this assumes that elements in the queue
# have a numerical member named 'score'
class PriorityQueue

  def initialize a=Array.new
    @queue = Array.new a
  end

  def sort!
    @queue.sort_by! { |i| -i.score }
  end

  def pop
    sort!
    @queue.pop
  end

  def push i
    @queue << i
  end

  def empty?
    @queue.empty?
  end

  # FIXME
  def to_s
    a = []
    @queue.each { |i|
      a << "#{i.to_s}[#{i.score}]"
    }
    "[#{a.join ', '}]"
  end
end