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