slices

package
v1.2.115 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2024 License: MIT Imports: 8 Imported by: 4

Documentation

Overview

Package slices defines various functions useful with slices of any type.

Example (MaxHeap)

This example inserts several ints into an MaxHeap, checks the maximum, and removes them in order of priority.

package main

import (
	"container/heap"
	"fmt"

	"github.com/searKing/golang/go/exp/slices"
)

func main() {
	h := &slices.MaxHeap[int]{2, 1, 5}
	heap.Init(h)
	heap.Push(h, 3)
	fmt.Printf("maximum: %d\n", (*h)[0])
	for h.Len() > 0 {
		fmt.Printf("%d ", heap.Pop(h))
	}

}
Output:

maximum: 5
5 3 2 1
Example (MinHeap)

This example inserts several ints into an MinHeap, checks the minimum, and removes them in order of priority.

package main

import (
	"container/heap"
	"fmt"

	"github.com/searKing/golang/go/exp/slices"
)

func main() {
	h := &slices.MinHeap[int]{2, 1, 5}
	heap.Init(h)
	heap.Push(h, 3)
	fmt.Printf("minimum: %d\n", (*h)[0])
	for h.Len() > 0 {
		fmt.Printf("%d ", heap.Pop(h))
	}

}
Output:

minimum: 1
1 2 3 5

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func And added in v1.2.13

func And[E comparable](s ...E) bool

And tests whether the slice satisfying c != zero within all c in the slice. return true if len(s) == 0

func AndFunc added in v1.2.13

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

AndFunc tests the slice satisfying f(c), or true if none do. return true if len(s) == 0

func Contains added in v1.2.38

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

Contains reports whether v is present in s. Deprecated: Use slices.Contains instead since go1.21.

func ContainsFunc added in v1.2.38

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

ContainsFunc reports whether v satisfying f(s[i]) is present in s. Deprecated: Use slices.ContainsFunc instead since go1.21.

func Filter added in v1.2.16

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

Filter returns a slice satisfying c != zero within all c in the slice. Filter modifies the contents of the slice s; it does not create a new slice.

func FilterFunc added in v1.2.16

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

FilterFunc returns a slice satisfying f(c) within all c in the slice. FilterFunc modifies the contents of the slice s; it does not create a new slice.

func FirstFunc added in v1.2.11

func FirstFunc[S ~[]E, E any](s S, f func(E) bool) (e E, ok bool)

FirstFunc returns the first item from the slice satisfying f(c), or zero if none do.

func FirstOrZero added in v1.2.11

func FirstOrZero[E comparable](s ...E) E

FirstOrZero returns the first non-zero item from the slice, or zero if none do.

func FirstOrZeroFunc added in v1.2.11

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

FirstOrZeroFunc returns the first item from the slice satisfying f(c), or zero if none do.

func FromSwigVector added in v1.2.107

func FromSwigVector[S ~[]E, E any](v SwigVector[E]) S

FromSwigVector returns a slice mapped by v.Get() within all c in the SwigVector[E]. FromSwigVector does not modify the contents of the SwigVector[E] v; it creates a new slice.

func FromSwigVectorFunc added in v1.2.107

func FromSwigVectorFunc[S ~[]R, E any, R any](v SwigVector[E], f func(E) R) S

FromSwigVectorFunc returns a slice mapped by mapped by f(v.Get()) within all c in the SwigVector[E]. FromSwigVector does not modify the contents of the SwigVector[E] v; it creates a new slice.

func Group added in v1.2.65

func Group[S ~[]E, M map[E]N, E comparable, N int](s S) M

Group returns a map group by all elements in s that have the same values.

If s is nil, Group returns nil (zero map).

If s is empty, Group returns an empty map.

Else, Group returns a map group by all elements in s that have the same values. TODO: accept [S ~[]E, M ~map[E]N, E comparable, N constraints.Number] if go support template type deduction

func GroupFunc added in v1.2.65

func GroupFunc[S ~[]E, M map[K]S, E any, K comparable](s S, f func(E) K) M

GroupFunc returns a map satisfying f(c) within all c in the map group by all elements in s that have the same values.

If s is nil, GroupFunc returns nil (zero map).

If s and f are both empty or nil, GroupFunc returns an empty map.

Else, GroupFunc returns a map satisfying f(c) within all c in the map group by all elements in s that have the same values. TODO: accept [S ~[]E, M ~map[K]S, E any, K comparable] if go support template type deduction

