codecin 7.22.10__py3-none-win_amd64.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.
- codecin/__init__.py +27 -0
- codecin/aot.py +1057 -0
- codecin/assembler.py +619 -0
- codecin/cbackend.py +3024 -0
- codecin/cin.py +4265 -0
- codecin/cli.py +670 -0
- codecin/codecin_native.dll +0 -0
- codecin/config.py +50 -0
- codecin/console.py +183 -0
- codecin/cpu.py +312 -0
- codecin/crom.py +449 -0
- codecin/cvm.py +708 -0
- codecin/disasm.py +149 -0
- codecin/environment.py +176 -0
- codecin/errors.py +54 -0
- codecin/isa.py +560 -0
- codecin/lib/array/pic.toml +10 -0
- codecin/lib/array/src/array.cin +103 -0
- codecin/lib/bigint/pic.toml +10 -0
- codecin/lib/bigint/src/bigint.cin +589 -0
- codecin/lib/bits/pic.toml +10 -0
- codecin/lib/bits/src/bits.cin +143 -0
- codecin/lib/bitset/pic.toml +10 -0
- codecin/lib/bitset/src/bitset.cin +374 -0
- codecin/lib/codec/pic.toml +10 -0
- codecin/lib/codec/src/codec.cin +563 -0
- codecin/lib/combin/pic.toml +10 -0
- codecin/lib/combin/src/combin.cin +414 -0
- codecin/lib/conv/pic.toml +10 -0
- codecin/lib/conv/src/conv.cin +151 -0
- codecin/lib/cppstd/pic.toml +10 -0
- codecin/lib/cppstd/src/cppstd.cin +418 -0
- codecin/lib/cstd/pic.toml +10 -0
- codecin/lib/cstd/src/cstd.cin +424 -0
- codecin/lib/csv/pic.toml +10 -0
- codecin/lib/csv/src/csv.cin +405 -0
- codecin/lib/dp/pic.toml +10 -0
- codecin/lib/dp/src/dp.cin +325 -0
- codecin/lib/ffi/pic.toml +10 -0
- codecin/lib/ffi/src/ffi.cin +163 -0
- codecin/lib/fmt/pic.toml +10 -0
- codecin/lib/fmt/src/fmt.cin +325 -0
- codecin/lib/frac/pic.toml +10 -0
- codecin/lib/frac/src/frac.cin +252 -0
- codecin/lib/gostd/pic.toml +10 -0
- codecin/lib/gostd/src/gostd.cin +415 -0
- codecin/lib/graph/pic.toml +10 -0
- codecin/lib/graph/src/graph.cin +367 -0
- codecin/lib/gui/pic.toml +10 -0
- codecin/lib/gui/src/gui.cin +218 -0
- codecin/lib/hash/pic.toml +10 -0
- codecin/lib/hash/src/hash.cin +67 -0
- codecin/lib/heap/pic.toml +10 -0
- codecin/lib/heap/src/heap.cin +199 -0
- codecin/lib/io/pic.toml +10 -0
- codecin/lib/io/src/io.cin +141 -0
- codecin/lib/json/pic.toml +10 -0
- codecin/lib/json/src/json.cin +84 -0
- codecin/lib/key/pic.toml +10 -0
- codecin/lib/key/src/key.cin +78 -0
- codecin/lib/math/pic.toml +10 -0
- codecin/lib/math/src/math.cin +50 -0
- codecin/lib/matrix/pic.toml +10 -0
- codecin/lib/matrix/src/matrix.cin +155 -0
- codecin/lib/net/pic.toml +10 -0
- codecin/lib/net/src/net.cin +171 -0
- codecin/lib/path/pic.toml +10 -0
- codecin/lib/path/src/path.cin +372 -0
- codecin/lib/queue/pic.toml +10 -0
- codecin/lib/queue/src/queue.cin +112 -0
- codecin/lib/rand/pic.toml +10 -0
- codecin/lib/rand/src/rand.cin +46 -0
- codecin/lib/set/pic.toml +10 -0
- codecin/lib/set/src/set.cin +269 -0
- codecin/lib/sort/pic.toml +10 -0
- codecin/lib/sort/src/sort.cin +93 -0
- codecin/lib/stat/pic.toml +10 -0
- codecin/lib/stat/src/stat.cin +138 -0
- codecin/lib/str/pic.toml +10 -0
- codecin/lib/str/src/str.cin +54 -0
- codecin/lib/termux/pic.toml +10 -0
- codecin/lib/termux/src/termux.cin +118 -0
- codecin/lib/test/pic.toml +10 -0
- codecin/lib/test/src/test.cin +74 -0
- codecin/lib/text/pic.toml +10 -0
- codecin/lib/text/src/text.cin +323 -0
- codecin/lib/time/pic.toml +10 -0
- codecin/lib/time/src/time.cin +54 -0
- codecin/lib/token/pic.toml +10 -0
- codecin/lib/token/src/token.cin +183 -0
- codecin/lib/tree/pic.toml +10 -0
- codecin/lib/tree/src/tree.cin +256 -0
- codecin/lib/unionfind/pic.toml +10 -0
- codecin/lib/unionfind/src/unionfind.cin +115 -0
- codecin/lib/validate/pic.toml +10 -0
- codecin/lib/validate/src/validate.cin +148 -0
- codecin/lib/vec/pic.toml +10 -0
- codecin/lib/vec/src/vec.cin +88 -0
- codecin/logger.py +145 -0
- codecin/memory.py +306 -0
- codecin/mkn.py +916 -0
- codecin/native/aot/aot.go +114 -0
- codecin/native/aot/stub_main.go.txt +23 -0
- codecin/native/build.ps1 +33 -0
- codecin/native/build.sh +48 -0
- codecin/native/cmd/cincc/main.go +66 -0
- codecin/native/compiler/codegen.go +2238 -0
- codecin/native/compiler/compile_test.go +1069 -0
- codecin/native/compiler/parser.go +1331 -0
- codecin/native/compiler/syscalls.go +166 -0
- codecin/native/compiler/tokenizer.go +394 -0
- codecin/native/compiler/types.go +217 -0
- codecin/native/engine/args.go +37 -0
- codecin/native/engine/audio.go +402 -0
- codecin/native/engine/audio_other.go +105 -0
- codecin/native/engine/audio_test.go +279 -0
- codecin/native/engine/audio_windows.go +190 -0
- codecin/native/engine/canvas.go +271 -0
- codecin/native/engine/console_other.go +9 -0
- codecin/native/engine/console_windows.go +43 -0
- codecin/native/engine/crom.go +86 -0
- codecin/native/engine/encode.go +93 -0
- codecin/native/engine/engine_test.go +198 -0
- codecin/native/engine/ffi_nocgo.go +20 -0
- codecin/native/engine/ffi_registry.go +69 -0
- codecin/native/engine/ffi_unix.go +109 -0
- codecin/native/engine/ffi_windows.go +117 -0
- codecin/native/engine/gui.go +209 -0
- codecin/native/engine/gui_stub.go +13 -0
- codecin/native/engine/gui_test.go +148 -0
- codecin/native/engine/gui_windows.go +604 -0
- codecin/native/engine/gui_x11.go +723 -0
- codecin/native/engine/host_test.go +139 -0
- codecin/native/engine/hostcrypto.go +30 -0
- codecin/native/engine/hostdesktop.go +173 -0
- codecin/native/engine/hostnet.go +127 -0
- codecin/native/engine/isa_gen.go +689 -0
- codecin/native/engine/keyboard.go +432 -0
- codecin/native/engine/keyboard_other.go +94 -0
- codecin/native/engine/keyboard_stub.go +17 -0
- codecin/native/engine/keyboard_termios_darwin.go +9 -0
- codecin/native/engine/keyboard_termios_linux.go +9 -0
- codecin/native/engine/keyboard_test.go +246 -0
- codecin/native/engine/keyboard_windows.go +208 -0
- codecin/native/engine/keyboard_windows_test.go +123 -0
- codecin/native/engine/lineedit.go +262 -0
- codecin/native/engine/lineedit_test.go +105 -0
- codecin/native/engine/lineint_unix.go +17 -0
- codecin/native/engine/lineint_windows.go +19 -0
- codecin/native/engine/mem_other.go +37 -0
- codecin/native/engine/mem_windows.go +33 -0
- codecin/native/engine/native.go +640 -0
- codecin/native/engine/netsock.go +321 -0
- codecin/native/engine/ops_ext.go +515 -0
- codecin/native/engine/stdin.go +72 -0
- codecin/native/engine/stdin_other.go +9 -0
- codecin/native/engine/stdin_windows.go +60 -0
- codecin/native/engine/system.go +313 -0
- codecin/native/engine/termux.go +180 -0
- codecin/native/engine/version_gen.go +7 -0
- codecin/native/engine/vkb_darwin.go +62 -0
- codecin/native/engine/vkb_linux.go +95 -0
- codecin/native/engine/vkb_stub.go +11 -0
- codecin/native/engine/vm.go +1677 -0
- codecin/native/go.mod +3 -0
- codecin/native/ir/ir.go +48 -0
- codecin/native/main.go +448 -0
- codecin/native/mc/aarch64.go +165 -0
- codecin/native/mc/aarch64_test.go +30 -0
- codecin/native/mc/elf.go +88 -0
- codecin/native/mc/print_test.go +52 -0
- codecin/native/mc/x86_64.go +1262 -0
- codecin/native/mc/x86_64_test.go +102 -0
- codecin/native.py +432 -0
- codecin/native_codegen.py +987 -0
- codecin/nativegen.py +786 -0
- codecin/object.py +539 -0
- codecin/optimizer.py +236 -0
- codecin/packaging/__init__.py +46 -0
- codecin/packaging/archive.py +177 -0
- codecin/packaging/model.py +519 -0
- codecin/packaging/spec.py +193 -0
- codecin/packaging/store.py +372 -0
- codecin/packaging/toml.py +541 -0
- codecin/packaging/vcs.py +215 -0
- codecin/pic.py +1917 -0
- codecin/pic_api.py +374 -0
- codecin/pic_login.py +519 -0
- codecin/pic_registry.py +515 -0
- codecin/registers.py +130 -0
- codecin/sdgcc.py +493 -0
- codecin/stats.py +230 -0
- codecin/stdlib.py +138 -0
- codecin/toolchain.py +396 -0
- codecin/toolkit.py +26 -0
- codecin/version.py +260 -0
- codecin-7.22.10.dist-info/METADATA +1077 -0
- codecin-7.22.10.dist-info/RECORD +202 -0
- codecin-7.22.10.dist-info/WHEEL +5 -0
- codecin-7.22.10.dist-info/entry_points.txt +5 -0
- codecin-7.22.10.dist-info/licenses/LICENSE +7 -0
- codecin-7.22.10.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
// Code CIN 官方标准库: 经典动态规划 (dp)
|
|
2
|
+
// 用法: import "dp.cin"
|
|
3
|
+
//
|
|
4
|
+
// 纯 CIN 实现, 不使用任何宿主能力内建, 也不做动态内存分配:
|
|
5
|
+
// 所有 DP 表都是库内全局定长数组 (滚动数组), 容量上限如下:
|
|
6
|
+
// DP_STR_MAX = 128 字符串类 DP (LCS / 编辑距离 / 回文) 处理的字节数上限
|
|
7
|
+
// DP_KNAP_CAP = 256 0/1 背包的容量上限
|
|
8
|
+
// DP_AMOUNT_MAX = 256 零钱问题的金额上限
|
|
9
|
+
// DP_LIS_MAX = 64 最长递增子序列的元素个数上限 (该算法是 O(n^2), 故意给小)
|
|
10
|
+
// DP_PATH_COLS = 256 网格最小路径和的列数上限
|
|
11
|
+
// DP_FIB_CACHE = 64 dp_fib_memo 的自建缓存容量
|
|
12
|
+
//
|
|
13
|
+
// 全局数据段占用 (每槽 8 字节):
|
|
14
|
+
// dp_row_prev[129] + dp_row_cur[129] = 258 槽
|
|
15
|
+
// dp_amt[257] = 257 槽
|
|
16
|
+
// dp_cap[257] = 257 槽
|
|
17
|
+
// dp_fib_cache[64] = 64 槽
|
|
18
|
+
// 标量 (dp_fib_ready / dp_fib_hi) = 2 槽
|
|
19
|
+
// 合计 838 槽 = 6704 字节, 小于默认 64 KiB 数据段。
|
|
20
|
+
//
|
|
21
|
+
// 语义说明与边界行为:
|
|
22
|
+
// * 容量超限一律**按上限截断**(与 heap_build 的做法一致), 不报错:
|
|
23
|
+
// cap > DP_KNAP_CAP 按 DP_KNAP_CAP 算; amount > DP_AMOUNT_MAX 按 DP_AMOUNT_MAX 算;
|
|
24
|
+
// 串长 > DP_STR_MAX 只看前 DP_STR_MAX 字节; n > DP_LIS_MAX 只看前 DP_LIS_MAX 个元素;
|
|
25
|
+
// 唯一的例外是 dp_min_path: cols > DP_PATH_COLS 时返回 -1 (列数参与行主序下标,
|
|
26
|
+
// 截断会把整张网格读错, 所以明确失败);
|
|
27
|
+
// * 字符串一律按**字节**比较 (s[i] 得到 0..255 的整数编码);
|
|
28
|
+
// * 所有返回值都是 int (64 位有符号), 斐波那契/阶乘/组合数都可能溢出回绕, 不报错;
|
|
29
|
+
// * 库内只有一份全局实例, 上面那些滚动数组是**跨函数复用的内部暂存区**,
|
|
30
|
+
// 调用方不要依赖它们在两次调用之间的内容, 也不要嵌套调用本库的不同函数。
|
|
31
|
+
|
|
32
|
+
const int DP_STR_MAX = 128
|
|
33
|
+
const int DP_KNAP_CAP = 256
|
|
34
|
+
const int DP_AMOUNT_MAX = 256
|
|
35
|
+
const int DP_LIS_MAX = 64
|
|
36
|
+
const int DP_PATH_COLS = 256
|
|
37
|
+
const int DP_FIB_CACHE = 64
|
|
38
|
+
|
|
39
|
+
// 内部暂存区 (详见文件头: 不同函数按需复用)
|
|
40
|
+
int dp_row_prev[DP_STR_MAX + 1]
|
|
41
|
+
int dp_row_cur[DP_STR_MAX + 1]
|
|
42
|
+
int dp_amt[DP_AMOUNT_MAX + 1]
|
|
43
|
+
int dp_cap[DP_KNAP_CAP + 1]
|
|
44
|
+
|
|
45
|
+
// dp_fib_memo 的缓存: dp_fib_ready == 0 表示还没建表
|
|
46
|
+
int dp_fib_cache[DP_FIB_CACHE]
|
|
47
|
+
int dp_fib_ready = 0
|
|
48
|
+
int dp_fib_hi = 0
|
|
49
|
+
|
|
50
|
+
// ---------------- 递推入门 ----------------
|
|
51
|
+
|
|
52
|
+
// 斐波那契数 (迭代, O(n)); fib(0)=0, fib(1)=1, n < 0 返回 0
|
|
53
|
+
// n >= 93 时结果超出 64 位有符号整数范围, 静默回绕
|
|
54
|
+
function dp_fib(int n) -> int {
|
|
55
|
+
if (n <= 0) { return 0 }
|
|
56
|
+
int a = 0
|
|
57
|
+
int b = 1
|
|
58
|
+
for (int i = 2; i <= n; i = i + 1) {
|
|
59
|
+
int c = a + b
|
|
60
|
+
a = b
|
|
61
|
+
b = c
|
|
62
|
+
}
|
|
63
|
+
return b
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 斐波那契数 (带自建缓存): 结果与 dp_fib 完全一致
|
|
67
|
+
// n < 0 返回 0; n >= DP_FIB_CACHE 时不做缓存, 直接走迭代实现
|
|
68
|
+
function dp_fib_memo(int n) -> int {
|
|
69
|
+
if (n < 0) { return 0 }
|
|
70
|
+
if (n >= DP_FIB_CACHE) { return dp_fib(n) }
|
|
71
|
+
if (dp_fib_ready == 0) {
|
|
72
|
+
dp_fib_cache[0] = 0
|
|
73
|
+
dp_fib_cache[1] = 1
|
|
74
|
+
dp_fib_hi = 1
|
|
75
|
+
dp_fib_ready = 1
|
|
76
|
+
}
|
|
77
|
+
while (dp_fib_hi < n) {
|
|
78
|
+
int nxt = dp_fib_hi + 1
|
|
79
|
+
dp_fib_cache[nxt] = dp_fib_cache[nxt - 1] + dp_fib_cache[nxt - 2]
|
|
80
|
+
dp_fib_hi = nxt
|
|
81
|
+
}
|
|
82
|
+
return dp_fib_cache[n]
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 阶乘 (迭代): fact(0)=1, fact(1)=1, n < 0 返回 0
|
|
86
|
+
// n >= 21 时结果超出 64 位有符号整数范围, 静默回绕 (建议 n <= 20)
|
|
87
|
+
function dp_fact(int n) -> int {
|
|
88
|
+
if (n < 0) { return 0 }
|
|
89
|
+
int r = 1
|
|
90
|
+
for (int i = 2; i <= n; i = i + 1) {
|
|
91
|
+
r = r * i
|
|
92
|
+
}
|
|
93
|
+
return r
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// 爬楼梯方案数 (每步 1 级或 2 级): n < 0 返回 0, n == 0 返回 1 (一步不走也算一种)
|
|
97
|
+
// 等价于 fib(n + 1); n 很大时同样按 64 位整数回绕
|
|
98
|
+
function dp_stairs(int n) -> int {
|
|
99
|
+
if (n < 0) { return 0 }
|
|
100
|
+
if (n == 0) { return 1 }
|
|
101
|
+
return dp_fib(n + 1)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// ---------------- 背包 ----------------
|
|
105
|
+
|
|
106
|
+
// 0/1 背包最大价值: n 件物品 (重量 w[i], 价值 v[i]), 容量 cap
|
|
107
|
+
// n <= 0 或 cap <= 0 返回 0;
|
|
108
|
+
// cap > DP_KNAP_CAP 时按 DP_KNAP_CAP 计算;
|
|
109
|
+
// 重量 < 0 的物品按"重量 0"处理 (等价于白拿一件); 重量 > cap 的物品自动跳过
|
|
110
|
+
function dp_knapsack01(int[] w, int[] v, int n, int cap) -> int {
|
|
111
|
+
if (n <= 0) { return 0 }
|
|
112
|
+
if (cap <= 0) { return 0 }
|
|
113
|
+
int c = cap
|
|
114
|
+
if (c > DP_KNAP_CAP) { c = DP_KNAP_CAP }
|
|
115
|
+
for (int j = 0; j <= c; j = j + 1) {
|
|
116
|
+
dp_cap[j] = 0
|
|
117
|
+
}
|
|
118
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
119
|
+
int wi = w[i]
|
|
120
|
+
if (wi < 0) { wi = 0 }
|
|
121
|
+
int vi = v[i]
|
|
122
|
+
if (wi > c) { continue }
|
|
123
|
+
for (int j = c; j >= wi; j = j - 1) {
|
|
124
|
+
int cand = dp_cap[j - wi] + vi
|
|
125
|
+
if (cand > dp_cap[j]) { dp_cap[j] = cand }
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return dp_cap[c]
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ---------------- 字符串 DP ----------------
|
|
132
|
+
|
|
133
|
+
// 最长公共子序列长度 (按字节比较, 滚动两行 O(min) 空间)
|
|
134
|
+
// 任一空串返回 0; 串长超过 DP_STR_MAX 时只看前 DP_STR_MAX 字节
|
|
135
|
+
function dp_lcs(string a, string b) -> int {
|
|
136
|
+
int la = strlen(a)
|
|
137
|
+
int lb = strlen(b)
|
|
138
|
+
if (la > DP_STR_MAX) { la = DP_STR_MAX }
|
|
139
|
+
if (lb > DP_STR_MAX) { lb = DP_STR_MAX }
|
|
140
|
+
for (int j = 0; j <= lb; j = j + 1) {
|
|
141
|
+
dp_row_prev[j] = 0
|
|
142
|
+
}
|
|
143
|
+
for (int i = 1; i <= la; i = i + 1) {
|
|
144
|
+
dp_row_cur[0] = 0
|
|
145
|
+
for (int j = 1; j <= lb; j = j + 1) {
|
|
146
|
+
if (a[i - 1] == b[j - 1]) {
|
|
147
|
+
dp_row_cur[j] = dp_row_prev[j - 1] + 1
|
|
148
|
+
} else {
|
|
149
|
+
int best = dp_row_prev[j]
|
|
150
|
+
if (dp_row_cur[j - 1] > best) { best = dp_row_cur[j - 1] }
|
|
151
|
+
dp_row_cur[j] = best
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
for (int j = 0; j <= lb; j = j + 1) {
|
|
155
|
+
dp_row_prev[j] = dp_row_cur[j]
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return dp_row_prev[lb]
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// 编辑距离 (Levenshtein: 插入 / 删除 / 替换 代价都是 1, 按字节比较)
|
|
162
|
+
// 任一空串时返回另一个串的长度; 串长超过 DP_STR_MAX 时只看前 DP_STR_MAX 字节
|
|
163
|
+
function dp_edit_distance(string a, string b) -> int {
|
|
164
|
+
int la = strlen(a)
|
|
165
|
+
int lb = strlen(b)
|
|
166
|
+
if (la > DP_STR_MAX) { la = DP_STR_MAX }
|
|
167
|
+
if (lb > DP_STR_MAX) { lb = DP_STR_MAX }
|
|
168
|
+
for (int j = 0; j <= lb; j = j + 1) {
|
|
169
|
+
dp_row_prev[j] = j
|
|
170
|
+
}
|
|
171
|
+
for (int i = 1; i <= la; i = i + 1) {
|
|
172
|
+
dp_row_cur[0] = i
|
|
173
|
+
for (int j = 1; j <= lb; j = j + 1) {
|
|
174
|
+
int cost = 1
|
|
175
|
+
if (a[i - 1] == b[j - 1]) { cost = 0 }
|
|
176
|
+
int best = dp_row_prev[j - 1] + cost // 替换 / 相同
|
|
177
|
+
if (dp_row_prev[j] + 1 < best) { best = dp_row_prev[j] + 1 }
|
|
178
|
+
if (dp_row_cur[j - 1] + 1 < best) { best = dp_row_cur[j - 1] + 1 }
|
|
179
|
+
dp_row_cur[j] = best
|
|
180
|
+
}
|
|
181
|
+
for (int j = 0; j <= lb; j = j + 1) {
|
|
182
|
+
dp_row_prev[j] = dp_row_cur[j]
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return dp_row_prev[lb]
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// 最长回文子串长度 (按字节; 中心扩展, 等价于区间 DP 的 O(n^2) 空间优化写法)
|
|
189
|
+
// 空串返回 0; 长度超过 DP_STR_MAX 时只看前 DP_STR_MAX 字节
|
|
190
|
+
function dp_longest_palindrome(string s) -> int {
|
|
191
|
+
int n = strlen(s)
|
|
192
|
+
if (n > DP_STR_MAX) { n = DP_STR_MAX }
|
|
193
|
+
if (n <= 0) { return 0 }
|
|
194
|
+
int best = 1
|
|
195
|
+
for (int c = 0; c < n; c = c + 1) {
|
|
196
|
+
int l = c
|
|
197
|
+
int r = c
|
|
198
|
+
while (l >= 0 && r < n && s[l] == s[r]) {
|
|
199
|
+
int len = r - l + 1
|
|
200
|
+
if (len > best) { best = len }
|
|
201
|
+
l = l - 1
|
|
202
|
+
r = r + 1
|
|
203
|
+
}
|
|
204
|
+
l = c
|
|
205
|
+
r = c + 1
|
|
206
|
+
while (l >= 0 && r < n && s[l] == s[r]) {
|
|
207
|
+
int len = r - l + 1
|
|
208
|
+
if (len > best) { best = len }
|
|
209
|
+
l = l - 1
|
|
210
|
+
r = r + 1
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return best
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// ---------------- 零钱问题 ----------------
|
|
217
|
+
|
|
218
|
+
// 最少硬币数 (每种面额可用无限次): 凑不出返回 -1
|
|
219
|
+
// amount < 0 返回 -1; amount == 0 返回 0 (不用任何硬币);
|
|
220
|
+
// 面额 <= 0 的硬币被忽略 (避免死循环); amount > DP_AMOUNT_MAX 时按 DP_AMOUNT_MAX 算
|
|
221
|
+
function dp_coin_change(int[] coins, int n, int amount) -> int {
|
|
222
|
+
if (amount < 0) { return -1 }
|
|
223
|
+
if (amount == 0) { return 0 }
|
|
224
|
+
int amt = amount
|
|
225
|
+
if (amt > DP_AMOUNT_MAX) { amt = DP_AMOUNT_MAX }
|
|
226
|
+
dp_amt[0] = 0
|
|
227
|
+
for (int j = 1; j <= amt; j = j + 1) {
|
|
228
|
+
dp_amt[j] = -1
|
|
229
|
+
}
|
|
230
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
231
|
+
int c = coins[i]
|
|
232
|
+
if (c <= 0) { continue }
|
|
233
|
+
for (int j = c; j <= amt; j = j + 1) {
|
|
234
|
+
if (dp_amt[j - c] < 0) { continue }
|
|
235
|
+
int cand = dp_amt[j - c] + 1
|
|
236
|
+
if (dp_amt[j] < 0 || cand < dp_amt[j]) { dp_amt[j] = cand }
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return dp_amt[amt]
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// 凑出 amount 的**组合数** (硬币顺序不计, 每种面额可用无限次)
|
|
243
|
+
// amount < 0 返回 0; amount == 0 返回 1 (空组合);
|
|
244
|
+
// 面额 <= 0 的硬币被忽略; amount > DP_AMOUNT_MAX 时按 DP_AMOUNT_MAX 算;
|
|
245
|
+
// 结果按 64 位整数回绕 (可能溢出, 建议 amount 较小)
|
|
246
|
+
function dp_coin_ways(int[] coins, int n, int amount) -> int {
|
|
247
|
+
if (amount < 0) { return 0 }
|
|
248
|
+
int amt = amount
|
|
249
|
+
if (amt > DP_AMOUNT_MAX) { amt = DP_AMOUNT_MAX }
|
|
250
|
+
for (int j = 0; j <= amt; j = j + 1) {
|
|
251
|
+
dp_amt[j] = 0
|
|
252
|
+
}
|
|
253
|
+
dp_amt[0] = 1
|
|
254
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
255
|
+
int c = coins[i]
|
|
256
|
+
if (c <= 0) { continue }
|
|
257
|
+
for (int j = c; j <= amt; j = j + 1) {
|
|
258
|
+
dp_amt[j] = dp_amt[j] + dp_amt[j - c]
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return dp_amt[amt]
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// ---------------- 序列 DP ----------------
|
|
265
|
+
|
|
266
|
+
// 最长**严格**递增子序列长度 (O(n^2)); n <= 0 返回 0
|
|
267
|
+
// n > DP_LIS_MAX 时只看前 DP_LIS_MAX 个元素
|
|
268
|
+
function dp_lis(int[] a, int n) -> int {
|
|
269
|
+
if (n <= 0) { return 0 }
|
|
270
|
+
int k = n
|
|
271
|
+
if (k > DP_LIS_MAX) { k = DP_LIS_MAX }
|
|
272
|
+
int best = 0
|
|
273
|
+
for (int i = 0; i < k; i = i + 1) {
|
|
274
|
+
dp_cap[i] = 1
|
|
275
|
+
for (int j = 0; j < i; j = j + 1) {
|
|
276
|
+
if (a[j] < a[i]) {
|
|
277
|
+
if (dp_cap[j] + 1 > dp_cap[i]) { dp_cap[i] = dp_cap[j] + 1 }
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
if (dp_cap[i] > best) { best = dp_cap[i] }
|
|
281
|
+
}
|
|
282
|
+
return best
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// 最大子段和 (Kadane, O(n)): 至少取一个元素, 全负数时返回其中最大的那个
|
|
286
|
+
// n <= 0 返回 0
|
|
287
|
+
function dp_max_subarray(int[] a, int n) -> int {
|
|
288
|
+
if (n <= 0) { return 0 }
|
|
289
|
+
int best = a[0]
|
|
290
|
+
int cur = a[0]
|
|
291
|
+
for (int i = 1; i < n; i = i + 1) {
|
|
292
|
+
if (cur < 0) {
|
|
293
|
+
cur = a[i]
|
|
294
|
+
} else {
|
|
295
|
+
cur = cur + a[i]
|
|
296
|
+
}
|
|
297
|
+
if (cur > best) { best = cur }
|
|
298
|
+
}
|
|
299
|
+
return best
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// ---------------- 网格 DP ----------------
|
|
303
|
+
|
|
304
|
+
// 网格最小路径和 (只能向右或向下走), 网格按**行主序一维**给出: grid[r * cols + c]
|
|
305
|
+
// 起点与终点都计入和; rows <= 0 或 cols <= 0 返回 0;
|
|
306
|
+
// cols > DP_PATH_COLS 返回 -1 (列数参与行主序下标, 定长滚动数组放不下, 明确失败)
|
|
307
|
+
function dp_min_path(int[] grid, int rows, int cols) -> int {
|
|
308
|
+
if (rows <= 0 || cols <= 0) { return 0 }
|
|
309
|
+
if (cols > DP_PATH_COLS) { return -1 }
|
|
310
|
+
for (int c = 0; c < cols; c = c + 1) {
|
|
311
|
+
dp_cap[c] = grid[c]
|
|
312
|
+
}
|
|
313
|
+
for (int c = 1; c < cols; c = c + 1) {
|
|
314
|
+
dp_cap[c] = dp_cap[c] + dp_cap[c - 1]
|
|
315
|
+
}
|
|
316
|
+
for (int r = 1; r < rows; r = r + 1) {
|
|
317
|
+
dp_cap[0] = dp_cap[0] + grid[r * cols]
|
|
318
|
+
for (int c = 1; c < cols; c = c + 1) {
|
|
319
|
+
int m = dp_cap[c]
|
|
320
|
+
if (dp_cap[c - 1] < m) { m = dp_cap[c - 1] }
|
|
321
|
+
dp_cap[c] = m + grid[r * cols + c]
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return dp_cap[cols - 1]
|
|
325
|
+
}
|
codecin/lib/ffi/pic.toml
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
|
|
2
|
+
[package]
|
|
3
|
+
name = "ffi"
|
|
4
|
+
version = "7.22.10"
|
|
5
|
+
description = "Code CIN 官方标准库: FFI 动态库调用便捷封装"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = ["ByUsi Studio"]
|
|
8
|
+
entry = "src/ffi.cin"
|
|
9
|
+
repo = "https://gitee.com/codecin/internal-libraries"
|
|
10
|
+
keywords = ["codecin", "stdlib", "ffi"]
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// Code CIN 官方标准库: FFI 动态库调用便捷封装 (ffi)
|
|
2
|
+
// 用法: import "ffi.cin"
|
|
3
|
+
// 依赖: 原生运行时 (dlopen / dlsym / ffi_call / ffi_callf / lib_close,
|
|
4
|
+
// SYS 140-144, Go 引擎实现); `--sandbox` 下宿主调用会被拦截。
|
|
5
|
+
//
|
|
6
|
+
// 调用约定 (与原生引擎 ffi_windows.go / ffi_unix.go 一致):
|
|
7
|
+
// * 最多 8 个参数, 每个占 8 字节, 从 argbuf 顺序读取;
|
|
8
|
+
// * ffi_call(fn, argbuf, n) -> int64 整数返回值;
|
|
9
|
+
// * ffi_callf(fn, argbuf, n) -> double 返回值 (IEEE754 位模式存 X0;
|
|
10
|
+
// CIN 的 float 本身就是位模式, 结果可直接赋给 float 变量);
|
|
11
|
+
// * 浮点参数请走 ffi_callf* 封装 (float 数组槽即 IEEE754 位模式),
|
|
12
|
+
// 整数参数走 ffi_call* 封装 (int 数组); 两种参数混用暂无封装;
|
|
13
|
+
// * dlopen / dlsym 失败返回 0 (句柄 0 无效)。
|
|
14
|
+
//
|
|
15
|
+
// 示例 (Windows):
|
|
16
|
+
// import "ffi.cin"
|
|
17
|
+
// int k32 = ffi_load("kernel32.dll")
|
|
18
|
+
// int tick = ffi_find(k32, "GetTickCount64")
|
|
19
|
+
// print(ffi_call0(tick)) // 开机毫秒数
|
|
20
|
+
// ffi_free(k32)
|
|
21
|
+
// 示例 (Linux/macOS):
|
|
22
|
+
// int libc = ffi_load("libc.so.6") // macOS: "libc.dylib" 或省略后缀
|
|
23
|
+
// int getpid = ffi_find(libc, "getpid")
|
|
24
|
+
// print(ffi_call0(getpid))
|
|
25
|
+
|
|
26
|
+
// FFI 参数上限 (与引擎一致; 仅供检查, 封装内部已固定)
|
|
27
|
+
const int ffi_max_args = 8
|
|
28
|
+
|
|
29
|
+
// ---------------- 加载与查找 ----------------
|
|
30
|
+
|
|
31
|
+
// 加载动态库, 返回句柄 (失败 0)。path 为平台相关文件名:
|
|
32
|
+
// Windows "foo.dll", Linux "libfoo.so" / "libfoo.so.6", macOS "libfoo.dylib"。
|
|
33
|
+
function ffi_load(string path) -> int {
|
|
34
|
+
return dlopen(path)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 查找导出符号 (函数/变量地址), 失败 0。
|
|
38
|
+
function ffi_find(int lib, string name) -> int {
|
|
39
|
+
return dlsym(lib, name)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 卸载动态库 (0 成功 / -1)。卸载后其函数句柄全部失效。
|
|
43
|
+
function ffi_free(int lib) -> int {
|
|
44
|
+
return lib_close(lib)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ---------------- 整数调用 (0..8 个 int 参数) ----------------
|
|
48
|
+
|
|
49
|
+
function ffi_call0(int fn) -> int {
|
|
50
|
+
int buf[8]
|
|
51
|
+
return ffi_call(fn, buf, 0)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function ffi_call1(int fn, int a0) -> int {
|
|
55
|
+
int buf[8]
|
|
56
|
+
buf[0] = a0
|
|
57
|
+
return ffi_call(fn, buf, 1)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function ffi_call2(int fn, int a0, int a1) -> int {
|
|
61
|
+
int buf[8]
|
|
62
|
+
buf[0] = a0
|
|
63
|
+
buf[1] = a1
|
|
64
|
+
return ffi_call(fn, buf, 2)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function ffi_call3(int fn, int a0, int a1, int a2) -> int {
|
|
68
|
+
int buf[8]
|
|
69
|
+
buf[0] = a0
|
|
70
|
+
buf[1] = a1
|
|
71
|
+
buf[2] = a2
|
|
72
|
+
return ffi_call(fn, buf, 3)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function ffi_call4(int fn, int a0, int a1, int a2, int a3) -> int {
|
|
76
|
+
int buf[8]
|
|
77
|
+
buf[0] = a0
|
|
78
|
+
buf[1] = a1
|
|
79
|
+
buf[2] = a2
|
|
80
|
+
buf[3] = a3
|
|
81
|
+
return ffi_call(fn, buf, 4)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function ffi_call5(int fn, int a0, int a1, int a2, int a3, int a4) -> int {
|
|
85
|
+
int buf[8]
|
|
86
|
+
buf[0] = a0
|
|
87
|
+
buf[1] = a1
|
|
88
|
+
buf[2] = a2
|
|
89
|
+
buf[3] = a3
|
|
90
|
+
buf[4] = a4
|
|
91
|
+
return ffi_call(fn, buf, 5)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function ffi_call6(int fn, int a0, int a1, int a2, int a3, int a4, int a5) -> int {
|
|
95
|
+
int buf[8]
|
|
96
|
+
buf[0] = a0
|
|
97
|
+
buf[1] = a1
|
|
98
|
+
buf[2] = a2
|
|
99
|
+
buf[3] = a3
|
|
100
|
+
buf[4] = a4
|
|
101
|
+
buf[5] = a5
|
|
102
|
+
return ffi_call(fn, buf, 6)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function ffi_call7(int fn, int a0, int a1, int a2, int a3, int a4, int a5,
|
|
106
|
+
int a6) -> int {
|
|
107
|
+
int buf[8]
|
|
108
|
+
buf[0] = a0
|
|
109
|
+
buf[1] = a1
|
|
110
|
+
buf[2] = a2
|
|
111
|
+
buf[3] = a3
|
|
112
|
+
buf[4] = a4
|
|
113
|
+
buf[5] = a5
|
|
114
|
+
buf[6] = a6
|
|
115
|
+
return ffi_call(fn, buf, 7)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function ffi_call8(int fn, int a0, int a1, int a2, int a3, int a4, int a5,
|
|
119
|
+
int a6, int a7) -> int {
|
|
120
|
+
int buf[8]
|
|
121
|
+
buf[0] = a0
|
|
122
|
+
buf[1] = a1
|
|
123
|
+
buf[2] = a2
|
|
124
|
+
buf[3] = a3
|
|
125
|
+
buf[4] = a4
|
|
126
|
+
buf[5] = a5
|
|
127
|
+
buf[6] = a6
|
|
128
|
+
buf[7] = a7
|
|
129
|
+
return ffi_call(fn, buf, 8)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// ---------------- 浮点调用 (1..4 个 float 参数) ----------------
|
|
133
|
+
// 返回值是 double (CIN float 位模式, 直接参与浮点运算即可)。
|
|
134
|
+
|
|
135
|
+
function ffi_callf1(int fn, float a0) -> float {
|
|
136
|
+
float buf[8]
|
|
137
|
+
buf[0] = a0
|
|
138
|
+
return ffi_callf(fn, buf, 1)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function ffi_callf2(int fn, float a0, float a1) -> float {
|
|
142
|
+
float buf[8]
|
|
143
|
+
buf[0] = a0
|
|
144
|
+
buf[1] = a1
|
|
145
|
+
return ffi_callf(fn, buf, 2)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function ffi_callf3(int fn, float a0, float a1, float a2) -> float {
|
|
149
|
+
float buf[8]
|
|
150
|
+
buf[0] = a0
|
|
151
|
+
buf[1] = a1
|
|
152
|
+
buf[2] = a2
|
|
153
|
+
return ffi_callf(fn, buf, 3)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function ffi_callf4(int fn, float a0, float a1, float a2, float a3) -> float {
|
|
157
|
+
float buf[8]
|
|
158
|
+
buf[0] = a0
|
|
159
|
+
buf[1] = a1
|
|
160
|
+
buf[2] = a2
|
|
161
|
+
buf[3] = a3
|
|
162
|
+
return ffi_callf(fn, buf, 4)
|
|
163
|
+
}
|
codecin/lib/fmt/pic.toml
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
|
|
2
|
+
[package]
|
|
3
|
+
name = "fmt"
|
|
4
|
+
version = "7.22.10"
|
|
5
|
+
description = "Code CIN 官方标准库: 排版与格式化输出"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = ["ByUsi Studio"]
|
|
8
|
+
entry = "src/fmt.cin"
|
|
9
|
+
repo = "https://gitee.com/codecin/internal-libraries"
|
|
10
|
+
keywords = ["codecin", "stdlib", "fmt"]
|