range.c: trivial optimizations
* range.c (range_bsearch): trivial optimizations, for Fixnum, and by keeping the last satisfied element. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49230 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
75a43dbe8d
commit
b13432890f
16
range.c
16
range.c
@ -570,8 +570,8 @@ is_integer_p(VALUE v)
|
|||||||
static VALUE
|
static VALUE
|
||||||
range_bsearch(VALUE range)
|
range_bsearch(VALUE range)
|
||||||
{
|
{
|
||||||
VALUE beg, end;
|
VALUE beg, end, satisfied = Qnil;
|
||||||
int smaller, satisfied = 0;
|
int smaller;
|
||||||
|
|
||||||
/* Implementation notes:
|
/* Implementation notes:
|
||||||
* Floats are handled by mapping them to 64 bits integers.
|
* Floats are handled by mapping them to 64 bits integers.
|
||||||
@ -592,11 +592,11 @@ range_bsearch(VALUE range)
|
|||||||
VALUE val = (expr); \
|
VALUE val = (expr); \
|
||||||
VALUE v = rb_yield(val); \
|
VALUE v = rb_yield(val); \
|
||||||
if (FIXNUM_P(v)) { \
|
if (FIXNUM_P(v)) { \
|
||||||
if (FIX2INT(v) == 0) return val; \
|
if (v == INT2FIX(0)) return val; \
|
||||||
smaller = FIX2INT(v) < 0; \
|
smaller = (SIGNED_VALUE)v < 0; \
|
||||||
} \
|
} \
|
||||||
else if (v == Qtrue) { \
|
else if (v == Qtrue) { \
|
||||||
satisfied = 1; \
|
satisfied = val; \
|
||||||
smaller = 1; \
|
smaller = 1; \
|
||||||
} \
|
} \
|
||||||
else if (v == Qfalse || v == Qnil) { \
|
else if (v == Qfalse || v == Qnil) { \
|
||||||
@ -634,8 +634,7 @@ range_bsearch(VALUE range)
|
|||||||
BSEARCH_CHECK(conv(low)); \
|
BSEARCH_CHECK(conv(low)); \
|
||||||
if (!smaller) return Qnil; \
|
if (!smaller) return Qnil; \
|
||||||
} \
|
} \
|
||||||
if (!satisfied) return Qnil; \
|
return satisfied; \
|
||||||
return conv(low); \
|
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
|
|
||||||
@ -678,8 +677,7 @@ range_bsearch(VALUE range)
|
|||||||
BSEARCH_CHECK(low);
|
BSEARCH_CHECK(low);
|
||||||
if (!smaller) return Qnil;
|
if (!smaller) return Qnil;
|
||||||
}
|
}
|
||||||
if (!satisfied) return Qnil;
|
return satisfied;
|
||||||
return low;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
rb_raise(rb_eTypeError, "can't do binary search for %s", rb_obj_classname(beg));
|
rb_raise(rb_eTypeError, "can't do binary search for %s", rb_obj_classname(beg));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user