-
Notifications
You must be signed in to change notification settings - Fork 6
/
sortyLen.go
55 lines (51 loc) · 1.29 KB
/
sortyLen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* Copyright (c) 2021, Serhat Şevki Dinçer.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package sorty
import (
"reflect"
"unsafe"
)
// IsSortedLen returns 0 if ar is sorted 'by length' in ascending order, otherwise
// it returns i > 0 with len(ar[i]) < len(ar[i-1]). ar's (underlying) type can be
//
// []string, [][]T // for any type T
//
// otherwise it panics.
//
//go:nosplit
func IsSortedLen(ar any) int {
slc, kind := extractSK(ar)
switch {
case kind == reflect.String:
s := *(*[]string)(unsafe.Pointer(&slc))
return isSortedLenS(s)
case kind >= sliceBias:
b := *(*[][]byte)(unsafe.Pointer(&slc))
return isSortedLenB(b)
}
panic("sorty: IsSortedLen: invalid input type")
}
// SortLen concurrently sorts ar 'by length' in ascending order. ar's (underlying)
// type can be
//
// []string, [][]T // for any type T
//
// otherwise it panics.
//
//go:nosplit
func SortLen(ar any) {
slc, kind := extractSK(ar)
switch {
case kind == reflect.String:
s := *(*[]string)(unsafe.Pointer(&slc))
sortLenS(s)
case kind >= sliceBias:
b := *(*[][]byte)(unsafe.Pointer(&slc))
sortLenB(b)
default:
panic("sorty: SortLen: invalid input type")
}
}