@@ -7,26 +7,28 @@ package slices
7
7
import "constraints"
8
8
9
9
// Sort sorts a slice of any ordered type in ascending order.
10
- func Sort [Elem constraints.Ordered ](x [] Elem ) {
10
+ func Sort [S ~ [] E , E constraints.Ordered ](x S ) {
11
11
n := len (x )
12
- quickSortOrdered (x , 0 , n , maxDepth (n ))
12
+ // Due to golang/go#48619, constraints of quickSortOrdered and its related
13
+ // functsions are not changed. So cast S to []E for now.
14
+ quickSortOrdered ([]E (x ), 0 , n , maxDepth (n ))
13
15
}
14
16
15
17
// Sort sorts the slice x in ascending order as determined by the less function.
16
18
// This sort is not guaranteed to be stable.
17
- func SortFunc [Elem any ](x [] Elem , less func (a , b Elem ) bool ) {
19
+ func SortFunc [S ~ [] E , E any ](x S , less func (a , b E ) bool ) {
18
20
n := len (x )
19
21
quickSortLessFunc (x , 0 , n , maxDepth (n ), less )
20
22
}
21
23
22
24
// SortStable sorts the slice x while keeping the original order of equal
23
25
// elements, using less to compare elements.
24
- func SortStableFunc [Elem any ](x [] Elem , less func (a , b Elem ) bool ) {
26
+ func SortStableFunc [S ~ [] E , E any ](x S , less func (a , b E ) bool ) {
25
27
stableLessFunc (x , len (x ), less )
26
28
}
27
29
28
30
// IsSorted reports whether x is sorted in ascending order.
29
- func IsSorted [Elem constraints.Ordered ](x [] Elem ) bool {
31
+ func IsSorted [S ~ [] E , E constraints.Ordered ](x S ) bool {
30
32
for i := len (x ) - 1 ; i > 0 ; i -- {
31
33
if x [i ] < x [i - 1 ] {
32
34
return false
@@ -37,7 +39,7 @@ func IsSorted[Elem constraints.Ordered](x []Elem) bool {
37
39
38
40
// IsSortedFunc reports whether x is sorted in ascending order, with less as the
39
41
// comparison function.
40
- func IsSortedFunc [Elem any ](x [] Elem , less func (a , b Elem ) bool ) bool {
42
+ func IsSortedFunc [S ~ [] E , E any ](x S , less func (a , b E ) bool ) bool {
41
43
for i := len (x ) - 1 ; i > 0 ; i -- {
42
44
if less (x [i ], x [i - 1 ]) {
43
45
return false
@@ -51,7 +53,7 @@ func IsSortedFunc[Elem any](x []Elem, less func(a, b Elem) bool) bool {
51
53
// which it could be inserted into the slice is returned; therefore, if the
52
54
// intention is to find target itself a separate check for equality with the
53
55
// element at the returned index is required.
54
- func BinarySearch [Elem constraints.Ordered ](x [] Elem , target Elem ) int {
56
+ func BinarySearch [S ~ [] E , E constraints.Ordered ](x S , target E ) int {
55
57
return search (len (x ), func (i int ) bool { return x [i ] >= target })
56
58
}
57
59
@@ -63,7 +65,7 @@ func BinarySearch[Elem constraints.Ordered](x []Elem, target Elem) int {
63
65
// the first true index. If there is no such index, BinarySearchFunc returns n.
64
66
// (Note that the "not found" return value is not -1 as in, for instance,
65
67
// strings.Index.) Search calls ok(i) only for i in the range [0, n).
66
- func BinarySearchFunc [Elem any ](x [] Elem , ok func (Elem ) bool ) int {
68
+ func BinarySearchFunc [S ~ [] E , E any ](x S , ok func (E ) bool ) int {
67
69
return search (len (x ), func (i int ) bool { return ok (x [i ]) })
68
70
}
69
71
0 commit comments