forked from microsoft/dpu-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csharplattice.py
27 lines (21 loc) · 1.02 KB
/
csharplattice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from functools import lru_cache
from typing import List, Set
from .lattice import Lattice
from dpu_utils.utils.dataloading import load_json_gz
class CSharpLattice(Lattice):
"""Represents a lattice structure of C# types."""
def __init__(self, elements: List[str], parent_relations: List[Set[int]]) -> None:
super().__init__(elements, parent_relations)
@lru_cache(maxsize=1024)
def parents(self, element: str) -> List[str]:
"""Get the parents of a given element"""
if element not in self._element_to_id:
if element.endswith("[]"):
inner_type = element[:-2]
inner_type_parents = self.parents(inner_type)
return list(sorted(set(inner_type_parent + "[]" for inner_type_parent in inner_type_parents)))
return super().parents(element)
@staticmethod
def load(filename: str) -> 'CSharpLattice':
types = load_json_gz(filename)
return CSharpLattice(types['types'], types['outgoingEdges'])