ruby-changes:26932
From: marcandre <ko1@a...>
Date: Wed, 30 Jan 2013 14:11:18 +0900 (JST)
Subject: [ruby-changes:26932] marcandRe: r38984 (trunk): * array.c (rb_ary_bsearch): Return enumerator if no block [#7725]
marcandre 2013-01-30 14:11:03 +0900 (Wed, 30 Jan 2013) New Revision: 38984 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38984 Log: * array.c (rb_ary_bsearch): Return enumerator if no block [#7725] * range.c (range_bsearch): ditto * test/ruby/test_array.rb: Test for above * test/ruby/test_range.rb: ditto Modified files: trunk/ChangeLog trunk/array.c trunk/range.c trunk/test/ruby/test_array.rb trunk/test/ruby/test_range.rb Index: array.c =================================================================== --- array.c (revision 38983) +++ array.c (revision 38984) @@ -2438,6 +2438,7 @@ rb_ary_bsearch(VALUE ary) https://github.com/ruby/ruby/blob/trunk/array.c#L2438 int smaller = 0, satisfied = 0; VALUE v, val; + RETURN_ENUMERATOR(ary, 0, 0); while (low < high) { mid = low + ((high - low) / 2); val = rb_ary_entry(ary, mid); Index: ChangeLog =================================================================== --- ChangeLog (revision 38983) +++ ChangeLog (revision 38984) @@ -1,3 +1,13 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Wed Jan 30 14:10:52 2013 Marc-Andre Lafortune <ruby-core@m...> + + * array.c (rb_ary_bsearch): Return enumerator if no block [#7725] + + * range.c (range_bsearch): ditto + + * test/ruby/test_array.rb: Test for above + + * test/ruby/test_range.rb: ditto + Wed Jan 30 13:53:43 2013 Marc-Andre Lafortune <ruby-core@m...> * lib/matrix.rb: Take conjugate for inner product Index: range.c =================================================================== --- range.c (revision 38983) +++ range.c (revision 38984) @@ -595,6 +595,7 @@ range_bsearch(VALUE range) https://github.com/ruby/ruby/blob/trunk/range.c#L595 #define BSEARCH(conv) \ do { \ + RETURN_ENUMERATOR(range, 0, 0); \ if (EXCL(range)) high--; \ org_high = high; \ while (low < high) { \ @@ -639,6 +640,7 @@ range_bsearch(VALUE range) https://github.com/ruby/ruby/blob/trunk/range.c#L640 VALUE low = beg; VALUE high = end; VALUE mid, org_high; + RETURN_ENUMERATOR(range, 0, 0); if (EXCL(range)) high = rb_funcall(high, '-', 1, INT2FIX(1)); org_high = high; Index: test/ruby/test_array.rb =================================================================== --- test/ruby/test_array.rb (revision 38983) +++ test/ruby/test_array.rb (revision 38984) @@ -2249,6 +2249,12 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2249 assert_raise(ArgumentError) { a.rotate!(1, 1) } end + def test_bsearch_with_no_block + enum = [1, 2, 42, 100, 666].bsearch + assert_nil enum.size + assert_equal 42, enum.each{|x| x >= 33 } + end + def test_bsearch_in_find_minimum_mode a = [0, 4, 7, 10, 12] assert_equal(4, a.bsearch {|x| x >= 4 }) Index: test/ruby/test_range.rb =================================================================== --- test/ruby/test_range.rb (revision 38983) +++ test/ruby/test_range.rb (revision 38984) @@ -356,6 +356,12 @@ class TestRange < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_range.rb#L356 assert_equal 42, (1..42).each.size end + def test_bsearch_with_no_block + enum = (42...666).bsearch + assert_nil enum.size + assert_equal 200, enum.each{|x| x >= 200 } + end + def test_bsearch_for_fixnum ary = [3, 4, 7, 9, 12] assert_equal(0, (0...ary.size).bsearch {|i| ary[i] >= 2 }) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/