1
// Copyright 2021 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
5
// Package slices defines various functions useful with slices of any type.
11
"golang.org/x/exp/constraints"
14
// Equal reports whether two slices are equal: the same length and all
15
// elements equal. If the lengths are different, Equal returns false.
16
// Otherwise, the elements are compared in increasing index order, and the
17
// comparison stops at the first unequal pair.
18
// Floating point NaNs are not considered equal.
19
func Equal[S ~[]E, E comparable](s1, s2 S) bool {
20
if len(s1) != len(s2) {
31
// EqualFunc reports whether two slices are equal using an equality
32
// function on each pair of elements. If the lengths are different,
33
// EqualFunc returns false. Otherwise, the elements are compared in
34
// increasing index order, and the comparison stops at the first index
35
// for which eq returns false.
36
func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool {
37
if len(s1) != len(s2) {
40
for i, v1 := range s1 {
49
// Compare compares the elements of s1 and s2, using [cmp.Compare] on each pair
50
// of elements. The elements are compared sequentially, starting at index 0,
51
// until one element is not equal to the other.
52
// The result of comparing the first non-matching elements is returned.
53
// If both slices are equal until one of them ends, the shorter slice is
54
// considered less than the longer one.
55
// The result is 0 if s1 == s2, -1 if s1 < s2, and +1 if s1 > s2.
56
func Compare[S ~[]E, E constraints.Ordered](s1, s2 S) int {
57
for i, v1 := range s1 {
62
if c := cmpCompare(v1, v2); c != 0 {
66
if len(s1) < len(s2) {
72
// CompareFunc is like [Compare] but uses a custom comparison function on each
74
// The result is the first non-zero result of cmp; if cmp always
75
// returns 0 the result is 0 if len(s1) == len(s2), -1 if len(s1) < len(s2),
76
// and +1 if len(s1) > len(s2).
77
func CompareFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, cmp func(E1, E2) int) int {
78
for i, v1 := range s1 {
83
if c := cmp(v1, v2); c != 0 {
87
if len(s1) < len(s2) {
93
// Index returns the index of the first occurrence of v in s,
94
// or -1 if not present.
95
func Index[S ~[]E, E comparable](s S, v E) int {
104
// IndexFunc returns the first index i satisfying f(s[i]),
106
func IndexFunc[S ~[]E, E any](s S, f func(E) bool) int {
115
// Contains reports whether v is present in s.
116
func Contains[S ~[]E, E comparable](s S, v E) bool {
117
return Index(s, v) >= 0
120
// ContainsFunc reports whether at least one
121
// element e of s satisfies f(e).
122
func ContainsFunc[S ~[]E, E any](s S, f func(E) bool) bool {
123
return IndexFunc(s, f) >= 0
126
// Insert inserts the values v... into s at index i,
127
// returning the modified slice.
128
// The elements at s[i:] are shifted up to make room.
129
// In the returned slice r, r[i] == v[0],
130
// and r[i+len(v)] == value originally at r[i].
131
// Insert panics if i is out of range.
132
// This function is O(len(s) + len(v)).
133
func Insert[S ~[]E, E any](s S, i int, v ...E) S {
140
return append(s, v...)
143
// Use append rather than make so that we bump the size of
144
// the slice up to the next storage class.
145
// This is what Grow does but we don't call Grow because
146
// that might copy the values twice.
147
s2 := append(s[:i], make(S, n+m-i)...)
149
copy(s2[i+m:], s[i:])
155
// s: aaaaaaaabbbbccccccccdddd
159
// s: aaaaaaaavvvvbbbbcccccccc
163
// a are the values that don't move in s.
164
// v are the values copied in from v.
165
// b and c are the values from s that are shifted up in index.
166
// d are the values that get overwritten, never to be seen again.
168
if !overlaps(v, s[i+m:]) {
169
// Easy case - v does not overlap either the c or d regions.
170
// (It might be in some of a or b, or elsewhere entirely.)
171
// The data we copy up doesn't write to v at all, so just do it.
176
// s: aaaaaaaabbbbbbbbcccccccc
179
// Note the b values are duplicated.
184
// s: aaaaaaaavvvvbbbbcccccccc
187
// That's the result we want.
191
// The hard case - v overlaps c or d. We can't just shift up
192
// the data because we'd move or clobber the values we're trying
194
// So instead, write v on top of d, then rotate.
198
// s: aaaaaaaabbbbccccccccvvvv
202
rotateRight(s[i:], m)
205
// s: aaaaaaaavvvvbbbbcccccccc
208
// That's the result we want.
212
// clearSlice sets all elements up to the length of s to the zero value of E.
213
// We may use the builtin clear func instead, and remove clearSlice, when upgrading
215
func clearSlice[S ~[]E, E any](s S) {
222
// Delete removes the elements s[i:j] from s, returning the modified slice.
223
// Delete panics if j > len(s) or s[i:j] is not a valid slice of s.
224
// Delete is O(len(s)-i), so if many items must be deleted, it is better to
225
// make a single call deleting them all together than to delete one at a time.
226
// Delete zeroes the elements s[len(s)-(j-i):len(s)].
227
func Delete[S ~[]E, E any](s S, i, j int) S {
228
_ = s[i:j:len(s)] // bounds check
235
s = append(s[:i], s[j:]...)
236
clearSlice(s[len(s):oldlen]) // zero/nil out the obsolete elements, for GC
240
// DeleteFunc removes any elements from s for which del returns true,
241
// returning the modified slice.
242
// DeleteFunc zeroes the elements between the new length and the original length.
243
func DeleteFunc[S ~[]E, E any](s S, del func(E) bool) S {
244
i := IndexFunc(s, del)
248
// Don't start copying elements until we find one to delete.
249
for j := i + 1; j < len(s); j++ {
250
if v := s[j]; !del(v) {
255
clearSlice(s[i:]) // zero/nil out the obsolete elements, for GC
259
// Replace replaces the elements s[i:j] by the given v, and returns the
260
// modified slice. Replace panics if s[i:j] is not a valid slice of s.
261
// When len(v) < (j-i), Replace zeroes the elements between the new length and the original length.
262
func Replace[S ~[]E, E any](s S, i, j int, v ...E) S {
263
_ = s[i:j] // verify that i:j is a valid subslice
266
return Insert(s, i, v...)
269
return append(s[:i], v...)
272
tot := len(s[:i]) + len(v) + len(s[j:])
274
// Too big to fit, allocate and copy over.
275
s2 := append(s[:i], make(S, tot-i)...) // See Insert
277
copy(s2[i+len(v):], s[j:])
284
// Easy, as v fits in the deleted portion.
287
copy(r[i+len(v):], s[j:])
289
clearSlice(s[tot:]) // zero/nil out the obsolete elements, for GC
293
// We are expanding (v is bigger than j-i).
294
// The situation is something like this:
295
// (example has i=4,j=8,len(s)=16,len(v)=6)
296
// s: aaaaxxxxbbbbbbbbyy
302
// y: area to expand into
304
if !overlaps(r[i+len(v):], v) {
305
// Easy, as v is not clobbered by the first copy.
306
copy(r[i+len(v):], s[j:])
311
// This is a situation where we don't have a single place to which
312
// we can copy v. Parts of it need to go to two different places.
313
// We want to copy the prefix of v into y and the suffix into x, then
314
// rotate |y| spots to the right.
318
// s: aaaavvvvbbbbbbbbvv
322
// If either of those two destinations don't alias v, then we're good.
323
y := len(v) - (j - i) // length of y portion
325
if !overlaps(r[i:j], v) {
327
copy(r[len(s):], v[:y])
328
rotateRight(r[i:], y)
331
if !overlaps(r[len(s):], v) {
332
copy(r[len(s):], v[:y])
334
rotateRight(r[i:], y)
338
// Now we know that v overlaps both x and y.
339
// That means that the entirety of b is *inside* v.
340
// So we don't need to preserve b at all; instead we
341
// can copy v first, then copy the b part of v out of
342
// v to the right destination.
343
k := startIdx(v, s[j:])
345
copy(r[i+len(v):], r[i+k:])
349
// Clone returns a copy of the slice.
350
// The elements are copied using assignment, so this is a shallow clone.
351
func Clone[S ~[]E, E any](s S) S {
352
// Preserve nil in case it matters.
356
return append(S([]E{}), s...)
359
// Compact replaces consecutive runs of equal elements with a single copy.
360
// This is like the uniq command found on Unix.
361
// Compact modifies the contents of the slice s and returns the modified slice,
362
// which may have a smaller length.
363
// Compact zeroes the elements between the new length and the original length.
364
func Compact[S ~[]E, E comparable](s S) S {
369
for k := 1; k < len(s); k++ {
377
clearSlice(s[i:]) // zero/nil out the obsolete elements, for GC
381
// CompactFunc is like [Compact] but uses an equality function to compare elements.
382
// For runs of elements that compare equal, CompactFunc keeps the first one.
383
// CompactFunc zeroes the elements between the new length and the original length.
384
func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S {
389
for k := 1; k < len(s); k++ {
390
if !eq(s[k], s[k-1]) {
397
clearSlice(s[i:]) // zero/nil out the obsolete elements, for GC
401
// Grow increases the slice's capacity, if necessary, to guarantee space for
402
// another n elements. After Grow(n), at least n elements can be appended
403
// to the slice without another allocation. If n is negative or too large to
404
// allocate the memory, Grow panics.
405
func Grow[S ~[]E, E any](s S, n int) S {
407
panic("cannot be negative")
409
if n -= cap(s) - len(s); n > 0 {
410
// TODO(https://go.dev/issue/53888): Make using []E instead of S
411
// to workaround a compiler bug where the runtime.growslice optimization
412
// does not take effect. Revert when the compiler is fixed.
413
s = append([]E(s)[:cap(s)], make([]E, n)...)[:len(s)]
418
// Clip removes unused capacity from the slice, returning s[:len(s):len(s)].
419
func Clip[S ~[]E, E any](s S) S {
420
return s[:len(s):len(s)]
423
// Rotation algorithm explanation:
430
// swap first 2 and last 2
434
// recursively rotate first left part by 2
444
// swap first 2 and last 2
448
// recursively rotate second part left by 6
453
// TODO: There are other rotate algorithms.
454
// This algorithm has the desirable property that it moves each element exactly twice.
455
// The triple-reverse algorithm is simpler and more cache friendly, but takes more writes.
456
// The follow-cycles algorithm can be 1-write but it is not very cache friendly.
458
// rotateLeft rotates b left by n spaces.
459
// s_final[i] = s_orig[i+r], wrapping around.
460
func rotateLeft[E any](s []E, r int) {
461
for r != 0 && r != len(s) {
463
swap(s[:r], s[len(s)-r:])
466
swap(s[:len(s)-r], s[r:])
467
s, r = s[len(s)-r:], r*2-len(s)
471
func rotateRight[E any](s []E, r int) {
472
rotateLeft(s, len(s)-r)
475
// swap swaps the contents of x and y. x and y must be equal length and disjoint.
476
func swap[E any](x, y []E) {
477
for i := 0; i < len(x); i++ {
478
x[i], y[i] = y[i], x[i]
482
// overlaps reports whether the memory ranges a[0:len(a)] and b[0:len(b)] overlap.
483
func overlaps[E any](a, b []E) bool {
484
if len(a) == 0 || len(b) == 0 {
487
elemSize := unsafe.Sizeof(a[0])
491
// TODO: use a runtime/unsafe facility once one becomes available. See issue 12445.
492
// Also see crypto/internal/alias/alias.go:AnyOverlap
493
return uintptr(unsafe.Pointer(&a[0])) <= uintptr(unsafe.Pointer(&b[len(b)-1]))+(elemSize-1) &&
494
uintptr(unsafe.Pointer(&b[0])) <= uintptr(unsafe.Pointer(&a[len(a)-1]))+(elemSize-1)
497
// startIdx returns the index in haystack where the needle starts.
498
// prerequisite: the needle must be aliased entirely inside the haystack.
499
func startIdx[E any](haystack, needle []E) int {
501
for i := range haystack {
502
if p == &haystack[i] {
506
// TODO: what if the overlap is by a non-integral number of Es?
507
panic("needle not found")
510
// Reverse reverses the elements of the slice in place.
511
func Reverse[S ~[]E, E any](s S) {
512
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
513
s[i], s[j] = s[j], s[i]