mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-30 06:22:44 +08:00
1
This commit is contained in:
parent
26b5ed72df
commit
dab552aed4
13
pkg/utilsv2/demo.go
Normal file
13
pkg/utilsv2/demo.go
Normal 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
|
||||
})
|
||||
|
||||
}
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user