class Sawmill::Util::Heap

A simple heap class.

Public Class Methods

new(data_=nil, &block_) click to toggle source

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

Public Instance Methods

<<(value_) click to toggle source
# File lib/sawmill/util/heap.rb, line 68
def <<(value_)
  add(value_)
end
add(value_) click to toggle source
# File lib/sawmill/util/heap.rb, line 61
def add(value_)
  @_heap << value_
  _sift_up(@_heap.length-1)
  self
end
clear() click to toggle source
# File lib/sawmill/util/heap.rb, line 100
def clear
  @_heap.clear
end
each!() { |remove| ... } click to toggle source
# File lib/sawmill/util/heap.rb, line 105
def each!
  while !empty?
    yield(remove)
  end
end
empty?() click to toggle source
# File lib/sawmill/util/heap.rb, line 95
def empty?
  @_heap.empty?
end
merge(enum_) click to toggle source
# File lib/sawmill/util/heap.rb, line 55
def merge(enum_)
  enum_.each{ |value_| add(value_) }
  self
end
peek() click to toggle source
# File lib/sawmill/util/heap.rb, line 85
def peek
  @_heap[0]
end
remove() click to toggle source
# 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
size() click to toggle source
# File lib/sawmill/util/heap.rb, line 90
def size
  @_heap.size
end