Class: Arknmax::Heap
- Inherits:
-
Object
- Object
- Arknmax::Heap
- Defined in:
- lib/arknmax/heap.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#data_structure ⇒ Object
readonly
Returns the value of attribute data_structure.
-
#io ⇒ Object
readonly
Returns the value of attribute io.
-
#size_limit ⇒ Object
readonly
Returns the value of attribute size_limit.
Instance Method Summary collapse
- #<<(num) ⇒ Object
-
#initialize(size_limit, io: STDOUT) ⇒ Heap
constructor
A new instance of Heap.
- #print_from_max ⇒ Object
- #print_from_min ⇒ Object
- #to_a ⇒ Object
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_structure ⇒ Object (readonly)
Returns the value of attribute data_structure.
8 9 10 |
# File 'lib/arknmax/heap.rb', line 8 def data_structure @data_structure end |
#io ⇒ Object (readonly)
Returns the value of attribute io.
8 9 10 |
# File 'lib/arknmax/heap.rb', line 8 def io @io end |
#size_limit ⇒ Object (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 |
#print_from_max ⇒ Object
24 25 26 |
# File 'lib/arknmax/heap.rb', line 24 def print_from_max to_a.reverse_each { |x| io.puts x } end |
#print_from_min ⇒ Object
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_a ⇒ Object
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 |