vx-toolkit 1.16.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- vx_toolkit/__init__.py +10 -0
- vx_toolkit/_bin/headers/collections/vec.c +8 -0
- vx_toolkit/_bin/headers/collections/vec.vxh +2 -0
- vx_toolkit/_bin/headers/cpp/greet.cpp +11 -0
- vx_toolkit/_bin/headers/cpp/greet.hpp +6 -0
- vx_toolkit/_bin/headers/cpp/greet.vxh +4 -0
- vx_toolkit/_bin/headers/fmath.c +3 -0
- vx_toolkit/_bin/headers/fmath.vxh +5 -0
- vx_toolkit/_bin/headers/math.c +6 -0
- vx_toolkit/_bin/headers/math.vxh +4 -0
- vx_toolkit/_bin/headers/sys.c +9 -0
- vx_toolkit/_bin/headers/sys.vxh +3 -0
- vx_toolkit/_bin/headers/text.c +17 -0
- vx_toolkit/_bin/headers/text.vxh +6 -0
- vx_toolkit/_bin/libvxrt.a +0 -0
- vx_toolkit/_bin/libvxtidy.dylib +0 -0
- vx_toolkit/_bin/std/README.md +90 -0
- vx_toolkit/_bin/std/bytes.vx +65 -0
- vx_toolkit/_bin/std/char.vx +83 -0
- vx_toolkit/_bin/std/collections/hashmap.vx +31 -0
- vx_toolkit/_bin/std/collections/linked_list.vx +166 -0
- vx_toolkit/_bin/std/collections/mod.vx +8 -0
- vx_toolkit/_bin/std/collections/set.vx +102 -0
- vx_toolkit/_bin/std/collections/vec.vx +40 -0
- vx_toolkit/_bin/std/conv.vx +57 -0
- vx_toolkit/_bin/std/error.vx +165 -0
- vx_toolkit/_bin/std/io/dir.vx +50 -0
- vx_toolkit/_bin/std/io/file.vx +154 -0
- vx_toolkit/_bin/std/io/mod.vx +5 -0
- vx_toolkit/_bin/std/io/path.vx +105 -0
- vx_toolkit/_bin/std/io/stream.vx +82 -0
- vx_toolkit/_bin/std/io.vx +6 -0
- vx_toolkit/_bin/std/iter.vx +67 -0
- vx_toolkit/_bin/std/macros.vx +117 -0
- vx_toolkit/_bin/std/math/math.vx +341 -0
- vx_toolkit/_bin/std/math/mod.vx +6 -0
- vx_toolkit/_bin/std/math/random.vx +104 -0
- vx_toolkit/_bin/std/mem/allocator.vx +9 -0
- vx_toolkit/_bin/std/mem/arena.vx +41 -0
- vx_toolkit/_bin/std/mem/general.vx +35 -0
- vx_toolkit/_bin/std/mem/mod.vx +11 -0
- vx_toolkit/_bin/std/mem/page.vx +23 -0
- vx_toolkit/_bin/std/mod.vx +5 -0
- vx_toolkit/_bin/std/option.vx +68 -0
- vx_toolkit/_bin/std/prelude.vx +13 -0
- vx_toolkit/_bin/std/reflect/mod.vx +129 -0
- vx_toolkit/_bin/std/string/codec.vx +146 -0
- vx_toolkit/_bin/std/string/core.vx +28 -0
- vx_toolkit/_bin/std/string/iter.vx +49 -0
- vx_toolkit/_bin/std/string/mod.vx +7 -0
- vx_toolkit/_bin/std/string/pattern.vx +84 -0
- vx_toolkit/_bin/std/string/util.vx +369 -0
- vx_toolkit/_bin/std/sys/coroutine.vx +46 -0
- vx_toolkit/_bin/std/sys/datetime.vx +160 -0
- vx_toolkit/_bin/std/sys/duration.vx +53 -0
- vx_toolkit/_bin/std/sys/env.vx +43 -0
- vx_toolkit/_bin/std/sys/mod.vx +10 -0
- vx_toolkit/_bin/std/sys/sync.vx +132 -0
- vx_toolkit/_bin/std/sys/time.vx +28 -0
- vx_toolkit/_bin/std/sys.vx +6 -0
- vx_toolkit/_bin/std/tests/run_all.vx +627 -0
- vx_toolkit/_bin/std/tests/smoke.vx +4 -0
- vx_toolkit/_bin/std/tests/test_collections.vx +42 -0
- vx_toolkit/_bin/std/tests/test_string_io.vx +202 -0
- vx_toolkit/_bin/vfmt +0 -0
- vx_toolkit/_bin/vlnk +0 -0
- vx_toolkit/_bin/vmake +0 -0
- vx_toolkit/_bin/vmake-lsp +0 -0
- vx_toolkit/_bin/vx +0 -0
- vx_toolkit/_bin/vxc +0 -0
- vx_toolkit/_bin/vxls +0 -0
- vx_toolkit/_dispatch.py +121 -0
- vx_toolkit-1.16.2.dist-info/METADATA +13 -0
- vx_toolkit-1.16.2.dist-info/RECORD +77 -0
- vx_toolkit-1.16.2.dist-info/WHEEL +4 -0
- vx_toolkit-1.16.2.dist-info/entry_points.txt +8 -0
- vx_toolkit-1.16.2.dist-info/licenses/LICENSE +198 -0
vx_toolkit/__init__.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// greet.cpp — @cppimp 示例实现(C++ 符号 greet,非 VX ABI)。
|
|
2
|
+
// vlnk 生成的 shim 调用本函数;字符串用静态缓冲返回以便 const char* 可用。
|
|
3
|
+
#include "greet.hpp"
|
|
4
|
+
|
|
5
|
+
#include <string>
|
|
6
|
+
|
|
7
|
+
const char* greet(const char* name) {
|
|
8
|
+
static std::string msg;
|
|
9
|
+
msg = std::string("Hello, ") + (name ? name : "") + "!";
|
|
10
|
+
return msg.c_str();
|
|
11
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/* text 库实现 — 导出 VX ABI 符号:_vx_text_length / _vx_text_is_empty /
|
|
2
|
+
* _vx_text_shout。覆盖 string 入参、int/bool 返回与 void 返回三种形态。 */
|
|
3
|
+
#include <stdint.h>
|
|
4
|
+
#include <stdio.h>
|
|
5
|
+
#include <string.h>
|
|
6
|
+
|
|
7
|
+
int32_t _vx_text_length(const char *s) {
|
|
8
|
+
return s ? (int32_t)strlen(s) : 0;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
int32_t _vx_text_is_empty(const char *s) { return (s == NULL || *s == '\0'); }
|
|
12
|
+
|
|
13
|
+
void _vx_text_shout(const char *s) {
|
|
14
|
+
if (!s) return;
|
|
15
|
+
for (const char *p = s; *p; ++p) putchar(*p);
|
|
16
|
+
putchar('\n');
|
|
17
|
+
}
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# VX 标准库(设计参考)
|
|
2
|
+
|
|
3
|
+
> **当前定位:前瞻设计稿,不可被当前工具链编译执行。**
|
|
4
|
+
>
|
|
5
|
+
> 本目录 v1.0 版本为早期 VM 执行路线设计(源码 → VM 字节码 → 虚拟机解释)。
|
|
6
|
+
> 工具链已转向原生路线(`vxc` → VXOBJ → `vlnk` → clang 链接为原生可执行),
|
|
7
|
+
> 原「VM 内建指令」前提整体作废,本文末尾的 VM 指令清单不再代表演进方向。
|
|
8
|
+
> 本目录保留作为标准库 API 设计参考,待编译器能力补齐后逐步原生化。
|
|
9
|
+
|
|
10
|
+
## 当前真实可用的能力
|
|
11
|
+
|
|
12
|
+
标准库的职能当前由两套机制承担:
|
|
13
|
+
|
|
14
|
+
| 机制 | 内容 | 位置 |
|
|
15
|
+
|------|------|------|
|
|
16
|
+
| 内置运行时(无需声明) | `out` / `out_int` / `out_bool` / `out_float` / `sys_argv_count` / `sys_argv_get` / `file_read` / `file_write` / `file_exists` / `puts` / `exit` / `malloc` / `free` | `src/runtime/vxrt.c` |
|
|
17
|
+
| 导入库(`@imp` / `@cppimp`) | math / text / fmath / collections/vec / cpp/greet / sys(`system(cmd)`)(VX ABI:`_vx_<lib>_<name>`) | `headers/` |
|
|
18
|
+
|
|
19
|
+
文件 I/O 与命令行参数等本目录中标记 `not implemented` 的能力,
|
|
20
|
+
在上述内置运行时中已有原生实现,可直接使用(见 README §2.1 / docs/SYNTAX.md §3.3)。
|
|
21
|
+
|
|
22
|
+
## 与编译器现状的差距(v1.12.0 实测)
|
|
23
|
+
|
|
24
|
+
47 个 `.vx` 文件中 36 个可被当前编译器解析,11 个失败,对照如下:
|
|
25
|
+
|
|
26
|
+
| 文件 | 依赖的不支持语法 |
|
|
27
|
+
|------|------------------|
|
|
28
|
+
| `error.vx`、`sys/coroutine.vx` | `enum` 声明 |
|
|
29
|
+
| `bytes.vx` | 泛型参数 `Vec<int>` |
|
|
30
|
+
| `reflect/mod.vx` | 类内常量字段(无类型注解赋值字段) |
|
|
31
|
+
| `macros.vx` | `macro` 声明 |
|
|
32
|
+
| `conv.vx` | 类型转换调用 `int(v)` |
|
|
33
|
+
| `char.vx` | 十六进制字面量 `0x4E00` |
|
|
34
|
+
| `collections/linked_list.vx` | 函数体内 `import` |
|
|
35
|
+
| `mem/arena.vx`、`mem/page.vx` | 空函数体(仅注释,无语句) |
|
|
36
|
+
| `tests/test_string_io.vx` | 字符串方法调用 `s.split(...)` |
|
|
37
|
+
|
|
38
|
+
> 可解析 ≠ 可执行:`import` 语法、全局状态、字符串方法等运行时支撑
|
|
39
|
+
> 同样未落地。`std/tests/` 从未接入 CTest,不代表有效回归覆盖。
|
|
40
|
+
|
|
41
|
+
## 编译器能力补齐后的演进路径
|
|
42
|
+
|
|
43
|
+
1. 解析缺口(上表)逐项支持后,std/ 源码先达到「全部可解析」;
|
|
44
|
+
2. `import` / 全局 `var`/`const` 代码生成落地后,std/ 以 `@imp` 或模块
|
|
45
|
+
编译单元形式接入工具链;
|
|
46
|
+
3. VM 前提的功能(协程、同步原语等)按原生语义重新设计或裁剪。
|
|
47
|
+
|
|
48
|
+
## 目录结构(设计稿)
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
std/
|
|
52
|
+
├── mod.vx # std 总入口
|
|
53
|
+
├── prelude.vx # 预导入模块
|
|
54
|
+
├── error.vx # 错误处理(零依赖)
|
|
55
|
+
├── macros.vx # 编译时宏工具(零依赖)
|
|
56
|
+
├── collections/ # 核心数据结构(vec / hashmap / linked_list / set)
|
|
57
|
+
├── string/ # 字符串处理(util / pattern / codec / iter / core)
|
|
58
|
+
├── math/ # 基础数学(math / random)
|
|
59
|
+
├── io/ # 输入输出与文件系统(file / stream / path / dir)
|
|
60
|
+
├── mem/ # 内存管理(allocator / arena / general / page)
|
|
61
|
+
├── sys/ # 系统与并发(time / duration / datetime / coroutine / sync / env)
|
|
62
|
+
├── iter.vx / option.vx / conv.vx / bytes.vx / char.vx / reflect/
|
|
63
|
+
└── tests/ # 设计期手写用例(未接入 CTest)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## 模块依赖关系(设计)
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
error (零依赖)
|
|
70
|
+
macros (零依赖)
|
|
71
|
+
↓
|
|
72
|
+
collections → error
|
|
73
|
+
↓
|
|
74
|
+
string → collections, error
|
|
75
|
+
↓
|
|
76
|
+
math → (弱依赖 collections)
|
|
77
|
+
↓
|
|
78
|
+
io → collections, string, error, sys
|
|
79
|
+
sys → collections, error
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## 语法约束(当前 VX 编译器限制)
|
|
83
|
+
|
|
84
|
+
v1.0 标准库基于设计期编译器能力编写:
|
|
85
|
+
|
|
86
|
+
- 使用 `class` 替代 `struct + impl`(VX 无 `impl` 块)
|
|
87
|
+
- 使用 `if/elif/else` 替代 `match` 表达式
|
|
88
|
+
- 使用函数返回值替代 `const` 常量
|
|
89
|
+
- 所有定义默认公开(VX 无 `pub` 关键字)
|
|
90
|
+
- 以 `var` 类型字段模拟泛型(`Option.value: var`)
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# ============================================================
|
|
2
|
+
# std::bytes —— 字节缓冲区
|
|
3
|
+
# 为 VXOBJ 二进制生成提供基础支持
|
|
4
|
+
# 底层用 Vec<int> 存储 0..255
|
|
5
|
+
# ============================================================
|
|
6
|
+
|
|
7
|
+
import std.collections.vec
|
|
8
|
+
import std.error
|
|
9
|
+
|
|
10
|
+
class ByteBuffer:
|
|
11
|
+
data: Vec<int> # Vec<int>
|
|
12
|
+
|
|
13
|
+
func new() -> ByteBuffer:
|
|
14
|
+
b = new ByteBuffer()
|
|
15
|
+
b.data = new Vec<int>()
|
|
16
|
+
return b
|
|
17
|
+
|
|
18
|
+
# 写入单字节(0..255)
|
|
19
|
+
func write_u8(v: int):
|
|
20
|
+
self.data.push(v % 256)
|
|
21
|
+
|
|
22
|
+
# 大端写入 32 位无符号整数
|
|
23
|
+
func write_u32_be(v: int):
|
|
24
|
+
self.write_u8(v / 16777216 % 256)
|
|
25
|
+
self.write_u8(v / 65536 % 256)
|
|
26
|
+
self.write_u8(v / 256 % 256)
|
|
27
|
+
self.write_u8(v % 256)
|
|
28
|
+
|
|
29
|
+
# 写入原始字节数组
|
|
30
|
+
func write_bytes(arr: Vec<int>):
|
|
31
|
+
i = 0
|
|
32
|
+
while i < len(arr):
|
|
33
|
+
self.data.push(arr[i] % 256)
|
|
34
|
+
i = i + 1
|
|
35
|
+
|
|
36
|
+
# 将字符串按 UTF-8 字节写入(每个字符一个码点字节,仅适合 ASCII/字节串)
|
|
37
|
+
func write_str(s: str):
|
|
38
|
+
i: int = 0
|
|
39
|
+
while i < len(s):
|
|
40
|
+
self.data.push(ord(s[i]) % 256)
|
|
41
|
+
i = i + 1
|
|
42
|
+
|
|
43
|
+
# 返回原始字节数组
|
|
44
|
+
func to_vec() -> Vec<int>:
|
|
45
|
+
return self.data.to_array()
|
|
46
|
+
|
|
47
|
+
# 当前缓冲区大小
|
|
48
|
+
func size() -> int:
|
|
49
|
+
return self.data.size()
|
|
50
|
+
|
|
51
|
+
# 清空缓冲区
|
|
52
|
+
func clear():
|
|
53
|
+
self.data.clear()
|
|
54
|
+
|
|
55
|
+
# 写入另一个 ByteBuffer 的内容
|
|
56
|
+
func write_buffer(other: ByteBuffer):
|
|
57
|
+
bytes: Vec<int> = other.to_vec()
|
|
58
|
+
self.write_bytes(bytes)
|
|
59
|
+
|
|
60
|
+
# 生成 4 字节大端长度前缀 + 内容
|
|
61
|
+
func write_with_length_prefix() -> ByteBuffer:
|
|
62
|
+
result = new ByteBuffer()
|
|
63
|
+
result.write_u32_be(self.size())
|
|
64
|
+
result.write_buffer(self)
|
|
65
|
+
return result
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# ============================================================
|
|
2
|
+
# std::char —— 字符分类工具
|
|
3
|
+
# 依赖 VM 内建 ord(c) 返回 Unicode 码点
|
|
4
|
+
# ============================================================
|
|
5
|
+
|
|
6
|
+
import std.error
|
|
7
|
+
|
|
8
|
+
class Char:
|
|
9
|
+
# 是否为 ASCII 数字 '0'..'9'
|
|
10
|
+
func is_digit(c: var) -> bool:
|
|
11
|
+
var code = ord(c)
|
|
12
|
+
return code >= 48 && code <= 57
|
|
13
|
+
|
|
14
|
+
# 是否为 ASCII 字母(含大小写)
|
|
15
|
+
func is_alpha(c: var) -> bool:
|
|
16
|
+
var code = ord(c)
|
|
17
|
+
return (code >= 65 && code <= 90) || (code >= 97 && code <= 122)
|
|
18
|
+
|
|
19
|
+
# 是否为 ASCII 字母或数字
|
|
20
|
+
func is_alnum(c: var) -> bool:
|
|
21
|
+
return Char.is_alpha(c) || Char.is_digit(c)
|
|
22
|
+
|
|
23
|
+
# 是否为空白字符(空格、\t、\n、\r、全角空格)
|
|
24
|
+
func is_whitespace(c: var) -> bool:
|
|
25
|
+
var code = ord(c)
|
|
26
|
+
return code == 32 || code == 9 || code == 10 || code == 13 || code == 12288
|
|
27
|
+
|
|
28
|
+
# 是否为小写 ASCII 字母
|
|
29
|
+
func is_lower(c: var) -> bool:
|
|
30
|
+
var code = ord(c)
|
|
31
|
+
return code >= 97 && code <= 122
|
|
32
|
+
|
|
33
|
+
# 是否为大写 ASCII 字母
|
|
34
|
+
func is_upper(c: var) -> bool:
|
|
35
|
+
var code = ord(c)
|
|
36
|
+
return code >= 65 && code <= 90
|
|
37
|
+
|
|
38
|
+
# 是否为 CJK 统一表意文字或常见 CJK 符号区
|
|
39
|
+
func is_cjk(c: var) -> bool:
|
|
40
|
+
var code = ord(c)
|
|
41
|
+
return (code >= 0x4E00 && code <= 0x9FFF) || (code >= 0x3000 && code <= 0x303F) || (code >= 0xFF00 && code <= 0xFFEF)
|
|
42
|
+
|
|
43
|
+
# 是否为下划线
|
|
44
|
+
func is_underscore(c: var) -> bool:
|
|
45
|
+
return ord(c) == 95
|
|
46
|
+
|
|
47
|
+
# 是否可作为标识符首字符(字母、下划线、CJK)
|
|
48
|
+
func is_ident_start(c: var) -> bool:
|
|
49
|
+
return Char.is_alpha(c) || Char.is_underscore(c) || Char.is_cjk(c)
|
|
50
|
+
|
|
51
|
+
# 是否可作为标识符后续字符(字母、数字、下划线、CJK)
|
|
52
|
+
func is_ident_part(c: var) -> bool:
|
|
53
|
+
return Char.is_alnum(c) || Char.is_underscore(c) || Char.is_cjk(c)
|
|
54
|
+
|
|
55
|
+
# 将字符转为小写(仅 ASCII)
|
|
56
|
+
func to_lower(c: var) -> var:
|
|
57
|
+
var code = ord(c)
|
|
58
|
+
if code >= 65 && code <= 90:
|
|
59
|
+
return chr(code + 32)
|
|
60
|
+
return c
|
|
61
|
+
|
|
62
|
+
# 将字符转为大写(仅 ASCII)
|
|
63
|
+
func to_upper(c: var) -> var:
|
|
64
|
+
var code = ord(c)
|
|
65
|
+
if code >= 97 && code <= 122:
|
|
66
|
+
return chr(code - 32)
|
|
67
|
+
return c
|
|
68
|
+
|
|
69
|
+
# 顶层便捷函数
|
|
70
|
+
func is_digit(c: var) -> bool:
|
|
71
|
+
return Char.is_digit(c)
|
|
72
|
+
|
|
73
|
+
func is_alpha(c: var) -> bool:
|
|
74
|
+
return Char.is_alpha(c)
|
|
75
|
+
|
|
76
|
+
func is_alnum(c: var) -> bool:
|
|
77
|
+
return Char.is_alnum(c)
|
|
78
|
+
|
|
79
|
+
func is_whitespace(c: var) -> bool:
|
|
80
|
+
return Char.is_whitespace(c)
|
|
81
|
+
|
|
82
|
+
func is_cjk(c: var) -> bool:
|
|
83
|
+
return Char.is_cjk(c)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# ============================================================
|
|
2
|
+
# std::collections::hashmap —— 哈希表
|
|
3
|
+
# ============================================================
|
|
4
|
+
|
|
5
|
+
class HashMap:
|
|
6
|
+
size: int
|
|
7
|
+
|
|
8
|
+
func new() -> int:
|
|
9
|
+
var m = new HashMap()
|
|
10
|
+
m.size = 0
|
|
11
|
+
return m
|
|
12
|
+
|
|
13
|
+
func insert(m: HashMap, key: var, val: var):
|
|
14
|
+
m.size = m.size + 1
|
|
15
|
+
|
|
16
|
+
func remove(m: HashMap, key: var) -> int:
|
|
17
|
+
if m.size > 0:
|
|
18
|
+
m.size = m.size - 1
|
|
19
|
+
return nil
|
|
20
|
+
|
|
21
|
+
func has_key(m: HashMap, key: var) -> bool:
|
|
22
|
+
return false
|
|
23
|
+
|
|
24
|
+
func size(m: HashMap) -> int:
|
|
25
|
+
return m.size
|
|
26
|
+
|
|
27
|
+
func is_empty(m: HashMap) -> bool:
|
|
28
|
+
return m.size == 0
|
|
29
|
+
|
|
30
|
+
func clear(m: HashMap):
|
|
31
|
+
m.size = 0
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# ============================================================
|
|
2
|
+
# std::collections::linked_list —— 双向链表
|
|
3
|
+
# ============================================================
|
|
4
|
+
|
|
5
|
+
import std.error
|
|
6
|
+
|
|
7
|
+
# ---- Node 类:链表节点 ----
|
|
8
|
+
class Node:
|
|
9
|
+
value: var
|
|
10
|
+
prev: var # Node 或 nil
|
|
11
|
+
next: var # Node 或 nil
|
|
12
|
+
|
|
13
|
+
func new(val: var) -> int:
|
|
14
|
+
var n = new Node()
|
|
15
|
+
n.value = val
|
|
16
|
+
n.prev = nil
|
|
17
|
+
n.next = nil
|
|
18
|
+
return n
|
|
19
|
+
|
|
20
|
+
# ---- LinkedList 类:双向链表 ----
|
|
21
|
+
class LinkedList:
|
|
22
|
+
head: var # Node 或 nil
|
|
23
|
+
tail: var # Node 或 nil
|
|
24
|
+
len: int
|
|
25
|
+
|
|
26
|
+
# ========== 构造器 ==========
|
|
27
|
+
|
|
28
|
+
func new() -> int:
|
|
29
|
+
var ll = new LinkedList()
|
|
30
|
+
ll.head = nil
|
|
31
|
+
ll.tail = nil
|
|
32
|
+
ll.len = 0
|
|
33
|
+
return ll
|
|
34
|
+
|
|
35
|
+
# ========== 头部操作 ==========
|
|
36
|
+
|
|
37
|
+
func push_front(val: var):
|
|
38
|
+
var node = new Node(val)
|
|
39
|
+
if self.head == nil:
|
|
40
|
+
self.head = node
|
|
41
|
+
self.tail = node
|
|
42
|
+
else:
|
|
43
|
+
node.next = self.head
|
|
44
|
+
self.head.prev = node
|
|
45
|
+
self.head = node
|
|
46
|
+
self.len = self.len + 1
|
|
47
|
+
|
|
48
|
+
func pop_front() -> int:
|
|
49
|
+
if self.head == nil:
|
|
50
|
+
return Result.err(error.value_error("LinkedList.pop_front: empty list"))
|
|
51
|
+
var val = self.head.value
|
|
52
|
+
if self.head == self.tail:
|
|
53
|
+
self.head = nil
|
|
54
|
+
self.tail = nil
|
|
55
|
+
else:
|
|
56
|
+
self.head = self.head.next
|
|
57
|
+
self.head.prev = nil
|
|
58
|
+
self.len = self.len - 1
|
|
59
|
+
return Result.ok(val)
|
|
60
|
+
|
|
61
|
+
func peek_front() -> int:
|
|
62
|
+
if self.head == nil:
|
|
63
|
+
return Result.err(error.value_error("LinkedList.peek_front: empty list"))
|
|
64
|
+
return Result.ok(self.head.value)
|
|
65
|
+
|
|
66
|
+
# ========== 尾部操作 ==========
|
|
67
|
+
|
|
68
|
+
func push_back(val: var):
|
|
69
|
+
var node = new Node(val)
|
|
70
|
+
if self.tail == nil:
|
|
71
|
+
self.head = node
|
|
72
|
+
self.tail = node
|
|
73
|
+
else:
|
|
74
|
+
node.prev = self.tail
|
|
75
|
+
self.tail.next = node
|
|
76
|
+
self.tail = node
|
|
77
|
+
self.len = self.len + 1
|
|
78
|
+
|
|
79
|
+
func pop_back() -> int:
|
|
80
|
+
if self.tail == nil:
|
|
81
|
+
return Result.err(error.value_error("LinkedList.pop_back: empty list"))
|
|
82
|
+
var val = self.tail.value
|
|
83
|
+
if self.head == self.tail:
|
|
84
|
+
self.head = nil
|
|
85
|
+
self.tail = nil
|
|
86
|
+
else:
|
|
87
|
+
self.tail = self.tail.prev
|
|
88
|
+
self.tail.next = nil
|
|
89
|
+
self.len = self.len - 1
|
|
90
|
+
return Result.ok(val)
|
|
91
|
+
|
|
92
|
+
func peek_back() -> int:
|
|
93
|
+
if self.tail == nil:
|
|
94
|
+
return Result.err(error.value_error("LinkedList.peek_back: empty list"))
|
|
95
|
+
return Result.ok(self.tail.value)
|
|
96
|
+
|
|
97
|
+
# ========== 查询 ==========
|
|
98
|
+
|
|
99
|
+
func size() -> int:
|
|
100
|
+
return self.len
|
|
101
|
+
|
|
102
|
+
func is_empty() -> bool:
|
|
103
|
+
if self.len == 0:
|
|
104
|
+
return true
|
|
105
|
+
return false
|
|
106
|
+
|
|
107
|
+
# 查找元素,返回第一个匹配的索引
|
|
108
|
+
func index_of(val: var) -> int:
|
|
109
|
+
var current = self.head
|
|
110
|
+
var idx = 0
|
|
111
|
+
while current != nil:
|
|
112
|
+
if current.value == val:
|
|
113
|
+
return idx
|
|
114
|
+
current = current.next
|
|
115
|
+
idx = idx + 1
|
|
116
|
+
return -1
|
|
117
|
+
|
|
118
|
+
# 检查是否包含
|
|
119
|
+
func contains(val: var) -> bool:
|
|
120
|
+
if LinkedList.index_of(self, val) >= 0:
|
|
121
|
+
return true
|
|
122
|
+
return false
|
|
123
|
+
|
|
124
|
+
# ========== 修改 ==========
|
|
125
|
+
|
|
126
|
+
# 清空链表
|
|
127
|
+
func clear():
|
|
128
|
+
self.head = nil
|
|
129
|
+
self.tail = nil
|
|
130
|
+
self.len = 0
|
|
131
|
+
|
|
132
|
+
# ========== 转换 ==========
|
|
133
|
+
|
|
134
|
+
# 转为 Vec
|
|
135
|
+
func to_vec() -> int:
|
|
136
|
+
import std.collections.vec
|
|
137
|
+
var result = new Vec()
|
|
138
|
+
var current = self.head
|
|
139
|
+
while current != nil:
|
|
140
|
+
result.push(current.value)
|
|
141
|
+
current = current.next
|
|
142
|
+
return result
|
|
143
|
+
|
|
144
|
+
# 转为内建数组
|
|
145
|
+
func to_array() -> int:
|
|
146
|
+
var result = []
|
|
147
|
+
var current = self.head
|
|
148
|
+
while current != nil:
|
|
149
|
+
result = result + [current.value]
|
|
150
|
+
current = current.next
|
|
151
|
+
return result
|
|
152
|
+
|
|
153
|
+
# ========== 字符串表示 ==========
|
|
154
|
+
|
|
155
|
+
func to_string() -> int:
|
|
156
|
+
var s = "["
|
|
157
|
+
var current = self.head
|
|
158
|
+
var first = true
|
|
159
|
+
while current != nil:
|
|
160
|
+
if first == false:
|
|
161
|
+
s = s + " <-> "
|
|
162
|
+
s = s + current.value
|
|
163
|
+
first = false
|
|
164
|
+
current = current.next
|
|
165
|
+
s = s + "]"
|
|
166
|
+
return s
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# ============================================================
|
|
2
|
+
# std::collections —— 模块入口
|
|
3
|
+
# ============================================================
|
|
4
|
+
|
|
5
|
+
import std.collections.vec
|
|
6
|
+
import std.collections.hashmap
|
|
7
|
+
import std.collections.linked_list
|
|
8
|
+
import std.collections.set
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# ============================================================
|
|
2
|
+
# std::collections::set —— 集合
|
|
3
|
+
# 基于 HashMap 实现,Zig 风格无 self
|
|
4
|
+
# ============================================================
|
|
5
|
+
|
|
6
|
+
import std.collections.hashmap
|
|
7
|
+
|
|
8
|
+
class Set:
|
|
9
|
+
inner: var # HashMap 实例
|
|
10
|
+
len: int
|
|
11
|
+
|
|
12
|
+
func new() -> int:
|
|
13
|
+
var s = new Set()
|
|
14
|
+
s.inner = HashMap.new()
|
|
15
|
+
s.len = 0
|
|
16
|
+
return s
|
|
17
|
+
|
|
18
|
+
func insert(s: Set, val: var):
|
|
19
|
+
if HashMap.has_key(s.inner, val) == false:
|
|
20
|
+
HashMap.insert(s.inner, val, true)
|
|
21
|
+
s.len = s.len + 1
|
|
22
|
+
|
|
23
|
+
func remove(s: Set, val: var) -> int:
|
|
24
|
+
var r = HashMap.remove(s.inner, val)
|
|
25
|
+
if r.is_ok:
|
|
26
|
+
s.len = s.len - 1
|
|
27
|
+
return r
|
|
28
|
+
|
|
29
|
+
func contains(s: Set, val: var) -> bool:
|
|
30
|
+
return HashMap.has_key(s.inner, val)
|
|
31
|
+
|
|
32
|
+
func size(s: Set) -> int:
|
|
33
|
+
return s.len
|
|
34
|
+
|
|
35
|
+
func is_empty(s: Set) -> bool:
|
|
36
|
+
if s.len == 0:
|
|
37
|
+
return true
|
|
38
|
+
return false
|
|
39
|
+
|
|
40
|
+
func clear(s: Set):
|
|
41
|
+
HashMap.clear(s.inner)
|
|
42
|
+
s.len = 0
|
|
43
|
+
|
|
44
|
+
func union(s: Set, other: var) -> int:
|
|
45
|
+
var result = Set.new()
|
|
46
|
+
var self_keys = HashMap.keys(s.inner)
|
|
47
|
+
var i = 0
|
|
48
|
+
while i < len(self_keys):
|
|
49
|
+
Set.insert(result, self_keys[i])
|
|
50
|
+
i = i + 1
|
|
51
|
+
var other_keys = HashMap.keys(other.inner)
|
|
52
|
+
i = 0
|
|
53
|
+
while i < len(other_keys):
|
|
54
|
+
Set.insert(result, other_keys[i])
|
|
55
|
+
i = i + 1
|
|
56
|
+
return result
|
|
57
|
+
|
|
58
|
+
func intersection(s: Set, other: var) -> int:
|
|
59
|
+
var result = Set.new()
|
|
60
|
+
var self_keys = HashMap.keys(s.inner)
|
|
61
|
+
var i = 0
|
|
62
|
+
while i < len(self_keys):
|
|
63
|
+
var key = self_keys[i]
|
|
64
|
+
if Set.contains(other, key):
|
|
65
|
+
Set.insert(result, key)
|
|
66
|
+
i = i + 1
|
|
67
|
+
return result
|
|
68
|
+
|
|
69
|
+
func difference(s: Set, other: var) -> int:
|
|
70
|
+
var result = Set.new()
|
|
71
|
+
var self_keys = HashMap.keys(s.inner)
|
|
72
|
+
var i = 0
|
|
73
|
+
while i < len(self_keys):
|
|
74
|
+
var key = self_keys[i]
|
|
75
|
+
if Set.contains(other, key) == false:
|
|
76
|
+
Set.insert(result, key)
|
|
77
|
+
i = i + 1
|
|
78
|
+
return result
|
|
79
|
+
|
|
80
|
+
func is_subset(s: Set, other: var) -> bool:
|
|
81
|
+
var self_keys = HashMap.keys(s.inner)
|
|
82
|
+
var i = 0
|
|
83
|
+
while i < len(self_keys):
|
|
84
|
+
if Set.contains(other, self_keys[i]) == false:
|
|
85
|
+
return false
|
|
86
|
+
i = i + 1
|
|
87
|
+
return true
|
|
88
|
+
|
|
89
|
+
func is_superset(s: Set, other: var) -> bool:
|
|
90
|
+
return Set.is_subset(other, s)
|
|
91
|
+
|
|
92
|
+
func to_string(s: Set) -> int:
|
|
93
|
+
var s_str = "Set{"
|
|
94
|
+
var self_keys = HashMap.keys(s.inner)
|
|
95
|
+
var i = 0
|
|
96
|
+
while i < len(self_keys):
|
|
97
|
+
if i > 0:
|
|
98
|
+
s_str = s_str + ", "
|
|
99
|
+
s_str = s_str + self_keys[i]
|
|
100
|
+
i = i + 1
|
|
101
|
+
s_str = s_str + "}"
|
|
102
|
+
return s_str
|