import t, c
import memhub
import string
from stdint import *


class list[T]:
    """堆上动态列表容器，基于 memhub.MemManager 多态分配器。

    用法:
        pool: memhub.MemManager = memhub.MemBuddy(arena, arena_size)
        nums: list[int] = list[int](pool)
        nums.append(42)
        x: int = nums.get(0)

    对于指针类型（如 AST | t.CPtr），传 elem_size=8 避免对象切片:
        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):
        # 堆分配 list[T] 对象 (6 字段 × 8 = 48 字节，无 vtable)
        return pool.alloc(48)

    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

    def __len__(self) -> t.CSizeT:
        return self.__count__

    def append(self, item: T):
        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]
