Skip to content

Commit

Permalink
Showing 1 changed file with 4 additions and 37 deletions.
41 changes: 4 additions & 37 deletions truffle/src/main/ruby/core/array.rb
Original file line number Diff line number Diff line change
@@ -153,47 +153,14 @@ def assoc(obj)
nil
end

def bsearch
def bsearch(&block)
return to_enum :bsearch unless block_given?

m = Rubinius::Mirror::Array.reflect self
idx = self.bsearch_index(&block)

tuple = m.tuple
return nil if idx.nil?

min = start = m.start
max = total = start + m.total

last_true = nil
i = start + m.total / 2

while max >= min and i >= start and i < total
x = yield tuple.at(i)

return tuple.at(i) if x == 0

case x
when Numeric
if x > 0
min = i + 1
else
max = i - 1
end
when true
last_true = i
max = i - 1
when false, nil
min = i + 1
else
raise TypeError, "Array#bsearch block must return Numeric or boolean"
end

i = min + (max - min) / 2
end

return tuple.at(i) if max > min
return tuple.at(last_true) if last_true

nil
self.at(idx)
end

def bsearch_index

0 comments on commit 8ea2dae

Please sign in to comment.