2024-01-11 20:37:32 +00:00
|
|
|
// Copyright 2021 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package slices defines various functions useful with slices of any type.
|
|
|
|
package slices
|
|
|
|
|
|
|
|
import (
|
2025-01-10 13:21:32 +00:00
|
|
|
"cmp"
|
|
|
|
"slices"
|
2024-01-11 20:37:32 +00:00
|
|
|
)
|
|
|
|
|
2025-01-10 13:21:32 +00:00
|
|
|
// TODO(adonovan): when https://go.dev/issue/32816 is accepted, all of
|
|
|
|
// these functions should be annotated (provisionally with "//go:fix
|
|
|
|
// inline") so that tools can safely and automatically replace calls
|
|
|
|
// to exp/slices with calls to std slices by inlining them.
|
|
|
|
|
2024-01-11 20:37:32 +00:00
|
|
|
// Equal reports whether two slices are equal: the same length and all
|
|
|
|
// elements equal. If the lengths are different, Equal returns false.
|
|
|
|
// Otherwise, the elements are compared in increasing index order, and the
|
|
|
|
// comparison stops at the first unequal pair.
|
|
|
|
// Floating point NaNs are not considered equal.
|
|
|
|
func Equal[S ~[]E, E comparable](s1, s2 S) bool {
|
2025-01-10 13:21:32 +00:00
|
|
|
return slices.Equal(s1, s2)
|
2024-01-11 20:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EqualFunc reports whether two slices are equal using an equality
|
|
|
|
// function on each pair of elements. If the lengths are different,
|
|
|
|
// EqualFunc returns false. Otherwise, the elements are compared in
|
|
|
|
// increasing index order, and the comparison stops at the first index
|
|
|
|
// for which eq returns false.
|
|
|
|
func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool {
|
2025-01-10 13:21:32 +00:00
|
|
|
return slices.EqualFunc(s1, s2, eq)
|
2024-01-11 20:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compare compares the elements of s1 and s2, using [cmp.Compare] on each pair
|
|
|
|
// of elements. The elements are compared sequentially, starting at index 0,
|
|
|
|
// until one element is not equal to the other.
|
|
|
|
// The result of comparing the first non-matching elements is returned.
|
|
|
|
// If both slices are equal until one of them ends, the shorter slice is
|
|
|
|
// considered less than the longer one.
|
|
|
|
// The result is 0 if s1 == s2, -1 if s1 < s2, and +1 if s1 > s2.
|
2025-01-10 13:21:32 +00:00
|
|
|
func Compare[S ~[]E, E cmp.Ordered](s1, s2 S) int {
|
|
|
|
return slices.Compare(s1, s2)
|
2024-01-11 20:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CompareFunc is like [Compare] but uses a custom comparison function on each
|
|
|
|
// pair of elements.
|
|
|
|
// The result is the first non-zero result of cmp; if cmp always
|
|
|
|
// returns 0 the result is 0 if len(s1) == len(s2), -1 if len(s1) < len(s2),
|
|
|
|
// and +1 if len(s1) > len(s2).
|
|
|
|
func CompareFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, cmp func(E1, E2) int) int {
|
2025-01-10 13:21:32 +00:00
|
|
|
return slices.CompareFunc(s1, s2, cmp)
|
2024-01-11 20:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Index returns the index of the first occurrence of v in s,
|
|
|
|
// or -1 if not present.
|
|
|
|
func Index[S ~[]E, E comparable](s S, v E) int {
|
2025-01-10 13:21:32 +00:00
|
|
|
return slices.Index(s, v)
|
2024-01-11 20:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IndexFunc returns the first index i satisfying f(s[i]),
|
|
|
|
// or -1 if none do.
|
|
|
|
func IndexFunc[S ~[]E, E any](s S, f func(E) bool) int {
|
2025-01-10 13:21:32 +00:00
|
|
|
return slices.IndexFunc(s, f)
|
2024-01-11 20:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Contains reports whether v is present in s.
|
|
|
|
func Contains[S ~[]E, E comparable](s S, v E) bool {
|
2025-01-10 13:21:32 +00:00
|
|
|
return slices.Contains(s, v)
|
2024-01-11 20:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ContainsFunc reports whether at least one
|
|
|
|
// element e of s satisfies f(e).
|
|
|
|
func ContainsFunc[S ~[]E, E any](s S, f func(E) bool) bool {
|
2025-01-10 13:21:32 +00:00
|
|
|
return slices.ContainsFunc(s, f)
|
2024-01-11 20:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert inserts the values v... into s at index i,
|
|
|
|
// returning the modified slice.
|
|
|
|
// The elements at s[i:] are shifted up to make room.
|
|
|
|
// In the returned slice r, r[i] == v[0],
|
|
|
|
// and r[i+len(v)] == value originally at r[i].
|
|
|
|
// Insert panics if i is out of range.
|
|
|
|
// This function is O(len(s) + len(v)).
|
|
|
|
func Insert[S ~[]E, E any](s S, i int, v ...E) S {
|
2025-01-10 13:21:32 +00:00
|
|
|
return slices.Insert(s, i, v...)
|
2024-01-11 20:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes the elements s[i:j] from s, returning the modified slice.
|
2025-01-10 13:21:32 +00:00
|
|
|
// Delete panics if j > len(s) or s[i:j] is not a valid slice of s.
|
|
|
|
// Delete is O(len(s)-i), so if many items must be deleted, it is better to
|
2024-01-11 20:37:32 +00:00
|
|
|
// make a single call deleting them all together than to delete one at a time.
|
2025-01-10 13:21:32 +00:00
|
|
|
// Delete zeroes the elements s[len(s)-(j-i):len(s)].
|
2024-01-11 20:37:32 +00:00
|
|
|
func Delete[S ~[]E, E any](s S, i, j int) S {
|
2025-01-10 13:21:32 +00:00
|
|
|
return slices.Delete(s, i, j)
|
2024-01-11 20:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFunc removes any elements from s for which del returns true,
|
|
|
|
// returning the modified slice.
|
2025-01-10 13:21:32 +00:00
|
|
|
// DeleteFunc zeroes the elements between the new length and the original length.
|
2024-01-11 20:37:32 +00:00
|
|
|
func DeleteFunc[S ~[]E, E any](s S, del func(E) bool) S {
|
2025-01-10 13:21:32 +00:00
|
|
|
return slices.DeleteFunc(s, del)
|
2024-01-11 20:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace replaces the elements s[i:j] by the given v, and returns the
|
|
|
|
// modified slice. Replace panics if s[i:j] is not a valid slice of s.
|
2025-01-10 13:21:32 +00:00
|
|
|
// When len(v) < (j-i), Replace zeroes the elements between the new length and the original length.
|
2024-01-11 20:37:32 +00:00
|
|
|
func Replace[S ~[]E, E any](s S, i, j int, v ...E) S {
|
2025-01-10 13:21:32 +00:00
|
|
|
return slices.Replace(s, i, j, v...)
|
2024-01-11 20:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clone returns a copy of the slice.
|
|
|
|
// The elements are copied using assignment, so this is a shallow clone.
|
|
|
|
func Clone[S ~[]E, E any](s S) S {
|
2025-01-10 13:21:32 +00:00
|
|
|
return slices.Clone(s)
|
2024-01-11 20:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compact replaces consecutive runs of equal elements with a single copy.
|
|
|
|
// This is like the uniq command found on Unix.
|
|
|
|
// Compact modifies the contents of the slice s and returns the modified slice,
|
|
|
|
// which may have a smaller length.
|
2025-01-10 13:21:32 +00:00
|
|
|
// Compact zeroes the elements between the new length and the original length.
|
2024-01-11 20:37:32 +00:00
|
|
|
func Compact[S ~[]E, E comparable](s S) S {
|
2025-01-10 13:21:32 +00:00
|
|
|
return slices.Compact(s)
|
2024-01-11 20:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CompactFunc is like [Compact] but uses an equality function to compare elements.
|
|
|
|
// For runs of elements that compare equal, CompactFunc keeps the first one.
|
2025-01-10 13:21:32 +00:00
|
|
|
// CompactFunc zeroes the elements between the new length and the original length.
|
2024-01-11 20:37:32 +00:00
|
|
|
func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S {
|
2025-01-10 13:21:32 +00:00
|
|
|
return slices.CompactFunc(s, eq)
|
2024-01-11 20:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Grow increases the slice's capacity, if necessary, to guarantee space for
|
|
|
|
// another n elements. After Grow(n), at least n elements can be appended
|
|
|
|
// to the slice without another allocation. If n is negative or too large to
|
|
|
|
// allocate the memory, Grow panics.
|
|
|
|
func Grow[S ~[]E, E any](s S, n int) S {
|
2025-01-10 13:21:32 +00:00
|
|
|
return slices.Grow(s, n)
|
2024-01-11 20:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clip removes unused capacity from the slice, returning s[:len(s):len(s)].
|
|
|
|
func Clip[S ~[]E, E any](s S) S {
|
2025-01-10 13:21:32 +00:00
|
|
|
return slices.Clip(s)
|
2024-01-11 20:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reverse reverses the elements of the slice in place.
|
|
|
|
func Reverse[S ~[]E, E any](s S) {
|
2025-01-10 13:21:32 +00:00
|
|
|
slices.Reverse(s)
|
2024-01-11 20:37:32 +00:00
|
|
|
}
|