Removed redundant m64 variable and tweaked uniform distribution test. (#1)

This commit is contained in:
Alex Vanin 2019-01-30 12:32:34 +03:00 committed by Evgeniy Kulikov
parent 9b6321f99c
commit cf9a7e5b89
2 changed files with 9 additions and 8 deletions

8
hrw.go
View file

@ -22,14 +22,14 @@ type (
} }
) )
const m64 = 18446744073709551615 // modulus (2**64-1)
func weight(x uint64, y uint64) uint64 { func weight(x uint64, y uint64) uint64 {
acc := x ^ y acc := x ^ y
// here used mmh3 64 bit finalizer
// https://github.com/aappleby/smhasher/blob/61a0530f28277f2e850bfc39600ce61d02b518de/src/MurmurHash3.cpp#L81
acc ^= acc >> 33 acc ^= acc >> 33
acc = (acc * 0xff51afd7ed558ccd) % m64 acc = acc * 0xff51afd7ed558ccd
acc ^= acc >> 33 acc ^= acc >> 33
acc = (acc * 0xc4ceb9fe1a85ec53) % m64 acc = acc * 0xc4ceb9fe1a85ec53
acc ^= acc >> 33 acc ^= acc >> 33
return acc return acc
} }

View file

@ -4,6 +4,7 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"hash/fnv" "hash/fnv"
"math"
"reflect" "reflect"
"strconv" "strconv"
"testing" "testing"
@ -54,14 +55,14 @@ func (h hashString) Hash() uint64 {
hs := fnv.New64() hs := fnv.New64()
// error always nil // error always nil
_, _ = hs.Write([]byte(h)) _, _ = hs.Write([]byte(h))
return (hs.Sum64() >> 1) % m64 return hs.Sum64() >> 1
} }
func hash(key []byte) uint64 { func hash(key []byte) uint64 {
h := fnv.New64() h := fnv.New64()
// error always nil // error always nil
_, _ = h.Write(key) _, _ = h.Write(key)
return (h.Sum64() >> 1) ^ m64 return (h.Sum64() >> 1) ^ math.MaxUint64
} }
func mur3hash(key []byte) uint64 { func mur3hash(key []byte) uint64 {
@ -184,7 +185,7 @@ func TestSortByWeight(t *testing.T) {
func TestUniformDistribution(t *testing.T) { func TestUniformDistribution(t *testing.T) {
var ( var (
i uint64 i uint64
size = uint64(4) size = uint64(10)
nodes = make([]uint64, 0, size) nodes = make([]uint64, 0, size)
counts = make(map[uint64]uint64) counts = make(map[uint64]uint64)
key = make([]byte, 16) key = make([]byte, 16)
@ -202,7 +203,7 @@ func TestUniformDistribution(t *testing.T) {
} }
mean := float64(keys) / float64(len(nodes)) mean := float64(keys) / float64(len(nodes))
delta := mean * 0.002 // 0.2% delta := mean * 0.01 // 1 %
for node, count := range counts { for node, count := range counts {
d := mean - float64(count) d := mean - float64(count)
if d > delta || (0-d) > delta { if d > delta || (0-d) > delta {