Skip to content

Commit ed69edb

Browse files
committed
Refactor path traversal to increase clarify
1 parent 5216f99 commit ed69edb

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

CaptureFile/CaptureFile.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ def record_generator(
555555
starting_record_number - 1,
556556
rightmost_path,
557557
height,
558-
self._config.fan_out**height,
558+
self._config.fan_out ** height,
559559
)
560560

561561
def _record_generator(
@@ -643,35 +643,36 @@ def record_at(self, record_number: int, /) -> Record:
643643
raise IndexError
644644
rightmost_nodes = self._current_master_node.rightmost_path.rightmost_nodes
645645
# Use "reversed" so we start at the root instead of the leaves
646-
nodes = reversed(rightmost_nodes)
647-
path = reversed(
648-
list(
649-
compute_path(
650-
# 1 is subtracted from record_number because Python is
651-
# 0-based while CaptureFile records start at 1
652-
record_number - 1,
653-
len(rightmost_nodes),
654-
self._config.fan_out,
655-
)
646+
root_to_leaf_rightmost_nodes = reversed(rightmost_nodes)
647+
root_to_leaf_path = reversed(
648+
leaf_to_root_path(
649+
# 1 is subtracted from record_number because Python is
650+
# 0-based while CaptureFile records start at 1
651+
record_number - 1,
652+
len(rightmost_nodes),
653+
self._config.fan_out,
656654
)
657655
)
658656

659657
# skip nodes as long as path follows rightmost nodes.
660-
while (child_index := next(path)) == len((node := next(nodes)).children):
658+
while (child_index := next(root_to_leaf_path)) == len(
659+
(current_rightmost_node := next(root_to_leaf_rightmost_nodes)).children
660+
):
661661
pass
662662

663663
# get first persistent child's data cooridnates. This child will refer
664664
# to either the record or the root of a perfect sub-tree of which no
665665
# decendant can be a rightmost node of the top level tree.
666-
child = node.children[child_index]
666+
current_child_coordinates = current_rightmost_node.children[child_index]
667667

668668
# iterate through the remainder of the path of child indexes until we
669669
# arrive at the data coordinates of the record
670-
for child_index in path:
671-
child_full_node = self._full_node_cache(child)
672-
child = child_full_node[child_index]
670+
for child_index in root_to_leaf_path:
671+
current_child_coordinates = self._full_node_cache(
672+
current_child_coordinates
673+
)[child_index]
673674

674-
return child.record(self)
675+
return current_child_coordinates.record(self)
675676

676677
def record_count(self, /) -> int:
677678
"""Returns the number of records available when the file was opened or
@@ -1387,11 +1388,14 @@ class InvalidCaptureFile(Exception):
13871388
pass
13881389

13891390

1390-
def compute_path(position: int, height: int, fan_out: int, /):
1391+
def leaf_to_root_path(position: int, height: int, fan_out: int, /) -> List[int]:
13911392
"""Compute the path of child indexes from the leaf through the nodes to the
13921393
root."""
13931394

1394-
while height > 0:
1395-
height -= 1
1395+
path = [0] * height
1396+
1397+
for i in range(height):
13961398
position, child_index = divmod(position, fan_out)
1397-
yield child_index
1399+
path[i] = child_index
1400+
1401+
return path

0 commit comments

Comments
 (0)