Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed May 30, 2024
1 parent e171abe commit c715379
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
40 changes: 17 additions & 23 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,10 +555,10 @@ func (n *node[V]) allRec(path []byte, is4 bool, yield func(netip.Prefix, V) bool
}

// for all children in this node do ...
for _, addr := range n.allChildAddrs() {
for i, addr := range n.allChildAddrs() {
octet := byte(addr)
path := append(slices.Clone(path), octet)
child := n.getChild(octet)
child := n.children[i]

if !child.allRec(path, is4, yield) {
// premature end of recursion
Expand Down Expand Up @@ -640,8 +640,7 @@ func (n *node[V]) subnets(path []byte, pfxOctet byte, pfxLen int, is4 bool) (res
//
// The iteration is in prefix sort order, it's a very complex implemenation compared with allRec.
func (n *node[V]) allRecSorted(path []byte, is4 bool, yield func(netip.Prefix, V) bool) bool {
// get slice of all child addrs in this node, sorted by addr
allChildren := n.allChildAddrs()
childAddrs := n.allChildAddrs()
childCursor := 0

// get slice of all indexes, sorted by idx
Expand All @@ -664,28 +663,26 @@ func (n *node[V]) allRecSorted(path []byte, is4 bool, yield func(netip.Prefix, V
}

// handle childs before the host routes of idx
for j, addr := range allChildren {
for j := childCursor; j < len(childAddrs); j++ {
addr := childAddrs[j]
octet := byte(addr)

if octetToBaseIndex(octet) >= lower {
// lower border of host routes
childCursor = j
break
}
c := n.getChild(octet)

c := n.children[j]
path := append(slices.Clone(path), octet)

// premature end?
if !c.allRecSorted(path, is4, yield) {
return false
}

childCursor = j + 1
childCursor++
}

// some childs handled? shrink the slice
allChildren = allChildren[childCursor:]
childCursor = 0

// now handle prefix for idx
pfx := cidrFromPath(path, idx, is4)
val, _ := n.getValByIndex(idx)
Expand All @@ -696,34 +693,31 @@ func (n *node[V]) allRecSorted(path []byte, is4 bool, yield func(netip.Prefix, V
}

// handle the children in host routes for this prefix
for j, addr := range allChildren {
for j := childCursor; j < len(childAddrs); j++ {
addr := childAddrs[j]
octet := byte(addr)
if octetToBaseIndex(octet) > upper {
// out of host routes
childCursor = j
break
}

c := n.getChild(octet)
c := n.children[j]
path := append(slices.Clone(path), octet)

// premature end?
if !c.allRecSorted(path, is4, yield) {
return false
}

childCursor = j + 1
childCursor++
}

// some childs handled? shrink the slice
allChildren = allChildren[childCursor:]
childCursor = 0
}

// handle all the rest of the children
for _, addr := range allChildren {
for j := childCursor; j < len(childAddrs); j++ {
addr := childAddrs[j]
octet := byte(addr)
c := n.getChild(octet)

c := n.children[j]
path := append(slices.Clone(path), octet)

// premature end?
Expand Down
9 changes: 5 additions & 4 deletions stringify.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,15 @@ func (n *node[V]) getKidsRec(parentIdx uint, path []byte, is4 bool) []kid[V] {
}

// the node may have childs, the rec-descent monster starts
for _, octet := range n.allChildAddrs() {
for i, addr := range n.allChildAddrs() {
octet := byte(addr)
// do a longest-prefix-match
if lpmIdx, _, _ := n.lpmByOctet(byte(octet)); lpmIdx == parentIdx {
if lpmIdx, _, _ := n.lpmByOctet(octet); lpmIdx == parentIdx {
// child is directKid, the path is needed to get back the prefixes
path := append(slices.Clone(path), byte(octet))
path := append(slices.Clone(path), octet)

// get next child node
c := n.getChild(byte(octet))
c := n.children[i]

// traverse, rec-descent call with next child node
directKids = append(directKids, c.getKidsRec(0, path, is4)...)
Expand Down

0 comments on commit c715379

Please sign in to comment.