@@ -15,7 +15,7 @@ import (
15
15
// Otherwise, the elements are compared in increasing index order, and the
16
16
// comparison stops at the first unequal pair.
17
17
// Floating point NaNs are not considered equal.
18
- func Equal [E comparable ](s1 , s2 [] E ) bool {
18
+ func Equal [S ~ [] E , E comparable ](s1 , s2 S ) bool {
19
19
if len (s1 ) != len (s2 ) {
20
20
return false
21
21
}
@@ -32,7 +32,7 @@ func Equal[E comparable](s1, s2 []E) bool {
32
32
// EqualFunc returns false. Otherwise, the elements are compared in
33
33
// increasing index order, and the comparison stops at the first index
34
34
// for which eq returns false.
35
- func EqualFunc [E1 , E2 any ](s1 [] E1 , s2 [] E2 , eq func (E1 , E2 ) bool ) bool {
35
+ func EqualFunc [S1 ~ [] E1 , S2 ~ [] E2 , E1 , E2 any ](s1 S1 , s2 S2 , eq func (E1 , E2 ) bool ) bool {
36
36
if len (s1 ) != len (s2 ) {
37
37
return false
38
38
}
@@ -52,7 +52,7 @@ func EqualFunc[E1, E2 any](s1 []E1, s2 []E2, eq func(E1, E2) bool) bool {
52
52
// If both slices are equal until one of them ends, the shorter slice is
53
53
// considered less than the longer one.
54
54
// The result is 0 if s1 == s2, -1 if s1 < s2, and +1 if s1 > s2.
55
- func Compare [E cmp.Ordered ](s1 , s2 [] E ) int {
55
+ func Compare [S ~ [] E , E cmp.Ordered ](s1 , s2 S ) int {
56
56
for i , v1 := range s1 {
57
57
if i >= len (s2 ) {
58
58
return + 1
@@ -73,7 +73,7 @@ func Compare[E cmp.Ordered](s1, s2 []E) int {
73
73
// The result is the first non-zero result of cmp; if cmp always
74
74
// returns 0 the result is 0 if len(s1) == len(s2), -1 if len(s1) < len(s2),
75
75
// and +1 if len(s1) > len(s2).
76
- func CompareFunc [E1 , E2 any ](s1 [] E1 , s2 [] E2 , cmp func (E1 , E2 ) int ) int {
76
+ func CompareFunc [S1 ~ [] E1 , S2 ~ [] E2 , E1 , E2 any ](s1 S1 , s2 S2 , cmp func (E1 , E2 ) int ) int {
77
77
for i , v1 := range s1 {
78
78
if i >= len (s2 ) {
79
79
return + 1
@@ -91,7 +91,7 @@ func CompareFunc[E1, E2 any](s1 []E1, s2 []E2, cmp func(E1, E2) int) int {
91
91
92
92
// Index returns the index of the first occurrence of v in s,
93
93
// or -1 if not present.
94
- func Index [E comparable ](s [] E , v E ) int {
94
+ func Index [S ~ [] E , E comparable ](s S , v E ) int {
95
95
for i := range s {
96
96
if v == s [i ] {
97
97
return i
@@ -102,7 +102,7 @@ func Index[E comparable](s []E, v E) int {
102
102
103
103
// IndexFunc returns the first index i satisfying f(s[i]),
104
104
// or -1 if none do.
105
- func IndexFunc [E any ](s [] E , f func (E ) bool ) int {
105
+ func IndexFunc [S ~ [] E , E any ](s S , f func (E ) bool ) int {
106
106
for i := range s {
107
107
if f (s [i ]) {
108
108
return i
@@ -112,13 +112,13 @@ func IndexFunc[E any](s []E, f func(E) bool) int {
112
112
}
113
113
114
114
// Contains reports whether v is present in s.
115
- func Contains [E comparable ](s [] E , v E ) bool {
115
+ func Contains [S ~ [] E , E comparable ](s S , v E ) bool {
116
116
return Index (s , v ) >= 0
117
117
}
118
118
119
119
// ContainsFunc reports whether at least one
120
120
// element e of s satisfies f(e).
121
- func ContainsFunc [E any ](s [] E , f func (E ) bool ) bool {
121
+ func ContainsFunc [S ~ [] E , E any ](s S , f func (E ) bool ) bool {
122
122
return IndexFunc (s , f ) >= 0
123
123
}
124
124
@@ -441,7 +441,7 @@ func Clip[S ~[]E, E any](s S) S {
441
441
442
442
// rotateLeft rotates b left by n spaces.
443
443
// s_final[i] = s_orig[i+r], wrapping around.
444
- func rotateLeft [S ~ [] E , E any ](s S , r int ) {
444
+ func rotateLeft [E any ](s [] E , r int ) {
445
445
for r != 0 && r != len (s ) {
446
446
if r * 2 <= len (s ) {
447
447
swap (s [:r ], s [len (s )- r :])
@@ -452,19 +452,19 @@ func rotateLeft[S ~[]E, E any](s S, r int) {
452
452
}
453
453
}
454
454
}
455
- func rotateRight [S ~ [] E , E any ](s S , r int ) {
455
+ func rotateRight [E any ](s [] E , r int ) {
456
456
rotateLeft (s , len (s )- r )
457
457
}
458
458
459
459
// swap swaps the contents of x and y. x and y must be equal length and disjoint.
460
- func swap [S ~ [] E , E any ](x , y S ) {
460
+ func swap [E any ](x , y [] E ) {
461
461
for i := 0 ; i < len (x ); i ++ {
462
462
x [i ], y [i ] = y [i ], x [i ]
463
463
}
464
464
}
465
465
466
466
// overlaps reports whether the memory ranges a[0:len(a)] and b[0:len(b)] overlap.
467
- func overlaps [S ~ [] E , E any ](a , b S ) bool {
467
+ func overlaps [E any ](a , b [] E ) bool {
468
468
if len (a ) == 0 || len (b ) == 0 {
469
469
return false
470
470
}
@@ -480,7 +480,7 @@ func overlaps[S ~[]E, E any](a, b S) bool {
480
480
481
481
// startIdx returns the index in haystack where the needle starts.
482
482
// prerequisite: the needle must be aliased entirely inside the haystack.
483
- func startIdx [S ~ [] E , E any ](haystack , needle S ) int {
483
+ func startIdx [E any ](haystack , needle [] E ) int {
484
484
p := & needle [0 ]
485
485
for i := range haystack {
486
486
if p == & haystack [i ] {
@@ -492,7 +492,7 @@ func startIdx[S ~[]E, E any](haystack, needle S) int {
492
492
}
493
493
494
494
// Reverse reverses the elements of the slice in place.
495
- func Reverse [E any ](s [] E ) {
495
+ func Reverse [S ~ [] E , E any ](s S ) {
496
496
for i , j := 0 , len (s )- 1 ; i < j ; i , j = i + 1 , j - 1 {
497
497
s [i ], s [j ] = s [j ], s [i ]
498
498
}
0 commit comments