diff options
Diffstat (limited to 'lib/nlp_ruby/PriorityQueue.rb')
-rw-r--r-- | lib/nlp_ruby/PriorityQueue.rb | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/nlp_ruby/PriorityQueue.rb b/lib/nlp_ruby/PriorityQueue.rb new file mode 100644 index 0000000..22662b3 --- /dev/null +++ b/lib/nlp_ruby/PriorityQueue.rb @@ -0,0 +1,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 + |