Skip to content

Commit c7e94f8

Browse files
committed
MNT: change recursive to iterative approach on hash search.
1 parent 759e1c2 commit c7e94f8

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

rocketpy/tools.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,28 +1314,22 @@ def find_obj_from_hash(obj, hash_, depth_limit=None):
13141314
The object whose '__rpy_hash' matches hash_, or None if not found.
13151315
"""
13161316

1317-
def _search(o, current_depth):
1317+
stack = [(obj, 0)]
1318+
while stack:
1319+
current_obj, current_depth = stack.pop()
13181320
if depth_limit is not None and current_depth > depth_limit:
1319-
return None
1321+
continue
13201322

1321-
if getattr(o, "__rpy_hash", None) == hash_:
1322-
return o
1323+
if getattr(current_obj, "__rpy_hash", None) == hash_:
1324+
return current_obj
13231325

1324-
if isinstance(o, dict):
1325-
for value in o.values():
1326-
result = _search(value, current_depth + 1)
1327-
if result is not None:
1328-
return result
1326+
if isinstance(current_obj, dict):
1327+
stack.extend((v, current_depth + 1) for v in current_obj.values())
13291328

1330-
elif isinstance(o, (list, tuple, set)):
1331-
for item in o:
1332-
result = _search(item, current_depth + 1)
1333-
if result is not None:
1334-
return result
1329+
elif isinstance(current_obj, (list, tuple, set)):
1330+
stack.extend((item, current_depth + 1) for item in current_obj)
13351331

1336-
return None
1337-
1338-
return _search(obj, 0)
1332+
return None
13391333

13401334

13411335
if __name__ == "__main__": # pragma: no cover

0 commit comments

Comments
 (0)