kanwei / algorithms

Ruby algorithms and data structures. C extensions
http://kanwei.github.io/algorithms/
MIT License
2.67k stars 352 forks source link

add top (?) that takes max #9

Open dblock opened 13 years ago

dblock commented 13 years ago

Not sure if this is a good name for it. We do a lot of fetching an array from the top of the heap (priority queue).

def top(max)
  results = []
  while results.size < max and size > 0 do 
    results << pop 
  end
  results
end

If you think it's a good idea, I'll make a proper pull request with a UT.

GarrisonJ commented 9 months ago

@dblock A good name for this might be take because it looks like this works same way as Array#take.

dblock commented 9 months ago

@GarrisonJ this is such an obvious and excellent answer, thank you