slices

package
v0.2.0 Latest Latest
Warning

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

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

Documentation

Overview

Package slices defines various types and functions useful with slices of any type. Unless otherwise specified, these functions all apply to the elements of a slice at index 0 <= i < len(s).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clip

func Clip[S ~[]E, E any](s S) S

Clip removes unused capacity from the slice, returning s[:len(s):len(s)].

func Clone

func Clone[S ~[]E, E any](s S) S

Clone returns a copy of the slice. The elements are copied using assignment, so this is a shallow clone.

func Compact

func Compact[S ~[]E, E comparable](s S) S

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; it does not create a new slice. When Compact discards m elements in total, it might not modify the elements s[len(s)-m:len(s)]. If those elements is pointers or contain pointers, Compact zeroing those elements so that objects they reference can be garbage collected.

func CompactFunc

func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S

CompactFunc is like Compact but uses a comparison function.

func Compare

func Compare[E constraints.Ordered](s1, s2 []E) int

Compare compares the elements of s1 and s2. 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. Comparisons involving floating point NaNs are ignored.

func CompareFunc

func CompareFunc[E1, E2 any](s1 []E1, s2 []E2, cmp func(E1, E2) int) int

CompareFunc is like Compare but uses a comparison function on each pair of elements. The elements are compared in increasing index order, and the comparisons stop after the first time cmp returns non-zero. 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 Contains

func Contains[E comparable](s []E, v E) bool

Contains reports whether v is present in s.

func ContainsFunc

func ContainsFunc[E any](s []E, f func(E) bool) bool

ContainsFunc reports whether at least one element e of s satisfies f(e).

func Count

func Count[S ~[]E, E comparable](s S, v E) int

Count returns the number of elements in the slices s that equals to v.

func CountFunc

func CountFunc[S ~[]E, E any](s S, eq func(E) bool) int

CountFunc is like Count but uses a comparison function.

func DeepClone

func DeepClone[S ~[]E, E any](s S) S

DeepClone is like Clone but if elements has an Clone method of the form "Clone() E", it use the result of e.Clone() as assignment right operand.

func Delete

func Delete[S ~[]E, E any](s S, i, j int) S

Delete removes the elements s[i:j] from s, returning the modified slice. Delete panics if s[i:j] is not a valid slice of s. Delete modifies the contents of the slice s; it does not create a new slice. Delete is O(len(s)-j), 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 might not modify the elements s[len(s)-(j-i):len(s)]. If those elements is pointers or contain pointers, Delete zeroing those elements so that objects they reference can be garbage collected.

func Equal

func Equal[E comparable](s1, s2 []E) bool

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. nil slices and empty non-nil slices are considered equal. Floating point NaNs are not considered equal.

func EqualFunc

func EqualFunc[E1, E2 any](s1 []E1, s2 []E2, eq func(E1, E2) bool) bool

EqualFunc is like Equal but using a comparison function on each pair of elements.

func Fill

func Fill[S ~[]E, E any](s S, init E) S

Fill fills elements of the slice s with initial value. The elements are copied using assignment, so this is a shallow copy.

func FillFunc

func FillFunc[S ~[]E, E any](s S, f func(int) E) S

FillFunc is like Fill but uses the function f. The function f is invoked with index as argument.

func Filter

func Filter[S ~[]E, E any](s S, f func(int, E) bool) S

Filter returns a new slice of elements satisfies f(i, v).

func ForEach

func ForEach[S ~[]E, E any](s S, f func(int, E))

ForEach applies function f to each element of the slice s in order.

func Grow

func Grow[S ~[]E, E any](s S, n int) S

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 Index

func Index[E comparable](s []E, v E) int

Index returns the index of the first occurrence of v in s, or -1 if not present.

func IndexFunc

func IndexFunc[E any](s []E, f func(E) bool) int

IndexFunc returns the first index i satisfying f(s[i]), or -1 if none do.

func Insert

func Insert[S ~[]E, E any](s S, i int, v ...E) S

Insert inserts the values v... into s at index i, returning the modified slice. In the returned slice r, r[i] == v[0]. Insert panics if i is out of range. This function is O(len(s) + len(v)).

func Map

func Map[S ~[]E1, E1, E2 any](s S, f func(int, E1) E2) []E2

Map manipulates a slice and transforms it to a slice of another type.

func Max

func Max[E constraints.Ordered](s ...E) E

Max returns the maximum element of the slice s, or panics if s is empty.

func Min

func Min[E constraints.Ordered](s ...E) E

Min returns the minimum element of the slices s, or panics if s is empty.

func PFilter

func PFilter[S ~[]E, E any](s S, f func(int, E) bool) S

