Skip to content

Commit

Permalink
fix: remove redundant func. (#664)
Browse files Browse the repository at this point in the history
* fix: remove redundant func.

* fix: remove sort slice.
  • Loading branch information
mo3et authored Aug 15, 2024
1 parent 7c1a9d4 commit a25de99
Showing 1 changed file with 2 additions and 50 deletions.
52 changes: 2 additions & 50 deletions internal/incrversion/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package incrversion
import (
"context"
"reflect"
"sort"

"github.com/openimsdk/openim-sdk-core/v3/pkg/db/db_interface"
"github.com/openimsdk/openim-sdk-core/v3/pkg/db/model_struct"
Expand Down Expand Up @@ -101,7 +100,7 @@ func (o *VersionSynchronizer[V, R]) Sync() error {
}
} else {
if len(delIDs) > 0 {
lvs.UIDList = DeleteElements(lvs.UIDList, delIDs)
lvs.UIDList = datautil.DeleteElems(lvs.UIDList, delIDs...)
}
if len(insert) > 0 {
lvs.UIDList = append(lvs.UIDList, datautil.Slice(insert, o.Key)...)
Expand Down Expand Up @@ -179,7 +178,7 @@ func (o *VersionSynchronizer[V, R]) CheckVersionSync() error {
}
if lvs.Version+1 == version {
if len(delIDs) > 0 {
lvs.UIDList = DeleteElements(lvs.UIDList, delIDs)
lvs.UIDList = datautil.DeleteElems(lvs.UIDList, delIDs...)
}
if len(insert) > 0 {
lvs.UIDList = append(lvs.UIDList, datautil.Slice(insert, o.Key)...)
Expand Down Expand Up @@ -232,50 +231,3 @@ func (o *VersionSynchronizer[V, R]) CheckVersionSync() error {
return o.Sync()
}
}

// DeleteElements removes elements from a slice that are contained in another slice, while maintaining the order of the slice
func DeleteElements[E comparable](es []E, toDelete []E) []E {
// Store the elements to be deleted in a hash set
deleteSet := make(map[E]struct{}, len(toDelete))
for _, e := range toDelete {
deleteSet[e] = struct{}{}
}

// Use an index j to track the new slice position
j := 0
for _, e := range es {
if _, found := deleteSet[e]; !found {
es[j] = e
j++
}
}
return es[:j]
}

// DeleteElement removes a specified element from a slice while maintaining the order of the slice
func DeleteElement[E comparable](es []E, element E) []E {
j := 0
for _, e := range es {
if e != element {
es[j] = e
j++
}
}
return es[:j]
}

// Slice converts slice types in batches and sorts the resulting slice using a custom comparator
func Slice[E any, T any](es []E, fn func(e E) T, less func(a, b T) bool) []T {
// Convert the slice
v := make([]T, len(es))
for i := 0; i < len(es); i++ {
v[i] = fn(es[i])
}

// Sort the slice
sort.Slice(v, func(i, j int) bool {
return less(v[i], v[j])
})

return v
}

0 comments on commit a25de99

Please sign in to comment.