Can someone explain to me what this GetCardinality method is doing?

后端 未结 2 1469
-上瘾入骨i
-上瘾入骨i 2020-12-25 10:17

I\'ve been looking into faceted search with Lucene.NET, I\'ve found a brilliant example here which explains a fair amount, apart from the fact that it completely overlooks t

2条回答
  •  盖世英雄少女心
    2020-12-25 10:48

    The _bitsSetArray256 array is initialised with values such that _bitsSetArray256[n] contains the number of bits set in the binary representation of n, for n in 0..255.

    For example, _bitsSetArray256[13] equals 3, because 13 in binary is 1101 which contains 3 1s.

    The reason for doing this is that it's far faster to pre-compute these values and store them, rather than having to work them out each time (or on-demand). It's not like the number of 1s in the binary representation of 13 is ever going to change, after all :)

    Within the for loop, we are looping through an array of uints. A C# uint is a 32-bit quantity, ie made up for 4 bytes. Our lookup table tells us how many bits are set in a byte, so we must process each of the four bytes. The bit manipulation in the count += line extracts each of the four bytes, then gets its bit count from the lookup array. Adding together the bit counts for all four bytes gives the bit count for the uint as a whole.

    So given a BitArray, this function digs into the uint[] m_array member, then returns the total number of bits set in the binary representation of the uints therein.

提交回复
热议问题