Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 513b63c319af
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1d313b29a427
Choose a head ref
  • 2 commits
  • 4 files changed
  • 1 contributor

Commits on May 30, 2015

  1. Implement Array#bsearch

    vais committed May 30, 2015
    Copy the full SHA
    97cfcfe View commit details
  2. Merge pull request #899 from vais/array-bsearch

    Implement Array#bsearch
    vais committed May 30, 2015
    Copy the full SHA
    1d313b2 View commit details
Showing with 44 additions and 15 deletions.
  1. +42 −0 opal/corelib/array.rb
  2. +1 −1 spec/corelib
  3. +0 −13 spec/filters/bugs/array.rb
  4. +1 −1 test/cruby
42 changes: 42 additions & 0 deletions opal/corelib/array.rb
Original file line number Diff line number Diff line change
@@ -472,6 +472,48 @@ def at(index)
}
end

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

%x{
var min = 0,
max = self.length,
mid,
val,
ret,
smaller = false,
satisfied = nil;
while (min < max) {
mid = min + Math.floor((max - min) / 2);
val = self[mid];
ret = block(val);
if (ret === $breaker) {
return $breaker.$v;
}
else if (ret === true) {
satisfied = val;
smaller = true;
}
else if (ret === false || ret === nil) {
smaller = false;
}
else if (ret.$$is_number) {
if (ret === 0) { return val; }
smaller = (ret < 0);
}
else {
#{raise TypeError, "wrong argument type #{`ret`.class} (must be numeric, true, false or nil)"}
}
if (smaller) { max = mid; } else { min = mid + 1; }
}
return satisfied;
}
end

def cycle(n = nil, &block)
return if empty? || n == 0

2 changes: 1 addition & 1 deletion spec/corelib
13 changes: 0 additions & 13 deletions spec/filters/bugs/array.rb
Original file line number Diff line number Diff line change
@@ -152,19 +152,6 @@
fails "Array#values_at properly handles recursive arrays"
fails "Array#hash returns the same hash for equal recursive arrays through hashes"

fails "Array#bsearch returns an Enumerator when not passed a block"
fails "Array#bsearch raises a TypeError if the block returns an Object"
fails "Array#bsearch raises a TypeError if the block returns a String"
fails "Array#bsearch with a block returning true or false returns nil if the block returns false for every element"
fails "Array#bsearch with a block returning true or false returns nil if the block returns nil for every element"
fails "Array#bsearch with a block returning true or false returns element at zero if the block returns true for every element"
fails "Array#bsearch with a block returning true or false returns the element at the smallest index for which block returns true"
fails "Array#bsearch with a block returning negative, zero, positive numbers returns nil if the block returns less than zero for every element"
fails "Array#bsearch with a block returning negative, zero, positive numbers returns nil if the block returns greater than zero for every element"
fails "Array#bsearch with a block returning negative, zero, positive numbers returns nil if the block never returns zero"
fails "Array#bsearch with a block returning negative, zero, positive numbers accepts (+/-)Float::INFINITY from the block"
fails "Array#bsearch with a block returning negative, zero, positive numbers returns an element at an index for which block returns 0.0"
fails "Array#bsearch with a block returning negative, zero, positive numbers returns an element at an index for which block returns 0"
fails "Array#first raises a RangeError when count is a Bignum"
fails "Array#hash calls to_int on result of calling hash on each element"
fails "Array#to_h converts empty array to empty hash"