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

feature/general-weights
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 {
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 * 0xff51afd7ed558ccd) % m64
acc = acc * 0xff51afd7ed558ccd
acc ^= acc >> 33
acc = (acc * 0xc4ceb9fe1a85ec53) % m64
acc = acc * 0xc4ceb9fe1a85ec53
acc ^= acc >> 33
return acc
}

View File

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