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 language: go
go: go:
- 1.11.x - 1.11.x
- 1.12.x
env: env:
- GO111MODULE=on - GO111MODULE=on
install: install:

23
hrw.go
View file

@ -82,33 +82,38 @@ func SortSliceByValue(slice interface{}, hash uint64) {
return return
} }
switch slice := slice.(type) { switch val.Index(0).Interface().(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 []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 []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]))))
} }
default: case Hasher:
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)