slices

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package slices provides various functions useful with slices of any type. Based on the slices package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append[T any](a mem.Allocator, s []T, elems ...T) []T

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

func Clone[T any](a mem.Allocator, s []T) []T

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

func Extend[T any](a mem.Allocator, s []T, other []T) []T

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

func Free[T any](a mem.Allocator, s []T)

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 IsSorted

func IsSorted[T gocmp.Ordered](x []T) bool

IsSorted reports whether x is sorted in ascending order.

func IsSortedFunc

func IsSortedFunc[T any](x []T, compare cmp.Func) bool

IsSortedFunc reports whether x is sorted in ascending order, with cmp as the comparison function as defined by SortFunc.

func IsSortedWith

func IsSortedWith(s Sorter) bool

IsSortedWith reports whether the slice is sorted according to the provided Sorter.

func Make

func Make[T any](a mem.Allocator, len int) []T

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

func MakeCap[T any](a mem.Allocator, len int, cap int) []T

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

func Max[T gocmp.Ordered](x []T) T

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

func MaxFunc[T any](x []T, compare cmp.Func) T

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

func Min[T gocmp.Ordered](x []T) T

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

func MinFunc[T any](x []T, compare cmp.Func) T

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 Sort

func Sort[T gocmp.Ordered](x []T)

Sort sorts a slice of any ordered type in ascending order.

func SortFunc

func SortFunc[T any](x []T, compare cmp.Func)

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

func SortStableFunc[T any](x []T, compare cmp.Func)

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.

func SortWith

func SortWith(s Sorter)

SortWith sorts the slice using the provided Sorter.

Types

type Slice

type Slice struct {
	// contains filtered or unexported fields
}

A Slice is a header for a slice of any type.

func Header[T any](s []T) Slice

Header returns the Slice header for a given slice.

type Sorter

type Sorter struct {
	// contains filtered or unexported fields
}

Sorter provides comparison and swapping operations for a slice of any type.

func NewSorter

func NewSorter[T any](s []T, compare cmp.Func) Sorter

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

func (s Sorter) Compare(i, j int) int

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].

func (Sorter) Less

func (s Sorter) Less(i, j int) bool

Less reports whether the element at index i should sort before the element at index j.

func (Sorter) Swap

func (s Sorter) Swap(i, j int)

Swap swaps the elements at indices i and j.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL