You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/utils/utils_v2.go

480 lines
8.9 KiB

2 years ago
package utils
2 years ago
2 years ago
import (
2 years ago
"encoding/json"
2 years ago
"sort"
)
// SliceSub a中存在,b中不存在 (a-b)
func SliceSub[E comparable](a, b []E) []E {
k := make(map[E]struct{})
for i := 0; i < len(b); i++ {
k[b[i]] = struct{}{}
}
t := make(map[E]struct{})
rs := make([]E, 0, len(a))
for i := 0; i < len(a); i++ {
e := a[i]
if _, ok := t[e]; ok {
continue
}
if _, ok := k[e]; ok {
continue
}
rs = append(rs, e)
t[e] = struct{}{}
}
return rs
}
2 years ago
// SliceSubAny a中存在,b中不存在 (a-b)
func SliceSubAny[E comparable, T any](a []E, b []T, fn func(t T) E) []E {
return SliceSub(a, Slice(b, fn))
}
2 years ago
// SliceAnySub a中存在,b中不存在 (a-b) fn 返回的是uuid
func SliceAnySub[E any, T comparable](a, b []E, fn func(t E) T) []E {
2 years ago
m := make(map[T]E)
for i := 0; i < len(b); i++ {
v := b[i]
m[fn(v)] = v
}
var es []E
for i := 0; i < len(a); i++ {
v := a[i]
if _, ok := m[fn(v)]; !ok {
es = append(es, v)
}
}
return es
2 years ago
}
2 years ago
// DistinctAny 去重
2 years ago
func DistinctAny[E any, K comparable](es []E, fn func(e E) K) []E {
v := make([]E, 0, len(es))
tmp := map[K]struct{}{}
for i := 0; i < len(es); i++ {
t := es[i]
2 years ago
k := fn(t)
if _, ok := tmp[k]; !ok {
tmp[k] = struct{}{}
v = append(v, t)
}
}
return v
}
2 years ago
// Distinct 去重
2 years ago
func Distinct[T comparable](ts []T) []T {
return DistinctAny(ts, func(t T) T {
2 years ago
return t
})
}
2 years ago
// Delete 删除切片元素, 支持负数删除倒数第几个
2 years ago
func Delete[E any](es []E, index ...int) []E {
2 years ago
switch len(index) {
case 0:
2 years ago
return es
2 years ago
case 1:
2 years ago
i := index[0]
if i < 0 {
2 years ago
i = len(es) + i
2 years ago
}
2 years ago
if len(es) <= i {
return es
2 years ago
}
2 years ago
return append(es[:i], es[i+1:]...)
2 years ago
default:
tmp := make(map[int]struct{})
2 years ago
for _, i := range index {
if i < 0 {
2 years ago
i = len(es) + i
2 years ago
}
tmp[i] = struct{}{}
2 years ago
}
2 years ago
v := make([]E, 0, len(es))
for i := 0; i < len(es); i++ {
2 years ago
if _, ok := tmp[i]; !ok {
2 years ago
v = append(v, es[i])
2 years ago
}
2 years ago
}
2 years ago
return v
2 years ago
}
}
2 years ago
2 years ago
// DeleteAt 删除切片元素, 支持负数删除倒数第几个
2 years ago
func DeleteAt[E any](es *[]E, index ...int) []E {
v := Delete(*es, index...)
*es = v
return v
}
2 years ago
// IndexAny get the index of the element
func IndexAny[E any, K comparable](e E, es []E, fn func(e E) K) int {
2 years ago
k := fn(e)
for i := 0; i < len(es); i++ {
if fn(es[i]) == k {
2 years ago
return i
}
}
return -1
}
2 years ago
// IndexOf get the index of the element
func IndexOf[E comparable](e E, es ...E) int {
return IndexAny(e, es, func(t E) E {
2 years ago
return t
})
}
2 years ago
// Contain 是否包含
func Contain[E comparable](e E, es ...E) bool {
return IndexOf(e, es...) >= 0
2 years ago
}
2 years ago
// DuplicateAny 是否有重复的
2 years ago
func DuplicateAny[E any, K comparable](es []E, fn func(e E) K) bool {
t := make(map[K]struct{})
for _, e := range es {
k := fn(e)
if _, ok := t[k]; ok {
return true
}
t[k] = struct{}{}
}
return false
}
2 years ago
// Duplicate 是否有重复的
2 years ago
func Duplicate[E comparable](es []E) bool {
return DuplicateAny(es, func(e E) E {
return e
})
}
2 years ago
// SliceToMapOkAny slice to map (自定义类型, 筛选)
2 years ago
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++ {
t := es[i]
if k, v, ok := fn(t); ok {
kv[k] = v
}
2 years ago
}
return kv
}
2 years ago
// SliceToMapAny slice to map (自定义类型)
2 years ago
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)
return k, v, true
})
}
// SliceToMap slice to map
func SliceToMap[E any, K comparable](es []E, fn func(e E) K) map[K]E {
return SliceToMapOkAny[E, K, E](es, func(e E) (K, E, bool) {
k := fn(e)
return k, e, true
})
}
// SliceSetAny slice to map[K]struct{}
func SliceSetAny[E any, K comparable](es []E, fn func(e E) K) map[K]struct{} {
return SliceToMapAny(es, func(e E) (K, struct{}) {
return fn(e), struct{}{}
})
}
2 years ago
func Filter[E, T any](es []E, fn func(e E) (T, bool)) []T {
rs := make([]T, 0, len(es))
2 years ago
for i := 0; i < len(es); i++ {
2 years ago
e := es[i]
if t, ok := fn(e); ok {
rs = append(rs, t)
2 years ago
}
}
return rs
}
2 years ago
// Slice 批量转换切片类型
2 years ago
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++ {
2 years ago
v[i] = fn(es[i])
2 years ago
}
return v
}
2 years ago
// SliceSet slice to map[E]struct{}
func SliceSet[E comparable](es []E) map[E]struct{} {
return SliceSetAny(es, func(e E) E {
return e
})
}
// HasKey get whether the map contains key
func HasKey[K comparable, V any](m map[K]V, k K) bool {
if m == nil {
return false
}
_, ok := m[k]
return ok
}
// Min get minimum value
func Min[E Ordered](e ...E) E {
v := e[0]
for _, t := range e[1:] {
if v > t {
v = t
}
}
return v
}
// Max get maximum value
func Max[E Ordered](e ...E) E {
v := e[0]
for _, t := range e[1:] {
if v < t {
v = t
}
}
return v
}
2 years ago
// BothExistAny 获取切片中共同存在的元素(交集)
2 years ago
func BothExistAny[E any, K comparable](es [][]E, fn func(e E) K) []E {
if len(es) == 0 {
return []E{}
}
var idx int
ei := make([]map[K]E, len(es))
for i := 0; i < len(ei); i++ {
e := es[i]
if len(e) == 0 {
return []E{}
}
kv := make(map[K]E)
for j := 0; j < len(e); j++ {
t := e[j]
k := fn(t)
kv[k] = t
}
ei[i] = kv
if len(kv) < len(ei[idx]) {
idx = i
}
}
v := make([]E, 0, len(ei[idx]))
for k := range ei[idx] {
all := true
for i := 0; i < len(ei); i++ {
if i == idx {
continue
}
if _, ok := ei[i][k]; !ok {
all = false
break
}
}
if !all {
continue
}
v = append(v, ei[idx][k])
}
return v
}
2 years ago
// BothExist 获取切片中共同存在的元素(交集)
2 years ago
func BothExist[E comparable](es ...[]E) []E {
return BothExistAny(es, func(e E) E {
return e
})
}
2 years ago
//// CompleteAny a中存在b的所有元素, 同时b中的所有元素a
//func CompleteAny[K comparable, E any](ks []K, es []E, fn func(e E) K) bool {
// if len(ks) == 0 && len(es) == 0 {
// return true
// }
// kn := make(map[K]uint8)
// for _, e := range Distinct(ks) {
// kn[e]++
// }
// for k := range SliceSetAny(es, fn) {
// kn[k]++
// }
// for _, n := range kn {
// if n != 2 {
// return false
// }
// }
// return true
//}
// Complete a和b去重后是否相等(忽略顺序)
2 years ago
func Complete[E comparable](a []E, b []E) bool {
2 years ago
return len(Single(a, b)) == 0
2 years ago
}
2 years ago
// Keys get map keys
func Keys[K comparable, V any](kv map[K]V) []K {
2 years ago
ks := make([]K, 0, len(kv))
for k := range kv {
ks = append(ks, k)
}
return ks
}
2 years ago
// Values get map values
func Values[K comparable, V any](kv map[K]V) []V {
2 years ago
vs := make([]V, 0, len(kv))
for k := range kv {
vs = append(vs, kv[k])
}
return vs
}
2 years ago
2 years ago
// Sort basic type sorting
func Sort[E Ordered](es []E, asc bool) []E {
SortAny(es, func(a, b E) bool {
2 years ago
if asc {
return a < b
} else {
return a > b
}
})
2 years ago
return es
2 years ago
}
2 years ago
// SortAny custom sort method
func SortAny[E any](es []E, fn func(a, b E) bool) {
sort.Sort(&sortSlice[E]{
ts: es,
2 years ago
fn: fn,
})
}
2 years ago
// If true -> a, false -> b
func If[T any](isa bool, a, b T) T {
if isa {
return a
}
return b
}
2 years ago
func ToPtr[T any](t T) *T {
return &t
}
2 years ago
// Equal 比较切片是否相对(包括元素顺序)
2 years ago
func Equal[E comparable](a []E, b []E) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
2 years ago
// Single a中存在,b中不存在 或 b中存在,a中不存在
2 years ago
func Single[E comparable](a, b []E) []E {
2 years ago
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
2 years ago
}
2 years ago
// Order 将ts按es排序
func Order[E comparable, T any](es []E, ts []T, fn func(t T) E) []T {
if len(es) == 0 || len(ts) == 0 {
return ts
}
kv := make(map[E][]T)
for i := 0; i < len(ts); i++ {
t := ts[i]
k := fn(t)
kv[k] = append(kv[k], t)
}
rs := make([]T, 0, len(ts))
for _, e := range es {
vs := kv[e]
delete(kv, e)
rs = append(rs, vs...)
}
for k := range kv {
rs = append(rs, kv[k]...)
}
return rs
}
func OrderPtr[E comparable, T any](es []E, ts *[]T, fn func(t T) E) []T {
*ts = Order(es, *ts, fn)
return *ts
}
2 years ago
func UniqueJoin(s ...string) string {
data, _ := json.Marshal(s)
return string(data)
}
2 years ago
type sortSlice[E any] struct {
ts []E
fn func(a, b E) bool
2 years ago
}
2 years ago
func (o *sortSlice[E]) Len() int {
2 years ago
return len(o.ts)
}
2 years ago
func (o *sortSlice[E]) Less(i, j int) bool {
2 years ago
return o.fn(o.ts[i], o.ts[j])
}
2 years ago
func (o *sortSlice[E]) Swap(i, j int) {
2 years ago
o.ts[i], o.ts[j] = o.ts[j], o.ts[i]
}
2 years ago
// Ordered types that can be sorted
2 years ago
type Ordered interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64 | ~string
}
2 years ago
func Unwrap(err error) error {
for err != nil {
unwrap, ok := err.(interface {
Unwrap() error
})
if !ok {
break
}
err = unwrap.Unwrap()
}
return err
}
2 years ago
// NotNilReplace 当new_不为空时, 将old设置为new_
func NotNilReplace[T any](old, new_ *T) {
2 years ago
if new_ == nil {
2 years ago
return
}
*old = *new_
}