From d9d72e06db07087aa54401fae8b259f0c4ccd649 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Wed, 29 Jan 2014 19:22:56 +0100 Subject: first usable version, name change => nlp_ruby --- lib/nlp_ruby/PriorityQueue.rb | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 lib/nlp_ruby/PriorityQueue.rb (limited to 'lib/nlp_ruby/PriorityQueue.rb') 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 + -- cgit v1.2.3