Class: Arknmax::Heap

Inherits:
Object
  • Object
show all
Defined in:
lib/arknmax/heap.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size_limit, io: STDOUT) ⇒ Heap

Returns a new instance of Heap.



10
11
12
13
14
# 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

Instance Attribute Details

#data_structureObject (readonly)

Returns the value of attribute data_structure.



8
9
10
# File 'lib/arknmax/heap.rb', line 8

def data_structure
  @data_structure
end

#ioObject (readonly)

Returns the value of attribute io.



8
9
10
# File 'lib/arknmax/heap.rb', line 8

def io
  @io
end

#size_limitObject (readonly)

Returns the value of attribute size_limit.



8
9
10
# File 'lib/arknmax/heap.rb', line 8

def size_limit
  @size_limit
end

Instance Method Details

#<<(num) ⇒ Object



16
17
18
19
20
21
22
# 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


24
25
26
# File 'lib/arknmax/heap.rb', line 24

def print_from_max
  to_a.reverse_each { |x| io.puts x }
end


28
29
30
# File 'lib/arknmax/heap.rb', line 28

def print_from_min
  data_structure.size.times { io.puts data_structure.next! }
end

#to_aObject



32
33
34
35
36
# 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