class Arknmax::Heap
Attributes
data_structure[R]
io[R]
size_limit[R]
Public Class Methods
new(size_limit, io: STDOUT)
click to toggle source
# File lib/arknmax/heap.rb, line 10 def initialize(size_limit, io: STDOUT) @size_limit = size_limit @io = io @data_structure = Containers::MinHeap.new end
Public Instance Methods
<<(num)
click to toggle source
# File lib/arknmax/heap.rb, line 16 def <<(num) return if limit_reached? && new_value_too_low?(num) data_structure.push(num) cut_extra_size! if overlimit? end
print_from_max()
click to toggle source
# File lib/arknmax/heap.rb, line 24 def print_from_max to_a.reverse_each { |x| io.puts x } end
print_from_min()
click to toggle source
# File lib/arknmax/heap.rb, line 28 def print_from_min data_structure.size.times { io.puts data_structure.next! } end
to_a()
click to toggle source
# File lib/arknmax/heap.rb, line 32 def to_a array = Array.new(data_structure.size) data_structure.size.times { |i| array[i] = data_structure.next! } array end
Private Instance Methods
cut_extra_size!()
click to toggle source
# File lib/arknmax/heap.rb, line 48 def cut_extra_size! data_structure.pop end
limit_reached?()
click to toggle source
# File lib/arknmax/heap.rb, line 44 def limit_reached? data_structure.length >= size_limit end
new_value_too_low?(num)
click to toggle source
# File lib/arknmax/heap.rb, line 52 def new_value_too_low?(num) data_structure.min >= num end
overlimit?()
click to toggle source
# File lib/arknmax/heap.rb, line 40 def overlimit? data_structure.length > size_limit end