Skip to content

Commit 8edfa40

Browse files
committed
refactor internal code
1 parent e8f5572 commit 8edfa40

23 files changed

+294
-214
lines changed

bartmethodsgenerated.go

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

common.go

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525
package bart
2626

2727
import (
28-
"fmt"
2928
"net/netip"
3029

3130
"github.com/gaissmai/bart/internal/nodes"
32-
"github.com/gaissmai/bart/internal/value"
3331

3432
// inlining hint, see also the TestInlineBitSet256Functions.
3533
// without this silent import the BitSet256 functions are not inlined
@@ -40,58 +38,10 @@ import (
4038
// internal/nodes. Aliased to keep the code readable.
4139
type stridePath = nodes.StridePath
4240

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-
5841
// DumpListNode contains CIDR, Value and Subnets, representing the trie
5942
// in a sorted, recursive representation, especially useful for serialization.
6043
type DumpListNode[V any] struct {
6144
CIDR netip.Prefix `json:"cidr"`
6245
Value V `json:"value"`
6346
Subnets []DumpListNode[V] `json:"subnets,omitempty"`
6447
}
65-
66-
// cloneFnFactory returns a CloneFunc.
67-
// If V implements Cloner[V], the returned function should perform
68-
// a deep copy using Clone(), otherwise it returns nil.
69-
func cloneFnFactory[V any]() nodes.CloneFunc[V] {
70-
var zero V
71-
// you can't assert directly on a type parameter
72-
if _, ok := any(zero).(Cloner[V]); ok {
73-
return cloneVal[V]
74-
}
75-
return nil
76-
}
77-
78-
// cloneVal returns a deep clone of val by calling Clone when
79-
// val implements Cloner[V]. If val does not implement
80-
// Cloner[V] or the Cloner receiver is nil (val is a nil pointer),
81-
// cloneVal returns val unchanged.
82-
func cloneVal[V any](val V) V {
83-
// you can't assert directly on a type parameter
84-
c, ok := any(val).(Cloner[V])
85-
if !ok || c == nil {
86-
return val
87-
}
88-
return c.Clone()
89-
}
90-
91-
// panicOnZST panics if V is a zero sized type.
92-
// bart.Fast must reject zero-sized types as payload.
93-
func panicOnZST[V any]() {
94-
if value.IsZST[V]() {
95-
panic(fmt.Errorf("%T is a zero-sized type, not allowed as payload for bart.Fast", *new(V)))
96-
}
97-
}

commonmethods_tmpl.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"strings"
2424

2525
"github.com/gaissmai/bart/internal/nodes"
26+
"github.com/gaissmai/bart/internal/value"
2627
)
2728

2829
type _NODE_TYPE[V any] struct{}
@@ -140,7 +141,7 @@ func (t *_TABLE_TYPE[V]) insertPersist(pfx netip.Prefix, val V) *_TABLE_TYPE[V]
140141

141142
// Create a cloning function for deep copying values;
142143
// returns nil if V does not implement the Cloner interface.
143-
cloneFn := cloneFnFactory[V]()
144+
cloneFn := value.CloneFnFactory[V]()
144145

145146
// Clone root node corresponding to the IP version, for copy-on-write.
146147
n := &pt.root4
@@ -243,7 +244,7 @@ func (t *_TABLE_TYPE[V]) DeletePersist(pfx netip.Prefix) *_TABLE_TYPE[V] {
243244

244245
// Create a cloning function for deep copying values;
245246
// returns nil if V does not implement the Cloner interface.
246-
cloneFn := cloneFnFactory[V]()
247+
cloneFn := value.CloneFnFactory[V]()
247248

248249
// Clone root node corresponding to the IP version, for copy-on-write.
249250
if is4 {
@@ -317,7 +318,7 @@ func (t *_TABLE_TYPE[V]) ModifyPersist(pfx netip.Prefix, cb func(_ V, ok bool) (
317318
val := oldVal
318319

319320
// to clone or not to clone ...
320-
cloneFn := cloneFnFactory[V]()
321+
cloneFn := value.CloneFnFactory[V]()
321322
if cloneFn != nil && ok {
322323
val = cloneFn(oldVal)
323324
}
@@ -483,7 +484,7 @@ func (t *_TABLE_TYPE[V]) Union(o *_TABLE_TYPE[V]) {
483484

484485
// Create a cloning function for deep copying values;
485486
// returns nil if V does not implement the Cloner interface.
486-
cloneFn := cloneFnFactory[V]()
487+
cloneFn := value.CloneFnFactory[V]()
487488

488489
dup4 := t.root4.UnionRec(cloneFn, &o.root4, 0)
489490
dup6 := t.root6.UnionRec(cloneFn, &o.root6, 0)
@@ -504,7 +505,7 @@ func (t *_TABLE_TYPE[V]) UnionPersist(o *_TABLE_TYPE[V]) *_TABLE_TYPE[V] {
504505

505506
// Create a cloning function for deep copying values;
506507
// returns nil if V does not implement the Cloner interface.
507-
cloneFn := cloneFnFactory[V]()
508+
cloneFn := value.CloneFnFactory[V]()
508509

509510
// new _TABLE_TYPE with root nodes just copied.
510511
pt := &_TABLE_TYPE[V]{
@@ -560,7 +561,7 @@ func (t *_TABLE_TYPE[V]) Clone() *_TABLE_TYPE[V] {
560561

561562
c := new(_TABLE_TYPE[V])
562563

563-
cloneFn := cloneFnFactory[V]()
564+
cloneFn := value.CloneFnFactory[V]()
564565

565566
c.root4 = *t.root4.CloneRec(cloneFn)
566567
c.root6 = *t.root6.CloneRec(cloneFn)

fast.go

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

1010
"github.com/gaissmai/bart/internal/art"
1111
"github.com/gaissmai/bart/internal/nodes"
12+
"github.com/gaissmai/bart/internal/value"
1213
)
1314

1415
// Fast follows the ART design by Knuth in using fixed arrays at each level
@@ -64,7 +65,7 @@ func (f *Fast[V]) rootNodeByVersion(is4 bool) *nodes.FastNode[V] {
6465
// The prefix is automatically canonicalized using pfx.Masked() to ensure
6566
// consistent behavior regardless of host bits in the input.
6667
func (t *Fast[V]) Insert(pfx netip.Prefix, val V) {
67-
t.once.Do(panicOnZST[V])
68+
t.once.Do(value.PanicOnZST[V])
6869
t.insert(pfx, val)
6970
}
7071

@@ -80,7 +81,7 @@ func (t *Fast[V]) Insert(pfx netip.Prefix, val V) {
8081
// Due to cloning overhead this is significantly slower than Insert,
8182
// typically taking μsec instead of nsec.
8283
func (t *Fast[V]) InsertPersist(pfx netip.Prefix, val V) *Fast[V] {
83-
t.once.Do(panicOnZST[V])
84+
t.once.Do(value.PanicOnZST[V])
8485
return t.insertPersist(pfx, val)
8586
}
8687

fastmethodsgenerated.go

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

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.

0 commit comments

Comments
 (0)