PFilter returns a new slice of elements satisfies f(i, v). f is call in a goroutine. Result may not keep the original order.

func PForEach

func PForEach[S ~[]E, E any](s S, f func(int, E))

PForEach applies function f to each element of the slice s in concurrency. f is call in a goroutine.

func PMap

func PMap[S ~[]E1, E1, E2 any](s S, f func(int, E1) E2) []E2

PMap manipulates a slice and transforms it to a slice of another type. f is call in a goroutine. Result keep the same order.

func Reduce

func Reduce[S ~[]E1, E1, E2 any](s S, f func(E2, int, E1) E2, init E2) E2

Reduce reduces the slice s to a signle value using a reduction function and a initial value.

func Repeat

func Repeat[E any](init E, count int) []E

Repeat returns a slice with count copies of initial value. It return nil slice if count is zero.

func RepeatFunc

func RepeatFunc[E any](f func(int) E, count int) []E

RepeatFunc is like Repeat but uses function f. The function f is invoked with index as argument.

func Replace

func Replace[S ~[]E, E any](s S, i, j int, v ...E) S

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.

func Reverse

func Reverse[S ~[]E, E any](s S) S

Reverse reverses the slice s so that the first element becomes the last, the second element becomes the second to last, and so on. Reverse modifies the contents of the slice s; it does not create a new slice.

func Shuffle

func Shuffle[S ~[]E, E any](s S) S

Shuffle returns a slice of shuffled elements of the slice s. Shuffle modifies the contents of the slice s; it does not create a new slice.

func SliceOf

func SliceOf[E any](vs ...E) []E

SliceOf returns a slice which contains the element vs. If returns nil if len(vs) == 0.

Types

type ComparableSlice

type ComparableSlice[E comparable] []E

ComparableSlice is like Slice but element type requires comparable.

func NewComparableSlice

func NewComparableSlice[E comparable](s []E) *ComparableSlice[E]

NewComparableSlice creates and initializes a new ComparableSlice using s as its initial contents. The new ComparableSlice takes ownership of s, and the caller should not use s after this call.

func (*ComparableSlice[E]) Append

func (s *ComparableSlice[E]) Append(x ...E)

Append appends the values x to s and updates the slice s.

func (*ComparableSlice[E]) AppendSlice

func (s *ComparableSlice[E]) AppendSlice(t []E)

AppendSlice appends a slice t to s and updates the slice s.

func (ComparableSlice[E]) Cap

func (s ComparableSlice[E]) Cap() int

Cap returns the capacity of s.

func (*ComparableSlice[E]) Clip

func (s *ComparableSlice[E]) Clip()

Clip removes unused capacity from the slice, updating s to s[:len(s):len(s)].

func (ComparableSlice[E]) Clone

func (s ComparableSlice[E]) Clone() *ComparableSlice[E]

Clone returns a copy of the slice. The elements are copied using assignment, so this is a shallow clone.

func (*ComparableSlice[E]) Compact

func (s *ComparableSlice[E]) Compact()

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; it does not create a new slice. When Compact discards m elements in total, it might not modify the elements s[len(s)-m:len(s)]. If those elements is pointers or contain pointers, Compact zeroing those elements so that objects they reference can be garbage collected.

func (*ComparableSlice[E]) CompactFunc

func (s *ComparableSlice[E]) CompactFunc(eq func(E, E) bool)

CompactFunc is like Compact but uses a comparison function.

func (ComparableSlice[E]) CompareFunc

func (s ComparableSlice[E]) CompareFunc(t []E, cmp func(E, E) int) int

CompareFunc is like Compare but uses a comparison function on each pair of elements. The elements are compared in increasing index order, and the comparisons stop after the first time cmp returns non-zero. 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 (ComparableSlice[E]) Contains

func (s ComparableSlice[E]) Contains(v E) bool

Contains reports whether v is present in s.

func (ComparableSlice[E]) ContainsFunc

func (s ComparableSlice[E]) ContainsFunc(f func(E) bool) bool

ContainsFunc reports whether at least one element e of s satisfies f(e).

func (ComparableSlice[E]) Count

func (s ComparableSlice[E]) Count(v E) int

Count returns the result applying Count to the receiver and v.

func (ComparableSlice[E]) CountFunc

func (s ComparableSlice[E]) CountFunc(eq func(E) bool) int

CountFunc returns the result applying CountFunc to the receiver and eq.

func (ComparableSlice[E]) DeepClone

func (s ComparableSlice[E]) DeepClone() *ComparableSlice[E]

DeepClone is like Clone but if elements has an Clone method of the form "Clone() E", it use the result of e.Clone() as assignment right operand.

func (*ComparableSlice[E]) Delete

func (s *ComparableSlice[E]) Delete(i, j int)

