Files
TransPyC/includes/_list.vp
T

112 lines
4.7 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import t, c
import memhub
import string
import stdio
from stdint import *
# [LISTDBG] 诊断(用后移除):list.__init__ 打印初始化结果、append 首次
# (Count==0) 打印 self/Data,配合 CTORDBG 定位二代编 SlotsBugTest cargs(list)
# 头覆盖。默认静默——由命令行 --enable-debug-log 开启(memhub.set_debug_log)
class list[T]:
"""Heap-allocated dynamic list container backed by memhub.MemManager polymorphic allocator.
Usage:
pool: memhub.MemManager = memhub.MemBuddy(arena, arena_size)
nums: list[int] = list[int](pool)
nums.append(42)
x: int = nums.get(0)
For pointer types (e.g. AST | t.CPtr), pass elem_size=8 to avoid object slicing:
lst: list[AST | t.CPtr] = list[AST | t.CPtr](pool, 8)
"""
__data__: t.CVoid | t.CPtr
__count__: t.CSizeT
__capacity__: t.CSizeT
__pool__: memhub.MemManager | t.CPtr
__elem_size__: t.CSizeT
__iter_index__: t.CSizeT
def __new__(self, pool: memhub.MemManager | t.CPtr, elem_size: t.CSizeT = 0):
ret = pool.alloc(48)
return ret
def __init__(self, pool: memhub.MemManager | t.CPtr, elem_size: t.CSizeT = 0):
self.__pool__ = pool
self.__count__ = 0
self.__capacity__ = 8
if elem_size == 0:
self.__elem_size__ = T.__sizeof__()
else:
self.__elem_size__ = elem_size
self.__data__ = pool.alloc(self.__capacity__ * self.__elem_size__)
self.__iter_index__ = 0
# [LISTDBG] 诊断(用后移除):打印初始化结果,与 CTORDBG cargs 六字段
# 交叉匹配:找到 cargs 对应记录即可判断 __init__ 是否执行/写入是否正确
if memhub.get_debug_log() != 0:
stdio.printf("[LISTDBG] list-init self=%llu data=%llu cnt=%llu cap=%llu pool=%llu es=%llu iter=%llu\n",
t.CUInt64T(self), t.CUInt64T(self.__data__),
t.CUInt64T(self.__count__), t.CUInt64T(self.__capacity__),
t.CUInt64T(self.__pool__), t.CUInt64T(self.__elem_size__),
t.CUInt64T(self.__iter_index__))
def __len__(self) -> t.CSizeT:
return self.__count__
def append(self, item: T):
# [LISTDBG] 诊断(用后移除):仅首次 append(Count==0)打印,确认
# append 作用在哪个 list、Data 指针是否有效(若 cargs 覆盖发生在此
# 前后,日志能显示 append 的 self/data 与 CTORDBG cargs/d0 的对应)
if memhub.get_debug_log() != 0 and self.__count__ == 0:
stdio.printf("[LISTDBG] append-first self=%llu data=%llu cnt=%llu cap=%llu es=%llu\n",
t.CUInt64T(self), t.CUInt64T(self.__data__),
t.CUInt64T(self.__count__), t.CUInt64T(self.__capacity__),
t.CUInt64T(self.__elem_size__))
if self.__count__ >= self.__capacity__:
new_cap: t.CSizeT = self.__capacity__ * 2
new_data: t.CPtr = self.__pool__.alloc(new_cap * self.__elem_size__)
if new_data == None: return
string.memcpy(new_data, self.__data__, self.__count__ * self.__elem_size__)
self.__data__ = new_data
self.__capacity__ = new_cap
elem_ptr: T | t.CPtr = t.CPtr(t.CUInt64T(self.__data__) + self.__count__ * self.__elem_size__)
elem_ptr[0] = item
self.__count__ += 1
def get(self, index: t.CSizeT) -> T:
elem_ptr: T | t.CPtr = t.CPtr(t.CUInt64T(self.__data__) + index * self.__elem_size__)
return elem_ptr[0]
def __getitem__(self, index: t.CSizeT) -> T:
elem_ptr: T | t.CPtr = t.CPtr(t.CUInt64T(self.__data__) + index * self.__elem_size__)
return elem_ptr[0]
def set(self, index: t.CSizeT, value: T):
elem_ptr: T | t.CPtr = t.CPtr(t.CUInt64T(self.__data__) + index * self.__elem_size__)
elem_ptr[0] = value
def __setitem__(self, index: t.CSizeT, value: T):
elem_ptr: T | t.CPtr = t.CPtr(t.CUInt64T(self.__data__) + index * self.__elem_size__)
elem_ptr[0] = value
def pop(self) -> T:
self.__count__ -= 1
elem_ptr: T | t.CPtr = t.CPtr(t.CUInt64T(self.__data__) + self.__count__ * self.__elem_size__)
return elem_ptr[0]
def clear(self):
self.__count__ = 0
def __iter__(self) -> list[T] | t.CPtr:
self.__iter_index__ = 0
return self
def __next__(self) -> T:
if self.__iter_index__ >= self.__count__:
raise StopIteration
idx: t.CSizeT = self.__iter_index__
self.__iter_index__ = idx + 1
elem_ptr: T | t.CPtr = t.CPtr(t.CUInt64T(self.__data__) + idx * self.__elem_size__)
return elem_ptr[0]