Skip to content

Commit 152c917

Browse files
committed
removed endpoint.Diff + added xslices.{Transform,Diff}
1 parent 1b2f8d4 commit 152c917

File tree

9 files changed

+260
-173
lines changed

9 files changed

+260
-173
lines changed

internal/endpoint/diff.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

internal/endpoint/diff_test.go

Lines changed: 0 additions & 127 deletions
This file was deleted.

internal/xslices/diff.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package xslices
2+
3+
import (
4+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xmath"
5+
)
6+
7+
func Diff[T any](from, to []T, cmp func(lhs, rhs T) int) (steady, added, dropped []T) {
8+
steady = make([]T, 0, xmath.Min(len(to), len(from)))
9+
added = make([]T, 0, len(to))
10+
dropped = make([]T, 0, len(from))
11+
12+
to = Sort(to, cmp)
13+
from = Sort(from, cmp)
14+
15+
for i, j := 0, 0; ; {
16+
switch {
17+
case i == len(from) && j == len(to):
18+
return steady, added, dropped
19+
case i == len(from) && j < len(to):
20+
added = append(added, to[j])
21+
j++
22+
case i < len(from) && j == len(to):
23+
dropped = append(dropped, from[i])
24+
i++
25+
default:
26+
cmp := cmp(from[i], to[j])
27+
switch {
28+
case cmp < 0:
29+
dropped = append(dropped, from[i])
30+
i++
31+
case cmp > 0:
32+
added = append(added, to[j])
33+
j++
34+
default:
35+
steady = append(steady, from[i])
36+
i++
37+
j++
38+
}
39+
}
40+
}
41+
}

internal/xslices/diff_test.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package xslices
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestDiff(t *testing.T) {
10+
for _, tt := range []struct {
11+
name string
12+
previous []int
13+
newest []int
14+
steady []int
15+
added []int
16+
dropped []int
17+
}{
18+
{
19+
name: "WithoutChanges",
20+
previous: []int{
21+
2,
22+
1,
23+
0,
24+
3,
25+
},
26+
newest: []int{
27+
1,
28+
3,
29+
2,
30+
0,
31+
},
32+
steady: []int{
33+
0,
34+
1,
35+
2,
36+
3,
37+
},
38+
added: []int{},
39+
dropped: []int{},
40+
},
41+
{
42+
name: "OnlyAdded",
43+
previous: []int{
44+
1,
45+
0,
46+
3,
47+
},
48+
newest: []int{
49+
1,
50+
3,
51+
2,
52+
0,
53+
},
54+
steady: []int{
55+
0,
56+
1,
57+
3,
58+
},
59+
added: []int{
60+
2,
61+
},
62+
dropped: []int{},
63+
},
64+
{
65+
name: "OnlyDropped",
66+
previous: []int{
67+
1,
68+
2,
69+
0,
70+
3,
71+
},
72+
newest: []int{
73+
1,
74+
3,
75+
0,
76+
},
77+
steady: []int{
78+
0,
79+
1,
80+
3,
81+
},
82+
added: []int{},
83+
dropped: []int{
84+
2,
85+
},
86+
},
87+
{
88+
name: "AddedAndDropped1",
89+
previous: []int{
90+
4,
91+
7,
92+
8,
93+
},
94+
newest: []int{
95+
1,
96+
3,
97+
0,
98+
},
99+
steady: []int{},
100+
added: []int{
101+
0,
102+
1,
103+
3,
104+
},
105+
dropped: []int{
106+
4,
107+
7,
108+
8,
109+
},
110+
},
111+
{
112+
name: "AddedAndDropped2",
113+
previous: []int{
114+
1,
115+
3,
116+
0,
117+
},
118+
newest: []int{
119+
4,
120+
7,
121+
8,
122+
},
123+
steady: []int{},
124+
added: []int{
125+
4,
126+
7,
127+
8,
128+
},
129+
dropped: []int{
130+
0,
131+
1,
132+
3,
133+
},
134+
},
135+
} {
136+
t.Run(tt.name, func(t *testing.T) {
137+
steady, added, dropped := Diff(tt.previous, tt.newest, func(lhs, rhs int) int {
138+
return lhs - rhs
139+
})
140+
require.Equal(t, tt.added, added)
141+
require.Equal(t, tt.dropped, dropped)
142+
require.Equal(t, tt.steady, steady)
143+
})
144+
}
145+
}

internal/xslices/sort.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build go1.21
2+
// +build go1.21
3+
4+
package xslices
5+
6+
import (
7+
"slices"
8+
)
9+
10+
func Sort[T any](in []T, cmp func(lhs, rhs T) int) (out []T) {
11+
out = Clone(in)
12+
13+
slices.SortFunc(out, cmp)
14+
15+
return out
16+
}

internal/xslices/sort_go1.20.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//go:build !go1.21
2+
// +build !go1.21
3+
4+
package xslices
5+
6+
import (
7+
"sort"
8+
)
9+
10+
func Sort[T any](in []T, cmp func(lhs, rhs T) int) (out []T) {
11+
out = Clone(in)
12+
13+
sort.Slice(out, func(i, j int) bool {
14+
return cmp(out[i], out[j]) < 0
15+
})
16+
17+
return out
18+
}

0 commit comments

Comments
 (0)