Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed May 6, 2024
1 parent a2710ad commit e9518c6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
44 changes: 18 additions & 26 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ func (t *Table[V]) Get(pfx netip.Prefix) (val V, ok bool) {
return
}
n = c
continue
}
return n.getValByPrefix(lastOctet, lastOctetBits)
}
Expand All @@ -181,10 +180,9 @@ func (t *Table[V]) Delete(pfx netip.Prefix) {
lastOctetBits := bits - (lastOctetIdx * strideLen)

// record path to deleted node
// purge dangling nodes after deletion
stack := [maxTreeDepth]*node[V]{}

// run variables, needed after for loop
// run variable as stackPointer, see below
var i int

// find the trie node
Expand All @@ -193,26 +191,24 @@ func (t *Table[V]) Delete(pfx netip.Prefix) {
stack[i] = n

if i == lastOctetIdx {
// try to delete prefix in trie node
if !n.deletePrefix(lastOctet, lastOctetBits) {
// prefix not in tree, nothing deleted
// nothing deleted
return
}

// escape, but purge dangling path if needed, see below
// maybe purge needed
break
}

// descend down to next level
if c := n.getChild(octets[i]); c != nil {
n = c
continue
c := n.getChild(octets[i])
if c == nil {
return
}

// no child found, nothing to delete
return
n = c
}

// purge dangling paths
// purge dangling nodes after successful deletion
for i > 0 {
if n.isEmpty() {
// purge empty node from parents children
Expand Down Expand Up @@ -252,12 +248,11 @@ func (t *Table[V]) Lookup(ip netip.Addr) (val V, ok bool) {
stack[i] = n

// go down in tight loop to leaf node
if c := n.getChild(octets[i]); c != nil {
n = c
continue
c := n.getChild(octets[i])
if c == nil {
break
}

break
n = c
}

// start backtracking at leaf node in tight loop
Expand Down Expand Up @@ -341,13 +336,12 @@ func (t *Table[V]) lpmByPrefix(pfx netip.Prefix) (depth int, baseIdx uint, val V
}

// go down in tight loop to leaf node
if c := n.getChild(octets[depth]); c != nil {
n = c
continue
c := n.getChild(octets[depth])
if c == nil {
pfxLen = strideLen
break
}

pfxLen = strideLen
break
n = c
}

// start backtracking with last node and octet
Expand Down Expand Up @@ -407,7 +401,6 @@ func (t *Table[V]) Subnets(pfx netip.Prefix) []netip.Prefix {
}

n = c
continue
}
return nil
}
Expand Down Expand Up @@ -449,7 +442,6 @@ func (t *Table[V]) Supernets(pfx netip.Prefix) []netip.Prefix {
break
}
n = c
continue
}

return result
Expand Down
2 changes: 1 addition & 1 deletion table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ func TestDeleteIsReverseOfInsert(t *testing.T) {
// Insert N prefixes, then delete those same prefixes in reverse
// order. Each deletion should exactly undo the internal structure
// changes that each insert did.
const N = 100
const N = 10_000

var tab Table[int]
prefixes := randomPrefixes(N)
Expand Down

0 comments on commit e9518c6

Please sign in to comment.