Skip to content

Commit eb8f7ac

Browse files
royscrjl493456442
authored andcommitted
expand commentary
1 parent 3749412 commit eb8f7ac

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

trie/iterator.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -555,39 +555,45 @@ func (it *nodeIterator) pop() {
555555
}
556556

557557
// reachedPath normalizes a path by truncating a terminator if present, and returns true if it is
558-
// greater than or equal to the target.
558+
// greater than or equal to the target. Using this, the path of a value node embedded a full node
559+
// will compare less than the full node's children.
559560
func reachedPath(path, target []byte) bool {
560561
if hasTerm(path) {
561562
path = path[:len(path)-1]
562563
}
563564
return bytes.Compare(path, target) >= 0
564565
}
565566

566-
// A value embedded in a fullNode occupies its last child slot, but we want to visit it last to
567-
// produce a pre-order traversal. These two functions handle the ordering by mapping index 0 to the
568-
// value, and shifting the indices of children over by 1.
567+
// A value embedded in a full node occupies the last slot (16) of the array of children. In order to
568+
// produce a pre-order traversal when iterating children, we jump to this last slot first, then go
569+
// back iterate the child nodes (and skip the last slot at the end):
570+
571+
// prevChildIndex returns the index of a child in a full node which precedes the given index when
572+
// performing a pre-order traversal.
569573
func prevChildIndex(index int) int {
570574
switch index {
571-
case 0:
575+
case 0: // We jumped back to iterate the children, from the value slot
572576
return 16
573-
case 16:
577+
case 16: // We jumped to the embedded value slot at the end, from the placeholder index
574578
return -1
575-
case 17:
579+
case 17: // We skipped the value slot after iterating all the children
576580
return 15
577-
default:
581+
default: // We are iterating the children in sequence
578582
return index - 1
579583
}
580584
}
581585

586+
// nextChildIndex returns the index of a child in a full node which follows the given index when
587+
// performing a pre-order traversal.
582588
func nextChildIndex(index int) int {
583589
switch index {
584-
case -1:
590+
case -1: // Jump from the placeholder index to the embedded value slot
585591
return 16
586-
case 15:
592+
case 15: // Skip the value slot after iterating the children
587593
return 17
588-
case 16:
594+
case 16: // From the embedded value slot, jump back to iterate the children
589595
return 0
590-
default:
596+
default: // Iterate children in sequence
591597
return index + 1
592598
}
593599
}

0 commit comments

Comments
 (0)