🐌 hot fix types

This commit is contained in:
Evgeniy Kulikov 2019-04-12 14:19:18 +03:00
parent e06640668d
commit 9a6fcdb1f8
No known key found for this signature in database
GPG key ID: BF6AEE0A2A699BF2
2 changed files with 29 additions and 27 deletions

45
hrw.go
View file

@ -82,89 +82,80 @@ func SortSliceByValue(slice interface{}, hash uint64) {
return
}
switch val.Index(0).Interface().(type) {
case int:
switch slice := slice.(type) {
case []int:
var key = make([]byte, 16)
slice := slice.([]int)
for i := 0; i < length; i++ {
binary.BigEndian.PutUint64(key, uint64(slice[i]))
rule = append(rule, weight(Hash(key), hash))
}
case uint:
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)
case []int8:
for i := 0; i < length; i++ {
key := byte(slice[i])
rule = append(rule, weight(Hash([]byte{key}), hash))
}
case uint8:
slice := slice.([]uint8)
case []uint8:
for i := 0; i < length; i++ {
key := slice[i]
rule = append(rule, weight(Hash([]byte{key}), hash))
}
case int16:
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:
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:
case []int32:
var key = make([]byte, 16)
slice := slice.([]int32)
for i := 0; i < length; i++ {
binary.BigEndian.PutUint32(key, uint32(slice[i]))
rule = append(rule, weight(Hash(key), hash))
}
case uint32:
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:
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:
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)
case []string:
for i := 0; i < length; i++ {
rule = append(rule, weight(hash,
Hash([]byte(slice[i]))))
}
case Hasher:
default:
if _, ok := val.Index(0).Interface().(Hasher); !ok {
return
}
for i := 0; i < length; i++ {
h := val.Index(i).Interface().(Hasher)
rule = append(rule, weight(hash, h.Hash()))
}
default:
return
}
rule = SortByWeight(rule, hash)

View file

@ -16,10 +16,16 @@ type (
actual interface{}
expect interface{}
}
Uint32Slice []uint32
)
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() {
// given a set of servers
servers := []string{
@ -188,6 +194,11 @@ func TestSortSliceByValueIntSlice(t *testing.T) {
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},
expect: []int64{1, 5, 3, 4, 2, 0},