hrw/hrw.go

212 lines
5 KiB
Go
Raw Normal View History

2019-01-29 22:58:30 +00:00
// Package hrw implements Rendezvous hashing.
// http://en.wikipedia.org/wiki/Rendezvous_hashing.
package hrw
import (
"encoding/binary"
2019-01-29 22:58:30 +00:00
"reflect"
"sort"
"github.com/spaolacci/murmur3"
2019-01-29 22:58:30 +00:00
)
type (
swapper func(i, j int)
// Hasher interface used by SortSliceByValue
2019-01-29 22:58:30 +00:00
Hasher interface{ Hash() uint64 }
hashed struct {
length int
sorted []uint64
weight []uint64
}
)
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
2019-01-29 22:58:30 +00:00
acc ^= acc >> 33
acc = acc * 0xff51afd7ed558ccd
2019-01-29 22:58:30 +00:00
acc ^= acc >> 33
acc = acc * 0xc4ceb9fe1a85ec53
2019-01-29 22:58:30 +00:00
acc ^= acc >> 33
return acc
}
func (h hashed) Len() int { return h.length }
func (h hashed) Less(i, j int) bool { return h.weight[h.sorted[i]] < h.weight[h.sorted[j]] }
func (h hashed) Swap(i, j int) { h.sorted[i], h.sorted[j] = h.sorted[j], h.sorted[i] }
// Hash uses murmur3 hash to return uint64
func Hash(key []byte) uint64 {
return murmur3.Sum64(key)
}
// SortByWeight receive nodes and hash, and sort it by weight
2019-01-29 22:58:30 +00:00
func SortByWeight(nodes []uint64, hash uint64) []uint64 {
var (
l = len(nodes)
h = hashed{
length: l,
sorted: make([]uint64, 0, l),
weight: make([]uint64, 0, l),
}
)
for i, node := range nodes {
h.sorted = append(h.sorted, uint64(i))
h.weight = append(h.weight, weight(node, hash))
}
sort.Sort(h)
return h.sorted
}
// SortSliceByValue received []T and hash to sort by value-weight
func SortSliceByValue(slice interface{}, hash uint64) {
2019-01-29 22:58:30 +00:00
t := reflect.TypeOf(slice)
if t.Kind() != reflect.Slice {
return
2019-01-29 22:58:30 +00:00
}
var (
val = reflect.ValueOf(slice)
swap = reflect.Swapper(slice)
length = val.Len()
rule = make([]uint64, 0, length)
)
if length == 0 {
return
2019-01-29 22:58:30 +00:00
}
switch val.Index(0).Interface().(type) {
case int:
var key = make([]byte, 16)
slice := slice.([]int)
2019-01-29 22:58:30 +00:00
for i := 0; i < length; i++ {
binary.BigEndian.PutUint64(key, uint64(slice[i]))
rule = append(rule, weight(Hash(key), hash))
2019-01-29 22:58:30 +00:00
}
case uint:
var key = make([]byte, 16)
slice := slice.([]uint)
for i := 0; i < length; i++ {
binary.BigEndian.PutUint64(key, uint64(slice[i]))
rule = append(rule, weight(Hash(key), hash))
}
case int8:
slice := slice.([]int8)
for i := 0; i < length; i++ {
key := byte(slice[i])
rule = append(rule, weight(Hash([]byte{key}), hash))
}
case uint8:
slice := slice.([]uint8)
for i := 0; i < length; i++ {
key := slice[i]
rule = append(rule, weight(Hash([]byte{key}), hash))
}
case int16:
var key = make([]byte, 8)
slice := slice.([]int16)
for i := 0; i < length; i++ {
binary.BigEndian.PutUint16(key, uint16(slice[i]))
rule = append(rule, weight(Hash(key), hash))
}
case uint16:
var key = make([]byte, 8)
slice := slice.([]uint16)
for i := 0; i < length; i++ {
binary.BigEndian.PutUint16(key, slice[i])
rule = append(rule, weight(Hash(key), hash))
}
case int32:
2019-02-01 09:57:05 +00:00
var key = make([]byte, 16)
slice := slice.([]int32)
2019-02-01 09:57:05 +00:00
for i := 0; i < length; i++ {
binary.BigEndian.PutUint32(key, uint32(slice[i]))
rule = append(rule, weight(Hash(key), hash))
}
case uint32:
var key = make([]byte, 16)
slice := slice.([]uint32)
for i := 0; i < length; i++ {
binary.BigEndian.PutUint32(key, slice[i])
rule = append(rule, weight(Hash(key), hash))
}
case int64:
var key = make([]byte, 32)
slice := slice.([]int64)
for i := 0; i < length; i++ {
binary.BigEndian.PutUint64(key, uint64(slice[i]))
rule = append(rule, weight(Hash(key), hash))
}
case uint64:
var key = make([]byte, 32)
slice := slice.([]uint64)
for i := 0; i < length; i++ {
binary.BigEndian.PutUint64(key, slice[i])
rule = append(rule, weight(Hash(key), hash))
}
case string:
slice := slice.([]string)
2019-01-29 22:58:30 +00:00
for i := 0; i < length; i++ {
rule = append(rule, weight(hash,
Hash([]byte(slice[i]))))
2019-01-29 22:58:30 +00:00
}
case Hasher:
2019-01-29 22:58:30 +00:00
for i := 0; i < length; i++ {
h := val.Index(i).Interface().(Hasher)
rule = append(rule, weight(hash, h.Hash()))
}
default:
return
2019-01-29 22:58:30 +00:00
}
rule = SortByWeight(rule, hash)
sortByRuleInverse(swap, uint64(length), rule)
2019-01-29 22:58:30 +00:00
}
// SortSliceByIndex received []T and hash to sort by index-weight
2019-01-29 22:58:30 +00:00
func SortSliceByIndex(slice interface{}, hash uint64) {
length := uint64(reflect.ValueOf(slice).Len())
swap := reflect.Swapper(slice)
rule := make([]uint64, 0, length)
for i := uint64(0); i < length; i++ {
rule = append(rule, i)
}
rule = SortByWeight(rule, hash)
sortByRuleInverse(swap, length, rule)
2019-01-29 22:58:30 +00:00
}
func sortByRuleDirect(swap swapper, length uint64, rule []uint64) {
2019-01-29 22:58:30 +00:00
done := make([]bool, length)
for i := uint64(0); i < length; i++ {
if done[i] {
continue
}
for j := rule[i]; !done[rule[j]]; j = rule[j] {
swap(int(i), int(j))
done[j] = true
}
}
}
func sortByRuleInverse(swap swapper, length uint64, rule []uint64) {
done := make([]bool, length)
for i := uint64(0); i < length; i++ {
if done[i] {
continue
}
for j := i; !done[rule[j]]; j = rule[j] {
swap(int(j), int(rule[j]))
done[j] = true
}
}
}