From f69e71b1983777b52ded6a13a29f2c923b157af8 Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Wed, 30 Sep 2026 00:34:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=20=5Fdict=20=E7=9A=84=E9=83=A8?= =?UTF-8?q?=E5=88=86=E5=86=99=E6=B3=95=E5=8D=87=E7=BA=A7=E5=88=B0=E6=9C=80?= =?UTF-8?q?=E6=96=B0=E8=8C=83=E5=BC=8F=EF=BC=8C=E8=A3=B8=E6=8C=87=E9=92=88?= =?UTF-8?q?=20uint64=20=E6=89=8B=E5=8A=A8=E8=AE=A1=E7=AE=97=E4=BE=BF?= =?UTF-8?q?=E5=AE=9C=E8=BF=98=E9=9C=80=E8=A6=81=E9=87=8D=E6=96=B0=E5=A4=84?= =?UTF-8?q?=E7=90=86=EF=BC=8C=E9=93=BE=E8=A1=A8=E6=9A=82=E6=9C=AA=E5=81=9A?= =?UTF-8?q?=20linkedlist=20=E8=8C=83=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _dict.vp | 125 +++++++++++++++++++++++++++---------------------------- 1 file changed, 62 insertions(+), 63 deletions(-) diff --git a/_dict.vp b/_dict.vp index 329e8bc..96ff4ee 100644 --- a/_dict.vp +++ b/_dict.vp @@ -2,7 +2,7 @@ import t, c import memhub import string import json -from stdint import * + # Variant type tags (consistent with _variant.py) define VARIANT_NONE = 0 # Untyped / null value @@ -29,14 +29,14 @@ class dict: d: dict = dict(mb) d.set_int("x", 42) p: t.CPtr = d["x"] - v: int = c.Deref((int | t.CPtr)(p)) # int 值按 i32 存,读回须给 int* 视图 + v: int = c.Deref((int*)(p)) # int 值按 i32 存,读回须给 int* 视图 # Nested dict inner: dict = dict(mb) inner.set_int("a", 1) d.set_dict("sub", inner) sub: dict = c.Deref(d["sub"]) - av: int = c.Deref((int | t.CPtr)(sub["a"])) + av: int = c.Deref((int*)(sub["a"])) """ __keys__: rawptr __values__: rawptr @@ -64,14 +64,14 @@ class dict: if string.strcmp(key_ptr[0], key) == 0: return i return self.__count__ - def __getitem__(self, key: str) -> t.CPtr: + def __getitem__(self, key: str) -> rawptr: """Return untyped pointer to value. Use c.Deref to resolve the actual value. - Example: p: t.CPtr = d["x"]; v: int = c.Deref((int | t.CPtr)(p))""" + Example: p: rawptr = d["x"]; v: int = *(int*(p))""" idx: sizet = self._find(key) if idx >= self.__count__: return None # Variant.ptr is located at offset 8 within Variant structure - val_ptr: str | t.CPtr = (str | t.CPtr)(uint64(self.__values__) + idx * _VARIANT_SIZE + 8) - return val_ptr[0] + val_ptr: str* = str*(uint64(self.__values__) + idx * _VARIANT_SIZE + 8) + return *val_ptr def __setitem__(self, key: str, val: t.CNeedPtr): """Store value as VARIANT_PTR. Address is automatically resolved via t.CNeedPtr. @@ -79,31 +79,31 @@ class dict: Use typed methods such as set_int / set_str for persistent storage.""" self._set_variant_raw(key, VARIANT_PTR, val) - def _set_variant_raw(self, key: str, vtype: t.CInt, ptr: t.CPtr): + def _set_variant_raw(self, key: str, vtype: int, ptr: t.CPtr): """Internal method: store Variant entry (vtype + ptr). No automatic address resolution.""" idx: sizet = self._find(key) if idx < self.__count__: # Update existing key - vtype_ptr: t.CInt | t.CPtr = t.CPtr(uint64(self.__values__) + idx * _VARIANT_SIZE) + vtype_ptr: int* = int*(uint64(self.__values__) + idx * _VARIANT_SIZE) vtype_ptr[0] = vtype - val_ptr: str | t.CPtr = (str | t.CPtr)(uint64(self.__values__) + idx * _VARIANT_SIZE + 8) + val_ptr: str* = str*(uint64(self.__values__) + idx * _VARIANT_SIZE + 8) val_ptr[0] = ptr return if self.__count__ >= self.__capacity__: new_cap: sizet = self.__capacity__ * 2 - new_keys: t.CPtr = self.__mbuddy__.alloc(new_cap * 8) - new_values: t.CPtr = self.__mbuddy__.alloc(new_cap * _VARIANT_SIZE) + new_keys: rawptr = self.__mbuddy__.alloc(new_cap * 8) + new_values: rawptr = self.__mbuddy__.alloc(new_cap * _VARIANT_SIZE) if new_keys is None or new_values is None: return string.memcpy(new_keys, self.__keys__, self.__count__ * 8) string.memcpy(new_values, self.__values__, self.__count__ * _VARIANT_SIZE) self.__keys__ = new_keys self.__values__ = new_values self.__capacity__ = new_cap - key_ptr: str | t.CPtr = (str | t.CPtr)(uint64(self.__keys__) + self.__count__ * 8) + key_ptr: str* = str*(uint64(self.__keys__) + self.__count__ * 8) key_ptr[0] = key - vtype_ptr: t.CInt | t.CPtr = t.CPtr(uint64(self.__values__) + self.__count__ * _VARIANT_SIZE) + vtype_ptr: int* = int*(uint64(self.__values__) + self.__count__ * _VARIANT_SIZE) vtype_ptr[0] = vtype - val_ptr: str | t.CPtr = (str | t.CPtr)(uint64(self.__values__) + self.__count__ * _VARIANT_SIZE + 8) + val_ptr: str* = str*(uint64(self.__values__) + self.__count__ * _VARIANT_SIZE + 8) val_ptr[0] = ptr self.__count__ += 1 @@ -113,9 +113,9 @@ class dict: def set_int(self, key: str, val: int): """Store integer value (VARIANT_INT). Value is copied to persistent storage within __mbuddy__.""" - storage: t.CPtr = self.__mbuddy__.alloc(8) - int_ptr: int | t.CPtr = t.CPtr(uint64(storage)) - int_ptr[0] = val + storage: rawptr = self.__mbuddy__.alloc(8) + int_ptr: int* = int*(storage) + *int_ptr = val self._set_variant_raw(key, VARIANT_INT, storage) def set_str(self, key: str, val: str): @@ -124,19 +124,19 @@ class dict: def set_float(self, key: str, val: float): """Store float value (VARIANT_FLOAT). Value is copied to persistent storage within __mbuddy__.""" - storage: t.CPtr = self.__mbuddy__.alloc(8) - float_ptr: float | t.CPtr = t.CPtr(uint64(storage)) - float_ptr[0] = val + storage: rawptr = self.__mbuddy__.alloc(8) + float_ptr: float* = float*(storage) + *float_ptr = val self._set_variant_raw(key, VARIANT_FLOAT, storage) - def set_double(self, key: str, val: t.CDouble): + def set_double(self, key: str, val: double): """Store double value (VARIANT_DOUBLE). Value is copied to persistent storage within __mbuddy__.""" - storage: t.CPtr = self.__mbuddy__.alloc(8) - dbl_ptr: t.CDouble | t.CPtr = t.CPtr(uint64(storage)) - dbl_ptr[0] = val + storage: rawptr = self.__mbuddy__.alloc(8) + dbl_ptr: double* = double*(storage) + *dbl_ptr = val self._set_variant_raw(key, VARIANT_DOUBLE, storage) - def set_dict(self, key: str, val: dict | t.CPtr): + def set_dict(self, key: str, val: dict*): """Store nested dictionary pointer (VARIANT_DICT).""" self._set_variant_raw(key, VARIANT_DICT, val) @@ -144,23 +144,23 @@ class dict: """Store nested list pointer (VARIANT_LIST).""" self._set_variant_raw(key, VARIANT_LIST, val) - def set_bool(self, key: str, val: t.CInt): + def set_bool(self, key: str, val: int): """Store boolean value (VARIANT_BOOL). Value is copied to persistent storage within __mbuddy__.""" - storage: t.CPtr = self.__mbuddy__.alloc(8) - bool_ptr: t.CInt | t.CPtr = t.CPtr(uint64(storage)) - bool_ptr[0] = val + storage: rawptr = self.__mbuddy__.alloc(8) + bool_ptr: int* = int*(storage) + *bool_ptr = val self._set_variant_raw(key, VARIANT_BOOL, storage) # ============================================================ # Query methods # ============================================================ - def get_vtype(self, key: str) -> t.CInt: + def get_vtype(self, key: str) -> int: """Return Variant type tag associated with specified key.""" idx: sizet = self._find(key) if idx >= self.__count__: return VARIANT_NONE - vtype_ptr: t.CInt | t.CPtr = t.CPtr(uint64(self.__values__) + idx * _VARIANT_SIZE) + vtype_ptr: int* = int*(uint64(self.__values__) + idx * _VARIANT_SIZE) return vtype_ptr[0] def get(self, key: str, default: t.CPtr) -> t.CPtr: @@ -168,17 +168,16 @@ class dict: idx: sizet = self._find(key) if idx >= self.__count__: return default - val_ptr: str | t.CPtr = (str | t.CPtr)(uint64(self.__values__) + idx * _VARIANT_SIZE + 8) - return val_ptr[0] + val_ptr: str* = str*(uint64(self.__values__) + idx * _VARIANT_SIZE + 8) + return *val_ptr - def contains(self, key: str) -> t.CInt: + def contains(self, key: str) -> int: """Check whether target key exists.""" idx: sizet = self._find(key) - if idx < self.__count__: - return 1 + if idx < self.__count__: return 1 return 0 - def __iter__(self) -> dict | t.CPtr: + def __iter__(self) -> dict*: self.__iter_index__ = 0 return self @@ -187,85 +186,85 @@ class dict: raise StopIteration idx: sizet = self.__iter_index__ self.__iter_index__ = idx + 1 - key_ptr: str | t.CPtr = (str | t.CPtr)(uint64(self.__keys__) + idx * 8) + key_ptr: str* = str*(uint64(self.__keys__) + idx * 8) return key_ptr[0] # ============================================================ # JSON utilities: serialization / deserialization via includes/json library # ============================================================ - def to_json(self, pool: rawptr[memhub.MemManager]) -> rawptr[json.JsonValue]: + def to_json(self, pool: memhub.MemManager*) -> json.JsonValue*: """Convert dict instance to JsonValue object (JSON_OBJECT). Automatically select corresponding JSON value type according to Variant tag.""" - obj: rawptr[json.JsonValue] = json.object(pool) + obj: json.JsonValue* = json.object(pool) i: sizet = 0 while i < self.__count__: - key_ptr: str | t.CPtr = (str | t.CPtr)(uint64(self.__keys__) + i * 8) - vtype_ptr: t.CInt | t.CPtr = t.CPtr(uint64(self.__values__) + i * _VARIANT_SIZE) - val_ptr: str | t.CPtr = (str | t.CPtr)(uint64(self.__values__) + i * _VARIANT_SIZE + 8) - vt: t.CInt = vtype_ptr[0] - vp: t.CPtr = val_ptr[0] - jv: rawptr[json.JsonValue] = json.null(pool) + key_ptr: str* = str*(uint64(self.__keys__) + i * 8) + vtype_ptr: int* = int*(uint64(self.__values__) + i * _VARIANT_SIZE) + val_ptr: str* = str*(uint64(self.__values__) + i * _VARIANT_SIZE + 8) + vt: int = vtype_ptr[0] + vp: rawptr = val_ptr[0] + jv: json.JsonValue* = json.null(pool) if jv != None: if vt == VARIANT_INT: jv.vtype = 2 # JSON_INT - ip: int | t.CPtr = t.CPtr(uint64(vp)) + ip: int* = int*(vp) jv.int_val = ip[0] elif vt == VARIANT_STR: jv.vtype = 4 # JSON_STRING jv.str_val = vp elif vt == VARIANT_BOOL: jv.vtype = 1 # JSON_BOOL - bp: t.CInt | t.CPtr = t.CPtr(uint64(vp)) + bp: int* = int*(vp) jv.bool_val = bp[0] elif vt == VARIANT_FLOAT: jv.vtype = 3 # JSON_FLOAT - fp: float | t.CPtr = t.CPtr(uint64(vp)) + fp: float* = float*(vp) jv.float_val = fp[0] elif vt == VARIANT_PTR: # Fallback: treat raw pointer as integer jv.vtype = 2 # JSON_INT - ip: int | t.CPtr = t.CPtr(uint64(vp)) + ip: int* = int*(vp) jv.int_val = ip[0] json.object_set(pool, obj, key_ptr[0], jv) i += 1 return obj - def from_json(self, pool: rawptr[memhub.MemManager], root: rawptr[json.JsonValue]): + def from_json(self, pool: memhub.MemManager*, root: json.JsonValue*): """Populate dict from JsonValue object. Extract values and store them as matching Variant types based on JSON type tag.""" if root == None: return if root.vtype != 6: # JSON_OBJECT return - cur: rawptr[json.JsonValue] = root.child + cur: json.JsonValue* = root.child while cur != None: if cur.key != None: if cur.vtype == 2: # JSON_INT - storage: t.CPtr = self.__mbuddy__.alloc(8) - ip: int | t.CPtr = t.CPtr(uint64(storage)) + storage: rawptr = self.__mbuddy__.alloc(8) + ip: int* = int*(storage) ip[0] = cur.int_val self._set_variant_raw(cur.key, VARIANT_INT, storage) elif cur.vtype == 4: # JSON_STRING self._set_variant_raw(cur.key, VARIANT_STR, cur.str_val) elif cur.vtype == 1: # JSON_BOOL - storage: t.CPtr = self.__mbuddy__.alloc(8) - bp: t.CInt | t.CPtr = t.CPtr(uint64(storage)) + storage: rawptr = self.__mbuddy__.alloc(8) + bp: int* = int*(storage) bp[0] = cur.bool_val self._set_variant_raw(cur.key, VARIANT_BOOL, storage) elif cur.vtype == 3: # JSON_FLOAT - storage: t.CPtr = self.__mbuddy__.alloc(8) - fp: float | t.CPtr = t.CPtr(uint64(storage)) + storage: rawptr = self.__mbuddy__.alloc(8) + fp: float* = float*(storage) fp[0] = cur.float_val self._set_variant_raw(cur.key, VARIANT_FLOAT, storage) cur = cur.next - def dumps(self, pool: rawptr[memhub.MemManager]) -> str: + def dumps(self, pool: memhub.MemManager*) -> str: """Serialize dict to JSON string.""" - obj: rawptr[json.JsonValue] = self.to_json(pool) + obj: json.JsonValue* = self.to_json(pool) return json.write(pool, obj, False) - def loads(self, pool: rawptr[memhub.MemManager], json_str: str): + def loads(self, pool: memhub.MemManager*, json_str: str): """Parse JSON string and populate dict instance.""" - root: rawptr[json.JsonValue] = json.parse(pool, json_str) + root: json.JsonValue* = json.parse(pool, json_str) self.from_json(pool, root)