Small refactoring (#2)

- Use val.Index(0).Interface().(type) in switch-case of SortSliceByValue
- add go1.12 to TravisCI
This commit is contained in:
Evgeniy Kulikov 2019-03-14 15:48:58 +03:00 committed by GitHub
parent 29dac15c96
commit e8ba64d915
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 9 deletions

View file

@ -1,6 +1,7 @@
language: go
go:
- 1.11.x
- 1.12.x
env:
- GO111MODULE=on
install:

23
hrw.go
View file

@ -82,33 +82,38 @@ func SortSliceByValue(slice interface{}, hash uint64) {
return
}
switch slice := slice.(type) {
case []int:
switch val.Index(0).Interface().(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 []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 []string:
case string:
slice := slice.([]string)
for i := 0; i < length; i++ {
rule = append(rule, weight(hash,
Hash([]byte(slice[i]))))
}
default:
if _, ok := val.Index(0).Interface().(Hasher); !ok {
return
}
case Hasher:
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)