func Intersect added in v1.2.38

func Intersect[S ~[]E, E comparable](s1, s2 S) S

Intersect returns a slice satisfying c != zero within all c in the slice. Intersect does not modify the contents of the slice s1 and s2; it creates a new slice.

func IntersectFunc added in v1.2.38

func IntersectFunc[S ~[]E, E any](s1, s2 S, f func(v1, v2 E) bool) S

IntersectFunc returns a slice satisfying f(c) within all c in the slice. IntersectFunc does not modify the contents of the slice s1 and s2; it creates a new slice.

func IsPartialSorted added in v1.2.74

func IsPartialSorted[S ~[]E, E constraints.Ordered](s S, k int) bool

IsPartialSorted reports whether data[:k] is partial sorted, as top k of data[:].

func LinearSearch

func LinearSearch[S ~[]E, E constraints.Ordered](x S, target E) (int, bool)

LinearSearch searches for target in a sorted slice and returns the position where target is found, or the position where target would appear in the sort order; it also returns a bool saying whether the target is really found in the slice. The slice must be sorted in increasing order. Note: Binary-search was compared using the benchmarks. The following code is equivalent to the linear search above:

pos := sort.Search(len(x), func(i int) bool {
    return target < x[i]
})

The binary search wins for very large boundary sets, but the linear search performs better up through arrays between 256 and 512 elements, so we continue to prefer linear search.

func LinearSearchFunc

func LinearSearchFunc[S ~[]E, E any](x S, target E, cmp func(E, E) int) (int, bool)

LinearSearchFunc works like LinearSearch, but uses a custom comparison function. The slice must be sorted in increasing order, where "increasing" is defined by cmp. cmp(a, b) is expected to return an integer comparing the two parameters: 0 if a == b, a negative number if a < b and a positive number if a > b.

func Map added in v1.2.51

func Map[S ~[]E, E any, R []M, M string](s S) R

Map returns a slice mapped by format "%v" within all c in the slice. Map does not modify the contents of the slice s; it creates a new slice. TODO: accept [S ~[]E, E any, R ~[]M, M ~string] if go support template type deduction

func MapFunc added in v1.2.51

func MapFunc[S ~[]E, E any, R []M, M any](s S, f func(E) M) R

MapFunc returns a slice mapped by f(c) within all c in the slice. MapFunc does not modify the contents of the slice s; it creates a new slice. TODO: accept [S ~[]E, E any, R ~[]M, M any] if go support template type deduction

func MapIndexFunc added in v1.2.76

func MapIndexFunc[S ~[]E, E any, R []M, M any](s S, f func(i int, e E) M) R

MapIndexFunc works like MapFunc, returns a slice mapped by f(i, c) within all c in the slice. MapIndexFunc does not modify the contents of the slice s; it creates a new slice. TODO: accept [S ~[]E, E any, R ~[]M, M any] if go support template type deduction

func Or added in v1.2.13

func Or[E comparable](s ...E) bool

Or tests whether the slice satisfying c != zero within any c in the slice. return true if len(s) == 0

func OrFunc added in v1.2.13

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

OrFunc tests the slice satisfying f(c), or false if none do. return true if len(s) == 0

func PartialSort added in v1.2.74

func PartialSort[S ~[]E, E constraints.Ordered](s S, k int)

PartialSort rearranges elements such that the range [0, m) contains the sorted m smallest elements in the range [first, data.Len). The order of equal elements is not guaranteed to be preserved. The order of the remaining elements in the range [m, data.Len) is unspecified.

The sort is not guaranteed to be stable: equal elements may be reversed from their original order.

PartialSort modifies the contents of the slice s; it does not create a new slice.

func PartialSortFunc added in v1.2.74

func PartialSortFunc[S ~[]E, E any](s S, k int, cmp func(E, E) int)

PartialSortFunc works like PartialSort, but uses a custom comparison function. The slice must be sorted in increasing order, where "increasing" is defined by cmp. cmp(a, b) is expected to return an integer comparing the two parameters: 0 if a == b, a negative number if a < b and a positive number if a > b.

func Reverse added in v1.2.7

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

Reverse reorder a slice of any ordered type in reverse order. Reverse modifies the contents of the slice s; it does not create a new slice. Deprecated: Use slices.Reverse instead since go1.21.

func SearchMax added in v1.2.76

func SearchMax[S ~[]E, E constraints.Ordered](s S) int

