A simple heap class.
Create a new heap.
# File lib/sawmill/util/heap.rb, line 49 def initialize(data_=nil, &block_) @_heap = data_ || [] @_comparator = block_ || ::Proc.new{ |a_,b_| a_ <=> b_ } end
# File lib/sawmill/util/heap.rb, line 68 def <<(value_) add(value_) end
# File lib/sawmill/util/heap.rb, line 61 def add(value_) @_heap << value_ _sift_up(@_heap.length-1) self end
# File lib/sawmill/util/heap.rb, line 100 def clear @_heap.clear end
# File lib/sawmill/util/heap.rb, line 105 def each! while !empty? yield(remove) end end
# File lib/sawmill/util/heap.rb, line 95 def empty? @_heap.empty? end
# File lib/sawmill/util/heap.rb, line 55 def merge(enum_) enum_.each{ |value_| add(value_) } self end
# File lib/sawmill/util/heap.rb, line 85 def peek @_heap[0] end
# File lib/sawmill/util/heap.rb, line 73 def remove ret_ = @_heap[0] if @_heap.length > 1 @_heap[0] = @_heap.pop _sift_down(0) else @_heap.clear end ret_ end
# File lib/sawmill/util/heap.rb, line 90 def size @_heap.size end