def _search_hkey(self, hkey):
hi = self.index_elements
lo = 0
hikey = self._index_max
lokey = self._index_min
if hkey < lokey:
return lo
elif hkey > hikey:
return hi
if cython.compiled:
dtype = self._dtype
if dtype is npuint64 or dtype is npuint32:
#lint:disable
PyObject_GetBuffer(self.index, 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]
if dtype is npuint64:
# TO-DO: better hints?
hint = (lo+hi)//2
return _c_search_hkey_ui64(hkey, pindex, stride0, hi, hint)
elif dtype is npuint32:
# TO-DO: better hints?
hint = (lo+hi)//2
return _c_search_hkey_ui32(hkey, pindex, stride0, hi, hint)
else:
raise AssertionError("Internal error")
finally:
PyBuffer_Release(cython.address(indexbuf))
#lint:enable
else:
raise AssertionError("Internal error")
else:
dtype = self.dtype
struct_dt = numpy.dtype([
('key', dtype),
('value', dtype),
])
return self.index.view(struct_dt).reshape(self.index.shape[0]).searchsorted(
numpy.array([(hkey,0)],dtype=struct_dt))[0]
评论列表
文章目录