mirror of
https://github.com/fhmq/hmq.git
synced 2026-09-03 00:04:53 +00:00
add vendor
This commit is contained in:
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Segment
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package fnv1a
|
||||
|
||||
const (
|
||||
// FNV-1a
|
||||
offset64 = uint64(14695981039346656037)
|
||||
prime64 = uint64(1099511628211)
|
||||
|
||||
// Init64 is what 64 bits hash values should be initialized with.
|
||||
Init64 = offset64
|
||||
)
|
||||
|
||||
// HashString64 returns the hash of s.
|
||||
func HashString64(s string) uint64 {
|
||||
return AddString64(Init64, s)
|
||||
}
|
||||
|
||||
// HashUint64 returns the hash of u.
|
||||
func HashUint64(u uint64) uint64 {
|
||||
return AddUint64(Init64, u)
|
||||
}
|
||||
|
||||
// AddString64 adds the hash of s to the precomputed hash value h.
|
||||
func AddString64(h uint64, s string) uint64 {
|
||||
/*
|
||||
This is an unrolled version of this algorithm:
|
||||
|
||||
for _, c := range s {
|
||||
h = (h ^ uint64(c)) * prime64
|
||||
}
|
||||
|
||||
It seems to be ~1.5x faster than the simple loop in BenchmarkHash64:
|
||||
|
||||
- BenchmarkHash64/hash_function-4 30000000 56.1 ns/op 642.15 MB/s 0 B/op 0 allocs/op
|
||||
- BenchmarkHash64/hash_function-4 50000000 38.6 ns/op 932.35 MB/s 0 B/op 0 allocs/op
|
||||
|
||||
*/
|
||||
|
||||
i := 0
|
||||
n := (len(s) / 8) * 8
|
||||
|
||||
for i != n {
|
||||
h = (h ^ uint64(s[i])) * prime64
|
||||
h = (h ^ uint64(s[i+1])) * prime64
|
||||
h = (h ^ uint64(s[i+2])) * prime64
|
||||
h = (h ^ uint64(s[i+3])) * prime64
|
||||
h = (h ^ uint64(s[i+4])) * prime64
|
||||
h = (h ^ uint64(s[i+5])) * prime64
|
||||
h = (h ^ uint64(s[i+6])) * prime64
|
||||
h = (h ^ uint64(s[i+7])) * prime64
|
||||
i += 8
|
||||
}
|
||||
|
||||
for _, c := range s[i:] {
|
||||
h = (h ^ uint64(c)) * prime64
|
||||
}
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
// AddUint64 adds the hash value of the 8 bytes of u to h.
|
||||
func AddUint64(h uint64, u uint64) uint64 {
|
||||
h = (h ^ ((u >> 56) & 0xFF)) * prime64
|
||||
h = (h ^ ((u >> 48) & 0xFF)) * prime64
|
||||
h = (h ^ ((u >> 40) & 0xFF)) * prime64
|
||||
h = (h ^ ((u >> 32) & 0xFF)) * prime64
|
||||
h = (h ^ ((u >> 24) & 0xFF)) * prime64
|
||||
h = (h ^ ((u >> 16) & 0xFF)) * prime64
|
||||
h = (h ^ ((u >> 8) & 0xFF)) * prime64
|
||||
h = (h ^ ((u >> 0) & 0xFF)) * prime64
|
||||
return h
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package fnv1a
|
||||
|
||||
const (
|
||||
// FNV-1a
|
||||
offset32 = uint32(2166136261)
|
||||
prime32 = uint32(16777619)
|
||||
|
||||
// Init32 is what 32 bits hash values should be initialized with.
|
||||
Init32 = offset32
|
||||
)
|
||||
|
||||
// HashString32 returns the hash of s.
|
||||
func HashString32(s string) uint32 {
|
||||
return AddString32(Init32, s)
|
||||
}
|
||||
|
||||
// HashUint32 returns the hash of u.
|
||||
func HashUint32(u uint32) uint32 {
|
||||
return AddUint32(Init32, u)
|
||||
}
|
||||
|
||||
// AddString32 adds the hash of s to the precomputed hash value h.
|
||||
func AddString32(h uint32, s string) uint32 {
|
||||
i := 0
|
||||
n := (len(s) / 8) * 8
|
||||
|
||||
for i != n {
|
||||
h = (h ^ uint32(s[i])) * prime32
|
||||
h = (h ^ uint32(s[i+1])) * prime32
|
||||
h = (h ^ uint32(s[i+2])) * prime32
|
||||
h = (h ^ uint32(s[i+3])) * prime32
|
||||
h = (h ^ uint32(s[i+4])) * prime32
|
||||
h = (h ^ uint32(s[i+5])) * prime32
|
||||
h = (h ^ uint32(s[i+6])) * prime32
|
||||
h = (h ^ uint32(s[i+7])) * prime32
|
||||
i += 8
|
||||
}
|
||||
|
||||
for _, c := range s[i:] {
|
||||
h = (h ^ uint32(c)) * prime32
|
||||
}
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
// AddUint32 adds the hash value of the 8 bytes of u to h.
|
||||
func AddUint32(h, u uint32) uint32 {
|
||||
h = (h ^ ((u >> 24) & 0xFF)) * prime32
|
||||
h = (h ^ ((u >> 16) & 0xFF)) * prime32
|
||||
h = (h ^ ((u >> 8) & 0xFF)) * prime32
|
||||
h = (h ^ ((u >> 0) & 0xFF)) * prime32
|
||||
return h
|
||||
}
|
||||
Reference in New Issue
Block a user