diff options
| author | Christopher Aue <[email protected]> | 2017-07-28 17:08:35 +0200 |
|---|---|---|
| committer | Christopher Aue <[email protected]> | 2017-07-28 23:51:49 +0200 |
| commit | a3bfd735a041078cd9497c739fb3e21efc0c36f0 (patch) | |
| tree | d55e69124bc8b201f3a44143379d4c4deda69902 /mrbgems/mruby-array-ext/mrblib/array.rb | |
| parent | 451574f1420d8533f44a06d9aca23b5647292228 (diff) | |
| download | mruby-a3bfd735a041078cd9497c739fb3e21efc0c36f0.tar.gz mruby-a3bfd735a041078cd9497c739fb3e21efc0c36f0.zip | |
Added Array#bsearch_index
Diffstat (limited to 'mrbgems/mruby-array-ext/mrblib/array.rb')
| -rw-r--r-- | mrbgems/mruby-array-ext/mrblib/array.rb | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index b5c2e7c47..716eabe06 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -595,18 +595,39 @@ class Array def bsearch(&block) return to_enum :bsearch unless block_given? + if idx = bsearch_index(&block) + self[idx] + else + nil + end + end + + ## + # call-seq: + # ary.bsearch_index {|x| block } -> int or nil + # + # By using binary search, finds an index of a value from this array which + # meets the given condition in O(log n) where n is the size of the array. + # + # It supports two modes, depending on the nature of the block and they are + # exactly the same as in the case of #bsearch method with the only difference + # being that this method returns the index of the element instead of the + # element itself. For more details consult the documentation for #bsearch. + + def bsearch_index(&block) + return to_enum :bsearch_index unless block_given? + low = 0 high = size satisfied = false while low < high mid = ((low+high)/2).truncate - val = self[mid] - res = block.call val + res = block.call self[mid] case res when 0 # find-any mode: Found! - return val + return mid when Numeric # find-any mode: Continue... in_lower_half = res < 0 when true # find-min mode @@ -625,7 +646,7 @@ class Array end end - satisfied ? self[low] : nil + satisfied ? low : nil end ## |
