mapped_struct.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:sharedbuffers 作者: jampp 项目源码 文件源码
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]
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号