bsearch()
public
By using binary search, finds a value in range which meets the given
condition in O(log n) where n is the size of
the range.
You can use this method in two use cases: a find-minimum mode and a
find-any mode. In either case, the elements of the range must be monotone
(or sorted) with respect to the block.
In find-minimum mode (this is a good choice for typical use case), the
block must return true or false, and there must be a value x so that:
-
the block returns false for any value which is less than x, and
-
the block returns true for any value which is greater than or equal to x.
If x is within the range, this method returns the value x. Otherwise, it
returns nil.
ary = [0, 4, 7, 10, 12]
(0...ary.size).bsearch {|i| ary[i] >= 4 }
(0...ary.size).bsearch {|i| ary[i] >= 6 }
(0...ary.size).bsearch {|i| ary[i] >= 8 }
(0...ary.size).bsearch {|i| ary[i] >= 100 }
(0.0...Float::INFINITY).bsearch {|x| Math.log(x) >= 0 }
In find-any mode (this behaves like libc’s bsearch(3)), the block must return a number,
and there must be two values x and y (x <= y) so that:
-
the block returns a positive number for v if v < x,
-
the block returns zero for v if x <= v < y, and
-
the block returns a negative number for v if y <= v.
This method returns any value which is within the intersection of the given
range and x…y (if any). If there is no value that satisfies the
condition, it returns nil.
ary = [0, 100, 100, 100, 200]
(0..4).bsearch {|i| 100 - ary[i] }
(0..4).bsearch {|i| 300 - ary[i] }
(0..4).bsearch {|i| 50 - ary[i] }
You must not mix the two modes at a time; the block must always return
either true/false, or always return a number. It is undefined which value
is actually picked up at each iteration.
Show source
static VALUE
range_bsearch(VALUE range)
{
VALUE beg, end;
int smaller, satisfied = 0;
/* Implementation notes:
* Floats are handled by mapping them to 64 bits integers.
* Apart from sign issues, floats and their 64 bits integer have the
* same order, assuming they are represented as exponent followed
* by the mantissa. This is true with or without implicit bit.
*
* Finding the average of two ints needs to be careful about
* potential overflow (since float to long can use 64 bits)
* as well as the fact that -1/2 can be 0 or -1 in C89.
*
* Note that -0.0 is mapped to the same int as 0.0 as we don't want
* (-1...0.0).bsearch to yield -0.0.
*/
#define BSEARCH_CHECK(val) \
do { \
VALUE v = rb_yield(val); \
if (FIXNUM_P(v)) { \
if (FIX2INT(v) == 0) return val; \
smaller = FIX2INT(v) < 0; \
} \
else if (v == Qtrue) { \
satisfied = 1; \
smaller = 1; \
} \
else if (v == Qfalse || v == Qnil) { \
smaller = 0; \
} \
else if (rb_obj_is_kind_of(v, rb_cNumeric)) { \
int cmp = rb_cmpint(rb_funcall(v, id_cmp, 1, INT2FIX(0)), v, INT2FIX(0)); \
if (!cmp) return val; \
smaller = cmp < 0; \
} \
else { \
rb_raise(rb_eTypeError, "wrong argument type %s" \
" (must be numeric, true, false or nil)", \
rb_obj_classname(v)); \
} \
} while (0)
#define BSEARCH(conv) \
do { \
RETURN_ENUMERATOR(range, 0, 0); \
if (EXCL(range)) high--; \
org_high = high; \
while (low < high) { \
mid = ((high < 0) == (low < 0)) ? low + ((high - low) / 2) \
: (low < -high) ? -((-1 - low - high)/2 + 1) : (low + high) / 2; \
BSEARCH_CHECK(conv(mid)); \
if (smaller) { \
high = mid; \
} \
else { \
low = mid + 1; \
} \
} \
if (low == org_high) { \
BSEARCH_CHECK(conv(low)); \
if (!smaller) return Qnil; \
} \
if (!satisfied) return Qnil; \
return conv(low); \
} while (0)
beg = RANGE_BEG(range);
end = RANGE_END(range);
if (FIXNUM_P(beg) && FIXNUM_P(end)) {
long low = FIX2LONG(beg);
long high = FIX2LONG(end);
long mid, org_high;
BSEARCH(INT2FIX);
}
#if SIZEOF_DOUBLE == 8 && defined(HAVE_INT64_T)
else if (RB_TYPE_P(beg, T_FLOAT) || RB_TYPE_P(end, T_FLOAT)) {
int64_t low = double_as_int64(RFLOAT_VALUE(rb_Float(beg)));
int64_t high = double_as_int64(RFLOAT_VALUE(rb_Float(end)));
int64_t mid, org_high;
BSEARCH(int64_as_double_to_num);
}
#endif
else if (is_integer_p(beg) && is_integer_p(end)) {
VALUE low = rb_to_int(beg);
VALUE high = rb_to_int(end);
VALUE mid, org_high;
RETURN_ENUMERATOR(range, 0, 0);
if (EXCL(range)) high = rb_funcall(high, '-', 1, INT2FIX(1));
org_high = high;
while (rb_cmpint(rb_funcall(low, id_cmp, 1, high), low, high) < 0) {
mid = rb_funcall(rb_funcall(high, '+', 1, low), id_div, 1, INT2FIX(2));
BSEARCH_CHECK(mid);
if (smaller) {
high = mid;
}
else {
low = rb_funcall(mid, '+', 1, INT2FIX(1));
}
}
if (rb_equal(low, org_high)) {
BSEARCH_CHECK(low);
if (!smaller) return Qnil;
}
if (!satisfied) return Qnil;
return low;
}
else {
rb_raise(rb_eTypeError, "can't do binary search for %s", rb_obj_classname(beg));
}
return range;
}