librt.vecs.vec[T]
| 1015 | |
| 1016 | @final |
| 1017 | class RVec(RType): |
| 1018 | """librt.vecs.vec[T]""" |
| 1019 | |
| 1020 | is_unboxed = True |
| 1021 | |
| 1022 | def __init__(self, item_type: RType) -> None: |
| 1023 | self.name = "vec[%s]" % item_type |
| 1024 | self.item_type = item_type |
| 1025 | self.names = ["len", "items"] |
| 1026 | self.dependencies = (LIBRT_VECS,) |
| 1027 | if isinstance(item_type, RUnion): |
| 1028 | non_opt = optional_value_type(item_type) |
| 1029 | else: |
| 1030 | non_opt = item_type |
| 1031 | if item_type in vec_buf_types: |
| 1032 | self._ctype = vec_c_types[item_type] |
| 1033 | self.buf_type = vec_buf_types[item_type] |
| 1034 | self.types = [c_pyssize_t_rprimitive, pointer_rprimitive] |
| 1035 | elif isinstance(non_opt, RVec): |
| 1036 | self._ctype = "VecNested" |
| 1037 | self.types = [c_pyssize_t_rprimitive, pointer_rprimitive] |
| 1038 | self.buf_type = VecNestedBufObject |
| 1039 | else: |
| 1040 | self._ctype = "VecT" |
| 1041 | self.types = [c_pyssize_t_rprimitive, pointer_rprimitive] |
| 1042 | self.buf_type = VecTBufObject |
| 1043 | |
| 1044 | @property |
| 1045 | def may_be_immortal(self) -> bool: |
| 1046 | return False |
| 1047 | |
| 1048 | def unwrap_item_type(self) -> RPrimitive | RInstance: |
| 1049 | """Return the non-optional value (non-vec) item type in a potentially nested vec.""" |
| 1050 | item_type = self.item_type |
| 1051 | while True: |
| 1052 | if isinstance(item_type, RUnion): |
| 1053 | value_type = optional_value_type(item_type) |
| 1054 | assert value_type is not None |
| 1055 | item_type = value_type |
| 1056 | elif isinstance(item_type, RVec): |
| 1057 | item_type = item_type.item_type |
| 1058 | elif isinstance(item_type, (RPrimitive, RInstance)): |
| 1059 | return item_type |
| 1060 | else: |
| 1061 | assert False, f"unexpected item type: {self.item_type}" |
| 1062 | |
| 1063 | def is_optional(self) -> bool: |
| 1064 | item_type = self.item_type |
| 1065 | if isinstance(item_type, RUnion): |
| 1066 | return True |
| 1067 | elif isinstance(item_type, RVec): |
| 1068 | return item_type.is_optional() |
| 1069 | return False |
| 1070 | |
| 1071 | def depth(self) -> int: |
| 1072 | item_type = self.item_type |
| 1073 | if isinstance(item_type, RUnion): |
| 1074 | value_type = optional_value_type(item_type) |
no outgoing calls
searching dependent graphs…