def hinted_bsearch(a, hkey, hint):
hi = len(a)
lo = 0
if hi <= lo:
return lo
elif hkey < a[0]:
return lo
elif hkey > a[hi-1]:
return hi
#lint:disable
PyObject_GetBuffer(a, cython.address(indexbuf), PyBUF_STRIDED_RO)
try:
if ( indexbuf.strides == cython.NULL
or indexbuf.len < hi * indexbuf.strides[0] ):
raise ValueError("Invalid buffer state")
pindex = cython.cast(cython.p_char, indexbuf.buf)
stride0 = indexbuf.strides[0]
#lint:enable
dtype = cython.cast('char*', a.dtype.char)[0]
if dtype == 'L' or dtype == 'Q':
# TO-DO: better hints?
return _c_search_hkey_ui64(hkey, pindex, stride0, hi, hint)
elif dtype == 'I':
# TO-DO: better hints?
return _c_search_hkey_ui32(hkey, pindex, stride0, hi, hint)
elif dtype == 'l' or dtype == 'q':
# TO-DO: better hints?
return _c_search_hkey_i64(hkey, pindex, stride0, hi, hint)
elif dtype == 'i':
# TO-DO: better hints?
return _c_search_hkey_i32(hkey, pindex, stride0, hi, hint)
elif dtype == 'd':
# TO-DO: better hints?
return _c_search_hkey_f64(hkey, pindex, stride0, hi, hint)
elif dtype == 'f':
# TO-DO: better hints?
return _c_search_hkey_f32(hkey, pindex, stride0, hi, hint)
else:
raise NotImplementedError("Unsupported array type %s" % (chr(dtype),))
finally:
PyBuffer_Release(cython.address(indexbuf)) #lint:ok
评论列表
文章目录