diff --git a/pkg/utilsv2/demo.go b/pkg/utilsv2/demo.go new file mode 100644 index 000000000..0979d390e --- /dev/null +++ b/pkg/utilsv2/demo.go @@ -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 + }) + +} diff --git a/pkg/utilsv2/slice.go b/pkg/utilsv2/slice.go index 4d1dca3ac..96245a276 100644 --- a/pkg/utilsv2/slice.go +++ b/pkg/utilsv2/slice.go @@ -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 }