-1

我正在检查一些从 C 转换为 C# 的代码。我对原始 C 有一个问题:

...
#define getblock(p, i) (p[i])
...
void MurmurHash3_x86_32 ( const void * key, int len,
                          uint32_t seed, void * out )
{
  const uint8_t * data = (const uint8_t*)key;
  const int nblocks = len / 4;
  int i;

  uint32_t h1 = seed;

  uint32_t c1 = 0xcc9e2d51;
  uint32_t c2 = 0x1b873593;

  const uint32_t * blocks = (const uint32_t *)(data + nblocks*4);

  for(i = -nblocks; i; i++)
  {
    uint32_t k1 = getblock(blocks,i);
...

这部分for(i = -nblocks; i; i++) ...是向后循环数据吗?我从未见过用负索引引用的数据。

4

3 回答 3

3

该变量由(假设)blocks提前初始化。然后循环从开头开始直到 指向的结尾,因此使用负索引。因此,它不是向后循环数据,而是向前循环。datanblockssizeof(uint32_t) == 4fordatablocks

于 2013-06-10T17:52:00.500 回答
3

No it's not looping through the data backwards. It starts at the beginning of data, and indexes up.

As you can see, here the pointer "blocks" is advanced past "data" already. It points "nblocks" past the beginning of data.

const uint32_t * blocks = (const uint32_t *)(data + nblocks*4);

So, you need a negative index to get to the beginning of data (-nblocks). The start of data is precisely at "blocks[-nblocks]". The "for" loop simply starts there, and counts up.

for(i = -nblocks; i; i++)
于 2013-06-10T18:06:10.757 回答
0

事实上,它是一种使用散列的算法(https://en.wikipedia.org/wiki/MurmurHash),您的来源可能是那个https://github.com/JeffBezanson/libsupport/blob/master/MurmurHash3.c;)

于 2013-06-10T17:47:26.033 回答