Class: Korekto::Heap
- Inherits:
-
Object
- Object
- Korekto::Heap
- Defined in:
- lib/korekto/heap.rb
Instance Method Summary collapse
- #add(s) ⇒ Object
- #combos ⇒ Object
- #each ⇒ Object
-
#initialize(limit) ⇒ Heap
constructor
A new instance of Heap.
Constructor Details
#initialize(limit) ⇒ Heap
Returns a new instance of Heap.
3 4 5 6 7 8 9 10 |
# File 'lib/korekto/heap.rb', line 3 def initialize(limit) @limit = limit @combos = (0...limit).to_a.combination(2) .sort{|ij, kl| ij[0]**2 + ij[1]**2 <=> kl[0]**2 + kl[1]**2} .map{|i, j| [[i,j], [j,i]]} .inject([]){|a, ij_kl| a<<ij_kl[0]; a<<ij_kl[1]} @a = [] end |
Instance Method Details
#add(s) ⇒ Object
12 13 14 15 |
# File 'lib/korekto/heap.rb', line 12 def add(s) @a.unshift s @a.pop if @a.length > @limit end |
#combos ⇒ Object
17 18 19 20 21 22 |
# File 'lib/korekto/heap.rb', line 17 def combos @combos.each do |i,j| next if [i,j].max >= @a.length yield(@a[i], @a[j]) end end |
#each ⇒ Object
24 |
# File 'lib/korekto/heap.rb', line 24 def each = @a.each{yield _1} |