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,269 @@
|
|
|
1
|
+
// Code CIN 官方标准库: 整数集合 (set)
|
|
2
|
+
// 用法: import "set.cin"
|
|
3
|
+
//
|
|
4
|
+
// 纯 CIN 实现, 不使用任何宿主能力内建, 也不做动态内存分配:
|
|
5
|
+
// 集合以位图形式存放在库内全局定长数组 set_data[6] 里
|
|
6
|
+
// (3 个槽 x 2 个 64 位字), 单个槽容纳 128 位。
|
|
7
|
+
//
|
|
8
|
+
// 元素范围 0..SET_MAX-1, 容量常量 SET_MAX = 128。
|
|
9
|
+
//
|
|
10
|
+
// 全局数据段占用 (每槽 8 字节):
|
|
11
|
+
// set_data[6] = 6 槽
|
|
12
|
+
// 合计 6 槽 = 48 字节, 远小于默认 64 KiB 数据段。
|
|
13
|
+
//
|
|
14
|
+
// 槽位模型 (所有接口的 which 只能是 0 / 1 / 2):
|
|
15
|
+
// which = 0 -> 集合 A
|
|
16
|
+
// which = 1 -> 集合 B
|
|
17
|
+
// which = 2 -> 集合 C (备用 / 结果槽)
|
|
18
|
+
// A 与 B 是两个互相独立的集合, C 给 set_*_into 存放中间结果。
|
|
19
|
+
// 调用 set_reset_all() 之前全局数据区已由运行时清零, 三个槽都是空集。
|
|
20
|
+
//
|
|
21
|
+
// 边界约定:
|
|
22
|
+
// * which 非法 (不在 0..2): 数值查询 (set_size / set_min / set_max_value /
|
|
23
|
+
// set_count_range / set_is_empty) 返回 -1; 布尔查询 (set_contains /
|
|
24
|
+
// set_is_subset / set_equals / set_is_full) 返回 0; 写入类
|
|
25
|
+
// (set_add / set_remove / set_reset / set_clear / set_fill /
|
|
26
|
+
// set_copy_into / set_union_into / set_intersect_into / set_diff_into)
|
|
27
|
+
// 返回 0, 且绝不写内存;
|
|
28
|
+
// * 元素 v 非法 (v < 0 或 v >= 128): set_add / set_remove / set_contains
|
|
29
|
+
// 返回 0, 不写内存;
|
|
30
|
+
// * 空集: set_size 为 0, set_is_empty 为 1, set_is_full 为 0,
|
|
31
|
+
// set_min / set_max_value 为 -1, set_count_range 为 0,
|
|
32
|
+
// set_to_str 为 "{}";
|
|
33
|
+
// * 非法 which 的 set_to_str 返回空串 "";
|
|
34
|
+
// * set_clear(which) 与 set_reset(which) 完全等价 (清空单个槽)。
|
|
35
|
+
|
|
36
|
+
int set_data[6]
|
|
37
|
+
|
|
38
|
+
// ---------------- 常量与内部辅助 ----------------
|
|
39
|
+
|
|
40
|
+
// 最大合法元素值 (SET_MAX - 1)
|
|
41
|
+
function set_max() -> int {
|
|
42
|
+
return 127
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// 容量上限 SET_MAX (元素个数上限)
|
|
46
|
+
function set_capacity() -> int {
|
|
47
|
+
return 128
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// 可用槽数 (which 的合法取值个数)
|
|
51
|
+
function set_slots() -> int {
|
|
52
|
+
return 3
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 内部辅助: which 是否合法 (0..2)
|
|
56
|
+
function set_valid(int which) -> int {
|
|
57
|
+
if (which < 0 || which > 2) { return 0 }
|
|
58
|
+
return 1
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// 内部辅助: 一个 64 位字的 popcount (含符号位)
|
|
62
|
+
function set_popcount64(int x) -> int {
|
|
63
|
+
int n = 0
|
|
64
|
+
int v = x
|
|
65
|
+
while (v != 0) {
|
|
66
|
+
n = n + (v & 1)
|
|
67
|
+
v = (v >> 1) & 0x7FFFFFFFFFFFFFFF
|
|
68
|
+
}
|
|
69
|
+
return n
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 内部辅助: 读取 which 的第 v 位; 调用前必须保证 which 合法且 0 <= v < 128
|
|
73
|
+
function set_raw_test(int which, int v) -> int {
|
|
74
|
+
return (set_data[which * 2 + idiv(v, 64)] >> (v % 64)) & 1
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// ---------------- 生命周期 ----------------
|
|
78
|
+
|
|
79
|
+
// 清空槽 which; 1 成功, 0 表示 which 非法 (无操作)
|
|
80
|
+
function set_reset(int which) -> int {
|
|
81
|
+
if (set_valid(which) == 0) { return 0 }
|
|
82
|
+
set_data[which * 2] = 0
|
|
83
|
+
set_data[which * 2 + 1] = 0
|
|
84
|
+
return 1
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// 清空全部 3 个槽
|
|
88
|
+
function set_reset_all() -> void {
|
|
89
|
+
for (int w = 0; w < 6; w = w + 1) {
|
|
90
|
+
set_data[w] = 0
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// set_reset 的别名 (按"清空集合"语义书写); 1 成功, 0 表示 which 非法
|
|
95
|
+
function set_clear(int which) -> int {
|
|
96
|
+
return set_reset(which)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// 把槽 which 置满 (全部 128 个元素); 1 成功, 0 表示 which 非法
|
|
100
|
+
function set_fill(int which) -> int {
|
|
101
|
+
if (set_valid(which) == 0) { return 0 }
|
|
102
|
+
set_data[which * 2] = 0xFFFFFFFFFFFFFFFF
|
|
103
|
+
set_data[which * 2 + 1] = 0xFFFFFFFFFFFFFFFF
|
|
104
|
+
return 1
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// 把 src 槽整体复制到 dst 槽 (允许 dst == src); 1 成功, 0 表示有非法槽
|
|
108
|
+
function set_copy_into(int dst, int src) -> int {
|
|
109
|
+
if (set_valid(dst) == 0 || set_valid(src) == 0) { return 0 }
|
|
110
|
+
for (int w = 0; w < 2; w = w + 1) {
|
|
111
|
+
set_data[dst * 2 + w] = set_data[src * 2 + w]
|
|
112
|
+
}
|
|
113
|
+
return 1
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ---------------- 增删查 ----------------
|
|
117
|
+
|
|
118
|
+
// 加入元素 v; 成功 1; v 越界 / which 非法 / 已存在 均返回 0
|
|
119
|
+
function set_add(int which, int v) -> int {
|
|
120
|
+
if (set_valid(which) == 0) { return 0 }
|
|
121
|
+
if (v < 0 || v > 127) { return 0 }
|
|
122
|
+
int idx = which * 2 + idiv(v, 64)
|
|
123
|
+
int bit = 1 << (v % 64)
|
|
124
|
+
if ((set_data[idx] & bit) != 0) { return 0 }
|
|
125
|
+
set_data[idx] = set_data[idx] | bit
|
|
126
|
+
return 1
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// 删除元素 v; 真正删掉返回 1; v 越界 / which 非法 / 本来不存在 返回 0
|
|
130
|
+
function set_remove(int which, int v) -> int {
|
|
131
|
+
if (set_valid(which) == 0) { return 0 }
|
|
132
|
+
if (v < 0 || v > 127) { return 0 }
|
|
133
|
+
int idx = which * 2 + idiv(v, 64)
|
|
134
|
+
int bit = 1 << (v % 64)
|
|
135
|
+
if ((set_data[idx] & bit) == 0) { return 0 }
|
|
136
|
+
set_data[idx] = set_data[idx] ^ bit
|
|
137
|
+
return 1
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// 是否包含元素 v; 1/0; which 非法或 v 越界返回 0
|
|
141
|
+
function set_contains(int which, int v) -> int {
|
|
142
|
+
if (set_valid(which) == 0) { return 0 }
|
|
143
|
+
if (v < 0 || v > 127) { return 0 }
|
|
144
|
+
return set_raw_test(which, v)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// ---------------- 统计 ----------------
|
|
148
|
+
|
|
149
|
+
// 元素个数; which 非法返回 -1
|
|
150
|
+
function set_size(int which) -> int {
|
|
151
|
+
if (set_valid(which) == 0) { return -1 }
|
|
152
|
+
return set_popcount64(set_data[which * 2]) + set_popcount64(set_data[which * 2 + 1])
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// 空集返回 1, 非空返回 0; which 非法返回 -1
|
|
156
|
+
function set_is_empty(int which) -> int {
|
|
157
|
+
if (set_valid(which) == 0) { return -1 }
|
|
158
|
+
if (set_data[which * 2] == 0 && set_data[which * 2 + 1] == 0) { return 1 }
|
|
159
|
+
return 0
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// 满集 (128 个元素全在) 返回 1, 否则 0; which 非法返回 0
|
|
163
|
+
function set_is_full(int which) -> int {
|
|
164
|
+
if (set_valid(which) == 0) { return 0 }
|
|
165
|
+
if (set_data[which * 2] == 0xFFFFFFFFFFFFFFFF && set_data[which * 2 + 1] == 0xFFFFFFFFFFFFFFFF) { return 1 }
|
|
166
|
+
return 0
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// 最小元素; 空集或 which 非法返回 -1
|
|
170
|
+
function set_min(int which) -> int {
|
|
171
|
+
if (set_valid(which) == 0) { return -1 }
|
|
172
|
+
for (int i = 0; i < 128; i = i + 1) {
|
|
173
|
+
if (set_raw_test(which, i) == 1) { return i }
|
|
174
|
+
}
|
|
175
|
+
return -1
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// 最大元素; 空集或 which 非法返回 -1
|
|
179
|
+
function set_max_value(int which) -> int {
|
|
180
|
+
if (set_valid(which) == 0) { return -1 }
|
|
181
|
+
int i = 127
|
|
182
|
+
while (i >= 0) {
|
|
183
|
+
if (set_raw_test(which, i) == 1) { return i }
|
|
184
|
+
i = i - 1
|
|
185
|
+
}
|
|
186
|
+
return -1
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// 落在闭区间 [lo, hi] 内的元素个数;
|
|
190
|
+
// lo / hi 自动钳制到 [0, 127]; 钳制后 hi < lo 返回 0; which 非法返回 -1
|
|
191
|
+
function set_count_range(int which, int lo, int hi) -> int {
|
|
192
|
+
if (set_valid(which) == 0) { return -1 }
|
|
193
|
+
int a = lo
|
|
194
|
+
int b = hi
|
|
195
|
+
if (a < 0) { a = 0 }
|
|
196
|
+
if (b > 127) { b = 127 }
|
|
197
|
+
if (b < a) { return 0 }
|
|
198
|
+
int n = 0
|
|
199
|
+
for (int i = a; i <= b; i = i + 1) {
|
|
200
|
+
if (set_raw_test(which, i) == 1) { n = n + 1 }
|
|
201
|
+
}
|
|
202
|
+
return n
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// ---------------- 集合运算 ----------------
|
|
206
|
+
|
|
207
|
+
// dst = a | b; 1 成功, 0 表示有非法槽 (dst 与 a / b 可以是同一个槽)
|
|
208
|
+
function set_union_into(int dst, int a, int b) -> int {
|
|
209
|
+
if (set_valid(dst) == 0 || set_valid(a) == 0 || set_valid(b) == 0) { return 0 }
|
|
210
|
+
for (int w = 0; w < 2; w = w + 1) {
|
|
211
|
+
set_data[dst * 2 + w] = set_data[a * 2 + w] | set_data[b * 2 + w]
|
|
212
|
+
}
|
|
213
|
+
return 1
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// dst = a & b; 1 成功, 0 表示有非法槽
|
|
217
|
+
function set_intersect_into(int dst, int a, int b) -> int {
|
|
218
|
+
if (set_valid(dst) == 0 || set_valid(a) == 0 || set_valid(b) == 0) { return 0 }
|
|
219
|
+
for (int w = 0; w < 2; w = w + 1) {
|
|
220
|
+
set_data[dst * 2 + w] = set_data[a * 2 + w] & set_data[b * 2 + w]
|
|
221
|
+
}
|
|
222
|
+
return 1
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// dst = a - b (差集); 1 成功, 0 表示有非法槽
|
|
226
|
+
function set_diff_into(int dst, int a, int b) -> int {
|
|
227
|
+
if (set_valid(dst) == 0 || set_valid(a) == 0 || set_valid(b) == 0) { return 0 }
|
|
228
|
+
for (int w = 0; w < 2; w = w + 1) {
|
|
229
|
+
int m = set_data[b * 2 + w] ^ 0xFFFFFFFFFFFFFFFF
|
|
230
|
+
set_data[dst * 2 + w] = set_data[a * 2 + w] & m
|
|
231
|
+
}
|
|
232
|
+
return 1
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// a 是否为 b 的子集 (空集是任何集合的子集); 1/0; 有非法槽返回 0
|
|
236
|
+
function set_is_subset(int a, int b) -> int {
|
|
237
|
+
if (set_valid(a) == 0 || set_valid(b) == 0) { return 0 }
|
|
238
|
+
for (int w = 0; w < 2; w = w + 1) {
|
|
239
|
+
int extra = set_data[a * 2 + w] & (set_data[b * 2 + w] ^ 0xFFFFFFFFFFFFFFFF)
|
|
240
|
+
if (extra != 0) { return 0 }
|
|
241
|
+
}
|
|
242
|
+
return 1
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// a 与 b 是否完全相同; 1/0; 有非法槽返回 0
|
|
246
|
+
function set_equals(int a, int b) -> int {
|
|
247
|
+
if (set_valid(a) == 0 || set_valid(b) == 0) { return 0 }
|
|
248
|
+
for (int w = 0; w < 2; w = w + 1) {
|
|
249
|
+
if (set_data[a * 2 + w] != set_data[b * 2 + w]) { return 0 }
|
|
250
|
+
}
|
|
251
|
+
return 1
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// ---------------- 输出 ----------------
|
|
255
|
+
|
|
256
|
+
// 集合的字符串形式, 形如 "{1, 3, 5}"; 空集为 "{}"; which 非法返回 ""
|
|
257
|
+
function set_to_str(int which) -> string {
|
|
258
|
+
if (set_valid(which) == 0) { return "" }
|
|
259
|
+
string out = "{"
|
|
260
|
+
int n = 0
|
|
261
|
+
for (int v = 0; v < 128; v = v + 1) {
|
|
262
|
+
if (set_raw_test(which, v) == 1) {
|
|
263
|
+
if (n > 0) { out = out + ", " }
|
|
264
|
+
out = out + int_to_str(v)
|
|
265
|
+
n = n + 1
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return out + "}"
|
|
269
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
|
|
2
|
+
[package]
|
|
3
|
+
name = "sort"
|
|
4
|
+
version = "7.22.10"
|
|
5
|
+
description = "Code CIN 官方标准库: 排序与查找"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = ["ByUsi Studio"]
|
|
8
|
+
entry = "src/sort.cin"
|
|
9
|
+
repo = "https://gitee.com/codecin/internal-libraries"
|
|
10
|
+
keywords = ["codecin", "stdlib", "sort"]
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Code CIN 官方标准库: 排序与查找 (sort)
|
|
2
|
+
// 用法: import "sort.cin"
|
|
3
|
+
|
|
4
|
+
// 是否已升序有序
|
|
5
|
+
function sort_is_sorted(int[] a, int n) -> int {
|
|
6
|
+
for (int i = 1; i < n; i = i + 1) {
|
|
7
|
+
if (a[i - 1] > a[i]) { return 0 }
|
|
8
|
+
}
|
|
9
|
+
return 1
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// 冒泡排序 (原地, 升序)
|
|
13
|
+
function sort_bubble(int[] a, int n) -> void {
|
|
14
|
+
for (int i = 0; i < n - 1; i = i + 1) {
|
|
15
|
+
for (int j = 0; j < n - 1 - i; j = j + 1) {
|
|
16
|
+
if (a[j] > a[j + 1]) {
|
|
17
|
+
int t = a[j]
|
|
18
|
+
a[j] = a[j + 1]
|
|
19
|
+
a[j + 1] = t
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// 选择排序 (原地, 升序)
|
|
26
|
+
function sort_selection(int[] a, int n) -> void {
|
|
27
|
+
for (int i = 0; i < n - 1; i = i + 1) {
|
|
28
|
+
int k = i
|
|
29
|
+
for (int j = i + 1; j < n; j = j + 1) {
|
|
30
|
+
if (a[j] < a[k]) { k = j }
|
|
31
|
+
}
|
|
32
|
+
if (k != i) {
|
|
33
|
+
int t = a[i]
|
|
34
|
+
a[i] = a[k]
|
|
35
|
+
a[k] = t
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// 插入排序 (原地, 升序)
|
|
41
|
+
function sort_insertion(int[] a, int n) -> void {
|
|
42
|
+
for (int i = 1; i < n; i = i + 1) {
|
|
43
|
+
int key = a[i]
|
|
44
|
+
int j = i - 1
|
|
45
|
+
while (j >= 0 && a[j] > key) {
|
|
46
|
+
a[j + 1] = a[j]
|
|
47
|
+
j = j - 1
|
|
48
|
+
}
|
|
49
|
+
a[j + 1] = key
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 快速排序区间 [lo, hi] (原地, 升序)
|
|
54
|
+
function sort_quick(int[] a, int lo, int hi) -> void {
|
|
55
|
+
if (lo >= hi) { return }
|
|
56
|
+
int pivot = a[idiv(lo + hi, 2)]
|
|
57
|
+
int i = lo
|
|
58
|
+
int j = hi
|
|
59
|
+
while (i <= j) {
|
|
60
|
+
while (a[i] < pivot) { i = i + 1 }
|
|
61
|
+
while (a[j] > pivot) { j = j - 1 }
|
|
62
|
+
if (i <= j) {
|
|
63
|
+
int t = a[i]
|
|
64
|
+
a[i] = a[j]
|
|
65
|
+
a[j] = t
|
|
66
|
+
i = i + 1
|
|
67
|
+
j = j - 1
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (lo < j) { sort_quick(a, lo, j) }
|
|
71
|
+
if (i < hi) { sort_quick(a, i, hi) }
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// 快速排序整体入口
|
|
75
|
+
function sort_quick_all(int[] a, int n) -> void {
|
|
76
|
+
if (n > 1) { sort_quick(a, 0, n - 1) }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// 二分查找 (数组需已升序), 返回下标或 -1
|
|
80
|
+
function bin_search(int[] a, int n, int v) -> int {
|
|
81
|
+
int lo = 0
|
|
82
|
+
int hi = n - 1
|
|
83
|
+
while (lo <= hi) {
|
|
84
|
+
int mid = idiv(lo + hi, 2)
|
|
85
|
+
if (a[mid] == v) { return mid }
|
|
86
|
+
if (a[mid] < v) {
|
|
87
|
+
lo = mid + 1
|
|
88
|
+
} else {
|
|
89
|
+
hi = mid - 1
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return -1
|
|
93
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
|
|
2
|
+
[package]
|
|
3
|
+
name = "stat"
|
|
4
|
+
version = "7.22.10"
|
|
5
|
+
description = "Code CIN 官方标准库: 顺序统计"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = ["ByUsi Studio"]
|
|
8
|
+
entry = "src/stat.cin"
|
|
9
|
+
repo = "https://gitee.com/codecin/internal-libraries"
|
|
10
|
+
keywords = ["codecin", "stdlib", "stat"]
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// Code CIN 官方标准库: 顺序统计 (stat)
|
|
2
|
+
// 用法: import "stat.cin"
|
|
3
|
+
//
|
|
4
|
+
// 与 vec.cin (浮点统计) 互补: 本库面向 int 数组的**顺序统计量**
|
|
5
|
+
// (中位数/众数/百分位/直方图) 与免排序的聚合量。
|
|
6
|
+
// 需要升序输入的接口以 _sorted 结尾。
|
|
7
|
+
|
|
8
|
+
// 求和
|
|
9
|
+
function stat_sum(int[] a, int n) -> int {
|
|
10
|
+
int s = 0
|
|
11
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
12
|
+
s = s + a[i]
|
|
13
|
+
}
|
|
14
|
+
return s
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// 最小值 (n <= 0 返回 0)
|
|
18
|
+
function stat_min(int[] a, int n) -> int {
|
|
19
|
+
if (n <= 0) { return 0 }
|
|
20
|
+
int m = a[0]
|
|
21
|
+
for (int i = 1; i < n; i = i + 1) {
|
|
22
|
+
if (a[i] < m) { m = a[i] }
|
|
23
|
+
}
|
|
24
|
+
return m
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// 最大值 (n <= 0 返回 0)
|
|
28
|
+
function stat_max(int[] a, int n) -> int {
|
|
29
|
+
if (n <= 0) { return 0 }
|
|
30
|
+
int m = a[0]
|
|
31
|
+
for (int i = 1; i < n; i = i + 1) {
|
|
32
|
+
if (a[i] > m) { m = a[i] }
|
|
33
|
+
}
|
|
34
|
+
return m
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 极差 (max - min)
|
|
38
|
+
function stat_range(int[] a, int n) -> int {
|
|
39
|
+
if (n <= 0) { return 0 }
|
|
40
|
+
return stat_max(a, n) - stat_min(a, n)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// 整数均值 (向零截断)
|
|
44
|
+
function stat_mean(int[] a, int n) -> int {
|
|
45
|
+
if (n <= 0) { return 0 }
|
|
46
|
+
return idiv(stat_sum(a, n), n)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 计数等于 v 的元素个数
|
|
50
|
+
function stat_count(int[] a, int n, int v) -> int {
|
|
51
|
+
int c = 0
|
|
52
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
53
|
+
if (a[i] == v) { c = c + 1 }
|
|
54
|
+
}
|
|
55
|
+
return c
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 中位数 (要求 a 已升序); 偶数个取中间两个的截断平均
|
|
59
|
+
function stat_median_sorted(int[] a, int n) -> int {
|
|
60
|
+
if (n <= 0) { return 0 }
|
|
61
|
+
if (n % 2 == 1) { return a[idiv(n, 2)] }
|
|
62
|
+
return idiv(a[idiv(n, 2) - 1] + a[idiv(n, 2)], 2)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// 众数 (出现次数最多; 并列时取较小值); O(n^2), 无需排序
|
|
66
|
+
function stat_mode(int[] a, int n) -> int {
|
|
67
|
+
if (n <= 0) { return 0 }
|
|
68
|
+
int best = a[0]
|
|
69
|
+
int best_cnt = 0
|
|
70
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
71
|
+
int c = stat_count(a, n, a[i])
|
|
72
|
+
if (c > best_cnt) {
|
|
73
|
+
best_cnt = c
|
|
74
|
+
best = a[i]
|
|
75
|
+
} else {
|
|
76
|
+
if (c == best_cnt && a[i] < best) { best = a[i] }
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return best
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 最近秩百分位 (要求 a 已升序, p 属于 0..100)
|
|
83
|
+
function stat_percentile_sorted(int[] a, int n, int p) -> int {
|
|
84
|
+
if (n <= 0) { return 0 }
|
|
85
|
+
if (p <= 0) { return a[0] }
|
|
86
|
+
if (p >= 100) { return a[n - 1] }
|
|
87
|
+
int rank = idiv(n * p + 99, 100) // ceil(n*p/100), 至少为 1
|
|
88
|
+
int idx = rank - 1
|
|
89
|
+
if (idx < 0) { idx = 0 }
|
|
90
|
+
if (idx > n - 1) { idx = n - 1 }
|
|
91
|
+
return a[idx]
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// 下四分位 / 上四分位 (要求已升序)
|
|
95
|
+
function stat_q1_sorted(int[] a, int n) -> int {
|
|
96
|
+
return stat_percentile_sorted(a, n, 25)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function stat_q3_sorted(int[] a, int n) -> int {
|
|
100
|
+
return stat_percentile_sorted(a, n, 75)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// 直方图: 把落在 [0, bins-1] 的值计入 hist, 越界值忽略
|
|
104
|
+
function stat_histogram(int[] a, int n, int[] hist, int bins) -> void {
|
|
105
|
+
for (int i = 0; i < bins; i = i + 1) {
|
|
106
|
+
hist[i] = 0
|
|
107
|
+
}
|
|
108
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
109
|
+
int v = a[i]
|
|
110
|
+
if (v >= 0 && v < bins) { hist[v] = hist[v] + 1 }
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// 方差 ×1000 (整数化, 便于比较/断言)
|
|
115
|
+
// var = (n*sum(x^2) - sum(x)^2) / n^2
|
|
116
|
+
function stat_variance_x1000(int[] a, int n) -> int {
|
|
117
|
+
if (n <= 0) { return 0 }
|
|
118
|
+
int s = 0
|
|
119
|
+
int sq = 0
|
|
120
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
121
|
+
s = s + a[i]
|
|
122
|
+
sq = sq + a[i] * a[i]
|
|
123
|
+
}
|
|
124
|
+
return idiv((n * sq - s * s) * 1000, n * n)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// 标准差 ×100 (整数化)
|
|
128
|
+
function stat_stdev_x100(int[] a, int n) -> int {
|
|
129
|
+
return stat_variance_x1000(a, n) / 10
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// 升序判定
|
|
133
|
+
function stat_is_sorted(int[] a, int n) -> int {
|
|
134
|
+
for (int i = 1; i < n; i = i + 1) {
|
|
135
|
+
if (a[i - 1] > a[i]) { return 0 }
|
|
136
|
+
}
|
|
137
|
+
return 1
|
|
138
|
+
}
|
codecin/lib/str/pic.toml
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
|
|
2
|
+
[package]
|
|
3
|
+
name = "str"
|
|
4
|
+
version = "7.22.10"
|
|
5
|
+
description = "Code CIN 纯 CIN 字符串扩展"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = ["ByUsi Studio"]
|
|
8
|
+
entry = "src/str.cin"
|
|
9
|
+
repo = "https://gitee.com/codecin/internal-libraries"
|
|
10
|
+
keywords = ["codecin", "stdlib", "str"]
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Code CIN 纯 CIN 字符串扩展 (codecin/lib/str.cin)
|
|
2
|
+
// 用法: import "str.cin"
|
|
3
|
+
// 依赖内建: upper / lower / substr / indexof / strlen / strcmp
|
|
4
|
+
// 注意: 这些函数分配新堆块; 大量循环拼接会消耗堆 (64 位槽, 不回收)。
|
|
5
|
+
|
|
6
|
+
function s_upper(string s) -> string {
|
|
7
|
+
return upper(s)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function s_lower(string s) -> string {
|
|
11
|
+
return lower(s)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function s_contains(string hay, string needle) -> int {
|
|
15
|
+
if (indexof(hay, needle) >= 0) { return 1 }
|
|
16
|
+
return 0
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function s_starts_with(string s, string prefix) -> int {
|
|
20
|
+
if (indexof(s, prefix) == 0) { return 1 }
|
|
21
|
+
return 0
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function s_ends_with(string s, string suffix) -> int {
|
|
25
|
+
int sl = strlen(s)
|
|
26
|
+
int fl = strlen(suffix)
|
|
27
|
+
if (fl > sl) { return 0 }
|
|
28
|
+
string tail = substr(s, sl - fl, fl)
|
|
29
|
+
if (strcmp(tail, suffix) == 0) { return 1 }
|
|
30
|
+
return 0
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function s_count(string hay, string needle) -> int {
|
|
34
|
+
if (strlen(needle) == 0) { return 0 }
|
|
35
|
+
int total = 0
|
|
36
|
+
int pos = 0
|
|
37
|
+
while (true) {
|
|
38
|
+
string rest = substr(hay, pos, 9999)
|
|
39
|
+
int at = indexof(rest, needle)
|
|
40
|
+
if (at < 0) { break }
|
|
41
|
+
total = total + 1
|
|
42
|
+
pos = pos + at + strlen(needle)
|
|
43
|
+
}
|
|
44
|
+
return total
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function s_repeat(string ch, int n) -> string {
|
|
48
|
+
string out = ""
|
|
49
|
+
while (n > 0) {
|
|
50
|
+
out = out + ch
|
|
51
|
+
n = n - 1
|
|
52
|
+
}
|
|
53
|
+
return out
|
|
54
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
|
|
2
|
+
[package]
|
|
3
|
+
name = "termux"
|
|
4
|
+
version = "7.22.10"
|
|
5
|
+
description = "Code CIN 官方标准库: Termux API 便捷封装"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = ["ByUsi Studio"]
|
|
8
|
+
entry = "src/termux.cin"
|
|
9
|
+
repo = "https://gitee.com/codecin/internal-libraries"
|
|
10
|
+
keywords = ["codecin", "stdlib", "termux"]
|