Skip to content

Commit

Permalink
internal renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Apr 20, 2024
1 parent 0166bb9 commit 84fa5bd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
10 changes: 5 additions & 5 deletions dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (t *Table[V]) dump(w io.Writer) error {
func (n *node[V]) dumpRec(w io.Writer, path []byte, is4 bool) {
n.dump(w, path, is4)

for i, child := range n.children.nodes {
for i, child := range n.children.childs {
octet := n.children.Select(uint(i))
child.dumpRec(w, append(path, byte(octet)), is4)
}
Expand Down Expand Up @@ -104,11 +104,11 @@ func (n *node[V]) dump(w io.Writer, path []byte, is4 bool) {
must(fmt.Fprintln(w))
}

if len(n.children.nodes) != 0 {
if len(n.children.childs) != 0 {
// print the childs for this node
must(fmt.Fprintf(w, "%schilds(#%d): ", indent, len(n.children.nodes)))
must(fmt.Fprintf(w, "%schilds(#%d): ", indent, len(n.children.childs)))

for i := range n.children.nodes {
for i := range n.children.childs {
octet := n.children.Select(uint(i))
must(fmt.Fprintf(w, "%s ", octetFmt(octet, is4)))
}
Expand Down Expand Up @@ -165,7 +165,7 @@ func (nt nodeType) String() string {
// hasType returns the nodeType.
func (n *node[V]) hasType() nodeType {
lenPefixes := len(n.prefixes.values)
lenChilds := len(n.children.nodes)
lenChilds := len(n.children.childs)

if lenPefixes == 0 && lenChilds != 0 {
return intermediateNode
Expand Down
6 changes: 3 additions & 3 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (t *Table[V]) readTableStats() map[string]any {
switch is4 {
case true:
stats.size4 += len(n.prefixes.values)
stats.childs4[len(n.children.nodes)]++
stats.childs4[len(n.children.childs)]++
stats.depth4[depth]++
stats.types4[n.hasType().String()]++

Expand All @@ -69,7 +69,7 @@ func (t *Table[V]) readTableStats() map[string]any {
}
case false:
stats.size6 += len(n.prefixes.values)
stats.childs6[len(n.children.nodes)]++
stats.childs6[len(n.children.childs)]++
stats.depth6[depth]++
stats.types6[n.hasType().String()]++

Expand Down Expand Up @@ -121,7 +121,7 @@ func (t *Table[V]) walk(cb metricWalkFunc[V]) {
func (n *node[V]) metricWalkRec(cb metricWalkFunc[V], depth int, is4 bool) {
cb(n, depth, is4)

for _, child := range n.children.nodes {
for _, child := range n.children.childs {
child.metricWalkRec(cb, depth+1, is4)
}
}
46 changes: 23 additions & 23 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
// A node can have prefixes or child nodes or both.
type node[V any] struct {
prefixes *strideTree[V]
children *childTree[V]
children *childSlice[V]
}

// strideTree, complete-binary-tree, popcount-compressed.
Expand All @@ -40,10 +40,10 @@ type strideTree[V any] struct {
values []V
}

// childTree, just a slice with nodes, also popcount-compressed
type childTree[V any] struct {
// childSlice, a slice with nodes, popcount-compressed
type childSlice[V any] struct {
*bitset.BitSet
nodes []*node[V]
childs []*node[V]
}

// newNode, BitSets have to be initialized.
Expand All @@ -54,9 +54,9 @@ func newNode[V any]() *node[V] {
values: nil,
},

children: &childTree[V]{
children: &childSlice[V]{
BitSet: bitset.New(0), // init BitSet
nodes: nil,
childs: nil,
},
}
}
Expand Down Expand Up @@ -231,44 +231,44 @@ func (p *strideTree[V]) allIndexes() []uint {

// rank is the key of the popcount compression algorithm,
// mapping between bitset index and slice index.
func (c *childTree[V]) rank(octet uint) int {
func (c *childSlice[V]) rank(octet uint) int {
return int(c.Rank(octet)) - 1
}

// insert the child into childTree.
func (c *childTree[V]) insert(octet uint, child *node[V]) {
func (c *childSlice[V]) insert(octet uint, child *node[V]) {
// insert into bitset and slice
c.Set(octet)
c.nodes = slices.Insert(c.nodes, c.rank(octet), child)
c.childs = slices.Insert(c.childs, c.rank(octet), child)
}

// delete the child at octet. It is valid to delete a non-existent child.
func (c *childTree[V]) delete(octet uint) {
func (c *childSlice[V]) delete(octet uint) {
if !c.Test(octet) {
return
}

rnk := c.rank(octet)

// delete from slice
c.nodes = slices.Delete(c.nodes, rnk, rnk+1)
c.childs = slices.Delete(c.childs, rnk, rnk+1)

// delete from bitset, followed by Compact to reduce memory consumption
c.Clear(octet)
c.Compact()
}

// get returns the child pointer for octet, or nil if none.
func (c *childTree[V]) get(octet uint) *node[V] {
func (c *childSlice[V]) get(octet uint) *node[V] {
if !c.Test(octet) {
return nil
}

return c.nodes[c.rank(octet)]
return c.childs[c.rank(octet)]
}

// allOctets returns the octets of all child nodes in ascending order.
func (c *childTree[V]) allOctets() []uint {
func (c *childSlice[V]) allOctets() []uint {
all := make([]uint, maxNodeChildren)
_, all = c.NextSetMany(0, all)
return all
Expand All @@ -278,7 +278,7 @@ func (c *childTree[V]) allOctets() []uint {

// isEmpty returns true if node has neither prefixes nor children.
func (n *node[V]) isEmpty() bool {
return len(n.prefixes.values) == 0 && len(n.children.nodes) == 0
return len(n.prefixes.values) == 0 && len(n.children.childs) == 0
}

// overlapsRec returns true if any IP in the nodes n or o overlaps.
Expand Down Expand Up @@ -352,8 +352,8 @@ func (n *node[V]) overlapsRec(o *node[V]) bool {
nOctets := [maxNodeChildren]bool{}
oOctets := [maxNodeChildren]bool{}

nOk = len(n.children.nodes) > 0
oOk = len(o.children.nodes) > 0
nOk = len(n.children.childs) > 0
oOk = len(o.children.childs) > 0
var nOctet, oOctet uint
// zig-zag, for all octets in both nodes ...
for {
Expand Down Expand Up @@ -386,7 +386,7 @@ func (n *node[V]) overlapsRec(o *node[V]) bool {

// 3. rec-descent call for childs with same octet

if len(n.children.nodes) > 0 && len(o.children.nodes) > 0 {
if len(n.children.childs) > 0 && len(o.children.childs) > 0 {
for i := 0; i < len(nOctets); i++ {
if nOctets[i] && oOctets[i] {
// get next child node for this octet
Expand Down Expand Up @@ -504,14 +504,14 @@ func (n *node[V]) cloneRec() *node[V] {
return c
}

c.prefixes.BitSet = n.prefixes.Clone() // deep
c.prefixes.BitSet = n.prefixes.BitSet.Clone() // deep
c.prefixes.values = slices.Clone(n.prefixes.values) // shallow

c.children.BitSet = n.children.Clone() // deep
c.children.nodes = slices.Clone(n.children.nodes) // shallow
c.children.BitSet = n.children.BitSet.Clone() // deep
c.children.childs = slices.Clone(n.children.childs) // shallow
// make it deep
for i, child := range c.children.nodes {
c.children.nodes[i] = child.cloneRec()
for i, child := range c.children.childs {
c.children.childs[i] = child.cloneRec()
}

return c
Expand Down
4 changes: 2 additions & 2 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1350,10 +1350,10 @@ func (t *Table[V]) numNodes() int {

func (t *Table[V]) numNodesRec(seen map[*node[V]]bool, n *node[V]) int {
ret := 1
if len(n.children.nodes) == 0 {
if len(n.children.childs) == 0 {
return ret
}
for _, c := range n.children.nodes {
for _, c := range n.children.childs {
if seen[c] {
continue
}
Expand Down

0 comments on commit 84fa5bd

Please sign in to comment.