diff options
author | Patrick Simianer <p@simianer.de> | 2014-01-29 19:22:56 +0100 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2014-01-29 19:22:56 +0100 |
commit | d9d72e06db07087aa54401fae8b259f0c4ccd649 (patch) | |
tree | 97f0444314c40d2894ac0892d5559101eda01acf /lib/nlp_ruby/PriorityQueue.rb | |
parent | 22644ed1365e566c8bf806bfff4ecd43c46ce089 (diff) |
first usable version, name change => nlp_ruby
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 + |