Skip to content

Commit 86df57f

Browse files
committed
fix purgeAndCompress
1 parent 475fa0d commit 86df57f

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

node.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -274,42 +274,44 @@ func (n *node[V]) insertAtDepthPersist(pfx netip.Prefix, val V, depth int) (exis
274274
}
275275

276276
// purgeAndCompress, purge empty nodes or compress nodes with single prefix or leaf.
277-
func (n *node[V]) purgeAndCompress(parentStack []*node[V], octets []uint8, is4 bool) {
277+
func (n *node[V]) purgeAndCompress(stack []*node[V], octets []uint8, is4 bool) {
278278
// unwind the stack
279-
for depth := len(parentStack) - 1; depth >= 0; depth-- {
280-
parent := parentStack[depth]
279+
for depth := len(stack) - 1; depth >= 0; depth-- {
280+
parent := stack[depth]
281281
addr := uint(octets[depth])
282282

283283
pfxCount := n.prefixes.Len()
284284
childCount := n.children.Len()
285285

286286
switch {
287287
case n.isEmpty():
288-
// just delete this empty node
288+
// just delete this empty node from parent
289289
parent.children.DeleteAt(addr)
290290

291291
case pfxCount == 0 && childCount == 1:
292292
switch kid := n.children.Items[0].(type) {
293293
case *node[V]:
294294
// fast exit, we are at an intermediate node
295-
// no further compression upwards possible
295+
// no further compression the stack upwards possible
296296
return
297297
case *leafNode[V]:
298-
// it's a leaf, delete this node and reinsert the leaf
298+
// just one leaf, delete this node and reinsert the leaf above
299299
parent.children.DeleteAt(addr)
300300

301-
// ... insert prefix at parents depth
301+
// ... reinsert the leaf at parents depth
302302
parent.insertAtDepth(kid.prefix, kid.value, depth)
303303
case *fringeNode[V]:
304-
// it's a fringe, we must rebuild the prefix!
305-
// get the last octet back from sparse array
304+
// get prefix back from addr
306305
lastOctet, _ := n.children.FirstSet()
307306

308-
// rebuild the prefix with octets, depth, ip version and lastOctet
309-
// it's the parent depth, so add +1 here for the kid
307+
// rebuild the prefix with octets, depth, ip version and addr
308+
// depth is the parent's depth, so add +1 here for the kid
310309
fringePfx := cidrForFringe(octets, depth+1, is4, lastOctet)
311310

312-
// ... insert prefix at parents depth
311+
// delete this node
312+
parent.children.DeleteAt(addr)
313+
314+
// ... reinsert prefix/value at parents depth
313315
parent.insertAtDepth(fringePfx, kid.value, depth)
314316
}
315317

0 commit comments

Comments
 (0)