Delete removes the elements s[i:j] from s, updating the slice s. Delete panics if s[i:j] is not a valid slice of s. Delete modifies the contents of the slice s; it does not create a new slice. Delete is O(len(s)-j), 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 might not modify the elements s[len(s)-(j-i):len(s)]. If those elements is pointers or contain pointers, Delete zeroing those elements so that objects they reference can be garbage collected.

func (ComparableSlice[E]) Equal

func (s ComparableSlice[E]) Equal(t []E) bool

Equal reports whether s equal to t: 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 (ComparableSlice[E]) EqualFunc

func (s ComparableSlice[E]) EqualFunc(t []E, eq func(E, E) bool) bool

EqualFunc reports whether s equal to t using a comparison 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 (ComparableSlice[E]) Fill

func (s ComparableSlice[E]) Fill(init E) ComparableSlice[E]

Fill returns the result applying Fill to the receiver and init.

func (ComparableSlice[E]) FillFunc

func (s ComparableSlice[E]) FillFunc(f func(int) E) ComparableSlice[E]

FillFunc returns the result applying FillFunc to the receiver and f.

func (ComparableSlice[E]) Filter

func (s ComparableSlice[E]) Filter(f func(int, E) bool) ComparableSlice[E]

Filter returns the result of applying Filter to the receiver and f.

func (ComparableSlice[E]) ForEach

func (s ComparableSlice[E]) ForEach(f func(int, E))

ForEach applies ForEach to the receiver and f.

func (*ComparableSlice[E]) Grow

func (s *ComparableSlice[E]) Grow(n int)

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 (ComparableSlice[E]) Index

func (s ComparableSlice[E]) Index(v E) int

Index returns the index of the first occurrence of v in s, or -1 if not present.

func (ComparableSlice[E]) IndexFunc

func (s ComparableSlice[E]) IndexFunc(f func(E) bool) int

IndexFunc returns the first index i satisfying f(s[i]), or -1 if none do.

func (*ComparableSlice[E]) Insert

func (s *ComparableSlice[E]) Insert(i int, v ...E)

Insert inserts the values v... into s at index i, updating the slice s. In the updated slice s, s[i] == v[0]. Insert panics if i is out of range. This function is O(len(s) + len(v)).

func (ComparableSlice[E]) IsNil

func (s ComparableSlice[E]) IsNil() bool

IsNil reports whether s is nil.

func (ComparableSlice[E]) Len

func (s ComparableSlice[E]) Len() int

Len returns the number of elements in s.

func (ComparableSlice[E]) PFilter

func (s ComparableSlice[E]) PFilter(f func(int, E) bool) ComparableSlice[E]

PFilter returns the result of applying PFilter to the receiver and f.

func (ComparableSlice[E]) PForEach

func (s ComparableSlice[E]) PForEach(f func(int, E))

PForEach applies PForEach to the receiver and f.

func (*ComparableSlice[E]) Replace

func (s *ComparableSlice[E]) Replace(i, j int, v ...E)

Replace replaces the elements s[i:j] by the given v, and updates the slice s. Replace panics if s[i:j] is not a valid slice of s.

func (ComparableSlice[E]) Reverse

func (s ComparableSlice[E]) Reverse() ComparableSlice[E]

Reverse returns the result of applying Reverse to the receiver.

func (ComparableSlice[E]) Shuffle

func (s ComparableSlice[E]) Shuffle() ComparableSlice[E]

Shuffle returns the result of applying Shuffle to the receiver.

func (ComparableSlice[E]) Slice

func (s ComparableSlice[E]) Slice(i, j int) ComparableSlice[E]

Slice returns s[i:j]. It panics if s[i:j] is not a valid slice of s.

func (ComparableSlice[E]) Slice3

func (s ComparableSlice[E]) Slice3(i, j, k int) ComparableSlice[E]

Slice3 is the 3-index form of the slice operation: it returns s[i:j:k]. It panics if s[i:j:k] is not a valid slice of s.

type Slice

type Slice[E any] []E

Slice attaches common methods to []E.

func NewSlice

func NewSlice[E any](s []E) *Slice[E]

NewSlice creates and initializes a new Slice using s as its initial contents. The new Slice takes ownership of s, and the caller should not use s after this call.

func (*Slice[E]) Append

func (s *Slice[E]) Append(x ...E)

Append appends the values x to s and updates the slice s.

func (*Slice[E]) AppendSlice

func (s *Slice[E]) AppendSlice(t []E)

AppendSlice appends a slice t to s and updates the slice s.

func (Slice[E]) Cap

func (s Slice[E]) Cap() int

Cap returns the capacity of s.

func (*Slice[E]) Clip

