Initial commit (code only without large binaries)
This commit is contained in:
30
EdgeNode/internal/utils/fnv/hash.go
Normal file
30
EdgeNode/internal/utils/fnv/hash.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package fnv
|
||||
|
||||
const (
|
||||
offset64 uint64 = 14695981039346656037
|
||||
prime64 uint64 = 1099511628211
|
||||
)
|
||||
|
||||
// HashString
|
||||
// 非unique Hash
|
||||
func HashString(key string) uint64 {
|
||||
var hash = offset64
|
||||
for _, b := range key {
|
||||
hash ^= uint64(b)
|
||||
hash *= prime64
|
||||
}
|
||||
return hash
|
||||
}
|
||||
|
||||
// Hash
|
||||
// 非unique Hash
|
||||
func Hash(key []byte) uint64 {
|
||||
var hash = offset64
|
||||
for _, b := range key {
|
||||
hash ^= uint64(b)
|
||||
hash *= prime64
|
||||
}
|
||||
return hash
|
||||
}
|
||||
32
EdgeNode/internal/utils/fnv/hash_test.go
Normal file
32
EdgeNode/internal/utils/fnv/hash_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package fnv_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fnv"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHash(t *testing.T) {
|
||||
for _, key := range []string{"costarring", "liquid", "hello"} {
|
||||
var h = fnv.HashString(key)
|
||||
t.Log(key + " => " + types.String(h))
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkHashString(b *testing.B) {
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
_ = fnv.HashString("abcdefh")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkHashString_Long(b *testing.B) {
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
_ = fnv.HashString("HELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLD")
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user