forked from TrueCloudLab/hrw
🐌 hot fix types
This commit is contained in:
parent
e06640668d
commit
9a6fcdb1f8
2 changed files with 29 additions and 27 deletions
45
hrw.go
45
hrw.go
|
@ -82,89 +82,80 @@ func SortSliceByValue(slice interface{}, hash uint64) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch val.Index(0).Interface().(type) {
|
switch slice := slice.(type) {
|
||||||
case int:
|
case []int:
|
||||||
var key = make([]byte, 16)
|
var key = make([]byte, 16)
|
||||||
slice := slice.([]int)
|
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
binary.BigEndian.PutUint64(key, uint64(slice[i]))
|
binary.BigEndian.PutUint64(key, uint64(slice[i]))
|
||||||
rule = append(rule, weight(Hash(key), hash))
|
rule = append(rule, weight(Hash(key), hash))
|
||||||
}
|
}
|
||||||
case uint:
|
case []uint:
|
||||||
var key = make([]byte, 16)
|
var key = make([]byte, 16)
|
||||||
slice := slice.([]uint)
|
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
binary.BigEndian.PutUint64(key, uint64(slice[i]))
|
binary.BigEndian.PutUint64(key, uint64(slice[i]))
|
||||||
rule = append(rule, weight(Hash(key), hash))
|
rule = append(rule, weight(Hash(key), hash))
|
||||||
}
|
}
|
||||||
case int8:
|
case []int8:
|
||||||
slice := slice.([]int8)
|
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
key := byte(slice[i])
|
key := byte(slice[i])
|
||||||
rule = append(rule, weight(Hash([]byte{key}), hash))
|
rule = append(rule, weight(Hash([]byte{key}), hash))
|
||||||
}
|
}
|
||||||
case uint8:
|
case []uint8:
|
||||||
slice := slice.([]uint8)
|
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
key := slice[i]
|
key := slice[i]
|
||||||
rule = append(rule, weight(Hash([]byte{key}), hash))
|
rule = append(rule, weight(Hash([]byte{key}), hash))
|
||||||
}
|
}
|
||||||
case int16:
|
case []int16:
|
||||||
var key = make([]byte, 8)
|
var key = make([]byte, 8)
|
||||||
slice := slice.([]int16)
|
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
binary.BigEndian.PutUint16(key, uint16(slice[i]))
|
binary.BigEndian.PutUint16(key, uint16(slice[i]))
|
||||||
rule = append(rule, weight(Hash(key), hash))
|
rule = append(rule, weight(Hash(key), hash))
|
||||||
}
|
}
|
||||||
case uint16:
|
case []uint16:
|
||||||
var key = make([]byte, 8)
|
var key = make([]byte, 8)
|
||||||
slice := slice.([]uint16)
|
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
binary.BigEndian.PutUint16(key, slice[i])
|
binary.BigEndian.PutUint16(key, slice[i])
|
||||||
rule = append(rule, weight(Hash(key), hash))
|
rule = append(rule, weight(Hash(key), hash))
|
||||||
}
|
}
|
||||||
case int32:
|
case []int32:
|
||||||
var key = make([]byte, 16)
|
var key = make([]byte, 16)
|
||||||
slice := slice.([]int32)
|
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
binary.BigEndian.PutUint32(key, uint32(slice[i]))
|
binary.BigEndian.PutUint32(key, uint32(slice[i]))
|
||||||
rule = append(rule, weight(Hash(key), hash))
|
rule = append(rule, weight(Hash(key), hash))
|
||||||
}
|
}
|
||||||
case uint32:
|
case []uint32:
|
||||||
var key = make([]byte, 16)
|
var key = make([]byte, 16)
|
||||||
slice := slice.([]uint32)
|
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
binary.BigEndian.PutUint32(key, slice[i])
|
binary.BigEndian.PutUint32(key, slice[i])
|
||||||
rule = append(rule, weight(Hash(key), hash))
|
rule = append(rule, weight(Hash(key), hash))
|
||||||
}
|
}
|
||||||
case int64:
|
case []int64:
|
||||||
var key = make([]byte, 32)
|
var key = make([]byte, 32)
|
||||||
slice := slice.([]int64)
|
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
binary.BigEndian.PutUint64(key, uint64(slice[i]))
|
binary.BigEndian.PutUint64(key, uint64(slice[i]))
|
||||||
rule = append(rule, weight(Hash(key), hash))
|
rule = append(rule, weight(Hash(key), hash))
|
||||||
}
|
}
|
||||||
case uint64:
|
case []uint64:
|
||||||
var key = make([]byte, 32)
|
var key = make([]byte, 32)
|
||||||
slice := slice.([]uint64)
|
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
binary.BigEndian.PutUint64(key, slice[i])
|
binary.BigEndian.PutUint64(key, slice[i])
|
||||||
rule = append(rule, weight(Hash(key), hash))
|
rule = append(rule, weight(Hash(key), hash))
|
||||||
}
|
}
|
||||||
case string:
|
case []string:
|
||||||
slice := slice.([]string)
|
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
rule = append(rule, weight(hash,
|
rule = append(rule, weight(hash,
|
||||||
Hash([]byte(slice[i]))))
|
Hash([]byte(slice[i]))))
|
||||||
}
|
}
|
||||||
case Hasher:
|
|
||||||
|
default:
|
||||||
|
if _, ok := val.Index(0).Interface().(Hasher); !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
h := val.Index(i).Interface().(Hasher)
|
h := val.Index(i).Interface().(Hasher)
|
||||||
rule = append(rule, weight(hash, h.Hash()))
|
rule = append(rule, weight(hash, h.Hash()))
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rule = SortByWeight(rule, hash)
|
rule = SortByWeight(rule, hash)
|
||||||
|
|
11
hrw_test.go
11
hrw_test.go
|
@ -16,10 +16,16 @@ type (
|
||||||
actual interface{}
|
actual interface{}
|
||||||
expect interface{}
|
expect interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Uint32Slice []uint32
|
||||||
)
|
)
|
||||||
|
|
||||||
var testKey = []byte("0xff51afd7ed558ccd")
|
var testKey = []byte("0xff51afd7ed558ccd")
|
||||||
|
|
||||||
|
func (p Uint32Slice) Len() int { return len(p) }
|
||||||
|
func (p Uint32Slice) Less(i, j int) bool { return p[i] < p[j] }
|
||||||
|
func (p Uint32Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||||
|
|
||||||
func Example() {
|
func Example() {
|
||||||
// given a set of servers
|
// given a set of servers
|
||||||
servers := []string{
|
servers := []string{
|
||||||
|
@ -188,6 +194,11 @@ func TestSortSliceByValueIntSlice(t *testing.T) {
|
||||||
expect: []uint32{1, 3, 5, 4, 2, 0},
|
expect: []uint32{1, 3, 5, 4, 2, 0},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
actual: Uint32Slice{0, 1, 2, 3, 4, 5},
|
||||||
|
expect: Uint32Slice{0, 1, 2, 3, 4, 5},
|
||||||
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
actual: []int64{0, 1, 2, 3, 4, 5},
|
actual: []int64{0, 1, 2, 3, 4, 5},
|
||||||
expect: []int64{1, 5, 3, 4, 2, 0},
|
expect: []int64{1, 5, 3, 4, 2, 0},
|
||||||
|
|
Loading…
Reference in a new issue