func (s *Slice[E]) Clip()

Clip removes unused capacity from the slice, updating s tp s[:len(s):len(s)].

func (Slice[E]) Clone

func (s Slice[E]) Clone() *Slice[E]

Clone returns a copy of the slice. The elements are copied using assignment, so this is a shallow clone.

func (*Slice[E]) CompactFunc

func (s *Slice[E]) CompactFunc(eq func(E, E) bool)

CompactFunc is like Compact but uses a comparison function.

func (Slice[E]) CompareFunc

func (s Slice[E]) CompareFunc(t []E, cmp func(E, E) int) int

CompareFunc compares the elements of s and t using a comparison function on each pair of elements. The elements are compared in increasing index order, and the comparisons stop after the first time cmp returns non-zero. 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 (Slice[E]) ContainsFunc

func (s Slice[E]) ContainsFunc(f func(E) bool) bool

ContainsFunc reports whether at least one element e of s satisfies f(e).

func (Slice[E]) CountFunc

func (s Slice[E]) CountFunc(eq func(E) bool) int

CountFunc returns the result applying CountFunc to the receiver and eq.

func (Slice[E]) DeepClone

func (s Slice[E]) DeepClone() *Slice[E]

DeepClone is like Clone but if elements has an Clone method of the form "Clone() E", it use the result of e.Clone() as assignment right operand.

func (*Slice[E]) Delete

func (s *Slice[E]) Delete(i, j int)

Delete removes the elements s[i:j] from s, updating the slice s. Delete panics if s[i:j] is not a valid slice of s. Delete modifies the contents of the slice s; it does not create a new slice. Delete is O(len(s)-j), 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 might not modify the elements s[len(s)-(j-i):len(s)]. If those elements is pointers or contain pointers, Delete zeroing those elements so that objects they reference can be garbage collected.

func (Slice[E]) EqualFunc

func (s Slice[E]) EqualFunc(t []E, eq func(E, E) bool) bool

EqualFunc reports whether s equal to t using a comparison 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 (Slice[E]) Fill

func (s Slice[E]) Fill(init E) Slice[E]

Fill returns the result applying Fill to the receiver and init.

func (Slice[E]) FillFunc

func (s Slice[E]) FillFunc(f func(int) E) Slice[E]

FillFunc returns the result applying FillFunc to the receiver and f.

func (Slice[E]) Filter

func (s Slice[E]) Filter(f func(int, E) bool) Slice[E]

Filter returns the result of applying Filter to the receiver and f.

func (Slice[E]) ForEach

func (s Slice[E]) ForEach(f func(int, E))

ForEach applies ForEach to the receiver and f.

func (*Slice[E]) Grow

func (s *Slice[E]) Grow(n int)

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 (Slice[E]) IndexFunc

func (s Slice[E]) IndexFunc(f func(E) bool) int

IndexFunc returns the first index i satisfying f(s[i]), or -1 if none do.

func (*Slice[E]) Insert

func (s *Slice[E]) Insert(i int, v ...E)

Insert inserts the values v... into s at index i, updating the slice s. In the updated slice s, s[i] == v[0]. Insert panics if i is out of range. This function is O(len(s) + len(v)).

func (Slice[E]) IsNil

func (s Slice[E]) IsNil() bool

IsNil reports whether s is nil.

func (Slice[E]) Len

func (s Slice[E]) Len() int

Len returns the number of elements in s.

func (Slice[E]) PFilter

func (s Slice[E]) PFilter(f func(int, E) bool) Slice[E]

PFilter returns the result of applying PFilter to the receiver and f.

func (Slice[E]) PForEach

func (s Slice[E]) PForEach(f func(int, E))

PForEach applies PForEach to the receiver and f.

func (*Slice[E]) Replace

func (s *Slice[E]) Replace(i, j int, v ...E)

Replace replaces the elements s[i:j] by the given v, and updates the slice s. Replace panics if s[i:j] is not a valid slice of s.

func (Slice[E]) Reverse

func (s Slice[E]) Reverse() Slice[E]

Reverse returns the result of applying Reverse to the receiver.

func (Slice[E]) Shuffle

func (s Slice[E]) Shuffle() Slice[E]

Shuffle returns the result of applying Shuffle to the receiver.

func (Slice[E]) Slice

func (s Slice[E]) Slice(i, j int) Slice[E]

Slice returns s[i:j]. It panics if s[i:j] is not a valid slice of s.

func (Slice[E]) Slice3

func (s Slice[E]) Slice3(i, j, k int) Slice[E]

Slice3 is the 3-index form of the slice operation: it returns s[i:j:k]. It panics if s[i:j:k] is not a valid slice of s.

Jump to

Keyboard shortcuts

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