Documentation
¶
Overview ¶
Package slices provides various functions useful with slices of any type. Based on the slices package.
Index ¶
- func Append[T any](a mem.Allocator, s []T, elems ...T) []T
- func Clone[T any](a mem.Allocator, s []T) []T
- func Contains[T comparable](s []T, v T) bool
- func Equal[T comparable](s1, s2 []T) bool
- func Extend[T any](a mem.Allocator, s []T, other []T) []T
- func Free[T any](a mem.Allocator, s []T)
- func Index[T comparable](s []T, v T) int
- func IsSorted[T gocmp.Ordered](x []T) bool
- func IsSortedFunc[T any](x []T, compare cmp.Func) bool
- func IsSortedWith(s Sorter) bool
- func Make[T any](a mem.Allocator, len int) []T
- func MakeCap[T any](a mem.Allocator, len int, cap int) []T
- func Max[T gocmp.Ordered](x []T) T
- func MaxFunc[T any](x []T, compare cmp.Func) T
- func Min[T gocmp.Ordered](x []T) T
- func MinFunc[T any](x []T, compare cmp.Func) T
- func Sort[T gocmp.Ordered](x []T)
- func SortFunc[T any](x []T, compare cmp.Func)
- func SortStableFunc[T any](x []T, compare cmp.Func)
- func SortStableWith(s Sorter)
- func SortWith(s Sorter)
- type Slice
- type Sorter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Append ¶
Append appends elements to a heap-allocated slice, growing it if needed. If the allocator is nil, uses the system allocator. Returns an updated allocated slice; the caller owns it.
func Clone ¶
Clone returns a shallow copy of the slice. If the allocator is nil, uses the system allocator. The returned slice is allocated; the caller owns it.
func Contains ¶
func Contains[T comparable](s []T, v T) bool
Contains reports whether v is present in s.
func Equal ¶
func Equal[T comparable](s1, s2 []T) bool
Equal reports whether two slices are equal: the same length and all elements equal. Empty and nil slices are considered equal.
func Extend ¶
Extend appends all elements from another heap-allocated slice, growing if needed. If the allocator is nil, uses the system allocator. Returns an updated allocated slice; the caller owns it.
func Free ¶
Free frees a previously allocated slice. If the allocator is nil, uses the system allocator.
func Index ¶
func Index[T comparable](s []T, v T) int
Index returns the index of the first occurrence of v in s, or -1 if not present.
func IsSortedFunc ¶
IsSortedFunc reports whether x is sorted in ascending order, with cmp as the comparison function as defined by SortFunc.
func IsSortedWith ¶
IsSortedWith reports whether the slice is sorted according to the provided Sorter.
func Make ¶
Make allocates a slice of type T with given length using allocator a. If the allocator is nil, uses the system allocator. The returned slice is allocated; the caller owns it.
func MakeCap ¶
MakeCap allocates a slice of type T with given length and capacity using allocator a. If the allocator is nil, uses the system allocator. The returned slice is allocated; the caller owns it.
func Max ¶
Max returns the maximal value in x. It panics if x is empty. For floating-point E, Max propagates NaNs (any NaN value in x forces the output to be NaN).
func MaxFunc ¶
MaxFunc returns the maximal value in x, using cmp to compare elements. It panics if x is empty. If there is more than one maximal element according to the cmp function, MaxFunc returns the first one.
func Min ¶
Min returns the minimal value in x. It panics if x is empty. For floating-point numbers, Min propagates NaNs (any NaN value in x forces the output to be NaN).
func MinFunc ¶
MinFunc returns the minimal value in x, using cmp to compare elements. It panics if x is empty. If there is more than one minimal element according to the cmp function, MinFunc returns the first one.
func SortFunc ¶
SortFunc sorts the slice x in ascending order as determined by the cmp function. This sort is not guaranteed to be stable. cmp(a, b) should return a negative number when a < b, a positive number when a > b and zero when a == b or a and b are incomparable in the sense of a strict weak ordering.
SortFunc requires that cmp is a strict weak ordering. See https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings. The function should return 0 for incomparable items.
func SortStableFunc ¶
SortStableFunc sorts the slice x while keeping the original order of equal elements, using cmp to compare elements in the same way as SortFunc.
func SortStableWith ¶
func SortStableWith(s Sorter)
SortStableWith sorts the slice using the provided Sorter while keeping the original order of equal elements.
Types ¶
type Slice ¶
type Slice struct {
// contains filtered or unexported fields
}
A Slice is a header for a slice of any type.
type Sorter ¶
type Sorter struct {
// contains filtered or unexported fields
}
Sorter provides comparison and swapping operations for a slice of any type.
func NewSorter ¶
NewSorter creates a Sorter for a given slice with a custom compare function. If compare is nil, compares by raw byte value (memcmp).
func (Sorter) Compare ¶
Compare compares the elements at indices i and j. Returns a negative value if s[i] < s[j], zero if they are equal, and a positive value if s[i] > s[j].