summaryrefslogtreecommitdiff
path: root/lib/nlp_ruby/PriorityQueue.rb
blob: f090e60e8a217155c42a4bc6c551b7d2941eeb38 (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
37
# 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
    sort!
  end

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

  def pop
    @queue.pop
  end

  def push i
    @queue << i
    sort!
  end

  def empty?
    @queue.empty?
  end

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