|
| 1 | +from base.ActivationRecord import ActivationRecord |
| 2 | +from base.ARNode import ARNode |
| 3 | + |
| 4 | + |
| 5 | +class ARTree: |
| 6 | + def __init__(self): |
| 7 | + self.root: ARNode |
| 8 | + self._size: int = 0 |
| 9 | + |
| 10 | + def build_tree(self, scopes): |
| 11 | + """ |
| 12 | + Builds the tree from the list of scopes. The first scope in the list is |
| 13 | + built-in scope and the root of the tree. Its only child is second scope in the list |
| 14 | + - the global scope. Other scopes are children or ancestors of the global scope. It |
| 15 | + is guaranteed that that each scope is unique and that at least built-in and global |
| 16 | + scopes are present in the list. |
| 17 | + """ |
| 18 | + self.root = ARNode(scopes[0]) |
| 19 | + self._size = 1 |
| 20 | + |
| 21 | + for scope in scopes[1:]: |
| 22 | + node = ARNode(scope) |
| 23 | + parent = self._find_parent(scope, self.root) |
| 24 | + parent.children.append(node) |
| 25 | + self._size += 1 |
| 26 | + |
| 27 | + def _find_parent(self, scope, root: ARNode) -> ARNode: |
| 28 | + """ |
| 29 | + Finds the parent of the given scope. It is guaranteed that the scope has a parent. |
| 30 | + """ |
| 31 | + |
| 32 | + if scope.enclosing_scope == root.scope and scope.scope_level == root.scope.scope_level + 1: |
| 33 | + return root |
| 34 | + else: |
| 35 | + for child in root.children: |
| 36 | + node = self._find_parent(scope, child) |
| 37 | + if node: |
| 38 | + return node |
| 39 | + |
| 40 | + return ARNode(None) |
| 41 | + |
| 42 | + def _find_node(self, name: str, level: int, root: ARNode) -> ARNode: |
| 43 | + """ |
| 44 | + Finds the node with the given name and level. It is guaranteed that the node exists. |
| 45 | + """ |
| 46 | + if root.scope.scope_name == name and root.scope.scope_level == level: |
| 47 | + return root |
| 48 | + else: |
| 49 | + for child in root.children: |
| 50 | + node = self._find_node(name, level, child) |
| 51 | + if node: |
| 52 | + return node |
| 53 | + |
| 54 | + return ARNode(None) |
| 55 | + |
| 56 | + def find(self, name: str, level: int) -> list[ActivationRecord]: |
| 57 | + """ |
| 58 | + Finds the activation record with the given name and level. It is guaranteed that the |
| 59 | + record exists. |
| 60 | + """ |
| 61 | + node = self._find_node(name, level, self.root) |
| 62 | + return node.ar_records |
| 63 | + |
| 64 | + def push(self, AR: ActivationRecord) -> None: |
| 65 | + node = self._find_node(AR.scope_name, AR.nesting_level, self.root) |
| 66 | + node.ar_records.append(AR) |
| 67 | + |
| 68 | + def __str__(self): |
| 69 | + return '\n'.join([i.__str__() for i in self._bf_traverse(self.root)]) |
| 70 | + |
| 71 | + def preorder_traverse(self, root: ARNode) -> list[ARNode]: |
| 72 | + """ |
| 73 | + Performs a pre-order traversal of the tree. |
| 74 | + Returns a list of nodes in the order they were visited. |
| 75 | + """ |
| 76 | + nodes = [root] |
| 77 | + |
| 78 | + for child in root.children: |
| 79 | + nodes += self.preorder_traverse(child) |
| 80 | + |
| 81 | + return nodes |
| 82 | + |
| 83 | + def bf_traverse(self) -> list[ARNode]: |
| 84 | + return self._bf_traverse(self.root) |
| 85 | + |
| 86 | + def _bf_traverse(self, root: ARNode) -> list[ARNode]: |
| 87 | + """ |
| 88 | + Performs a breadth-first traversal of the tree. |
| 89 | + Returns a list of nodes in the order they were visited. |
| 90 | + """ |
| 91 | + nodes = [root] |
| 92 | + queue = [root] |
| 93 | + |
| 94 | + while queue: |
| 95 | + node = queue.pop(0) |
| 96 | + for child in node.children: |
| 97 | + nodes.append(child) |
| 98 | + queue.append(child) |
| 99 | + |
| 100 | + return nodes |
| 101 | + |
| 102 | + def __len__(self) -> int: |
| 103 | + return self._size |
| 104 | + |
| 105 | + @property |
| 106 | + def size(self) -> int: |
| 107 | + return self._size |
0 commit comments