Skip to content

Commit 5372624

Browse files
committed
slices: relax the constraints to type whose underlying type is slice
Like what we done in exp/maps, exp/slices would be better to accept a type whose underlying type is slice. Due to golang/go#48619, constraints of quickSortOrdered and its related functions are not changed. So cast S to []E for now.
1 parent 073fb13 commit 5372624

File tree

4 files changed

+32
-30
lines changed

4 files changed

+32
-30
lines changed

slices/slices.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import "constraints"
1414
// Otherwise, the elements are compared in increasing index order, and the
1515
// comparison stops at the first unequal pair.
1616
// Floating point NaNs are not considered equal.
17-
func Equal[E comparable](s1, s2 []E) bool {
17+
func Equal[S ~[]E, E comparable](s1, s2 S) bool {
1818
if len(s1) != len(s2) {
1919
return false
2020
}
@@ -31,7 +31,7 @@ func Equal[E comparable](s1, s2 []E) bool {
3131
// EqualFunc returns false. Otherwise, the elements are compared in
3232
// increasing index order, and the comparison stops at the first index
3333
// for which eq returns false.
34-
func EqualFunc[E1, E2 any](s1 []E1, s2 []E2, eq func(E1, E2) bool) bool {
34+
func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool {
3535
if len(s1) != len(s2) {
3636
return false
3737
}

slices/sort.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,28 @@ package slices
77
import "constraints"
88

99
// 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) {
1111
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))
1315
}
1416

1517
// Sort sorts the slice x in ascending order as determined by the less function.
1618
// 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) {
1820
n := len(x)
1921
quickSortLessFunc(x, 0, n, maxDepth(n), less)
2022
}
2123

2224
// SortStable sorts the slice x while keeping the original order of equal
2325
// 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) {
2527
stableLessFunc(x, len(x), less)
2628
}
2729

2830
// 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 {
3032
for i := len(x) - 1; i > 0; i-- {
3133
if x[i] < x[i-1] {
3234
return false
@@ -37,7 +39,7 @@ func IsSorted[Elem constraints.Ordered](x []Elem) bool {
3739

3840
// IsSortedFunc reports whether x is sorted in ascending order, with less as the
3941
// 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 {
4143
for i := len(x) - 1; i > 0; i-- {
4244
if less(x[i], x[i-1]) {
4345
return false
@@ -51,7 +53,7 @@ func IsSortedFunc[Elem any](x []Elem, less func(a, b Elem) bool) bool {
5153
// which it could be inserted into the slice is returned; therefore, if the
5254
// intention is to find target itself a separate check for equality with the
5355
// 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 {
5557
return search(len(x), func(i int) bool { return x[i] >= target })
5658
}
5759

@@ -63,7 +65,7 @@ func BinarySearch[Elem constraints.Ordered](x []Elem, target Elem) int {
6365
// the first true index. If there is no such index, BinarySearchFunc returns n.
6466
// (Note that the "not found" return value is not -1 as in, for instance,
6567
// 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 {
6769
return search(len(x), func(i int) bool { return ok(x[i]) })
6870
}
6971

slices/zsortfunc.go

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

slices/zsortordered.go

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)