1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-09 09:35:03 +09:00

Tree builder fixes

This commit is contained in:
mcrakhman 2024-11-25 22:20:21 +01:00
parent 3c6284c750
commit c8a5002d8c
No known key found for this signature in database
GPG key ID: DED12CFEF5B8396B
3 changed files with 137 additions and 14 deletions

View file

@ -146,25 +146,31 @@ func ContainsSorted[T constraints.Ordered](seq []T, subseq []T) bool {
}
func DiscardDuplicatesSorted[T comparable](sorted []T) []T {
cnt := 0
for i := 0; i < len(sorted)-1; i++ {
if sorted[i] != sorted[i+1] {
if len(sorted) <= 1 {
return sorted
}
cnt := 1
for i := 1; i < len(sorted)-1; i++ {
if sorted[i] != sorted[i-1] {
sorted[cnt] = sorted[i]
cnt++
sorted[cnt] = sorted[i+1]
}
}
return sorted[:cnt+1]
return sorted[:cnt]
}
func DiscardDuplicatesSortedFunc[T any](sorted []T, equal func(T, T) bool) []T {
cnt := 0
for i := 0; i < len(sorted)-1; i++ {
if !equal(sorted[i], sorted[i+1]) {
if len(sorted) <= 1 {
return sorted
}
cnt := 1
for i := 1; i < len(sorted)-1; i++ {
if !equal(sorted[i], sorted[i-1]) {
sorted[cnt] = sorted[i]
cnt++
sorted[cnt] = sorted[i+1]
}
}
return sorted[:cnt+1]
return sorted[:cnt]
}
func DiscardFromSlice[T any](elements []T, isDiscarded func(T) bool) []T {