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.
Files changed (202) hide show
  1. codecin/__init__.py +27 -0
  2. codecin/aot.py +1057 -0
  3. codecin/assembler.py +619 -0
  4. codecin/cbackend.py +3024 -0
  5. codecin/cin.py +4265 -0
  6. codecin/cli.py +670 -0
  7. codecin/codecin_native.dll +0 -0
  8. codecin/config.py +50 -0
  9. codecin/console.py +183 -0
  10. codecin/cpu.py +312 -0
  11. codecin/crom.py +449 -0
  12. codecin/cvm.py +708 -0
  13. codecin/disasm.py +149 -0
  14. codecin/environment.py +176 -0
  15. codecin/errors.py +54 -0
  16. codecin/isa.py +560 -0
  17. codecin/lib/array/pic.toml +10 -0
  18. codecin/lib/array/src/array.cin +103 -0
  19. codecin/lib/bigint/pic.toml +10 -0
  20. codecin/lib/bigint/src/bigint.cin +589 -0
  21. codecin/lib/bits/pic.toml +10 -0
  22. codecin/lib/bits/src/bits.cin +143 -0
  23. codecin/lib/bitset/pic.toml +10 -0
  24. codecin/lib/bitset/src/bitset.cin +374 -0
  25. codecin/lib/codec/pic.toml +10 -0
  26. codecin/lib/codec/src/codec.cin +563 -0
  27. codecin/lib/combin/pic.toml +10 -0
  28. codecin/lib/combin/src/combin.cin +414 -0
  29. codecin/lib/conv/pic.toml +10 -0
  30. codecin/lib/conv/src/conv.cin +151 -0
  31. codecin/lib/cppstd/pic.toml +10 -0
  32. codecin/lib/cppstd/src/cppstd.cin +418 -0
  33. codecin/lib/cstd/pic.toml +10 -0
  34. codecin/lib/cstd/src/cstd.cin +424 -0
  35. codecin/lib/csv/pic.toml +10 -0
  36. codecin/lib/csv/src/csv.cin +405 -0
  37. codecin/lib/dp/pic.toml +10 -0
  38. codecin/lib/dp/src/dp.cin +325 -0
  39. codecin/lib/ffi/pic.toml +10 -0
  40. codecin/lib/ffi/src/ffi.cin +163 -0
  41. codecin/lib/fmt/pic.toml +10 -0
  42. codecin/lib/fmt/src/fmt.cin +325 -0
  43. codecin/lib/frac/pic.toml +10 -0
  44. codecin/lib/frac/src/frac.cin +252 -0
  45. codecin/lib/gostd/pic.toml +10 -0
  46. codecin/lib/gostd/src/gostd.cin +415 -0
  47. codecin/lib/graph/pic.toml +10 -0
  48. codecin/lib/graph/src/graph.cin +367 -0
  49. codecin/lib/gui/pic.toml +10 -0
  50. codecin/lib/gui/src/gui.cin +218 -0
  51. codecin/lib/hash/pic.toml +10 -0
  52. codecin/lib/hash/src/hash.cin +67 -0
  53. codecin/lib/heap/pic.toml +10 -0
  54. codecin/lib/heap/src/heap.cin +199 -0
  55. codecin/lib/io/pic.toml +10 -0
  56. codecin/lib/io/src/io.cin +141 -0
  57. codecin/lib/json/pic.toml +10 -0
  58. codecin/lib/json/src/json.cin +84 -0
  59. codecin/lib/key/pic.toml +10 -0
  60. codecin/lib/key/src/key.cin +78 -0
  61. codecin/lib/math/pic.toml +10 -0
  62. codecin/lib/math/src/math.cin +50 -0
  63. codecin/lib/matrix/pic.toml +10 -0
  64. codecin/lib/matrix/src/matrix.cin +155 -0
  65. codecin/lib/net/pic.toml +10 -0
  66. codecin/lib/net/src/net.cin +171 -0
  67. codecin/lib/path/pic.toml +10 -0
  68. codecin/lib/path/src/path.cin +372 -0
  69. codecin/lib/queue/pic.toml +10 -0
  70. codecin/lib/queue/src/queue.cin +112 -0
  71. codecin/lib/rand/pic.toml +10 -0
  72. codecin/lib/rand/src/rand.cin +46 -0
  73. codecin/lib/set/pic.toml +10 -0
  74. codecin/lib/set/src/set.cin +269 -0
  75. codecin/lib/sort/pic.toml +10 -0
  76. codecin/lib/sort/src/sort.cin +93 -0
  77. codecin/lib/stat/pic.toml +10 -0
  78. codecin/lib/stat/src/stat.cin +138 -0
  79. codecin/lib/str/pic.toml +10 -0
  80. codecin/lib/str/src/str.cin +54 -0
  81. codecin/lib/termux/pic.toml +10 -0
  82. codecin/lib/termux/src/termux.cin +118 -0
  83. codecin/lib/test/pic.toml +10 -0
  84. codecin/lib/test/src/test.cin +74 -0
  85. codecin/lib/text/pic.toml +10 -0
  86. codecin/lib/text/src/text.cin +323 -0
  87. codecin/lib/time/pic.toml +10 -0
  88. codecin/lib/time/src/time.cin +54 -0
  89. codecin/lib/token/pic.toml +10 -0
  90. codecin/lib/token/src/token.cin +183 -0
  91. codecin/lib/tree/pic.toml +10 -0
  92. codecin/lib/tree/src/tree.cin +256 -0
  93. codecin/lib/unionfind/pic.toml +10 -0
  94. codecin/lib/unionfind/src/unionfind.cin +115 -0
  95. codecin/lib/validate/pic.toml +10 -0
  96. codecin/lib/validate/src/validate.cin +148 -0
  97. codecin/lib/vec/pic.toml +10 -0
  98. codecin/lib/vec/src/vec.cin +88 -0
  99. codecin/logger.py +145 -0
  100. codecin/memory.py +306 -0
  101. codecin/mkn.py +916 -0
  102. codecin/native/aot/aot.go +114 -0
  103. codecin/native/aot/stub_main.go.txt +23 -0
  104. codecin/native/build.ps1 +33 -0
  105. codecin/native/build.sh +48 -0
  106. codecin/native/cmd/cincc/main.go +66 -0
  107. codecin/native/compiler/codegen.go +2238 -0
  108. codecin/native/compiler/compile_test.go +1069 -0
  109. codecin/native/compiler/parser.go +1331 -0
  110. codecin/native/compiler/syscalls.go +166 -0
  111. codecin/native/compiler/tokenizer.go +394 -0
  112. codecin/native/compiler/types.go +217 -0
  113. codecin/native/engine/args.go +37 -0
  114. codecin/native/engine/audio.go +402 -0
  115. codecin/native/engine/audio_other.go +105 -0
  116. codecin/native/engine/audio_test.go +279 -0
  117. codecin/native/engine/audio_windows.go +190 -0
  118. codecin/native/engine/canvas.go +271 -0
  119. codecin/native/engine/console_other.go +9 -0
  120. codecin/native/engine/console_windows.go +43 -0
  121. codecin/native/engine/crom.go +86 -0
  122. codecin/native/engine/encode.go +93 -0
  123. codecin/native/engine/engine_test.go +198 -0
  124. codecin/native/engine/ffi_nocgo.go +20 -0
  125. codecin/native/engine/ffi_registry.go +69 -0
  126. codecin/native/engine/ffi_unix.go +109 -0
  127. codecin/native/engine/ffi_windows.go +117 -0
  128. codecin/native/engine/gui.go +209 -0
  129. codecin/native/engine/gui_stub.go +13 -0
  130. codecin/native/engine/gui_test.go +148 -0
  131. codecin/native/engine/gui_windows.go +604 -0
  132. codecin/native/engine/gui_x11.go +723 -0
  133. codecin/native/engine/host_test.go +139 -0
  134. codecin/native/engine/hostcrypto.go +30 -0
  135. codecin/native/engine/hostdesktop.go +173 -0
  136. codecin/native/engine/hostnet.go +127 -0
  137. codecin/native/engine/isa_gen.go +689 -0
  138. codecin/native/engine/keyboard.go +432 -0
  139. codecin/native/engine/keyboard_other.go +94 -0
  140. codecin/native/engine/keyboard_stub.go +17 -0
  141. codecin/native/engine/keyboard_termios_darwin.go +9 -0
  142. codecin/native/engine/keyboard_termios_linux.go +9 -0
  143. codecin/native/engine/keyboard_test.go +246 -0
  144. codecin/native/engine/keyboard_windows.go +208 -0
  145. codecin/native/engine/keyboard_windows_test.go +123 -0
  146. codecin/native/engine/lineedit.go +262 -0
  147. codecin/native/engine/lineedit_test.go +105 -0
  148. codecin/native/engine/lineint_unix.go +17 -0
  149. codecin/native/engine/lineint_windows.go +19 -0
  150. codecin/native/engine/mem_other.go +37 -0
  151. codecin/native/engine/mem_windows.go +33 -0
  152. codecin/native/engine/native.go +640 -0
  153. codecin/native/engine/netsock.go +321 -0
  154. codecin/native/engine/ops_ext.go +515 -0
  155. codecin/native/engine/stdin.go +72 -0
  156. codecin/native/engine/stdin_other.go +9 -0
  157. codecin/native/engine/stdin_windows.go +60 -0
  158. codecin/native/engine/system.go +313 -0
  159. codecin/native/engine/termux.go +180 -0
  160. codecin/native/engine/version_gen.go +7 -0
  161. codecin/native/engine/vkb_darwin.go +62 -0
  162. codecin/native/engine/vkb_linux.go +95 -0
  163. codecin/native/engine/vkb_stub.go +11 -0
  164. codecin/native/engine/vm.go +1677 -0
  165. codecin/native/go.mod +3 -0
  166. codecin/native/ir/ir.go +48 -0
  167. codecin/native/main.go +448 -0
  168. codecin/native/mc/aarch64.go +165 -0
  169. codecin/native/mc/aarch64_test.go +30 -0
  170. codecin/native/mc/elf.go +88 -0
  171. codecin/native/mc/print_test.go +52 -0
  172. codecin/native/mc/x86_64.go +1262 -0
  173. codecin/native/mc/x86_64_test.go +102 -0
  174. codecin/native.py +432 -0
  175. codecin/native_codegen.py +987 -0
  176. codecin/nativegen.py +786 -0
  177. codecin/object.py +539 -0
  178. codecin/optimizer.py +236 -0
  179. codecin/packaging/__init__.py +46 -0
  180. codecin/packaging/archive.py +177 -0
  181. codecin/packaging/model.py +519 -0
  182. codecin/packaging/spec.py +193 -0
  183. codecin/packaging/store.py +372 -0
  184. codecin/packaging/toml.py +541 -0
  185. codecin/packaging/vcs.py +215 -0
  186. codecin/pic.py +1917 -0
  187. codecin/pic_api.py +374 -0
  188. codecin/pic_login.py +519 -0
  189. codecin/pic_registry.py +515 -0
  190. codecin/registers.py +130 -0
  191. codecin/sdgcc.py +493 -0
  192. codecin/stats.py +230 -0
  193. codecin/stdlib.py +138 -0
  194. codecin/toolchain.py +396 -0
  195. codecin/toolkit.py +26 -0
  196. codecin/version.py +260 -0
  197. codecin-7.22.10.dist-info/METADATA +1077 -0
  198. codecin-7.22.10.dist-info/RECORD +202 -0
  199. codecin-7.22.10.dist-info/WHEEL +5 -0
  200. codecin-7.22.10.dist-info/entry_points.txt +5 -0
  201. codecin-7.22.10.dist-info/licenses/LICENSE +7 -0
  202. codecin-7.22.10.dist-info/top_level.txt +1 -0