SearchMax uses liner search to find and return the biggest index i in [0, n) at which f(i) is min(f...), assuming that on the range [0, n), Search returns the first true index. If there is no such index, Search returns n. (Note that the "not found" return value is not -1 as in, for instance, strings.Index.) Search calls f(i) only for i in the range [0, n).

func SearchMin added in v1.2.76

func SearchMin[S ~[]E, E constraints.Ordered](s S) int

SearchMin uses liner search to find and return the smallest index i in [0, n) at which f(i) is min(f...), assuming that on the range [0, n), Search returns the first true index. If there is no such index, Search returns n. (Note that the "not found" return value is not -1 as in, for instance, strings.Index.) SearchMin calls f(i) only for i in the range [0, n).

func SearchMinFunc added in v1.2.76

func SearchMinFunc[S ~[]E, E any](s S, cmp func(E, E) int) int

SearchMinFunc works like SearchMin, but uses a custom comparison function. The slice is sorted in any order, where "increasing" is defined by cmp. cmp(a, b) is expected to return an integer comparing the two parameters: 0 if a == b, a negative number if a < b and a positive number if a > b.

func Split added in v1.2.31

func Split[S ~[]E, E any](s S, sep int) []S

Split slices s into all subslices separated by sep and returns a slice of the subslices between those separators.

If s is less than sep and sep is more than zero, Split returns a slice of length 1 whose only element is s.

If s is nil, Split returns nil (zero subslices).

If both s and sep are empty or zero, Split returns an empty slice.

If sep is <= zero, Split splits after each element, as chunk size is 1.

It is equivalent to SplitN with a count of -1.

func SplitMap added in v1.2.105

func SplitMap[M ~map[K]V, S ~[]E, K comparable, V any, E any](s S) M

SplitMap slices s into all key-value pairs and returns a map of the key-value pairs.

If s is nil, SplitMap returns nil (zero map).

If len(s) is odd, it treats args[len(args)-1] as a value with a missing value.

func SplitN

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

SplitN slices s into subslices and returns a slice of the subslices.

The count determines the number of subslices to return:

  n > 0: at most n subslices; the last subslices will be the unsplit remainder.
		The count determines the number of subslices to return:
  		sep > 0: Split splits every sep as chunk size; the last subslices will be the unsplit remainder.
  		sep <= 0: take len(S)/n as chunk size
  n == 0: the result is nil (zero subslices)
  n < 0: all subslices as n == len(s)

Edge cases for s and sep (for example, zero) are handled as described in the documentation for Split.

func ToSwigVector added in v1.2.107

func ToSwigVector[S ~[]E, E any](s S, v SwigVector[E])

ToSwigVector returns a SwigVector[E] mapped by v.Get() within all c in the slice. ToSwigVector does not modify the contents of the slice s; it modifies the SwigVector[E] v.

func ToSwigVectorFunc added in v1.2.107

func ToSwigVectorFunc[S ~[]E, E any, R any](s S, v SwigVector[R], f func(E) R)

ToSwigVectorFunc returns a SwigVector[E] mapped by f(c) within all c in the slice. ToSwigVectorFunc does not modify the contents of the slice s; it modifies the SwigVector[E] v.

func TypeAssertFilter added in v1.2.58

func TypeAssertFilter[S ~[]E, M ~[]R, E any, R any](s S) M

TypeAssertFilter returns a slice satisfying r, ok := any(c).(R); ok == true within all r in the slice. TypeAssertFilter does not modify the contents of the slice s; it creates a new slice.

func TypeAssertFilterFunc added in v1.2.58

func TypeAssertFilterFunc[S ~[]E, M ~[]R, E any, R any](s S, f func(E) (R, bool)) M

TypeAssertFilterFunc returns a slice satisfying f(c) within all c in the slice. TypeAssertFilterFunc does not modify the contents of the slice s; it creates a new slice.

func Union added in v1.2.38

func Union[S ~[]E, E comparable](s1, s2 S) S

Union returns a slice satisfying c != zero within all c in the slice. Union replaces consecutive runs of equal elements with a single copy. This is like the uniq command found on Unix. Union does not modify the contents of the slice s1 and s2; it creates a new slice.

func UnionFunc added in v1.2.38

func UnionFunc[S ~[]E, E any](s1, s2 S, f func(v1, v2 E) bool) S

UnionFunc returns a slice satisfying f(c) within all c in the slice. UnionFunc does not modify the contents of the slice s1 and s2; it creates a new slice.

