Method: Array#bsearch_upper_boundary

Defined in:
lib/array_bsearch.rb

#bsearch_upper_boundary(range = 0 ... self.length, &block) ⇒ Object

Return the upper boundary. (outside)



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/array_bsearch.rb', line 43

def bsearch_upper_boundary (range = 0 ... self.length, &block)
  lower  = range.first() -1
  upper = if range.exclude_end? then range.last else range.last + 1 end
  while lower + 1 != upper
    mid = ((lower + upper) / 2).to_i # for working with mathn.rb (Rational)
    if yield(self[mid]) <= 0
      lower = mid
    else
      upper = mid
    end
  end
  return lower + 1 # outside of the matching range.
end