func TestStringsWithSwapper(t *testing.T) { data := strings With(len(data), reflectutil.Swapper(data[:]), func(i, j int) bool { return data[i] < data[j] }) if !StringsAreSorted(data[:]) { t.Errorf("sorted %v", strings) t.Errorf(" got %v", data) } }
func BenchmarkStableInt1K_WithSwapper(b *testing.B) { b.StopTimer() unsorted := make([]int, 1<<10) for i := range unsorted { unsorted[i] = i ^ 0x2cc } data := make([]int, len(unsorted)) for i := 0; i < b.N; i++ { copy(data, unsorted) b.StartTimer() Stable(MakeInterface(len(data), reflectutil.Swapper(data), func(i, j int) bool { return data[i] < data[j] })) b.StopTimer() } }
func BenchmarkSortString1K_WithSwapper(b *testing.B) { b.StopTimer() unsorted := make([]string, 1<<10) for i := range unsorted { unsorted[i] = strconv.Itoa(i ^ 0x2cc) } data := make([]string, len(unsorted)) for i := 0; i < b.N; i++ { copy(data, unsorted) b.StartTimer() With(len(data), reflectutil.Swapper(data), func(i, j int) bool { return data[i] < data[j] }) b.StopTimer() } }
// SliceSorter returns a sort.Interface to sort the provided slice // using the provided less function. // If the provided interface is not a slice, the function panics. func SliceSorter(slice interface{}, less func(i, j int) bool) Interface { return MakeInterface(reflect.ValueOf(slice).Len(), reflectutil.Swapper(slice), less) }