Skip to content

Commit

Permalink
harmonize code and var names
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Jan 3, 2025
1 parent ebb8dea commit 8400795
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 95 deletions.
21 changes: 13 additions & 8 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (n *node[V]) insertAtDepth(pfx netip.Prefix, val V, depth int) (exists bool
// 10.12.0.0/15 -> 12
// 10.12.0.0/16 -> 12
// 10.12.10.9/32 -> 9
sigOctet := octets[sigOctetIdx]
// sigOctet := octets[sigOctetIdx]

// 10.0.0.0/8 -> 8
// 10.12.0.0/15 -> 7
Expand All @@ -143,8 +143,14 @@ func (n *node[V]) insertAtDepth(pfx netip.Prefix, val V, depth int) (exists bool

// find the proper trie node to insert prefix
// start with prefix octet at depth
for ; depth < sigOctetIdx; depth++ {
addr := uint(octets[depth])
for ; depth <= sigOctetIdx; depth++ {
octet := octets[depth]
addr := uint(octet)

// last significant octet: insert/override prefix/val into node
if depth == sigOctetIdx {
return n.prefixes.InsertAt(pfxToIdx(octet, sigOctetBits), val)
}

if !n.children.Test(addr) {
// insert prefix path compressed
Expand All @@ -156,6 +162,7 @@ func (n *node[V]) insertAtDepth(pfx netip.Prefix, val V, depth int) (exists bool
case *node[V]:
// descend down to next trie level
n = k
continue
case *leaf[V]:
// reached a path compressed prefix
// override value in slot if prefixes are equal
Expand All @@ -177,8 +184,7 @@ func (n *node[V]) insertAtDepth(pfx netip.Prefix, val V, depth int) (exists bool
}
}

// last significant octet: insert/override prefix/val into node
return n.prefixes.InsertAt(pfxToIdx(sigOctet, sigOctetBits), val)
panic("unreachable")
}

// purgeAndCompress, purge empty nodes or compress nodes with single prefix or leaf.
Expand Down Expand Up @@ -211,9 +217,8 @@ func (n *node[V]) purgeAndCompress(parentStack []*node[V], childPath []byte, is4
case pfxCount == 0 && childCount == 1:
// if single child is a leaf, shift it up one level
// and override current node with this leaf
child := n.children.Items[0]
if k, ok := child.(*leaf[V]); ok {
parent.children.InsertAt(addr, k)
if leafPtr, ok := n.children.Items[0].(*leaf[V]); ok {
parent.children.InsertAt(addr, leafPtr)
}
}

Expand Down
17 changes: 11 additions & 6 deletions overlaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,21 @@ func overlapsTwoChilds[V any](nChild, oChild any, depth int) bool {
// Needed for path compressed prefix some level down in the node trie.
func (n *node[V]) overlapsPrefixAtDepth(pfx netip.Prefix, depth int) bool {
ip := pfx.Addr()
octets := ip.AsSlice()
bits := pfx.Bits()

sigOctetIdx := (bits - 1) / strideLen
sigOctetBits := bits - (sigOctetIdx * strideLen)

for i := depth; i < sigOctetIdx; i++ {
addr := uint(octets[i])
octets := ip.AsSlice()

for ; depth <= sigOctetIdx; depth++ {
octet := octets[depth]
addr := uint(octet)

// full octet path in node trie, check overlap with last prefix octet
if depth == sigOctetIdx {
return n.overlapsIdx(octet, sigOctetBits)
}

// test if any route overlaps prefix´ so far
// no best match needed, forward tests without backtracking
Expand All @@ -246,7 +253,6 @@ func (n *node[V]) overlapsPrefixAtDepth(pfx netip.Prefix, depth int) bool {
}

if !n.children.Test(addr) {
// no full octet path in node trie
return false
}

Expand All @@ -260,8 +266,7 @@ func (n *node[V]) overlapsPrefixAtDepth(pfx netip.Prefix, depth int) bool {
}
}

// full octet path in node trie, check overlap with last prefix octet
return n.overlapsIdx(octets[sigOctetIdx], sigOctetBits)
panic("unreachable")
}

// overlapsIdx returns true if node overlaps with prefix.
Expand Down
Loading

0 comments on commit 8400795

Please sign in to comment.