slices

package
v1.10.6 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const SmallArrayLen = 32

Variables

This section is empty.

Functions

func BinarySearch

func BinarySearch[V constraints.Ordered](arr []constraints2.CompareKey[V], x constraints2.CompareKey[V]) int

BinarySearch 二分查找

func Cast added in v1.5.0

func Cast[T1, T2 any, T1S ~[]T1](s T1S) []T2

func Cast2 added in v1.5.0

func Cast2[T1, T2 any, T1S ~[]T1, T2S ~[]T2](s T1S) T2S

func Classify added in v1.5.11

func Classify[S ~[]T, T any, K comparable, V any](s S, getKV func(T) (K, V)) map[K][]V

将切片按照某个key分类

func Contains

func Contains[S ~[]T, T comparable](s S, v T) bool

func ContainsByKey added in v1.5.0

func ContainsByKey[S ~[]cmp.EqualKey[K], K comparable](s S, v K) bool

func Deduplicate

func Deduplicate[S ~[]T, T comparable](slice S) S

去重

func Difference

func Difference[S ~[]T, T comparable](a, b S) (S, S)

两个数组各自相对的差集,返回为A-B,B-A

func DifferenceByKey

func DifferenceByKey[S ~[]E, E cmp.EqualKey[T], T comparable](a, b S) (S, S)

取差集,通过循环比较key

func DifferenceSet

func DifferenceSet[S ~[]T, T comparable](a S, b S) S

取差集,返回为A-B

func DifferenceSetByKey

func DifferenceSetByKey[S ~[]E, E cmp.EqualKey[T], T comparable](a S, b S) S

指定key取差集,返回为A-B

func Filter

func Filter[S ~[]T, T any](fn func(T) bool, src S) S

func ForEach

func ForEach[S ~[]T, T any](s S, handle func(idx int, v T))

func ForEachIndex

func ForEachIndex[S ~[]T, T any](s S, handle func(i int))

遍历切片,参数为下标,利用闭包实现遍历

func ForEachValue

func ForEachValue[S ~[]T, T any](s S, handle func(v T))

func GrowSlice added in v1.5.11

func GrowSlice(et *reflect.Type, old reflect.Slice, cap int) reflect.Slice

func GuardSlice added in v1.5.11

func GuardSlice(buf *[]byte, n int)

func HasCoincide

func HasCoincide[S ~[]T, T comparable](s1, s2 S) bool

没有泛型,范例,实际需根据不同类型各写一遍,用CmpKey,基本类型又用不了,go需要能给基本类型实现方法不能给外部类型实现方法 1.20以后字段均是comparable的结构体也是comparable的 判断是否有重合元素

func HasCoincideByKey

func HasCoincideByKey[S ~[]E, E cmp.EqualKey[T], T comparable](s1, s2 S) bool

func In

func In[S ~[]T, T comparable](v T, s S) bool

func InByKey

func InByKey[S ~[]E, E cmp.EqualKey[K], K comparable](key K, s S) bool

func Intersection

func Intersection[S ~[]T, T comparable](a S, b S) S

取交集

func IntersectionByKey

func IntersectionByKey[S ~[]E, E cmp.EqualKey[T], T comparable](a S, b S) S

默认保留前一个的,靠前的元素

func IntersectionMap

func IntersectionMap[S ~[]T, T comparable](a S, b S) map[T]struct{}

func Join

func Join[S ~[]T, T fmt.Stringer](s S, sep string) string

func JoinByIndex

func JoinByIndex[S ~[]T, T any](s S, toString func(i int) string, sep string) string

func JoinByValue

func JoinByValue[S ~[]T, T any](s S, toString func(v T) string, sep string) string

func Map

func Map[T1, T2 any, T1S ~[]T1](s T1S, fn func(T1) T2) []T2

func Map2 added in v1.5.0

func Map2[T1, T2 any, T1S ~[]T1, T2S ~[]T2](s T1S, fn func(T1) T2) T2S

func Max

func Max[S ~[]T, T constraints.Number](s S) T

func Min

