This commit is contained in:
withchao 2023-02-02 15:42:14 +08:00
parent c1af64854c
commit c3f6a0fa15
3 changed files with 106 additions and 20 deletions

View File

@ -1,13 +0,0 @@
package utilsv2
import "Open_IM/pkg/common/db/table"
func demo() {
groups := []*table.GroupModel{}
groups = DuplicateRemovalAny(groups, func(t *table.GroupModel) string {
return t.GroupID
})
}

View File

@ -1,6 +1,7 @@
package utilsv2
func DuplicateRemovalAny[T any, V comparable](ts []T, fn func(t T) V) []T {
// DistinctAny 切片去重
func DistinctAny[T any, V comparable](ts []T, fn func(t T) V) []T {
v := make([]T, 0, len(ts))
tmp := map[V]struct{}{}
for i := 0; i < len(ts); i++ {
@ -14,25 +15,27 @@ func DuplicateRemovalAny[T any, V comparable](ts []T, fn func(t T) V) []T {
return v
}
func DuplicateRemoval[T comparable](ts []T) []T {
return DuplicateRemovalAny(ts, func(t T) T {
// Distinct 切片去重
func Distinct[T comparable](ts []T) []T {
return DistinctAny(ts, func(t T) T {
return t
})
}
// DeleteAt 删除切片元素, 支持负数删除倒数第几个
func DeleteAt[T any](ts []T, index ...int) []T {
switch len(index) {
case 0:
return ts
case 1:
i := index[0]
if len(ts) <= i || len(ts) < -i {
return ts
}
if i < 0 {
i = len(ts) + i
}
return append(ts[:index[0]], ts[index[0]+1:]...)
if len(ts) <= i {
return ts
}
return append(ts[:i], ts[i+1:]...)
default:
tmp := make(map[int]struct{})
for _, i := range index {
@ -50,3 +53,55 @@ func DeleteAt[T any](ts []T, index ...int) []T {
return v
}
}
// IndexAny 获取元素所在的下标
func IndexAny[T any, V comparable](ts []T, t T, fn func(t T) V) int {
k := fn(t)
for i := 0; i < len(ts); i++ {
if fn(ts[i]) == k {
return i
}
}
return -1
}
// IndexOf 可比较的元素index下标
func IndexOf[T comparable](ts []T, t T) int {
return IndexAny(ts, t, func(t T) T {
return t
})
}
// IsContain 是否包含元素
func IsContain[T comparable](ts []T, t T) bool {
return IndexOf(ts, t) >= 0
}
// SliceToMap 切片转map
func SliceToMap[T any, K comparable](ts []T, fn func(t T) K) map[K]T {
kv := make(map[K]T)
for i := 0; i < len(ts); i++ {
t := ts[i]
k := fn(t)
kv[k] = t
}
return kv
}
// MapKey map获取所有key
func MapKey[K comparable, V any](kv map[K]V) []K {
ks := make([]K, 0, len(kv))
for k := range kv {
ks = append(ks, k)
}
return ks
}
// MapValue map获取所有key
func MapValue[K comparable, V any](kv map[K]V) []V {
vs := make([]V, 0, len(kv))
for k := range kv {
vs = append(vs, kv[k])
}
return vs
}

44
pkg/utilsv2/slice_test.go Normal file
View File

@ -0,0 +1,44 @@
package utilsv2
import (
"fmt"
"testing"
)
func TestDistinct(t *testing.T) {
arr := []int{1, 1, 1, 4, 4, 5, 2, 3, 3, 3, 6}
fmt.Println(Distinct(arr))
}
func TestDeleteAt(t *testing.T) {
arr := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println(DeleteAt(arr, 0, 1, -1, -2))
fmt.Println(DeleteAt(arr))
fmt.Println(DeleteAt(arr, 1))
}
func TestSliceToMap(t *testing.T) {
type Item struct {
ID string
Name string
}
list := []Item{
{ID: "111", Name: "111"},
{ID: "222", Name: "222"},
{ID: "333", Name: "333"},
}
m := SliceToMap(list, func(t Item) string {
return t.ID
})
fmt.Printf("%+v\n", m)
}
func TestIndexOf(t *testing.T) {
arr := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println(IndexOf(arr, 3))
}