Kodama's home / tips.

priority queue for Ruby.

Download: pqueue.rb.

A priority(or ordered,sorted) queue class " PQueue" for Ruby.

Sample script

require "pqueue"

pq=PQueue.new(proc{|x,y| x>y})
pq.push(2)
pq.push(3)
pq.push(4)
pq.push(3)
pq.push(2)
pq.push(4)
print "size:"+pq.size.to_s+"\n"
print "each_pop: "
pq.each_pop{|x| print x.to_s+" "}
print "\n"

Result

size:6
each_pop: 4 4 3 3 2 2 

See also:

Ruby and Ruby Application Archives.
Kodama's home / tips.