func Min[S ~[]T, T constraints.Number](s S) T

func OrderedArrayIntersection

func OrderedArrayIntersection[S ~[]T, T constraints.Ordered](a S, b S) S

有序数组取交集

func Ptr2SlicePtr added in v1.5.11

func Ptr2SlicePtr(s unsafe.Pointer, l int, c int) unsafe.Pointer

func Reduce

func Reduce[S ~[]T, T any](slices S, fn func(T, T) T) T

func RemoveDuplicates

func RemoveDuplicates[S ~[]T, T comparable](s S) S

func RemoveDuplicatesByKey

func RemoveDuplicatesByKey[S ~[]E, E cmp.EqualKey[T], T comparable](s S) S

默认保留先遍历到的

func RemoveDuplicatesByKeyRetainBehind added in v1.5.1

func RemoveDuplicatesByKeyRetainBehind[S ~[]E, E cmp.EqualKey[T], T comparable](s S) S

func Reverse

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

func ReverseForEach

func ReverseForEach[S ~[]T, T any](s S, handle func(idx int, v T))

func Sort

func Sort[T constraints.Ordered](s []T)

func SortByKey added in v1.5.11

func SortByKey[K constraints.Ordered, T cmpi.CompareKey[K]](s []T)

func Swap

func Swap[S ~[]T, T any](s S, i, j int)

func ToMap added in v1.5.11

func ToMap[S ~[]T, T any, K comparable, V any](s S, getKV func(T) (K, V)) map[K]V

将切片转换为map

func Union

func Union[S ~[]T, T comparable](a S, b S) S

取并集

func UnionAndIntersectionAndDifference added in v1.5.1

func UnionAndIntersectionAndDifference[S ~[]T, T comparable](a, b S) (S, S, S, S)

交集和差集,返回A∪B A∩B,A-B,B-A

func UnionByKey

func UnionByKey[S ~[]E, E cmp.EqualKey[T], T comparable](a S, b S) S

默认保留第一个的,靠前的

Types

type Array

type Array[S, T any] []S

学学kotlin的定义

func (Array[S, T]) Map

func (a Array[S, T]) Map(fn func(S) T) []T

type ComparableSlice added in v1.5.11

type ComparableSlice[T comparable] []T

func (ComparableSlice[T]) Deduplicate added in v1.5.11

func (slices ComparableSlice[T]) Deduplicate() ComparableSlice[T]

去重

type Index

type Index[T any, I comparable] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Index 索引

func NewIndex

func NewIndex[T any, I comparable]() *Index[T, I]

func (*Index[T, I]) Add

func (i *Index[T, I]) Add(idx I, res T)

func (*Index[T, I]) Get

func (i *Index[T, I]) Get(idx I) T

func (*Index[T, I]) Remove

func (i *Index[T, I]) Remove(idx I)

func (*Index[T, I]) Set

func (i *Index[T, I]) Set(idx I, v T)

type MapSlice added in v1.5.11

type MapSlice[T, V any] Slice[T]

func (MapSlice[T, V]) Map added in v1.5.11

func (slice MapSlice[T, V]) Map(fn func(T) V) []V

type Slice added in v1.4.0

type Slice[T any] []T

func (Slice[T]) Every added in v1.4.0

func (slice Slice[T]) Every(fn func(T) bool) bool

func (Slice[T]) Filter added in v1.4.0

func (slice Slice[T]) Filter(fn func(T) bool) []T

func (Slice[T]) ForEach added in v1.4.0

func (slice Slice[T]) ForEach(fn func(T))

func (Slice[T]) Len added in v1.4.0

func (slice Slice[T]) Len() int

func (Slice[T]) Reduce added in v1.4.0

func (slice Slice[T]) Reduce(fn func(T, T) T) T

func (Slice[T]) Some added in v1.4.0

func (slice Slice[T]) Some(fn func(T) bool) bool

func (Slice[T]) Zip added in v1.4.0

func (slice Slice[T]) Zip(s []T) [][2]T

Jump to

Keyboard shortcuts

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