[#972] pilorama: Remove removeDuplicatesInPlace()

Also, check that slices.CompareFunc() indeed passes all the tests before
removal.

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2024-02-08 19:14:34 +03:00 committed by Evgenii Stratonikov
parent e39a714c25
commit 45fd4e4ff1
2 changed files with 2 additions and 83 deletions

View file

@ -2,6 +2,7 @@ package pilorama
import ( import (
"encoding/binary" "encoding/binary"
"slices"
"sort" "sort"
"sync" "sync"
"time" "time"
@ -50,7 +51,7 @@ func (b *batch) run() {
sort.Slice(b.operations, func(i, j int) bool { sort.Slice(b.operations, func(i, j int) bool {
return b.operations[i].Time < b.operations[j].Time return b.operations[i].Time < b.operations[j].Time
}) })
b.operations = removeDuplicatesInPlace(b.operations) b.operations = slices.CompactFunc(b.operations, func(x, y *Move) bool { return x.Time == y.Time })
// Our main use-case is addition of new items. In this case, // Our main use-case is addition of new items. In this case,
// we do not need to perform undo()/redo(), just do(). // we do not need to perform undo()/redo(), just do().
@ -115,15 +116,3 @@ func (b *batch) run() {
b.results[i] <- err b.results[i] <- err
} }
} }
func removeDuplicatesInPlace(a []*Move) []*Move {
equalCount := 0
for i := 1; i < len(a); i++ {
if a[i].Time == a[i-1].Time {
equalCount++
} else {
a[i-equalCount] = a[i]
}
}
return a[:len(a)-equalCount]
}

View file

@ -1,70 +0,0 @@
package pilorama
import (
"testing"
"github.com/stretchr/testify/require"
)
func Test_removeDuplicatesInPlace(t *testing.T) {
testCases := []struct {
before []int
after []int
}{
{
before: []int{},
after: []int{},
},
{
before: []int{1},
after: []int{1},
},
{
before: []int{1, 2},
after: []int{1, 2},
},
{
before: []int{1, 2, 3},
after: []int{1, 2, 3},
},
{
before: []int{1, 1, 2},
after: []int{1, 2},
},
{
before: []int{1, 2, 2},
after: []int{1, 2},
},
{
before: []int{1, 2, 2, 3},
after: []int{1, 2, 3},
},
{
before: []int{1, 1, 1},
after: []int{1},
},
{
before: []int{1, 1, 2, 2},
after: []int{1, 2},
},
{
before: []int{1, 1, 1, 2, 3, 3, 3},
after: []int{1, 2, 3},
},
}
for _, tc := range testCases {
ops := make([]*Move, len(tc.before))
for i := range ops {
ops[i] = &Move{Meta: Meta{Time: Timestamp(tc.before[i])}}
}
expected := make([]*Move, len(tc.after))
for i := range expected {
expected[i] = &Move{Meta: Meta{Time: Timestamp(tc.after[i])}}
}
actual := removeDuplicatesInPlace(ops)
require.Equal(t, expected, actual, "%d", tc.before)
}
}