Skip to content

Commit 1848a0c

Browse files
committed
refactor internal code
1 parent e8f5572 commit 1848a0c

14 files changed

+130
-129
lines changed

common.go

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,6 @@ import (
4040
// internal/nodes. Aliased to keep the code readable.
4141
type stridePath = nodes.StridePath
4242

43-
// Equaler is a generic interface for types that can decide their own
44-
// equality logic. It can be used to override the potentially expensive
45-
// default comparison with [reflect.DeepEqual].
46-
type Equaler[V any] interface {
47-
Equal(other V) bool
48-
}
49-
50-
// Cloner is an interface that enables deep cloning of values of type V.
51-
// If a value implements Cloner[V], Table methods such as InsertPersist,
52-
// ModifyPersist, DeletePersist, UnionPersist, Union and Clone will use
53-
// its Clone method to perform deep copies.
54-
type Cloner[V any] interface {
55-
Clone() V
56-
}
57-
5843
// DumpListNode contains CIDR, Value and Subnets, representing the trie
5944
// in a sorted, recursive representation, especially useful for serialization.
6045
type DumpListNode[V any] struct {
@@ -66,10 +51,10 @@ type DumpListNode[V any] struct {
6651
// cloneFnFactory returns a CloneFunc.
6752
// If V implements Cloner[V], the returned function should perform
6853
// a deep copy using Clone(), otherwise it returns nil.
69-
func cloneFnFactory[V any]() nodes.CloneFunc[V] {
54+
func cloneFnFactory[V any]() value.CloneFunc[V] {
7055
var zero V
7156
// you can't assert directly on a type parameter
72-
if _, ok := any(zero).(Cloner[V]); ok {
57+
if _, ok := any(zero).(value.Cloner[V]); ok {
7358
return cloneVal[V]
7459
}
7560
return nil
@@ -81,7 +66,7 @@ func cloneFnFactory[V any]() nodes.CloneFunc[V] {
8166
// cloneVal returns val unchanged.
8267
func cloneVal[V any](val V) V {
8368
// you can't assert directly on a type parameter
84-
c, ok := any(val).(Cloner[V])
69+
c, ok := any(val).(value.Cloner[V])
8570
if !ok || c == nil {
8671
return val
8772
}

internal/nodes/bart.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/gaissmai/bart/internal/lpm"
1010
"github.com/gaissmai/bart/internal/sparse"
11+
"github.com/gaissmai/bart/internal/value"
1112
)
1213

1314
// BartNode is a trie level BartNode in the multibit routing table.
@@ -191,7 +192,7 @@ func (n *BartNode[V]) Lookup(idx uint8) (val V, ok bool) {
191192
//
192193
// Note: The returned node is a new instance with copied slices but only shallow copies of nested nodes,
193194
// except for LeafNode and FringeNode children which are cloned according to cloneFn.
194-
func (n *BartNode[V]) CloneFlat(cloneFn CloneFunc[V]) *BartNode[V] {
195+
func (n *BartNode[V]) CloneFlat(cloneFn value.CloneFunc[V]) *BartNode[V] {
195196
if n == nil {
196197
return nil
197198
}
@@ -248,7 +249,7 @@ func (n *BartNode[V]) CloneFlat(cloneFn CloneFunc[V]) *BartNode[V] {
248249
//
249250
// Returns a new instance of BartNode[V] which is a complete deep clone of the
250251
// receiver node with all descendants.
251-
func (n *BartNode[V]) CloneRec(cloneFn CloneFunc[V]) *BartNode[V] {
252+
func (n *BartNode[V]) CloneRec(cloneFn value.CloneFunc[V]) *BartNode[V] {
252253
if n == nil {
253254
return nil
254255
}

internal/nodes/bartmethodsgenerated.go

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/nodes/commonmethods_tmpl.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,23 @@ type _NODE_TYPE[V any] struct {
3131
Children struct{ bitset.BitSet256 }
3232
}
3333

34-
func (n *_NODE_TYPE[V]) IsEmpty() (_ bool) { return }
35-
func (n *_NODE_TYPE[V]) PrefixCount() (_ int) { return }
36-
func (n *_NODE_TYPE[V]) ChildCount() (_ int) { return }
37-
func (n *_NODE_TYPE[V]) MustGetPrefix(uint8) (_ V) { return }
38-
func (n *_NODE_TYPE[V]) MustGetChild(uint8) (_ any) { return }
39-
func (n *_NODE_TYPE[V]) InsertPrefix(uint8, V) (_ bool) { return }
40-
func (n *_NODE_TYPE[V]) DeletePrefix(uint8) (_ bool) { return }
41-
func (n *_NODE_TYPE[V]) GetChild(uint8) (_ any, _ bool) { return }
42-
func (n *_NODE_TYPE[V]) GetPrefix(uint8) (_ V, _ bool) { return }
43-
func (n *_NODE_TYPE[V]) InsertChild(uint8, any) (_ bool) { return }
44-
func (n *_NODE_TYPE[V]) DeleteChild(uint8) (_ bool) { return }
45-
func (n *_NODE_TYPE[V]) CloneRec(CloneFunc[V]) (_ *_NODE_TYPE[V]) { return }
46-
func (n *_NODE_TYPE[V]) CloneFlat(CloneFunc[V]) (_ *_NODE_TYPE[V]) { return }
47-
func (n *_NODE_TYPE[V]) AllIndices() (seq2 iter.Seq2[uint8, V]) { return }
48-
func (n *_NODE_TYPE[V]) AllChildren() (seq2 iter.Seq2[uint8, any]) { return }
49-
func (n *_NODE_TYPE[V]) Contains(uint8) (_ bool) { return }
50-
func (n *_NODE_TYPE[V]) LookupIdx(uint8) (_ uint8, _ V, _ bool) { return }
34+
func (n *_NODE_TYPE[V]) IsEmpty() (_ bool) { return }
35+
func (n *_NODE_TYPE[V]) PrefixCount() (_ int) { return }
36+
func (n *_NODE_TYPE[V]) ChildCount() (_ int) { return }
37+
func (n *_NODE_TYPE[V]) MustGetPrefix(uint8) (_ V) { return }
38+
func (n *_NODE_TYPE[V]) MustGetChild(uint8) (_ any) { return }
39+
func (n *_NODE_TYPE[V]) InsertPrefix(uint8, V) (_ bool) { return }
40+
func (n *_NODE_TYPE[V]) DeletePrefix(uint8) (_ bool) { return }
41+
func (n *_NODE_TYPE[V]) GetChild(uint8) (_ any, _ bool) { return }
42+
func (n *_NODE_TYPE[V]) GetPrefix(uint8) (_ V, _ bool) { return }
43+
func (n *_NODE_TYPE[V]) InsertChild(uint8, any) (_ bool) { return }
44+
func (n *_NODE_TYPE[V]) DeleteChild(uint8) (_ bool) { return }
45+
func (n *_NODE_TYPE[V]) CloneRec(value.CloneFunc[V]) (_ *_NODE_TYPE[V]) { return }
46+
func (n *_NODE_TYPE[V]) CloneFlat(value.CloneFunc[V]) (_ *_NODE_TYPE[V]) { return }
47+
func (n *_NODE_TYPE[V]) AllIndices() (seq2 iter.Seq2[uint8, V]) { return }
48+
func (n *_NODE_TYPE[V]) AllChildren() (seq2 iter.Seq2[uint8, any]) { return }
49+
func (n *_NODE_TYPE[V]) Contains(uint8) (_ bool) { return }
50+
func (n *_NODE_TYPE[V]) LookupIdx(uint8) (_ uint8, _ V, _ bool) { return }
5151

5252
// ### GENERATE DELETE END ###
5353

@@ -145,7 +145,7 @@ func (n *_NODE_TYPE[V]) Insert(pfx netip.Prefix, val V, depth int) (exists bool)
145145
// InsertPersist is similar to insert but the receiver isn't modified.
146146
// Assumes the caller has pre-cloned the root (COW). It clones the
147147
// internal nodes along the descent path before mutating them.
148-
func (n *_NODE_TYPE[V]) InsertPersist(cloneFn CloneFunc[V], pfx netip.Prefix, val V, depth int) (exists bool) {
148+
func (n *_NODE_TYPE[V]) InsertPersist(cloneFn value.CloneFunc[V], pfx netip.Prefix, val V, depth int) (exists bool) {
149149
ip := pfx.Addr() // the pfx must be in canonical form
150150
octets := ip.AsSlice()
151151
lastOctetPlusOne, lastBits := LastOctetPlusOneAndLastBits(pfx)
@@ -396,7 +396,7 @@ func (n *_NODE_TYPE[V]) Delete(pfx netip.Prefix) (exists bool) {
396396
// DeletePersist is similar to delete but does not mutate the original trie.
397397
// Assumes the caller has pre-cloned the root (COW). It clones the
398398
// internal nodes along the descent path before mutating them.
399-
func (n *_NODE_TYPE[V]) DeletePersist(cloneFn CloneFunc[V], pfx netip.Prefix) (exists bool) {
399+
func (n *_NODE_TYPE[V]) DeletePersist(cloneFn value.CloneFunc[V], pfx netip.Prefix) (exists bool) {
400400
ip := pfx.Addr() // the pfx must be in canonical form
401401
is4 := ip.Is4()
402402
octets := ip.AsSlice()
@@ -745,7 +745,7 @@ func (n *_NODE_TYPE[V]) EqualRec(o *_NODE_TYPE[V]) bool {
745745

746746
for idx, nVal := range n.AllIndices() {
747747
oVal := o.MustGetPrefix(idx) // mustGet is ok, bitsets are equal
748-
if !Equal(nVal, oVal) {
748+
if !value.Equal(nVal, oVal) {
749749
return false
750750
}
751751
}
@@ -779,7 +779,7 @@ func (n *_NODE_TYPE[V]) EqualRec(o *_NODE_TYPE[V]) bool {
779779
}
780780

781781
// compare values
782-
if !Equal(nKid.Value, oKid.Value) {
782+
if !value.Equal(nKid.Value, oKid.Value) {
783783
return false
784784
}
785785

@@ -791,7 +791,7 @@ func (n *_NODE_TYPE[V]) EqualRec(o *_NODE_TYPE[V]) bool {
791791
}
792792

793793
// compare values
794-
if !Equal(nKid.Value, oKid.Value) {
794+
if !value.Equal(nKid.Value, oKid.Value) {
795795
return false
796796
}
797797

@@ -1257,9 +1257,9 @@ func (n *_NODE_TYPE[V]) DirectItemsRec(parentIdx uint8, path StridePath, depth i
12571257
// The merge operation is destructive on the receiver n, but leaves the source node o unchanged.
12581258
//
12591259
// Returns the number of duplicate prefixes that were overwritten during merging.
1260-
func (n *_NODE_TYPE[V]) UnionRec(cloneFn CloneFunc[V], o *_NODE_TYPE[V], depth int) (duplicates int) {
1260+
func (n *_NODE_TYPE[V]) UnionRec(cloneFn value.CloneFunc[V], o *_NODE_TYPE[V], depth int) (duplicates int) {
12611261
if cloneFn == nil {
1262-
cloneFn = copyVal
1262+
cloneFn = value.CopyVal
12631263
}
12641264

12651265
buf := [256]uint8{}
@@ -1290,9 +1290,9 @@ func (n *_NODE_TYPE[V]) UnionRec(cloneFn CloneFunc[V], o *_NODE_TYPE[V], depth i
12901290
}
12911291

12921292
// UnionRecPersist is similar to unionRec but performs an immutable union of nodes.
1293-
func (n *_NODE_TYPE[V]) UnionRecPersist(cloneFn CloneFunc[V], o *_NODE_TYPE[V], depth int) (duplicates int) {
1293+
func (n *_NODE_TYPE[V]) UnionRecPersist(cloneFn value.CloneFunc[V], o *_NODE_TYPE[V], depth int) (duplicates int) {
12941294
if cloneFn == nil {
1295-
cloneFn = copyVal
1295+
cloneFn = value.CopyVal
12961296
}
12971297

12981298
buf := [256]uint8{}
@@ -1341,7 +1341,7 @@ func (n *_NODE_TYPE[V]) UnionRecPersist(cloneFn CloneFunc[V], o *_NODE_TYPE[V],
13411341
// fringe, node <-- insert new node, push this fringe down, union rec-descent
13421342
// fringe, leaf <-- insert new node, push this fringe down, insert other leaf at depth+1
13431343
// fringe, fringe <-- just overwrite value
1344-
func (n *_NODE_TYPE[V]) handleMatrix(cloneFn CloneFunc[V], thisExists bool, thisChild, otherChild any, addr uint8, depth int) int {
1344+
func (n *_NODE_TYPE[V]) handleMatrix(cloneFn value.CloneFunc[V], thisExists bool, thisChild, otherChild any, addr uint8, depth int) int {
13451345
// Do ALL type assertions upfront - reduces line noise
13461346
var (
13471347
thisNode, thisIsNode = thisChild.(*_NODE_TYPE[V])
@@ -1458,7 +1458,7 @@ func (n *_NODE_TYPE[V]) handleMatrix(cloneFn CloneFunc[V], thisExists bool, this
14581458
// fringe, node <-- insert new node, push this fringe down, union rec-descent
14591459
// fringe, leaf <-- insert new node, push this fringe down, insert other leaf at depth+1
14601460
// fringe, fringe <-- just overwrite value
1461-
func (n *_NODE_TYPE[V]) handleMatrixPersist(cloneFn CloneFunc[V], thisExists bool, thisChild, otherChild any, addr uint8, depth int) int {
1461+
func (n *_NODE_TYPE[V]) handleMatrixPersist(cloneFn value.CloneFunc[V], thisExists bool, thisChild, otherChild any, addr uint8, depth int) int {
14621462
// Do ALL type assertions upfront - reduces line noise
14631463
var (
14641464
thisNode, thisIsNode = thisChild.(*_NODE_TYPE[V])

internal/nodes/commontests_tmpl.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,8 @@ func TestDump_NonZST__NODE_TYPE(t *testing.T) {
689689

690690
// TestFprintRec_ZST verifies FprintRec does not print values for zero-sized types.
691691
func TestFprintRec_ZST__NODE_TYPE(t *testing.T) {
692+
t.Parallel()
693+
692694
node := new(_NODE_TYPE[struct{}])
693695

694696
pfx := mpp("10.0.0.0/7")

internal/nodes/fast.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/gaissmai/bart/internal/bitset"
1010
"github.com/gaissmai/bart/internal/lpm"
11+
"github.com/gaissmai/bart/internal/value"
1112
)
1213

1314
// FastNode is a trie level node in the multibit routing table.
@@ -297,7 +298,7 @@ func (n *FastNode[V]) allot(idx uint8, oldValPtr, valPtr *V) {
297298
// CloneFlat returns a shallow copy of the current FastNode[V],
298299
// Its semantics are identical to [bartNode.CloneFlat] but the
299300
// implementation is more complex.
300-
func (n *FastNode[V]) CloneFlat(cloneFn CloneFunc[V]) *FastNode[V] {
301+
func (n *FastNode[V]) CloneFlat(cloneFn value.CloneFunc[V]) *FastNode[V] {
301302
if n == nil {
302303
return nil
303304
}
@@ -354,7 +355,7 @@ func (n *FastNode[V]) CloneFlat(cloneFn CloneFunc[V]) *FastNode[V] {
354355

355356
// CloneRec performs a recursive deep copy of the FastNode[V] and all its descendants.
356357
// Its semantics are identical to [bartNode.cloneRec].
357-
func (n *FastNode[V]) CloneRec(cloneFn CloneFunc[V]) *FastNode[V] {
358+
func (n *FastNode[V]) CloneRec(cloneFn value.CloneFunc[V]) *FastNode[V] {
358359
if n == nil {
359360
return nil
360361
}

0 commit comments

Comments
 (0)