Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c64d283

Browse files
committed
array.c: bsearch_index
* array.c (rb_ary_bsearch_index): Implement Array#bsearch_index method, which is similar to bsearch and returns the index or nil. [Feature #10730] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50839 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 4162f90 commit c64d283

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Fri Jun 12 16:28:17 2015 Radan Skoric <[email protected]>
2+
3+
* array.c (rb_ary_bsearch_index): Implement Array#bsearch_index
4+
method, which is similar to bsearch and returns the index or
5+
nil. [Feature #10730]
6+
17
Thu Jun 11 19:11:22 2015 SHIBATA Hiroshi <[email protected]>
28

39
* ext/zlib/zlib.c: Fix indentation for rdoc.

array.c

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2547,6 +2547,8 @@ rb_ary_sort(VALUE ary)
25472547
return ary;
25482548
}
25492549

2550+
static long ary_bsearch_index(VALUE ary);
2551+
25502552
/*
25512553
* call-seq:
25522554
* ary.bsearch {|x| block } -> elem
@@ -2602,22 +2604,52 @@ rb_ary_sort(VALUE ary)
26022604

26032605
static VALUE
26042606
rb_ary_bsearch(VALUE ary)
2607+
{
2608+
long index_result = ary_bsearch_index(ary);
2609+
2610+
if (index_result < 0) return rb_ary_entry(ary, index_result);
2611+
return INT2FIX(index_result);
2612+
}
2613+
2614+
/*
2615+
* call-seq:
2616+
* ary.bsearch_index {|x| block } -> int or nil
2617+
*
2618+
* By using binary search, finds an index of a value from this array which
2619+
* meets the given condition in O(log n) where n is the size of the array.
2620+
*
2621+
* It supports two modes, depending on the nature of the block and they are
2622+
* exactly the same as in the case of #bsearch method with the only difference
2623+
* being that this method returns the index of the element instead of the
2624+
* element itself. For more details consult the documentation for #bsearch.
2625+
*/
2626+
2627+
static VALUE
2628+
rb_ary_bsearch_index(VALUE ary)
2629+
{
2630+
long index_result = ary_bsearch_index(ary);
2631+
2632+
returns INT2FIX(index_result);
2633+
}
2634+
2635+
static long
2636+
ary_bsearch_index(VALUE ary)
26052637
{
26062638
long low = 0, high = RARRAY_LEN(ary), mid;
2607-
int smaller = 0;
2608-
VALUE v, val, satisfied = Qnil;
2639+
int smaller = 0, satisfied = 0;
2640+
VALUE v, val;
26092641

26102642
RETURN_ENUMERATOR(ary, 0, 0);
26112643
while (low < high) {
26122644
mid = low + ((high - low) / 2);
26132645
val = rb_ary_entry(ary, mid);
26142646
v = rb_yield(val);
26152647
if (FIXNUM_P(v)) {
2616-
if (v == INT2FIX(0)) return val;
2648+
if (v == INT2FIX(0)) return mid;
26172649
smaller = (SIGNED_VALUE)v < 0; /* Fixnum preserves its sign-bit */
26182650
}
26192651
else if (v == Qtrue) {
2620-
satisfied = val;
2652+
satisfied = 1;
26212653
smaller = 1;
26222654
}
26232655
else if (v == Qfalse || v == Qnil) {
@@ -2626,7 +2658,7 @@ rb_ary_bsearch(VALUE ary)
26262658
else if (rb_obj_is_kind_of(v, rb_cNumeric)) {
26272659
const VALUE zero = INT2FIX(0);
26282660
switch (rb_cmpint(rb_funcallv(v, id_cmp, 1, &zero), v, zero)) {
2629-
case 0: return val;
2661+
case 0: return mid;
26302662
case 1: smaller = 1; break;
26312663
case -1: smaller = 0;
26322664
}
@@ -2643,7 +2675,8 @@ rb_ary_bsearch(VALUE ary)
26432675
low = mid + 1;
26442676
}
26452677
}
2646-
return satisfied;
2678+
if (!satisfied) return -1;
2679+
return low;
26472680
}
26482681

26492682

@@ -5858,6 +5891,7 @@ Init_Array(void)
58585891
rb_define_method(rb_cArray, "drop", rb_ary_drop, 1);
58595892
rb_define_method(rb_cArray, "drop_while", rb_ary_drop_while, 0);
58605893
rb_define_method(rb_cArray, "bsearch", rb_ary_bsearch, 0);
5894+
rb_define_method(rb_cArray, "bsearch_index", rb_ary_bsearch_index, 0);
58615895
rb_define_method(rb_cArray, "any?", rb_ary_any_p, 0);
58625896

58635897
id_cmp = rb_intern("<=>");

test/ruby/test_array.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,6 +2529,41 @@ def test_bsearch_in_find_any_mode
25292529
assert_include([4, 7], a.bsearch {|x| (2**100).coerce((1 - x / 4) * (2**100)).first })
25302530
end
25312531

2532+
def test_bsearch_index_typechecks_return_values
2533+
assert_raise(TypeError) do
2534+
[1, 2, 42, 100, 666].bsearch_index {"not ok"}
2535+
end
2536+
assert_equal [1, 2, 42, 100, 666].bsearch_index {}, [1, 2, 42, 100, 666].bsearch_index {false}
2537+
end
2538+
2539+
def test_bsearch_index_with_no_block
2540+
enum = [1, 2, 42, 100, 666].bsearch_index
2541+
assert_nil enum.size
2542+
assert_equal 2, enum.each{|x| x >= 33 }
2543+
end
2544+
2545+
def test_bsearch_index_in_find_minimum_mode
2546+
a = [0, 4, 7, 10, 12]
2547+
assert_equal(1, a.bsearch_index {|x| x >= 4 })
2548+
assert_equal(2, a.bsearch_index {|x| x >= 6 })
2549+
assert_equal(0, a.bsearch_index {|x| x >= -1 })
2550+
assert_equal(nil, a.bsearch_index {|x| x >= 100 })
2551+
end
2552+
2553+
def test_bsearch_index_in_find_any_mode
2554+
a = [0, 4, 7, 10, 12]
2555+
assert_include([1, 2], a.bsearch_index {|x| 1 - x / 4 })
2556+
assert_equal(nil, a.bsearch_index {|x| 4 - x / 2 })
2557+
assert_equal(nil, a.bsearch_index {|x| 1 })
2558+
assert_equal(nil, a.bsearch_index {|x| -1 })
2559+
2560+
assert_include([1, 2], a.bsearch_index {|x| (1 - x / 4) * (2**100) })
2561+
assert_equal(nil, a.bsearch_index {|x| 1 * (2**100) })
2562+
assert_equal(nil, a.bsearch_index {|x| (-1) * (2**100) })
2563+
2564+
assert_include([1, 2], a.bsearch_index {|x| (2**100).coerce((1 - x / 4) * (2**100)).first })
2565+
end
2566+
25322567
def test_shared_marking
25332568
reduce = proc do |s|
25342569
s.gsub(/(verify_internal_consistency_reachable_i:\sWB\smiss\s\S+\s\(T_ARRAY\)\s->\s)\S+\s\((proc|T_NONE)\)\n

0 commit comments

Comments
 (0)