Skip to content

Commit e066406

Browse files
committed
🐿 Extend SortSliceByValue with more types
1 parent 7462e26 commit e066406

File tree

2 files changed

+122
-13
lines changed

2 files changed

+122
-13
lines changed

hrw.go

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,73 @@ func SortSliceByValue(slice interface{}, hash uint64) {
8686
case int:
8787
var key = make([]byte, 16)
8888
slice := slice.([]int)
89-
9089
for i := 0; i < length; i++ {
9190
binary.BigEndian.PutUint64(key, uint64(slice[i]))
9291
rule = append(rule, weight(Hash(key), hash))
9392
}
93+
case uint:
94+
var key = make([]byte, 16)
95+
slice := slice.([]uint)
96+
for i := 0; i < length; i++ {
97+
binary.BigEndian.PutUint64(key, uint64(slice[i]))
98+
rule = append(rule, weight(Hash(key), hash))
99+
}
100+
case int8:
101+
slice := slice.([]int8)
102+
for i := 0; i < length; i++ {
103+
key := byte(slice[i])
104+
rule = append(rule, weight(Hash([]byte{key}), hash))
105+
}
106+
case uint8:
107+
slice := slice.([]uint8)
108+
for i := 0; i < length; i++ {
109+
key := slice[i]
110+
rule = append(rule, weight(Hash([]byte{key}), hash))
111+
}
112+
case int16:
113+
var key = make([]byte, 8)
114+
slice := slice.([]int16)
115+
for i := 0; i < length; i++ {
116+
binary.BigEndian.PutUint16(key, uint16(slice[i]))
117+
rule = append(rule, weight(Hash(key), hash))
118+
}
119+
case uint16:
120+
var key = make([]byte, 8)
121+
slice := slice.([]uint16)
122+
for i := 0; i < length; i++ {
123+
binary.BigEndian.PutUint16(key, slice[i])
124+
rule = append(rule, weight(Hash(key), hash))
125+
}
94126
case int32:
95127
var key = make([]byte, 16)
96128
slice := slice.([]int32)
97-
98129
for i := 0; i < length; i++ {
99130
binary.BigEndian.PutUint32(key, uint32(slice[i]))
100131
rule = append(rule, weight(Hash(key), hash))
101132
}
133+
case uint32:
134+
var key = make([]byte, 16)
135+
slice := slice.([]uint32)
136+
for i := 0; i < length; i++ {
137+
binary.BigEndian.PutUint32(key, slice[i])
138+
rule = append(rule, weight(Hash(key), hash))
139+
}
140+
case int64:
141+
var key = make([]byte, 32)
142+
slice := slice.([]int64)
143+
for i := 0; i < length; i++ {
144+
binary.BigEndian.PutUint64(key, uint64(slice[i]))
145+
rule = append(rule, weight(Hash(key), hash))
146+
}
147+
case uint64:
148+
var key = make([]byte, 32)
149+
slice := slice.([]uint64)
150+
for i := 0; i < length; i++ {
151+
binary.BigEndian.PutUint64(key, slice[i])
152+
rule = append(rule, weight(Hash(key), hash))
153+
}
102154
case string:
103155
slice := slice.([]string)
104-
105156
for i := 0; i < length; i++ {
106157
rule = append(rule, weight(hash,
107158
Hash([]byte(slice[i]))))

hrw_test.go

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ import (
99
"testing"
1010
)
1111

12-
type hashString string
12+
type (
13+
hashString string
14+
unknown byte
15+
slices struct {
16+
actual interface{}
17+
expect interface{}
18+
}
19+
)
1320

1421
var testKey = []byte("0xff51afd7ed558ccd")
1522

@@ -44,6 +51,7 @@ func Example() {
4451
// trying GET six.example.com/examples/object-key
4552
// trying GET five.example.com/examples/object-key
4653
}
54+
4755
func (h hashString) Hash() uint64 {
4856
return Hash([]byte(h))
4957
}
@@ -118,8 +126,8 @@ func TestSortSliceByValueFail(t *testing.T) {
118126
})
119127

120128
t.Run("must 'fail' for unknown type", func(t *testing.T) {
121-
actual := []byte{1, 2, 3, 4, 5}
122-
expect := []byte{1, 2, 3, 4, 5}
129+
actual := []unknown{1, 2, 3, 4, 5}
130+
expect := []unknown{1, 2, 3, 4, 5}
123131
hash := Hash(testKey)
124132
SortSliceByValue(actual, hash)
125133
if !reflect.DeepEqual(actual, expect) {
@@ -139,12 +147,64 @@ func TestSortSliceByValueHasher(t *testing.T) {
139147
}
140148

141149
func TestSortSliceByValueIntSlice(t *testing.T) {
142-
actual := []int{0, 1, 2, 3, 4, 5}
143-
expect := []int{2, 3, 1, 4, 0, 5}
150+
cases := []slices{
151+
{
152+
actual: []int{0, 1, 2, 3, 4, 5},
153+
expect: []int{2, 3, 1, 4, 0, 5},
154+
},
155+
156+
{
157+
actual: []uint{0, 1, 2, 3, 4, 5},
158+
expect: []uint{2, 3, 1, 4, 0, 5},
159+
},
160+
161+
{
162+
actual: []int8{0, 1, 2, 3, 4, 5},
163+
expect: []int8{2, 0, 5, 1, 4, 3},
164+
},
165+
166+
{
167+
actual: []uint8{0, 1, 2, 3, 4, 5},
168+
expect: []uint8{2, 0, 5, 1, 4, 3},
169+
},
170+
171+
{
172+
actual: []int16{0, 1, 2, 3, 4, 5},
173+
expect: []int16{5, 4, 0, 3, 2, 1},
174+
},
175+
176+
{
177+
actual: []uint16{0, 1, 2, 3, 4, 5},
178+
expect: []uint16{5, 4, 0, 3, 2, 1},
179+
},
180+
181+
{
182+
actual: []int32{0, 1, 2, 3, 4, 5},
183+
expect: []int32{1, 3, 5, 4, 2, 0},
184+
},
185+
186+
{
187+
actual: []uint32{0, 1, 2, 3, 4, 5},
188+
expect: []uint32{1, 3, 5, 4, 2, 0},
189+
},
190+
191+
{
192+
actual: []int64{0, 1, 2, 3, 4, 5},
193+
expect: []int64{1, 5, 3, 4, 2, 0},
194+
},
195+
196+
{
197+
actual: []uint64{0, 1, 2, 3, 4, 5},
198+
expect: []uint64{1, 5, 3, 4, 2, 0},
199+
},
200+
}
144201
hash := Hash(testKey)
145-
SortSliceByValue(actual, hash)
146-
if !reflect.DeepEqual(actual, expect) {
147-
t.Errorf("Was %#v, but expected %#v", actual, expect)
202+
203+
for _, tc := range cases {
204+
SortSliceByValue(tc.actual, hash)
205+
if !reflect.DeepEqual(tc.actual, tc.expect) {
206+
t.Errorf("Was %#v, but expected %#v", tc.actual, tc.expect)
207+
}
148208
}
149209
}
150210

@@ -326,7 +386,6 @@ func TestUniformDistribution(t *testing.T) {
326386
"Chi2 condition for .9 is not met (expected %.2f <= %.2f)",
327387
chi2, chiTable[size-1])
328388
}
329-
330389
})
331390

332391
t.Run("sortByInt32Value", func(t *testing.T) {
@@ -367,7 +426,6 @@ func TestUniformDistribution(t *testing.T) {
367426
"Chi2 condition for .9 is not met (expected %.2f <= %.2f)",
368427
chi2, chiTable[size-1])
369428
}
370-
371429
})
372430

373431
t.Run("hash collision", func(t *testing.T) {

0 commit comments

Comments
 (0)