This commit is contained in:
withchao 2023-02-06 11:53:19 +08:00
parent b801cde5c4
commit 32d99595fb

View File

@ -241,35 +241,33 @@ func BothExist[E comparable](es ...[]E) []E {
}) })
} }
// CompleteAny a中存在b的所有元素, 同时b中的所有元素a //// 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 {
if len(ks) == 0 && len(es) == 0 { // if len(ks) == 0 && len(es) == 0 {
return true // return true
} // }
kn := make(map[K]uint8) // kn := make(map[K]uint8)
for _, e := range Distinct(ks) { // for _, e := range Distinct(ks) {
kn[e]++ // kn[e]++
} // }
for k := range SliceSetAny(es, fn) { // for k := range SliceSetAny(es, fn) {
kn[k]++ // kn[k]++
} // }
for _, n := range kn { // for _, n := range kn {
if n != 2 { // if n != 2 {
return false // return false
} // }
} // }
return true // return true
} //}
// Complete a中存在b的所有元素, 同时b中的所有元素a // Complete a和b去重后是否相等(忽略顺序)
func Complete[E comparable](a []E, b []E) bool { func Complete[E comparable](a []E, b []E) bool {
return CompleteAny(a, b, func(e E) E { return len(Single(a, b)) == 0
return e
})
} }
// MapKey get map keys // Keys get map keys
func MapKey[K comparable, V any](kv map[K]V) []K { func Keys[K comparable, V any](kv map[K]V) []K {
ks := make([]K, 0, len(kv)) ks := make([]K, 0, len(kv))
for k := range kv { for k := range kv {
ks = append(ks, k) ks = append(ks, k)
@ -277,8 +275,8 @@ func MapKey[K comparable, V any](kv map[K]V) []K {
return ks return ks
} }
// MapValue get map values // Values get map values
func MapValue[K comparable, V any](kv map[K]V) []V { func Values[K comparable, V any](kv map[K]V) []V {
vs := make([]V, 0, len(kv)) vs := make([]V, 0, len(kv))
for k := range kv { for k := range kv {
vs = append(vs, kv[k]) vs = append(vs, kv[k])
@ -327,7 +325,7 @@ func Equal[E comparable](a []E, b []E) bool {
return true return true
} }
// Single a和b中都只存在一个 // Single a中存在,b中不存在 或 b中存在,a中不存在
func Single[E comparable](a, b []E) []E { func Single[E comparable](a, b []E) []E {
kn := make(map[E]uint8) kn := make(map[E]uint8)
for _, e := range Distinct(a) { for _, e := range Distinct(a) {