This commit is contained in:
withchao 2023-02-03 18:10:49 +08:00
parent bec4a89f7b
commit 516285952d

View File

@ -5,7 +5,7 @@ import (
"sort"
)
// DistinctAny remove duplicate elements
// DistinctAny 去重
func DistinctAny[E any, K comparable](es []E, fn func(e E) K) []E {
v := make([]E, 0, len(es))
tmp := map[K]struct{}{}
@ -20,14 +20,14 @@ func DistinctAny[E any, K comparable](es []E, fn func(e E) K) []E {
return v
}
// Distinct remove duplicate elements
// Distinct 去重
func Distinct[T comparable](ts []T) []T {
return DistinctAny(ts, func(t T) T {
return t
})
}
// Delete delete slice element, support negative number to delete the penultimate
// Delete 删除切片元素, 支持负数删除倒数第几个
func Delete[E any](es []E, index ...int) []E {
switch len(index) {
case 0:
@ -59,7 +59,7 @@ func Delete[E any](es []E, index ...int) []E {
}
}
// DeleteAt delete slice element, support negative number to delete the penultimate
// DeleteAt 删除切片元素, 支持负数删除倒数第几个
func DeleteAt[E any](es *[]E, index ...int) []E {
v := Delete(*es, index...)
*es = v
@ -84,12 +84,12 @@ func IndexOf[E comparable](es []E, e E) int {
})
}
// Contain include element or not
// Contain 是否包含
func Contain[E comparable](es []E, e E) bool {
return IndexOf(es, e) >= 0
}
// DuplicateAny judge whether it is repeated
// DuplicateAny 是否有重复的
func DuplicateAny[E any, K comparable](es []E, fn func(e E) K) bool {
t := make(map[K]struct{})
for _, e := range es {
@ -102,14 +102,14 @@ func DuplicateAny[E any, K comparable](es []E, fn func(e E) K) bool {
return false
}
// Duplicate judge whether it is repeated
// Duplicate 是否有重复的
func Duplicate[E comparable](es []E) bool {
return DuplicateAny(es, func(e E) E {
return e
})
}
// SliceToMapOkAny slice to map
// SliceToMapOkAny slice to map (自定义类型, 筛选)
func SliceToMapOkAny[E any, K comparable, V any](es []E, fn func(e E) (K, V, bool)) map[K]V {
kv := make(map[K]V)
for i := 0; i < len(es); i++ {
@ -121,7 +121,7 @@ func SliceToMapOkAny[E any, K comparable, V any](es []E, fn func(e E) (K, V, boo
return kv
}
// SliceToMapAny slice to map
// SliceToMapAny slice to map (自定义类型)
func SliceToMapAny[E any, K comparable, V any](es []E, fn func(e E) (K, V)) map[K]V {
return SliceToMapOkAny(es, func(e E) (K, V, bool) {
k, v := fn(e)
@ -144,6 +144,7 @@ func SliceSetAny[E any, K comparable](es []E, fn func(e E) K) map[K]struct{} {
})
}
// Slice 批量转换切片类型
func Slice[E any, T any](es []E, fn func(e E) T) []T {
v := make([]T, len(es))
for i := 0; i < len(es); i++ {
@ -190,7 +191,7 @@ func Max[E Ordered](e ...E) E {
return v
}
// BothExistAny get elements common to multiple slices
// BothExistAny 获取切片中共同存在的元素(交集)
func BothExistAny[E any, K comparable](es [][]E, fn func(e E) K) []E {
if len(es) == 0 {
return []E{}
@ -233,41 +234,40 @@ func BothExistAny[E any, K comparable](es [][]E, fn func(e E) K) []E {
return v
}
// BothExist get elements common to multiple slices
// BothExist 获取切片中共同存在的元素(交集)
func BothExist[E comparable](es ...[]E) []E {
return BothExistAny(es, func(e E) E {
return e
})
}
// CompleteAny complete inclusion
// CompleteAny a中存在b的所有元素, 同时b中的所有元素a
func CompleteAny[K comparable, E any](ks []K, es []E, fn func(e E) K) bool {
a := SliceSetAny(es, fn)
for k := range SliceSet(ks) {
if !HasKey(a, k) {
return false
}
delete(a, k)
}
return len(a) == 0
}
func Complete[E comparable](a []E, b []E) bool {
if len(a) == 0 && len(b) == 0 {
if len(ks) == 0 && len(es) == 0 {
return true
}
if (len(a) == 0 && len(b) != 0) || (len(a) != 0 && len(b) == 0) {
return false
kn := make(map[K]uint8)
for _, e := range Distinct(ks) {
kn[e]++
}
t := SliceSet(a)
for _, e := range b {
if _, ok := t[e]; !ok {
for k := range SliceSetAny(es, fn) {
kn[k]++
}
for _, n := range kn {
if n != 2 {
return false
}
}
return true
}
// Complete a中存在b的所有元素, 同时b中的所有元素a
func Complete[E comparable](a []E, b []E) bool {
return CompleteAny(a, b, func(e E) E {
return e
})
}
// MapKey get map keys
func MapKey[K comparable, V any](kv map[K]V) []K {
ks := make([]K, 0, len(kv))
@ -314,6 +314,7 @@ func If[T any](isa bool, a, b T) T {
return b
}
// Equal 比较切片是否相对(包括元素顺序)
func Equal[E comparable](a []E, b []E) bool {
if len(a) != len(b) {
return false
@ -326,11 +327,22 @@ func Equal[E comparable](a []E, b []E) bool {
return true
}
// Single
// Single a和b中都只存在一个
func Single[E comparable](a, b []E) []E {
return nil
kn := make(map[E]uint8)
for _, e := range Distinct(a) {
kn[e]++
}
for _, e := range Distinct(b) {
kn[e]++
}
v := make([]E, 0, len(kn))
for k, n := range kn {
if n == 1 {
v = append(v, k)
}
}
return v
}
func UniqueJoin(s ...string) string {