This commit is contained in:
withchao 2023-02-02 11:55:48 +08:00
parent 26b5ed72df
commit dab552aed4
2 changed files with 21 additions and 29 deletions

13
pkg/utilsv2/demo.go Normal file
View File

@ -0,0 +1,13 @@
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,23 +1,10 @@
package utilsv2
import "Open_IM/pkg/common/db/table"
//func DuplicateRemoval[T comparable](ts []T) []T {
// v := make([]T, 0, len(ts))
// tmp := map[T]struct{}{}
// for _, t := range ts {
// if _, ok := tmp[t]; !ok {
// tmp[t] = struct{}{}
// v = append(v, t)
// }
// }
// return v
//}
func DuplicateRemovalAny[T any, V comparable](ts []T, fn func(t T) V) []T {
v := make([]T, 0, len(ts))
tmp := map[V]struct{}{}
for _, t := range ts {
for i := 0; i < len(ts); i++ {
t := ts[i]
k := fn(t)
if _, ok := tmp[k]; !ok {
tmp[k] = struct{}{}
@ -33,16 +20,6 @@ func DuplicateRemoval[T comparable](ts []T) []T {
})
}
func demo() {
groups := []*table.GroupModel{}
groups = DuplicateRemovalAny(groups, func(t *table.GroupModel) string {
return t.GroupID
})
}
func DeleteAt[T any](ts []T, index ...int) []T {
switch len(index) {
case 0:
@ -54,10 +31,12 @@ func DeleteAt[T any](ts []T, index ...int) []T {
for _, v := range index {
tmp[v] = struct{}{}
}
for i, t := range ts {
v := make([]T, 0, len(ts))
for i := 0; i < len(ts); i++ {
if _, ok := tmp[i]; !ok {
v = append(v, ts[i])
}
}
return v
}
return nil
}