Sha256: 76830b6cbf3f49c92c5f4a5f7c9c544a2c9d6cbd7280dbdcd97330f71ab4e805
Contents?: true
Size: 1.13 KB
Versions: 3
Compression:
Stored size: 1.13 KB
Contents
#include "ruby.h" #include "stdio.h" /* def binary_search(ary, value) upper = ary.size - 1 lower = 0 idx = 0 while(lower <= upper) do idx = (lower + upper) / 2 comp = ary[idx].value <=> value if comp == 0 return idx elsif comp > 0 upper = idx - 1 else lower = idx + 1 end end return upper end */ static VALUE binary_search(VALUE self, VALUE ary, VALUE number) { int upper = RARRAY_LEN(ary) - 1; int lower = 0; int idx = 0; unsigned int r = NUM2UINT(number); ID value = rb_intern("value"); while (lower <= upper) { idx = (lower + upper) / 2; VALUE continuumValue = rb_funcall(RARRAY_PTR(ary)[idx], value, 0); unsigned int l = NUM2UINT(continuumValue); if (l == r) { return INT2FIX(idx); } else if (l > r) { upper = idx - 1; } else { lower = idx + 1; } } return INT2FIX(upper); } VALUE cContinuum; void Init_binary_search() { cContinuum = rb_define_module("Continuum"); rb_define_module_function(cContinuum, "binary_search", binary_search, 2); }
Version data entries
3 entries across 3 versions & 2 rubygems
Version | Path |
---|---|
mperham-memcache-client-1.6.1 | ext/memcache/binary_search.c |
mperham-memcache-client-1.6.2 | ext/memcache/binary_search.c |
memcache-client-1.6.2 | ext/memcache/binary_search.c |