Skip to content

Commit 2f47f4f

Browse files
committed
implement UnionPersist
1 parent 4ea8cc3 commit 2f47f4f

File tree

10 files changed

+783
-339
lines changed

10 files changed

+783
-339
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ See the `ExampleLite_concurrent` and `ExampleTable_concurrent` tests for concret
9393
func (t *Table[V]) GetAndDelete(pfx netip.Prefix) (val V, ok bool)
9494
func (t *Table[V]) GetAndDeletePersist(pfx netip.Prefix) (pt *Table[V], val V, ok bool)
9595

96-
func (t *Table[V]) Union(o *Table[V])
9796
func (t *Table[V]) Clone() *Table[V]
97+
func (t *Table[V]) Union(o *Table[V])
98+
func (t *Table[V]) UnionPersist(o *Table[V]) *Table[V]
9899

99100
func (t *Table[V]) OverlapsPrefix(pfx netip.Prefix) bool
100101

@@ -149,8 +150,9 @@ Some delegated methods are pointless without a payload.
149150
func (l *Lite) InsertPersist(pfx netip.Prefix) *Lite
150151
func (l *Lite) DeletePersist(pfx netip.Prefix) *Lite
151152

152-
func (l *Lite) Union(o *Lite)
153153
func (l *Lite) Clone() *Lite
154+
func (l *Lite) Union(o *Lite)
155+
func (l *Lite) UnionPersist(o *Lite) *Lite
154156

155157
func (l *Lite) Overlaps(o *Lite) bool
156158
func (l *Lite) Overlaps4(o *Lite) bool

cloner.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ func cloneVal[V any](val V) V {
2828
return any(val).(Cloner[V]).Clone()
2929
}
3030

31+
// copyVal just copies the value.
32+
func copyVal[V any](val V) V {
33+
return val
34+
}
35+
3136
// cloneLeaf creates and returns a copy of the leafNode receiver.
3237
// If cloneFn is nil, the value is copied directly without modification.
3338
// Otherwise, cloneFn is applied to the value for deep cloning.

lite.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ func (l *Lite) Union(o *Lite) {
7171
l.Table.Union(&o.Table)
7272
}
7373

74+
// UnionPersist is an adapter for the underlying table.
75+
func (l *Lite) UnionPersist(o *Lite) *Lite {
76+
tbl := l.Table.UnionPersist(&o.Table)
77+
//nolint:govet // copy of *tbl is here by intention
78+
return &Lite{*tbl}
79+
}
80+
7481
// Overlaps4 is an adapter for the underlying table.
7582
func (l *Lite) Overlaps4(o *Lite) bool {
7683
return l.Table.Overlaps4(&o.Table)

lite_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,40 @@ func TestLiteUnion(t *testing.T) {
676676
}
677677
}
678678

679+
func TestLiteUnionPersist(t *testing.T) {
680+
t.Parallel()
681+
682+
for range 10 {
683+
t.Run("Union", func(t *testing.T) {
684+
t.Parallel()
685+
prng := rand.New(rand.NewPCG(42, 42))
686+
pfx1 := randomRealWorldPrefixes(prng, 1_000)
687+
pfx2 := randomRealWorldPrefixes(prng, 2_000)
688+
689+
golden := new(Lite)
690+
for _, pfx := range append(pfx1, pfx2...) {
691+
golden.Insert(pfx)
692+
}
693+
694+
tbl1 := new(Lite)
695+
for _, pfx := range pfx1 {
696+
tbl1.Insert(pfx)
697+
}
698+
699+
tbl2 := new(Lite)
700+
for _, pfx := range pfx2 {
701+
tbl2.Insert(pfx)
702+
}
703+
704+
pTbl := tbl1.UnionPersist(tbl2)
705+
706+
if pTbl.dumpString() != golden.dumpString() {
707+
t.Errorf("UnionPersist: got:\n%swant:\n%s", pTbl.dumpString(), golden.dumpString())
708+
}
709+
})
710+
}
711+
}
712+
679713
func TestLiteStringEmpty(t *testing.T) {
680714
t.Parallel()
681715
tbl := new(Lite)

0 commit comments

Comments
 (0)