我在哪里可以找到源Python的散列()函数或算法? [英] Where can I find source or algorithm of Python's hash() function?
问题描述
>>> hash("\x01")
128000384
>>> hash("\x02")
256000771
>>> hash("\x03")
384001154
>>> hash("\x04")
512001541
有趣的部分是 128000384×2
不是 256000771
,也别人
我只是想知道怎么说算法的工作,并希望了解关于它的东西。
I am just wondering how that algorithm works and want to learn something on it.
推荐答案
如果您下载的Python源$ C $ C,你会发现它是肯定的! 但要记住,散列函数被用于各种不同的对象实现的。
If you download the source code of Python, you will find it for sure! But bear in mind the hash function is implemented for each kind of objects differently.
例如,你会发现单向code散列对象/ UNI $ C $的功能cobject.c
UNI功能code_hash
。您可能需要看起来有点多,找到字符串哈希函数。查找结构定义你感兴趣的对象,并在现场 tp_hash
,你会发现,计算该对象的散列code的作用。
For example, you will find the unicode hash function in Objects/unicodeobject.c
in the function unicode_hash
. You might have to look a bit more to find the string hash function. Find the structure defining the object you are interested in, and in the field tp_hash
, you will find the function that compute the hash code of that object.
对于字符串对象:确切的code在对象/ stringobject.c
在函数中 string_hash
:
For the string object: The exact code is found in Objects/stringobject.c
in the function string_hash
:
static long string_hash(PyStringObject *a)
{
register Py_ssize_t len;
register unsigned char *p;
register long x;
if (a->ob_shash != -1)
return a->ob_shash;
len = Py_SIZE(a);
p = (unsigned char *) a->ob_sval;
x = *p << 7;
while (--len >= 0)
x = (1000003*x) ^ *p++;
x ^= Py_SIZE(a);
if (x == -1)
x = -2;
a->ob_shash = x;
return x;
}
这篇关于我在哪里可以找到源Python的散列()函数或算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!