Skip to content

Commit

Permalink
chg: use more performant data type for finding element lineage
Browse files Browse the repository at this point in the history
- For insert/pop on the left side: O(1) for deque vs. O(n) for list.
- Avoids creating lots of new lists with [] + [].
- Did not investigate whether the "flat" filter is still necessary.
  • Loading branch information
lindsay-stevens committed Nov 20, 2023
1 parent f353d54 commit 26026a6
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions pyxform/survey_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
import json
import re
from collections import deque
from functools import lru_cache
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -188,16 +189,14 @@ def get_lineage(self):
"""
Return a the list [root, ..., self._parent, self]
"""
result = [self]
result = deque((self,))
current_element = self
while current_element.parent:
current_element = current_element.parent
result = [current_element] + result
result.appendleft(current_element)
# For some reason the root element has a True flat property...
output = [result[0]]
for item in result[1:]:
if not item.get("flat"):
output.append(item)
output = [result.popleft()]
output.extend([i for i in result if not i.get("flat")])
return output

def get_root(self):
Expand Down

0 comments on commit 26026a6

Please sign in to comment.