1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-08-03 01:38:00 +08:00

fix hamt delete issue

License: MIT
Signed-off-by: Jeromy <jeromyj@gmail.com>
This commit is contained in:
Jeromy
2017-11-18 08:39:38 -08:00
parent cc01b7f188
commit f8eaae329f
3 changed files with 27 additions and 22 deletions

View File

@ -2,6 +2,7 @@ package hamt
import (
"math/big"
"math/bits"
)
// hashBits is a helper that allows the reading of the 'next n bits' as an integer.
@ -39,25 +40,10 @@ func (hb *hashBits) Next(i int) int {
}
}
const (
m1 = 0x5555555555555555 //binary: 0101...
m2 = 0x3333333333333333 //binary: 00110011..
m4 = 0x0f0f0f0f0f0f0f0f //binary: 4 zeros, 4 ones ...
h01 = 0x0101010101010101 //the sum of 256 to the power of 0,1,2,3...
)
// from https://en.wikipedia.org/wiki/Hamming_weight
func popCountUint64(x uint64) int {
x -= (x >> 1) & m1 //put count of each 2 bits into those 2 bits
x = (x & m2) + ((x >> 2) & m2) //put count of each 4 bits into those 4 bits
x = (x + (x >> 4)) & m4 //put count of each 8 bits into those 8 bits
return int((x * h01) >> 56)
}
func popCount(i *big.Int) int {
var n int
for _, v := range i.Bits() {
n += popCountUint64(uint64(v))
n += bits.OnesCount64(uint64(v))
}
return n
}