@@ -0,0 +1,183 @@
1
+ // Code CIN 官方标准库: 切分与分词 (token)
2
+ // 用法: import "token.cin"
3
+ //
4
+ // CIN 没有字符串数组类型, 所以本库不返回数组, 而是提供"按序号取第 n 个 token"
5
+ // 的 API: tok_count / tok_get / tok_len / tok_find / tok_first / tok_last,
6
+ // 外加按空白切分的便捷封装 tok_word_count / tok_word_get (因为没有字符串数组,
7
+ // 本库不提供 join)。纯 CIN 实现, 只用非宿主内建 (strlen / substr / strcmp),
8
+ // 全路径可用。
9
+ //
10
+ // 切分语义 (与 Go strings.Split 一致):
11
+ // * delim 按**整串**匹配 (多字符分隔符算一个整体), 不是"字符集合";
12
+ // * **保留空 token**: 连续分隔符之间、串首/串尾分隔符旁边都产生空 token,
13
+ // 例如 tok_count("a,,b", ",") == 3、tok_count(",a,", ",") == 3;
14
+ // * 空串切分后是 1 个空 token (tok_count("", ",") == 1);
15
+ // * delim 为空串时整个 s 是唯一 token (tok_count(s, "") == 1), 越界序号返回 "";
16
+ // * tok_word_* 走另一套约定: 连续空白算一个分隔符, 忽略首尾空白, 不产生空词。
17
+ //
18
+ // 字节语义: 下标、长度都是**字节**偏移 (与 strlen / substr / s[i] 一致), 非 ASCII
19
+ // 一律按 UTF-8 字节序列处理, 不做码点解码; delim 请用完整字符, 否则可能把多字节
20
+ // 字符切开。tok_get / tok_first / tok_last / tok_find 会为返回值分配新堆块,
21
+ // tok_count / tok_len 不分配。
22
+
23
+ // ---------------- 内部辅助函数 (非公开 API, 统一 tok_ 前缀) ----------------
24
+
25
+ // delim 是否正好从 s 的 at 处开始 (逐字节比较, 不分配堆块)
26
+ function tok_match_at(string s, string delim, int at) -> int {
27
+ if (at < 0) { return 0 }
28
+ int n = strlen(s)
29
+ int dl = strlen(delim)
30
+ if (at + dl > n) { return 0 }
31
+ int i = 0
32
+ while (i < dl) {
33
+ if (s[at + i] != delim[i]) { return 0 }
34
+ i = i + 1
35
+ }
36
+ return 1
37
+ }
38
+
39
+ // 是否空白: 空格 / \t / \n / \r
40
+ function tok_is_space_byte(int c) -> int {
41
+ if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { return 1 }
42
+ return 0
43
+ }
44
+
45
+ // ---------------- 公开 API ----------------
46
+
47
+ // token 个数 (保留空 token); delim 为空串时固定为 1
48
+ function tok_count(string s, string delim) -> int {
49
+ int n = strlen(s)
50
+ int dl = strlen(delim)
51
+ if (dl == 0) { return 1 }
52
+ int count = 1
53
+ int i = 0
54
+ while (i < n) {
55
+ if (tok_match_at(s, delim, i) == 1) {
56
+ count = count + 1
57
+ i = i + dl
58
+ } else {
59
+ i = i + 1
60
+ }
61
+ }
62
+ return count
63
+ }
64
+
65
+ // 第 idx 个 token 的内容 (新堆块); idx 越界或为负返回空串。
66
+ // delim 为空串时只有 idx == 0 有效, 返回整个 s。
67
+ function tok_get(string s, string delim, int idx) -> string {
68
+ int n = strlen(s)
69
+ int dl = strlen(delim)
70
+ if (idx < 0) { return "" }
71
+ if (dl == 0) {
72
+ if (idx == 0) { return s }
73
+ return ""
74
+ }
75
+ int cur = 0
76
+ int start = 0
77
+ int i = 0
78
+ while (i < n) {
79
+ if (tok_match_at(s, delim, i) == 1) {
80
+ if (cur == idx) { return substr(s, start, i - start) }
81
+ cur = cur + 1
82
+ i = i + dl
83
+ start = i
84
+ } else {
85
+ i = i + 1
86
+ }
87
+ }
88
+ if (cur == idx) { return substr(s, start, n - start) }
89
+ return ""
90
+ }
91
+
92
+ // 第 idx 个 token 的字节长度; idx 越界或为负返回 -1 (不分配堆块)。
93
+ // delim 为空串时只有 idx == 0 有效, 返回 strlen(s)。
94
+ function tok_len(string s, string delim, int idx) -> int {
95
+ int n = strlen(s)
96
+ int dl = strlen(delim)
97
+ if (idx < 0) { return -1 }
98
+ if (dl == 0) {
99
+ if (idx == 0) { return n }
100
+ return -1
101
+ }
102
+ int cur = 0
103
+ int start = 0
104
+ int i = 0
105
+ while (i < n) {
106
+ if (tok_match_at(s, delim, i) == 1) {
107
+ if (cur == idx) { return i - start }
108
+ cur = cur + 1
109
+ i = i + dl
110
+ start = i
111
+ } else {
112
+ i = i + 1
113
+ }
114
+ }
115
+ if (cur == idx) { return n - start }
116
+ return -1
117
+ }
118
+
119
+ // 第一个内容与 t 逐字节相同 (区分大小写) 的 token 序号; 未找到 -1。
120
+ // 空 token 可以被 t = "" 命中。
121
+ function tok_find(string s, string delim, string t) -> int {
122
+ int total = tok_count(s, delim)
123
+ int k = 0
124
+ while (k < total) {
125
+ if (strcmp(tok_get(s, delim, k), t) == 0) { return k }
126
+ k = k + 1
127
+ }
128
+ return -1
129
+ }
130
+
131
+ // 第一个 token; 与 tok_get(s, delim, 0) 等价 (空串 / delim 为空串都返回整个 s)
132
+ function tok_first(string s, string delim) -> string {
133
+ return tok_get(s, delim, 0)
134
+ }
135
+
136
+ // 最后一个 token; tok_count(s, delim) 为 0 的情况不存在, 因此总有一个 token
137
+ function tok_last(string s, string delim) -> string {
138
+ return tok_get(s, delim, tok_count(s, delim) - 1)
139
+ }
140
+
141
+ // 空白分隔的单词个数; 连续空白算一个, 忽略首尾, 全空白为 0。
142
+ // 空白 = 空格 / \t / \n / \r。
143
+ function tok_word_count(string s) -> int {
144
+ int n = strlen(s)
145
+ int count = 0
146
+ int in_word = 0
147
+ int i = 0
148
+ while (i < n) {
149
+ if (tok_is_space_byte(s[i]) == 1) {
150
+ in_word = 0
151
+ } else {
152
+ if (in_word == 0) {
153
+ count = count + 1
154
+ in_word = 1
155
+ }
156
+ }
157
+ i = i + 1
158
+ }
159
+ return count
160
+ }
161
+
162
+ // 第 idx 个空白分隔单词 (新堆块); idx 越界或为负返回空串
163
+ function tok_word_get(string s, int idx) -> string {
164
+ int n = strlen(s)
165
+ if (idx < 0) { return "" }
166
+ int cur = 0
167
+ int i = 0
168
+ while (i < n) {
169
+ while (i < n) {
170
+ if (tok_is_space_byte(s[i]) == 0) { break }
171
+ i = i + 1
172
+ }
173
+ if (i >= n) { break }
174
+ int start = i
175
+ while (i < n) {
176
+ if (tok_is_space_byte(s[i]) == 1) { break }
177
+ i = i + 1
178
+ }
179
+ if (cur == idx) { return substr(s, start, i - start) }
180
+ cur = cur + 1
181
+ }
182
+ return ""
183
+ }
@@ -0,0 +1,10 @@
1
+ # 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
2
+ [package]
3
+ name = "tree"
4
+ version = "7.22.10"
5
+ description = "Code CIN 官方标准库: 定长二叉搜索树"
6
+ license = "MIT"
7
+ authors = ["ByUsi Studio"]
8
+ entry = "src/tree.cin"
9
+ repo = "https://gitee.com/codecin/internal-libraries"
10
+ keywords = ["codecin", "stdlib", "tree"]
@@ -0,0 +1,256 @@
1
+ // Code CIN 官方标准库: 定长二叉搜索树 (tree)
2
+ // 用法: import "tree.cin"
3
+ //
4
+ // 纯 CIN 实现, 不使用任何宿主能力内建, 也不做动态内存分配:
5
+ // 节点存放在库内全局数组里, 容量上限 TREE_CAP = 64。
6
+ //
7
+ // 全局数据段占用 (每槽 8 字节):
8
+ // tree_key[64] + tree_left[64] + tree_right[64] = 192 槽
9
+ // tree_aux_node[128] + tree_aux_val[128] = 256 槽
10
+ // 标量 (tree_root / tree_count) = 2 槽
11
+ // 合计 450 槽 = 3600 字节, 远小于默认 64 KiB 数据段。
12
+ //
13
+ // 语义说明:
14
+ // * 库内只有一份全局实例, 同一程序内共享; 需要多棵树请用 struct 自行封装;
15
+ // * 不支持删除节点, 因此节点一旦分配就始终有效, 已有节点恰好是下标 0..tree_size()-1;
16
+ // * 重复插入被拒绝 (返回 0), 树中不含重复键;
17
+ // * 空树: tree_min/tree_max 返回 0, tree_height 返回 0, 三个遍历返回 ""。
18
+
19
+ int tree_key[64]
20
+ int tree_left[64]
21
+ int tree_right[64]
22
+
23
+ int tree_root = -1
24
+ int tree_count = 0
25
+
26
+ // 遍历/求高用的显式栈 (避免递归)
27
+ int tree_aux_node[128]
28
+ int tree_aux_val[128]
29
+
30
+ // ---------------- 生命周期 ----------------
31
+
32
+ // 容量上限
33
+ function tree_capacity() -> int {
34
+ return 64
35
+ }
36
+
37
+ // 重置为空树 (全局数组由运行时清零, 这里只需复位索引)
38
+ function tree_reset() -> void {
39
+ tree_root = -1
40
+ tree_count = 0
41
+ }
42
+
43
+ // 清空 (等价于 tree_reset)
44
+ function tree_clear() -> void {
45
+ tree_reset()
46
+ }
47
+
48
+ // ---------------- 状态查询 ----------------
49
+
50
+ function tree_size() -> int {
51
+ return tree_count
52
+ }
53
+
54
+ // 空树返回 1
55
+ function tree_is_empty() -> int {
56
+ if (tree_count == 0) { return 1 }
57
+ return 0
58
+ }
59
+
60
+ // ---------------- 插入与查找 ----------------
61
+
62
+ // 分配一个新节点; 满时返回 -1 (内部辅助)
63
+ function tree_new_node(int k) -> int {
64
+ if (tree_count >= 64) { return -1 }
65
+ int idx = tree_count
66
+ tree_key[idx] = k
67
+ tree_left[idx] = -1
68
+ tree_right[idx] = -1
69
+ tree_count = tree_count + 1
70
+ return idx
71
+ }
72
+
73
+ // 插入; 成功返回 1, 重复键或已满返回 0
74
+ function tree_insert(int k) -> int {
75
+ if (tree_root < 0) {
76
+ int first = tree_new_node(k)
77
+ if (first < 0) { return 0 }
78
+ tree_root = first
79
+ return 1
80
+ }
81
+ int cur = tree_root
82
+ while (true) {
83
+ if (k == tree_key[cur]) { return 0 }
84
+ if (k < tree_key[cur]) {
85
+ if (tree_left[cur] < 0) {
86
+ int l = tree_new_node(k)
87
+ if (l < 0) { return 0 }
88
+ tree_left[cur] = l
89
+ return 1
90
+ }
91
+ cur = tree_left[cur]
92
+ } else {
93
+ if (tree_right[cur] < 0) {
94
+ int r = tree_new_node(k)
95
+ if (r < 0) { return 0 }
96
+ tree_right[cur] = r
97
+ return 1
98
+ }
99
+ cur = tree_right[cur]
100
+ }
101
+ }
102
+ return 0
103
+ }
104
+
105
+ // 是否包含键 k, 返回 1/0
106
+ function tree_contains(int k) -> int {
107
+ int cur = tree_root
108
+ while (cur >= 0) {
109
+ if (k == tree_key[cur]) { return 1 }
110
+ if (k < tree_key[cur]) {
111
+ cur = tree_left[cur]
112
+ } else {
113
+ cur = tree_right[cur]
114
+ }
115
+ }
116
+ return 0
117
+ }
118
+
119
+ // 最小键; 空树返回 0
120
+ function tree_min() -> int {
121
+ if (tree_root < 0) { return 0 }
122
+ int cur = tree_root
123
+ while (tree_left[cur] >= 0) {
124
+ cur = tree_left[cur]
125
+ }
126
+ return tree_key[cur]
127
+ }
128
+
129
+ // 最大键; 空树返回 0
130
+ function tree_max() -> int {
131
+ if (tree_root < 0) { return 0 }
132
+ int cur = tree_root
133
+ while (tree_right[cur] >= 0) {
134
+ cur = tree_right[cur]
135
+ }
136
+ return tree_key[cur]
137
+ }
138
+
139
+ // 高度 (按节点数计: 空树 0, 只有根 1)
140
+ function tree_height() -> int {
141
+ if (tree_root < 0) { return 0 }
142
+ int sp = 0
143
+ tree_aux_node[sp] = tree_root
144
+ tree_aux_val[sp] = 1
145
+ sp = sp + 1
146
+ int best = 0
147
+ while (sp > 0) {
148
+ sp = sp - 1
149
+ int nd = tree_aux_node[sp]
150
+ int d = tree_aux_val[sp]
151
+ if (d > best) { best = d }
152
+ if (tree_left[nd] >= 0) {
153
+ tree_aux_node[sp] = tree_left[nd]
154
+ tree_aux_val[sp] = d + 1
155
+ sp = sp + 1
156
+ }
157
+ if (tree_right[nd] >= 0) {
158
+ tree_aux_node[sp] = tree_right[nd]
159
+ tree_aux_val[sp] = d + 1
160
+ sp = sp + 1
161
+ }
162
+ }
163
+ return best
164
+ }
165
+
166
+ // 叶子节点个数 (无左子且无右子); 空树返回 0
167
+ function tree_count_leaves() -> int {
168
+ int c = 0
169
+ for (int i = 0; i < tree_count; i = i + 1) {
170
+ if (tree_left[i] < 0 && tree_right[i] < 0) { c = c + 1 }
171
+ }
172
+ return c
173
+ }
174
+
175
+ // ---------------- 遍历 (空格分隔, 空树返回 "") ----------------
176
+
177
+ // 中序: 升序序列
178
+ function tree_inorder_str() -> string {
179
+ string out = ""
180
+ int first = 1
181
+ int sp = 0
182
+ int cur = tree_root
183
+ while (sp > 0 || cur >= 0) {
184
+ while (cur >= 0) {
185
+ tree_aux_node[sp] = cur
186
+ sp = sp + 1
187
+ cur = tree_left[cur]
188
+ }
189
+ sp = sp - 1
190
+ int nd = tree_aux_node[sp]
191
+ if (first == 0) { out = out + " " }
192
+ out = out + int_to_str(tree_key[nd])
193
+ first = 0
194
+ cur = tree_right[nd]
195
+ }
196
+ return out
197
+ }
198
+
199
+ // 前序: 根-左-右
200
+ function tree_preorder_str() -> string {
201
+ string out = ""
202
+ int first = 1
203
+ if (tree_root < 0) { return out }
204
+ int sp = 0
205
+ tree_aux_node[sp] = tree_root
206
+ sp = sp + 1
207
+ while (sp > 0) {
208
+ sp = sp - 1
209
+ int nd = tree_aux_node[sp]
210
+ if (first == 0) { out = out + " " }
211
+ out = out + int_to_str(tree_key[nd])
212
+ first = 0
213
+ if (tree_right[nd] >= 0) {
214
+ tree_aux_node[sp] = tree_right[nd]
215
+ sp = sp + 1
216
+ }
217
+ if (tree_left[nd] >= 0) {
218
+ tree_aux_node[sp] = tree_left[nd]
219
+ sp = sp + 1
220
+ }
221
+ }
222
+ return out
223
+ }
224
+
225
+ // 后序: 左-右-根
226
+ function tree_postorder_str() -> string {
227
+ string out = ""
228
+ int first = 1
229
+ if (tree_root < 0) { return out }
230
+ int sp1 = 0
231
+ int sp2 = 0
232
+ tree_aux_node[sp1] = tree_root
233
+ sp1 = sp1 + 1
234
+ while (sp1 > 0) {
235
+ sp1 = sp1 - 1
236
+ int nd = tree_aux_node[sp1]
237
+ tree_aux_val[sp2] = nd
238
+ sp2 = sp2 + 1
239
+ if (tree_left[nd] >= 0) {
240
+ tree_aux_node[sp1] = tree_left[nd]
241
+ sp1 = sp1 + 1
242
+ }
243
+ if (tree_right[nd] >= 0) {
244
+ tree_aux_node[sp1] = tree_right[nd]
245
+ sp1 = sp1 + 1
246
+ }
247
+ }
248
+ while (sp2 > 0) {
249
+ sp2 = sp2 - 1
250
+ int nd = tree_aux_val[sp2]
251
+ if (first == 0) { out = out + " " }
252
+ out = out + int_to_str(tree_key[nd])
253
+ first = 0
254
+ }
255
+ return out
256
+ }
@@ -0,0 +1,10 @@
1
+ # 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
2
+ [package]
3
+ name = "unionfind"
4
+ version = "7.22.10"
5
+ description = "Code CIN 官方标准库: 并查集"
6
+ license = "MIT"
7
+ authors = ["ByUsi Studio"]
8
+ entry = "src/unionfind.cin"
9
+ repo = "https://gitee.com/codecin/internal-libraries"
10
+ keywords = ["codecin", "stdlib", "unionfind"]
@@ -0,0 +1,115 @@
1
+ // Code CIN 官方标准库: 并查集 (unionfind)
2
+ // 用法: import "unionfind.cin"
3
+ //
4
+ // 纯 CIN 实现, 不使用任何宿主能力内建, 也不做动态内存分配:
5
+ // 三个状态数组都是库内全局定长数组, 容量上限 UF_CAP = 64。
6
+ // 合并策略为按秩合并 + 路径压缩, 单次操作近似 O(alpha(n))。
7
+ //
8
+ // 全局数据段占用 (每槽 8 字节):
9
+ // uf_parent[64] + uf_rank_arr[64] + uf_sz[64] = 192 槽
10
+ // 标量 (uf_n / uf_comps) = 2 槽
11
+ // 合计 194 槽 = 1552 字节, 远小于默认 64 KiB 数据段。
12
+ //
13
+ // 语义说明:
14
+ // * 库内只有一份全局实例, 同一程序内共享; 需要多个并查集请用 struct 自行封装;
15
+ // * 元素编号为 0..uf_n-1, 越界编号 (负数或 >= uf_n) 是"非法元素";
16
+ // * uf_find / uf_rank 对非法元素返回 -1, uf_size 对非法元素返回 0,
17
+ // uf_connected / uf_union 只要有一个非法就返回 0;
18
+ // * 调用 uf_reset(n) 之前 uf_n = 0, 此时所有编号都是非法的。
19
+
20
+ int uf_parent[64]
21
+ int uf_rank_arr[64]
22
+ int uf_sz[64]
23
+
24
+ int uf_n = 0
25
+ int uf_comps = 0
26
+
27
+ // ---------------- 生命周期 ----------------
28
+
29
+ // 容量上限
30
+ function uf_capacity() -> int {
31
+ return 64
32
+ }
33
+
34
+ // 重新初始化: 元素为 0..n-1, 每个元素自成一个集合
35
+ // n <= 0 钳制为 0, n > 64 钳制为 64
36
+ function uf_reset(int n) -> void {
37
+ int k = n
38
+ if (k < 0) { k = 0 }
39
+ if (k > 64) { k = 64 }
40
+ uf_n = k
41
+ uf_comps = k
42
+ for (int i = 0; i < k; i = i + 1) {
43
+ uf_parent[i] = i
44
+ uf_rank_arr[i] = 0
45
+ uf_sz[i] = 1
46
+ }
47
+ }
48
+
49
+ // ---------------- 查询 ----------------
50
+
51
+ // 当前连通分量个数
52
+ function uf_count() -> int {
53
+ return uf_comps
54
+ }
55
+
56
+ // 元素 x 所在集合的代表元 (路径压缩); 非法元素返回 -1
57
+ function uf_find(int x) -> int {
58
+ if (x < 0 || x >= uf_n) { return -1 }
59
+ int r = x
60
+ while (uf_parent[r] != r) {
61
+ r = uf_parent[r]
62
+ }
63
+ int cur = x
64
+ while (uf_parent[cur] != r) {
65
+ int nxt = uf_parent[cur]
66
+ uf_parent[cur] = r
67
+ cur = nxt
68
+ }
69
+ return r
70
+ }
71
+
72
+ // 元素 x 所在集合的秩; 非法元素返回 -1
73
+ function uf_rank(int x) -> int {
74
+ int r = uf_find(x)
75
+ if (r < 0) { return -1 }
76
+ return uf_rank_arr[r]
77
+ }
78
+
79
+ // 元素 x 所在集合的大小; 非法元素返回 0
80
+ function uf_size(int x) -> int {
81
+ int r = uf_find(x)
82
+ if (r < 0) { return 0 }
83
+ return uf_sz[r]
84
+ }
85
+
86
+ // a 与 b 是否在同一集合; 任一非法返回 0
87
+ function uf_connected(int a, int b) -> int {
88
+ int ra = uf_find(a)
89
+ int rb = uf_find(b)
90
+ if (ra < 0 || rb < 0) { return 0 }
91
+ if (ra == rb) { return 1 }
92
+ return 0
93
+ }
94
+
95
+ // ---------------- 合并 ----------------
96
+
97
+ // 合并 a 与 b; 真正发生合并返回 1, 已同集合返回 0, 任一非法返回 0
98
+ function uf_union(int a, int b) -> int {
99
+ int ra = uf_find(a)
100
+ int rb = uf_find(b)
101
+ if (ra < 0 || rb < 0) { return 0 }
102
+ if (ra == rb) { return 0 }
103
+ if (uf_rank_arr[ra] < uf_rank_arr[rb]) {
104
+ int t = ra
105
+ ra = rb
106
+ rb = t
107
+ }
108
+ uf_parent[rb] = ra
109
+ uf_sz[ra] = uf_sz[ra] + uf_sz[rb]
110
+ if (uf_rank_arr[ra] == uf_rank_arr[rb]) {
111
+ uf_rank_arr[ra] = uf_rank_arr[ra] + 1
112
+ }
113
+ uf_comps = uf_comps - 1
114
+ return 1
115
+ }
@@ -0,0 +1,10 @@
1
+ # 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
2
+ [package]
3
+ name = "validate"
4
+ version = "7.22.10"
5
+ description = "Code CIN 官方标准库: 字符/字符串校验"
6
+ license = "MIT"
7
+ authors = ["ByUsi Studio"]
8
+ entry = "src/validate.cin"
9
+ repo = "https://gitee.com/codecin/internal-libraries"
10
+ keywords = ["codecin", "stdlib", "validate"]