Source File
slices.go
Belonging Package
golang.org/x/exp/slices
// 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 slicesimport ()// 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.////go:fix inlinefunc [ ~[], comparable](, ) bool {return slices.Equal(, )}// 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.////go:fix inlinefunc [ ~[], ~[], , any]( , , func(, ) bool) bool {return slices.EqualFunc(, , )}// 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.////go:fix inlinefunc [ ~[], cmp.Ordered](, ) int {return slices.Compare(, )}// 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).////go:fix inlinefunc [ ~[], ~[], , any]( , , func(, ) int) int {return slices.CompareFunc(, , )}// Index returns the index of the first occurrence of v in s,// or -1 if not present.////go:fix inlinefunc [ ~[], comparable]( , ) int {return slices.Index(, )}// IndexFunc returns the first index i satisfying f(s[i]),// or -1 if none do.////go:fix inlinefunc [ ~[], any]( , func() bool) int {return slices.IndexFunc(, )}// Contains reports whether v is present in s.////go:fix inlinefunc [ ~[], comparable]( , ) bool {return slices.Contains(, )}// ContainsFunc reports whether at least one// element e of s satisfies f(e).////go:fix inlinefunc [ ~[], any]( , func() bool) bool {return slices.ContainsFunc(, )}// 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)).////go:fix inlinefunc [ ~[], any]( , int, ...) {return slices.Insert(, , ...)}// Delete removes the elements s[i:j] from s, returning the modified slice.// 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// make a single call deleting them all together than to delete one at a time.// Delete zeroes the elements s[len(s)-(j-i):len(s)].////go:fix inlinefunc [ ~[], any]( , , int) {return slices.Delete(, , )}// DeleteFunc removes any elements from s for which del returns true,// returning the modified slice.// DeleteFunc zeroes the elements between the new length and the original length.////go:fix inlinefunc [ ~[], any]( , func() bool) {return slices.DeleteFunc(, )}// 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.// When len(v) < (j-i), Replace zeroes the elements between the new length and the original length.////go:fix inlinefunc [ ~[], any]( , , int, ...) {return slices.Replace(, , , ...)}// Clone returns a copy of the slice.// The elements are copied using assignment, so this is a shallow clone.////go:fix inlinefunc [ ~[], any]( ) {return slices.Clone()}// 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.// Compact zeroes the elements between the new length and the original length.////go:fix inlinefunc [ ~[], comparable]( ) {return slices.Compact()}// CompactFunc is like [Compact] but uses an equality function to compare elements.// For runs of elements that compare equal, CompactFunc keeps the first one.// CompactFunc zeroes the elements between the new length and the original length.////go:fix inlinefunc [ ~[], any]( , func(, ) bool) {return slices.CompactFunc(, )}// 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.////go:fix inlinefunc [ ~[], any]( , int) {return slices.Grow(, )}// Clip removes unused capacity from the slice, returning s[:len(s):len(s)].////go:fix inlinefunc [ ~[], any]( ) {return slices.Clip()}// Reverse reverses the elements of the slice in place.////go:fix inlinefunc [ ~[], any]( ) {slices.Reverse()}
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |