猿问

在 Go 中计算 hashCode

Java 对象有一个hashCode,它比加密哈希便宜。如何在 Go 中实现这样的 hashCode?


郎朗坤
浏览 397回答 1
1回答

大话西游666

Go 编程语言是开源的。你可以查看它的标准库,了解快速高效的 Go 哈希实现。这里是:runtime/hash64.go对于 64 位架构runtime/hash32.go用于 32 位架构它们未导出,但如果您在应用程序中需要它们,您只需将代码复制到您的项目中即可。另请注意,如果您的 CPU 支持它,Go 运行时将使用aeshash您的 CPU 的功能(更多信息请点击此处:Go 如何计算地图中键的哈希值?)。引用较短的32 位版本:const (&nbsp; &nbsp; // Constants for multiplication: four random odd 32-bit numbers.&nbsp; &nbsp; m1 = 3168982561&nbsp; &nbsp; m2 = 3339683297&nbsp; &nbsp; m3 = 832293441&nbsp; &nbsp; m4 = 2336365089)func memhash(p unsafe.Pointer, seed, s uintptr) uintptr {&nbsp; &nbsp; if GOARCH == "386" && GOOS != "nacl" && useAeshash {&nbsp; &nbsp; &nbsp; &nbsp; return aeshash(p, seed, s)&nbsp; &nbsp; }&nbsp; &nbsp; h := uint32(seed + s*hashkey[0])tail:&nbsp; &nbsp; switch {&nbsp; &nbsp; case s == 0:&nbsp; &nbsp; case s < 4:&nbsp; &nbsp; &nbsp; &nbsp; h ^= uint32(*(*byte)(p))&nbsp; &nbsp; &nbsp; &nbsp; h ^= uint32(*(*byte)(add(p, s>>1))) << 8&nbsp; &nbsp; &nbsp; &nbsp; h ^= uint32(*(*byte)(add(p, s-1))) << 16&nbsp; &nbsp; &nbsp; &nbsp; h = rotl_15(h*m1) * m2&nbsp; &nbsp; case s == 4:&nbsp; &nbsp; &nbsp; &nbsp; h ^= readUnaligned32(p)&nbsp; &nbsp; &nbsp; &nbsp; h = rotl_15(h*m1) * m2&nbsp; &nbsp; case s <= 8:&nbsp; &nbsp; &nbsp; &nbsp; h ^= readUnaligned32(p)&nbsp; &nbsp; &nbsp; &nbsp; h = rotl_15(h*m1) * m2&nbsp; &nbsp; &nbsp; &nbsp; h ^= readUnaligned32(add(p, s-4))&nbsp; &nbsp; &nbsp; &nbsp; h = rotl_15(h*m1) * m2&nbsp; &nbsp; case s <= 16:&nbsp; &nbsp; &nbsp; &nbsp; h ^= readUnaligned32(p)&nbsp; &nbsp; &nbsp; &nbsp; h = rotl_15(h*m1) * m2&nbsp; &nbsp; &nbsp; &nbsp; h ^= readUnaligned32(add(p, 4))&nbsp; &nbsp; &nbsp; &nbsp; h = rotl_15(h*m1) * m2&nbsp; &nbsp; &nbsp; &nbsp; h ^= readUnaligned32(add(p, s-8))&nbsp; &nbsp; &nbsp; &nbsp; h = rotl_15(h*m1) * m2&nbsp; &nbsp; &nbsp; &nbsp; h ^= readUnaligned32(add(p, s-4))&nbsp; &nbsp; &nbsp; &nbsp; h = rotl_15(h*m1) * m2&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; v1 := h&nbsp; &nbsp; &nbsp; &nbsp; v2 := uint32(seed * hashkey[1])&nbsp; &nbsp; &nbsp; &nbsp; v3 := uint32(seed * hashkey[2])&nbsp; &nbsp; &nbsp; &nbsp; v4 := uint32(seed * hashkey[3])&nbsp; &nbsp; &nbsp; &nbsp; for s >= 16 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v1 ^= readUnaligned32(p)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v1 = rotl_15(v1*m1) * m2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p = add(p, 4)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v2 ^= readUnaligned32(p)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v2 = rotl_15(v2*m2) * m3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p = add(p, 4)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v3 ^= readUnaligned32(p)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v3 = rotl_15(v3*m3) * m4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p = add(p, 4)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v4 ^= readUnaligned32(p)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; v4 = rotl_15(v4*m4) * m1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p = add(p, 4)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s -= 16&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; h = v1 ^ v2 ^ v3 ^ v4&nbsp; &nbsp; &nbsp; &nbsp; goto tail&nbsp; &nbsp; }&nbsp; &nbsp; h ^= h >> 17&nbsp; &nbsp; h *= m3&nbsp; &nbsp; h ^= h >> 13&nbsp; &nbsp; h *= m4&nbsp; &nbsp; h ^= h >> 16&nbsp; &nbsp; return uintptr(h)}// Note: in order to get the compiler to issue rotl instructions, we// need to constant fold the shift amount by hand.// TODO: convince the compiler to issue rotl instructions after inlining.func rotl_15(x uint32) uint32 {&nbsp; &nbsp; return (x << 15) | (x >> (32 - 15))}
随时随地看视频慕课网APP

相关分类

Go
我要回答