func Uniq added in v1.2.38

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

Uniq returns a slice satisfying c != zero within all c in the slice. Uniq modifies the contents of the slice s; it does not create a new slice.

func UniqFunc added in v1.2.38

func UniqFunc[S ~[]E, E any](s S, f func(v1, v2 E) bool) S

UniqFunc returns a slice satisfying f(c) within all c in the slice.

Types

type Heap added in v1.2.74

type Heap[S ~[]E, E any] struct {
	S S

	Comparator func(v1 E, v2 E) int
}

func NewHeapFunc added in v1.2.74

func NewHeapFunc[S ~[]E, E any](s S, cmp func(a E, b E) int) *Heap[S, E]

func NewHeapMax added in v1.2.75

func NewHeapMax[S ~[]E, E constraints.Ordered](s S) *Heap[S, E]

func NewHeapMin added in v1.2.75

func NewHeapMin[S ~[]E, E constraints.Ordered](s S) *Heap[S, E]

func (Heap[S, E]) Len added in v1.2.74

func (h Heap[S, E]) Len() int

func (Heap[S, E]) Less added in v1.2.74

func (h Heap[S, E]) Less(i, j int) bool

func (*Heap[S, E]) Pop added in v1.2.74

func (h *Heap[S, E]) Pop() any

func (*Heap[S, E]) Push added in v1.2.74

func (h *Heap[S, E]) Push(x any)

func (Heap[S, E]) Swap added in v1.2.74

func (h Heap[S, E]) Swap(i, j int)

type MaxHeap added in v1.2.74

type MaxHeap[E constraints.Ordered] []E

An MaxHeap is a max-heap of slices.

func (MaxHeap[E]) Len added in v1.2.74

func (h MaxHeap[E]) Len() int

func (MaxHeap[E]) Less added in v1.2.74

func (h MaxHeap[E]) Less(i, j int) bool

func (*MaxHeap[E]) Pop added in v1.2.74

func (h *MaxHeap[E]) Pop() any

func (*MaxHeap[E]) Push added in v1.2.74

func (h *MaxHeap[E]) Push(x any)

func (MaxHeap[E]) Swap added in v1.2.74

func (h MaxHeap[E]) Swap(i, j int)

type MinHeap added in v1.2.74

type MinHeap[E constraints.Ordered] []E

An MinHeap is a min-heap of slices.

func (MinHeap[E]) Len added in v1.2.74

func (h MinHeap[E]) Len() int

func (MinHeap[E]) Less added in v1.2.74

func (h MinHeap[E]) Less(i, j int) bool

func (*MinHeap[E]) Pop added in v1.2.74

func (h *MinHeap[E]) Pop() any

func (*MinHeap[E]) Push added in v1.2.74

func (h *MinHeap[E]) Push(x any)

func (MinHeap[E]) Swap added in v1.2.74

func (h MinHeap[E]) Swap(i, j int)

type SortSlice added in v1.2.74

type SortSlice[E constraints.Ordered] []E

SortSlice attaches the methods of Interface to []E, sorting in increasing order.

func (SortSlice[E]) Len added in v1.2.74

func (x SortSlice[E]) Len() int

func (SortSlice[E]) Less added in v1.2.74

func (x SortSlice[E]) Less(i, j int) bool

Less reports whether x[i] should be ordered before x[j], as required by the sort Interface. Note that floating-point comparison by itself is not a transitive relation: it does not report a consistent ordering for not-a-number (NaN) values. This implementation of Less places NaN values before any others, by using:

x[i] < x[j] || (math.IsNaN(x[i]) && !math.IsNaN(x[j]))

func (SortSlice[E]) Sort added in v1.2.74

func (x SortSlice[E]) Sort()

Sort is a convenience method: x.Sort() calls Sort(x).

func (SortSlice[E]) Swap added in v1.2.74

func (x SortSlice[E]) Swap(i, j int)

type SwigVector added in v1.2.107

type SwigVector[E any] interface {
	Size() int64
	Capacity() int64
	Reserve(int64)
	IsEmpty() bool
	Clear()
	Add(e E)
	Get(i int) E
	Set(i int, e E)
}

SwigVector represents Go wrapper with std::vector<T>

%include <std_vector.i> %template(vector_float) std::vector<float>; See: https://www.swig.org/Doc4.1/Go.html

Jump to

Keyboard shortcuts

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