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,367 @@
|
|
|
1
|
+
// Code CIN 官方标准库: 定长图 (graph)
|
|
2
|
+
// 用法: import "graph.cin"
|
|
3
|
+
//
|
|
4
|
+
// 纯 CIN 实现, 不使用任何宿主能力内建, 也不做动态内存分配:
|
|
5
|
+
// 图存放在库内全局**定长邻接矩阵**里 (行主序一维数组), 节点容量上限
|
|
6
|
+
// GRAPH_MAX = 16, 边权是 int。
|
|
7
|
+
//
|
|
8
|
+
// 容量上限:
|
|
9
|
+
// GRAPH_MAX = 16 个节点 (顶点编号 0..graph_nodes()-1)
|
|
10
|
+
// 邻接矩阵 16 x 16 = 256 个格子 (边与权重各一份)
|
|
11
|
+
//
|
|
12
|
+
// 全局数据段占用 (每槽 8 字节):
|
|
13
|
+
// graph_edge[256] + graph_w[256] = 512 槽
|
|
14
|
+
// graph_seen[16] + graph_queue[16] + graph_stack[16] = 48 槽
|
|
15
|
+
// graph_indeg[16] = 16 槽
|
|
16
|
+
// 标量 (graph_n / graph_dir / graph_cnt) = 3 槽
|
|
17
|
+
// 合计 579 槽 = 4632 字节, 远小于默认 64 KiB 数据段。
|
|
18
|
+
//
|
|
19
|
+
// 语义说明:
|
|
20
|
+
// * 库内只有一份全局实例, 同一程序内共享; 需要多张图请用 struct 自行封装;
|
|
21
|
+
// * 节点编号越界 (负数或 >= graph_nodes()) 一律返回明确失败值, 不会静默写内存:
|
|
22
|
+
// graph_has_edge / graph_weight 返回 0, graph_degree / graph_in_degree 返回 -1,
|
|
23
|
+
// graph_add_edge / graph_topo_order 走失败分支 (0 / -1), graph_bfs / graph_dfs /
|
|
24
|
+
// graph_dijkstra 返回 0 且不写输出数组;
|
|
25
|
+
// * 边是否存在单独记录在 graph_edge 里, 因此**权重可以是 0 或负数**:
|
|
26
|
+
// 判断边是否存在一律用 graph_has_edge, 不要用 graph_weight != 0;
|
|
27
|
+
// * graph_reset(n, directed) 的 n <= 0 钳制为 0, n > GRAPH_MAX 钳制为 GRAPH_MAX;
|
|
28
|
+
// * 无向图里 (u, v) 与 (v, u) 是同一条边 (加边时两侧同时写入), 有向图按 u -> v;
|
|
29
|
+
// * 自环 (u == v) 允许存在: 无向图里只算一条边, 也只贡献 1 度;
|
|
30
|
+
// * 重复加同一条边返回 0 (邻接矩阵不支持平行边);
|
|
31
|
+
// * 所有遍历/最短路都以**节点编号升序**扫描邻居, 因此结果完全确定;
|
|
32
|
+
// * graph_dijkstra 要求边权 >= 0; 出现负权时"松弛结果 < 0"的边会被忽略
|
|
33
|
+
// (不报错, 此时最短距离没有保证)。
|
|
34
|
+
|
|
35
|
+
const int GRAPH_MAX = 16
|
|
36
|
+
|
|
37
|
+
int graph_edge[GRAPH_MAX * GRAPH_MAX]
|
|
38
|
+
int graph_w[GRAPH_MAX * GRAPH_MAX]
|
|
39
|
+
|
|
40
|
+
// 遍历用的内部工作区 (BFS 队列 / DFS 栈 / 访问标记 / 入度)
|
|
41
|
+
int graph_seen[GRAPH_MAX]
|
|
42
|
+
int graph_queue[GRAPH_MAX]
|
|
43
|
+
int graph_stack[GRAPH_MAX]
|
|
44
|
+
int graph_indeg[GRAPH_MAX]
|
|
45
|
+
|
|
46
|
+
int graph_n = 0
|
|
47
|
+
int graph_dir = 0
|
|
48
|
+
int graph_cnt = 0
|
|
49
|
+
|
|
50
|
+
// ---------------- 状态查询 ----------------
|
|
51
|
+
|
|
52
|
+
// 容量上限 (节点数), 固定返回 GRAPH_MAX = 16
|
|
53
|
+
function graph_max() -> int {
|
|
54
|
+
return GRAPH_MAX
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// 当前节点数 (graph_reset 设定的值, 已经过钳制)
|
|
58
|
+
function graph_nodes() -> int {
|
|
59
|
+
return graph_n
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// 是否为有向图: 1 有向, 0 无向
|
|
63
|
+
function graph_is_directed() -> int {
|
|
64
|
+
return graph_dir
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// 当前边数; 无向图里 (u, v) 与 (v, u) 只算一条
|
|
68
|
+
function graph_edge_count() -> int {
|
|
69
|
+
return graph_cnt
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// ---------------- 生命周期 ----------------
|
|
73
|
+
|
|
74
|
+
// 重新设定节点数与方向, 并把所有边清空
|
|
75
|
+
// n <= 0 钳制为 0; n > GRAPH_MAX 钳制为 GRAPH_MAX; directed 非零表示有向图
|
|
76
|
+
function graph_reset(int n, int directed) -> void {
|
|
77
|
+
int k = n
|
|
78
|
+
if (k < 0) { k = 0 }
|
|
79
|
+
if (k > GRAPH_MAX) { k = GRAPH_MAX }
|
|
80
|
+
graph_n = k
|
|
81
|
+
if (directed) {
|
|
82
|
+
graph_dir = 1
|
|
83
|
+
} else {
|
|
84
|
+
graph_dir = 0
|
|
85
|
+
}
|
|
86
|
+
graph_clear()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 删除所有边, **保留**节点数与有向标志
|
|
90
|
+
function graph_clear() -> void {
|
|
91
|
+
for (int i = 0; i < GRAPH_MAX * GRAPH_MAX; i = i + 1) {
|
|
92
|
+
graph_edge[i] = 0
|
|
93
|
+
graph_w[i] = 0
|
|
94
|
+
}
|
|
95
|
+
graph_cnt = 0
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// ---------------- 内部辅助 ----------------
|
|
99
|
+
|
|
100
|
+
// 内部辅助: 节点编号 u 是否合法
|
|
101
|
+
function graph_aux_node(int u) -> int {
|
|
102
|
+
if (u < 0 || u >= graph_n) { return 0 }
|
|
103
|
+
return 1
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// 内部辅助: 无向视角下 u 与 v 之间是否有边 (有向图取两个方向的并集)
|
|
107
|
+
function graph_aux_arc(int u, int v) -> int {
|
|
108
|
+
if (graph_edge[u * GRAPH_MAX + v] == 1) { return 1 }
|
|
109
|
+
if (graph_edge[v * GRAPH_MAX + u] == 1) { return 1 }
|
|
110
|
+
return 0
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ---------------- 边 ----------------
|
|
114
|
+
|
|
115
|
+
// 加边 u -> v (无向图则两侧都加), 权重 w
|
|
116
|
+
// 成功返回 1; 节点越界或这条边已存在返回 0 (状态不变)
|
|
117
|
+
function graph_add_edge(int u, int v, int w) -> int {
|
|
118
|
+
if (graph_aux_node(u) == 0 || graph_aux_node(v) == 0) { return 0 }
|
|
119
|
+
if (graph_edge[u * GRAPH_MAX + v] == 1) { return 0 }
|
|
120
|
+
graph_edge[u * GRAPH_MAX + v] = 1
|
|
121
|
+
graph_w[u * GRAPH_MAX + v] = w
|
|
122
|
+
if (graph_dir == 0) {
|
|
123
|
+
graph_edge[v * GRAPH_MAX + u] = 1
|
|
124
|
+
graph_w[v * GRAPH_MAX + u] = w
|
|
125
|
+
}
|
|
126
|
+
graph_cnt = graph_cnt + 1
|
|
127
|
+
return 1
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// u 到 v 是否存在边, 1/0; 节点越界返回 0
|
|
131
|
+
function graph_has_edge(int u, int v) -> int {
|
|
132
|
+
if (graph_aux_node(u) == 0 || graph_aux_node(v) == 0) { return 0 }
|
|
133
|
+
return graph_edge[u * GRAPH_MAX + v]
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// 边 u -> v 的权重; 无这条边或节点越界返回 0
|
|
137
|
+
// 权重本身可以是 0, 所以判断"有没有边"请用 graph_has_edge
|
|
138
|
+
function graph_weight(int u, int v) -> int {
|
|
139
|
+
if (graph_has_edge(u, v) == 0) { return 0 }
|
|
140
|
+
return graph_w[u * GRAPH_MAX + v]
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// 出度 (无向图就是邻居数, 自环算 1); 节点越界返回 -1
|
|
144
|
+
function graph_degree(int u) -> int {
|
|
145
|
+
if (graph_aux_node(u) == 0) { return -1 }
|
|
146
|
+
int d = 0
|
|
147
|
+
for (int v = 0; v < graph_n; v = v + 1) {
|
|
148
|
+
if (graph_edge[u * GRAPH_MAX + v] == 1) { d = d + 1 }
|
|
149
|
+
}
|
|
150
|
+
return d
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// 入度 (无向图与 graph_degree 相同, 自环算 1); 节点越界返回 -1
|
|
154
|
+
function graph_in_degree(int u) -> int {
|
|
155
|
+
if (graph_aux_node(u) == 0) { return -1 }
|
|
156
|
+
int d = 0
|
|
157
|
+
for (int v = 0; v < graph_n; v = v + 1) {
|
|
158
|
+
if (graph_edge[v * GRAPH_MAX + u] == 1) { d = d + 1 }
|
|
159
|
+
}
|
|
160
|
+
return d
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// ---------------- 遍历 ----------------
|
|
164
|
+
|
|
165
|
+
// 从 start 出发的广度优先遍历 (按出边方向; 邻居按编号升序入队)
|
|
166
|
+
// 返回访问到的节点数 cnt;
|
|
167
|
+
// order[0..cnt-1] 访问顺序;
|
|
168
|
+
// out_dist[0..graph_n-1] start 到各点的距离 (边数), 不可达写 -1;
|
|
169
|
+
// start 越界或图为空: 返回 0, order 不写, out_dist 全部写 -1
|
|
170
|
+
function graph_bfs(int start, int[] order, int[] out_dist) -> int {
|
|
171
|
+
for (int i = 0; i < graph_n; i = i + 1) {
|
|
172
|
+
graph_seen[i] = 0
|
|
173
|
+
out_dist[i] = -1
|
|
174
|
+
}
|
|
175
|
+
if (graph_aux_node(start) == 0) { return 0 }
|
|
176
|
+
int head = 0
|
|
177
|
+
int tail = 0
|
|
178
|
+
graph_queue[tail] = start
|
|
179
|
+
tail = tail + 1
|
|
180
|
+
graph_seen[start] = 1
|
|
181
|
+
out_dist[start] = 0
|
|
182
|
+
int cnt = 0
|
|
183
|
+
while (head < tail) {
|
|
184
|
+
int u = graph_queue[head]
|
|
185
|
+
head = head + 1
|
|
186
|
+
order[cnt] = u
|
|
187
|
+
cnt = cnt + 1
|
|
188
|
+
for (int v = 0; v < graph_n; v = v + 1) {
|
|
189
|
+
if (graph_edge[u * GRAPH_MAX + v] == 0) { continue }
|
|
190
|
+
if (graph_seen[v] == 1) { continue }
|
|
191
|
+
graph_seen[v] = 1
|
|
192
|
+
out_dist[v] = out_dist[u] + 1
|
|
193
|
+
graph_queue[tail] = v
|
|
194
|
+
tail = tail + 1
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return cnt
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// 从 start 出发的深度优先遍历 (**显式栈**, 不使用递归, 栈深不会随图增长)
|
|
201
|
+
// 返回访问到的节点数 cnt, order[0..cnt-1] 写入访问顺序;
|
|
202
|
+
// 邻居按编号**降序**压栈, 因此编号小的邻居先被访问到;
|
|
203
|
+
// start 越界或图为空返回 0 (order 不写)
|
|
204
|
+
function graph_dfs(int start, int[] order) -> int {
|
|
205
|
+
for (int i = 0; i < graph_n; i = i + 1) {
|
|
206
|
+
graph_seen[i] = 0
|
|
207
|
+
}
|
|
208
|
+
if (graph_aux_node(start) == 0) { return 0 }
|
|
209
|
+
int sp = 0
|
|
210
|
+
graph_stack[sp] = start
|
|
211
|
+
sp = sp + 1
|
|
212
|
+
graph_seen[start] = 1
|
|
213
|
+
int cnt = 0
|
|
214
|
+
while (sp > 0) {
|
|
215
|
+
sp = sp - 1
|
|
216
|
+
int u = graph_stack[sp]
|
|
217
|
+
order[cnt] = u
|
|
218
|
+
cnt = cnt + 1
|
|
219
|
+
for (int v = graph_n - 1; v >= 0; v = v - 1) {
|
|
220
|
+
if (graph_edge[u * GRAPH_MAX + v] == 0) { continue }
|
|
221
|
+
if (graph_seen[v] == 1) { continue }
|
|
222
|
+
graph_seen[v] = 1
|
|
223
|
+
graph_stack[sp] = v
|
|
224
|
+
sp = sp + 1
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return cnt
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// ---------------- 连通性 ----------------
|
|
231
|
+
|
|
232
|
+
// 连通分量个数 (按**无向视角**统计; 有向图得到的是弱连通分量数)
|
|
233
|
+
// 空图返回 0, 每个孤立节点各算一个分量
|
|
234
|
+
function graph_component_count() -> int {
|
|
235
|
+
int comps = 0
|
|
236
|
+
for (int i = 0; i < graph_n; i = i + 1) {
|
|
237
|
+
graph_seen[i] = 0
|
|
238
|
+
}
|
|
239
|
+
for (int s = 0; s < graph_n; s = s + 1) {
|
|
240
|
+
if (graph_seen[s] == 1) { continue }
|
|
241
|
+
comps = comps + 1
|
|
242
|
+
int head = 0
|
|
243
|
+
int tail = 0
|
|
244
|
+
graph_queue[tail] = s
|
|
245
|
+
tail = tail + 1
|
|
246
|
+
graph_seen[s] = 1
|
|
247
|
+
while (head < tail) {
|
|
248
|
+
int u = graph_queue[head]
|
|
249
|
+
head = head + 1
|
|
250
|
+
for (int v = 0; v < graph_n; v = v + 1) {
|
|
251
|
+
if (graph_aux_arc(u, v) == 0) { continue }
|
|
252
|
+
if (graph_seen[v] == 1) { continue }
|
|
253
|
+
graph_seen[v] = 1
|
|
254
|
+
graph_queue[tail] = v
|
|
255
|
+
tail = tail + 1
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return comps
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// 是否连通 (无向视角, 有向图问的是弱连通); 空图与单节点都返回 1
|
|
263
|
+
function graph_connected() -> int {
|
|
264
|
+
if (graph_component_count() <= 1) { return 1 }
|
|
265
|
+
return 0
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// ---------------- 环与拓扑序 ----------------
|
|
269
|
+
|
|
270
|
+
// 是否存在环 (自环算环): 1/0; 空图返回 0
|
|
271
|
+
// 无向图判定式: 边数 > 节点数 - 连通分量数 (无平行边, 因此充要);
|
|
272
|
+
// 有向图: 拓扑排序能否覆盖全部节点
|
|
273
|
+
function graph_is_cyclic() -> int {
|
|
274
|
+
if (graph_n <= 0) { return 0 }
|
|
275
|
+
if (graph_dir == 1) {
|
|
276
|
+
// 借 graph_stack 当拓扑序输出缓冲 (两者不会同时使用)
|
|
277
|
+
if (graph_topo_order(graph_stack) != graph_n) { return 1 }
|
|
278
|
+
return 0
|
|
279
|
+
}
|
|
280
|
+
if (graph_cnt > graph_n - graph_component_count()) { return 1 }
|
|
281
|
+
return 0
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// 有向图的拓扑排序 (Kahn: 反复取**编号最小**的入度 0 节点, 结果确定)
|
|
285
|
+
// 成功返回节点数, order[0..n-1] 写入拓扑序;
|
|
286
|
+
// 有环返回 -1 (order 只写入已确定的前缀);
|
|
287
|
+
// 无向图返回 -1 (无向图没有拓扑序); 空有向图返回 0
|
|
288
|
+
function graph_topo_order(int[] order) -> int {
|
|
289
|
+
if (graph_dir == 0) { return -1 }
|
|
290
|
+
if (graph_n <= 0) { return 0 }
|
|
291
|
+
for (int i = 0; i < graph_n; i = i + 1) {
|
|
292
|
+
graph_indeg[i] = 0
|
|
293
|
+
}
|
|
294
|
+
for (int u = 0; u < graph_n; u = u + 1) {
|
|
295
|
+
for (int v = 0; v < graph_n; v = v + 1) {
|
|
296
|
+
if (graph_edge[u * GRAPH_MAX + v] == 1) {
|
|
297
|
+
graph_indeg[v] = graph_indeg[v] + 1
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
int cnt = 0
|
|
302
|
+
for (int step = 0; step < graph_n; step = step + 1) {
|
|
303
|
+
int pick = -1
|
|
304
|
+
for (int i = 0; i < graph_n; i = i + 1) {
|
|
305
|
+
if (graph_indeg[i] == 0) {
|
|
306
|
+
pick = i
|
|
307
|
+
break
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
if (pick < 0) { break }
|
|
311
|
+
order[cnt] = pick
|
|
312
|
+
cnt = cnt + 1
|
|
313
|
+
graph_indeg[pick] = -1
|
|
314
|
+
for (int v = 0; v < graph_n; v = v + 1) {
|
|
315
|
+
if (graph_edge[pick * GRAPH_MAX + v] == 1) {
|
|
316
|
+
graph_indeg[v] = graph_indeg[v] - 1
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
if (cnt != graph_n) { return -1 }
|
|
321
|
+
return cnt
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// ---------------- 最短路 ----------------
|
|
325
|
+
|
|
326
|
+
// 单源最短路径 (Dijkstra, O(n^2) 邻接矩阵版)
|
|
327
|
+
// dist[0..graph_n-1] 写入 src 到各点的最短距离, **不可达写 -1**;
|
|
328
|
+
// 返回可达节点数 (含 src 自身);
|
|
329
|
+
// src 越界或图为空返回 0 且不写 dist;
|
|
330
|
+
// 有向图按出边方向松弛, 无向图两个方向都松弛;
|
|
331
|
+
// 要求边权 >= 0: 负权会让"松弛结果 < 0"的边被忽略, 此时结果没有保证
|
|
332
|
+
function graph_dijkstra(int src, int[] dist) -> int {
|
|
333
|
+
if (graph_aux_node(src) == 0) { return 0 }
|
|
334
|
+
for (int i = 0; i < graph_n; i = i + 1) {
|
|
335
|
+
dist[i] = -1
|
|
336
|
+
graph_seen[i] = 0
|
|
337
|
+
}
|
|
338
|
+
dist[src] = 0
|
|
339
|
+
for (int step = 0; step < graph_n; step = step + 1) {
|
|
340
|
+
int pick = -1
|
|
341
|
+
int best = 0
|
|
342
|
+
for (int i = 0; i < graph_n; i = i + 1) {
|
|
343
|
+
if (graph_seen[i] == 1) { continue }
|
|
344
|
+
if (dist[i] < 0) { continue }
|
|
345
|
+
if (pick < 0 || dist[i] < best) {
|
|
346
|
+
pick = i
|
|
347
|
+
best = dist[i]
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (pick < 0) { break }
|
|
351
|
+
graph_seen[pick] = 1
|
|
352
|
+
for (int v = 0; v < graph_n; v = v + 1) {
|
|
353
|
+
if (graph_edge[pick * GRAPH_MAX + v] == 0) { continue }
|
|
354
|
+
if (graph_seen[v] == 1) { continue }
|
|
355
|
+
int nd = dist[pick] + graph_w[pick * GRAPH_MAX + v]
|
|
356
|
+
if (nd < 0) { continue }
|
|
357
|
+
if (dist[v] < 0 || nd < dist[v]) {
|
|
358
|
+
dist[v] = nd
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
int cnt = 0
|
|
363
|
+
for (int i = 0; i < graph_n; i = i + 1) {
|
|
364
|
+
if (dist[i] >= 0) { cnt = cnt + 1 }
|
|
365
|
+
}
|
|
366
|
+
return cnt
|
|
367
|
+
}
|
codecin/lib/gui/pic.toml
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
|
|
2
|
+
[package]
|
|
3
|
+
name = "gui"
|
|
4
|
+
version = "7.22.10"
|
|
5
|
+
description = "Code CIN 官方标准库: GUI 窗口 + 画布绘图助手"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = ["ByUsi Studio"]
|
|
8
|
+
entry = "src/gui.cin"
|
|
9
|
+
repo = "https://gitee.com/codecin/internal-libraries"
|
|
10
|
+
keywords = ["codecin", "stdlib", "gui"]
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// Code CIN 官方标准库: GUI 窗口 + 画布绘图助手 (gui)
|
|
2
|
+
// 用法: import "gui.cin"
|
|
3
|
+
//
|
|
4
|
+
// ============ 一、窗口 (5.8.0 新增, 依赖原生 Go 运行时) ============
|
|
5
|
+
//
|
|
6
|
+
// 依赖原生 Go 运行时 (Windows 用 Win32, Linux 用 X11); 其他平台与
|
|
7
|
+
// --sandbox 下 gui_new 返回 -1, 所有函数优雅失败不崩溃。
|
|
8
|
+
//
|
|
9
|
+
// 绘图模型: g_new 成功后, 画布内建 (set_color / fill_rect / fill_circle /
|
|
10
|
+
// draw_line / draw_text) 直接画进窗口后备缓冲, 每次调用 g_update() 才呈现。
|
|
11
|
+
// **窗口打开期间不要再调用 canvas() 与本库的图表助手** —— 它们会把画布
|
|
12
|
+
// 重新绑定为离屏画布, 窗口从此不再更新; g_clear 已用 g_active() 做防护。
|
|
13
|
+
//
|
|
14
|
+
// 事件模型: 键盘事件与终端共用同一键码队列 (get_key / key_hit, 见 key.cin),
|
|
15
|
+
// 窗口获得焦点时按键会进入该队列; 关闭请求 (点 X / Alt+F4 / WM 删除协议)
|
|
16
|
+
// 由 g_closed() 报告, 主循环需主动检查。
|
|
17
|
+
//
|
|
18
|
+
// ============ 二、离屏绘图助手 (历史 API, 5.7.x 起) ============
|
|
19
|
+
//
|
|
20
|
+
// 依赖画布内建 (canvas / save_png / show_canvas 等, 原生能力), 用于
|
|
21
|
+
// 离屏绘制图表并导出 PNG / 弹窗预览; 不打开交互窗口, 与窗口 API 互不影响
|
|
22
|
+
// (除非窗口打开期间调用, 见上面的警告)。
|
|
23
|
+
|
|
24
|
+
// ==================== 一、窗口 ====================
|
|
25
|
+
|
|
26
|
+
// 窗口尺寸 (g_new 成功后记录, 供 g_clear 与离屏回退使用)
|
|
27
|
+
int g_w = 0
|
|
28
|
+
int g_h = 0
|
|
29
|
+
|
|
30
|
+
// 鼠标键位掩码 (mouse_button 返回值的位含义)
|
|
31
|
+
const int G_BTN_LEFT = 1
|
|
32
|
+
const int G_BTN_RIGHT = 2
|
|
33
|
+
const int G_BTN_MIDDLE = 4
|
|
34
|
+
|
|
35
|
+
// 打开窗口 (w/h 自动钳制到 1..4096); 成功返回 0, 失败 (无显示服务 /
|
|
36
|
+
// 平台不支持) 返回 -1。成功后画布内建直画窗口。
|
|
37
|
+
// (5.8.0 起签名变更: 旧的无窗口 g_new(w, h) 请直接使用 canvas(w, h))
|
|
38
|
+
function g_new(int w, int h, string title) -> int {
|
|
39
|
+
int r = gui_new(w, h, title)
|
|
40
|
+
if (r == 0) {
|
|
41
|
+
g_w = w
|
|
42
|
+
g_h = h
|
|
43
|
+
}
|
|
44
|
+
return r
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 呈现当前帧 (处理事件 + 把后备缓冲上屏); 无窗口返回 -1。
|
|
48
|
+
function g_update() -> int {
|
|
49
|
+
return gui_update()
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 关闭窗口 (画布保留, 仍可 save_png 导出); 恒返回 0。
|
|
53
|
+
function g_close() -> int {
|
|
54
|
+
int r = gui_close()
|
|
55
|
+
g_w = 0
|
|
56
|
+
g_h = 0
|
|
57
|
+
return r
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// 用户是否已请求关闭 (点 X / Alt+F4): 1 是 / 0 否
|
|
61
|
+
function g_closed() -> int {
|
|
62
|
+
return gui_closed()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// 窗口是否已打开: 1 是 / 0 否
|
|
66
|
+
function g_active() -> int {
|
|
67
|
+
return gui_active()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 整屏填色。窗口打开: 直画窗口; 未打开但调用过 g_new (后被 g_close 关闭):
|
|
71
|
+
// 回退离屏画布仍可 save_png; 从未指定尺寸: 返回 -1。
|
|
72
|
+
function g_clear(int rgb) -> int {
|
|
73
|
+
if (g_active() == 1) {
|
|
74
|
+
set_color(rgb)
|
|
75
|
+
fill_rect(0, 0, g_w, g_h)
|
|
76
|
+
return 0
|
|
77
|
+
}
|
|
78
|
+
if (g_w > 0) {
|
|
79
|
+
canvas(g_w, g_h)
|
|
80
|
+
set_color(rgb)
|
|
81
|
+
fill_rect(0, 0, g_w, g_h)
|
|
82
|
+
return 0
|
|
83
|
+
}
|
|
84
|
+
return -1
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// 鼠标光标 x (窗口客户区坐标); 无窗口返回 -1
|
|
88
|
+
function g_mouse_x() -> int {
|
|
89
|
+
return mouse_x()
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// 鼠标光标 y (窗口客户区坐标); 无窗口返回 -1
|
|
93
|
+
function g_mouse_y() -> int {
|
|
94
|
+
return mouse_y()
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// 按住的鼠标键位掩码 (bit0 左 / bit1 右 / bit2 中); 无窗口返回 -1
|
|
98
|
+
function g_mouse_buttons() -> int {
|
|
99
|
+
return mouse_button()
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 左键是否按住 (需窗口打开): 1 是 / 0 否
|
|
103
|
+
function g_mouse_left() -> int {
|
|
104
|
+
if (g_active() == 0) { return 0 }
|
|
105
|
+
if ((mouse_button() & G_BTN_LEFT) != 0) { return 1 }
|
|
106
|
+
return 0
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// 右键是否按住 (需窗口打开): 1 是 / 0 否
|
|
110
|
+
function g_mouse_right() -> int {
|
|
111
|
+
if (g_active() == 0) { return 0 }
|
|
112
|
+
if ((mouse_button() & G_BTN_RIGHT) != 0) { return 1 }
|
|
113
|
+
return 0
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// 中键是否按住 (需窗口打开): 1 是 / 0 否
|
|
117
|
+
function g_mouse_middle() -> int {
|
|
118
|
+
if (g_active() == 0) { return 0 }
|
|
119
|
+
if ((mouse_button() & G_BTN_MIDDLE) != 0) { return 1 }
|
|
120
|
+
return 0
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 弹出屏幕/触摸键盘: 0 成功 / -1 不可用
|
|
124
|
+
// (Windows: TabTip/输入体验; Linux: onboard/matchbox-keyboard/xvkbd;
|
|
125
|
+
// macOS: 辅助功能键盘)
|
|
126
|
+
function g_vkb_show() -> int {
|
|
127
|
+
return vkb_show()
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// 收起屏幕/触摸键盘: 0 成功 / -1 不可用
|
|
131
|
+
function g_vkb_hide() -> int {
|
|
132
|
+
return vkb_hide()
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// ==================== 二、离屏绘图助手 ====================
|
|
136
|
+
|
|
137
|
+
// 打包 RGB 颜色 (0xRRGGBB)
|
|
138
|
+
function g_rgb(int r, int g, int b) -> int {
|
|
139
|
+
return ((r & 255) << 16) | ((g & 255) << 8) | (b & 255)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// 画一个矩形边框 (线宽 1)
|
|
143
|
+
function g_rect_outline(int x, int y, int w, int h, int rgb) -> void {
|
|
144
|
+
set_color(rgb)
|
|
145
|
+
draw_line(x, y, x + w - 1, y)
|
|
146
|
+
draw_line(x, y + h - 1, x + w - 1, y + h - 1)
|
|
147
|
+
draw_line(x, y, x, y + h - 1)
|
|
148
|
+
draw_line(x + w - 1, y, x + w - 1, y + h - 1)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// 柱状图 (自动按最大值缩放; 深蓝柱子; 离屏画布)
|
|
152
|
+
function g_bar_chart(int[] values, int n, int w, int h) -> void {
|
|
153
|
+
canvas(w, h)
|
|
154
|
+
set_color(g_rgb(255, 255, 255))
|
|
155
|
+
fill_rect(0, 0, w, h)
|
|
156
|
+
if (n <= 0) { return }
|
|
157
|
+
int maxv = 1
|
|
158
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
159
|
+
if (values[i] > maxv) { maxv = values[i] }
|
|
160
|
+
}
|
|
161
|
+
int bw = idiv(w, n)
|
|
162
|
+
if (bw < 1) { bw = 1 }
|
|
163
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
164
|
+
int bh = idiv(values[i] * h, maxv)
|
|
165
|
+
set_color(g_rgb(60, 120, 200))
|
|
166
|
+
fill_rect(i * bw, h - bh, bw - 1, bh)
|
|
167
|
+
}
|
|
168
|
+
g_rect_outline(0, 0, w, h, g_rgb(0, 0, 0))
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// 网格 (浅灰)
|
|
172
|
+
function g_grid(int w, int h, int step) -> void {
|
|
173
|
+
set_color(g_rgb(200, 200, 200))
|
|
174
|
+
int x = 0
|
|
175
|
+
while (x < w) {
|
|
176
|
+
draw_line(x, 0, x, h - 1)
|
|
177
|
+
x = x + step
|
|
178
|
+
}
|
|
179
|
+
int y = 0
|
|
180
|
+
while (y < h) {
|
|
181
|
+
draw_line(0, y, w - 1, y)
|
|
182
|
+
y = y + step
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// 折线图 (深红; 离屏画布)
|
|
187
|
+
function g_line_chart(int[] values, int n, int w, int h) -> void {
|
|
188
|
+
canvas(w, h)
|
|
189
|
+
set_color(g_rgb(255, 255, 255))
|
|
190
|
+
fill_rect(0, 0, w, h)
|
|
191
|
+
if (n <= 1) { return }
|
|
192
|
+
int maxv = 1
|
|
193
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
194
|
+
if (values[i] > maxv) { maxv = values[i] }
|
|
195
|
+
}
|
|
196
|
+
int stepx = idiv(w - 1, n - 1)
|
|
197
|
+
if (stepx < 1) { stepx = 1 }
|
|
198
|
+
set_color(g_rgb(200, 40, 40))
|
|
199
|
+
int px = 0
|
|
200
|
+
int py = h - 1 - idiv(values[0] * (h - 1), maxv)
|
|
201
|
+
for (int i = 1; i < n; i = i + 1) {
|
|
202
|
+
int cx = i * stepx
|
|
203
|
+
int cy = h - 1 - idiv(values[i] * (h - 1), maxv)
|
|
204
|
+
draw_line(px, py, cx, cy)
|
|
205
|
+
px = cx
|
|
206
|
+
py = cy
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// 导出 PNG (0 成功 / -1 失败)
|
|
211
|
+
function g_save(string path) -> int {
|
|
212
|
+
return save_png(path)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// 调用系统查看器弹出窗口
|
|
216
|
+
function g_show() -> int {
|
|
217
|
+
return show_canvas()
|
|
218
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
|
|
2
|
+
[package]
|
|
3
|
+
name = "hash"
|
|
4
|
+
version = "7.22.10"
|
|
5
|
+
description = "Code CIN 官方标准库: 哈希"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = ["ByUsi Studio"]
|
|
8
|
+
entry = "src/hash.cin"
|
|
9
|
+
repo = "https://gitee.com/codecin/internal-libraries"
|
|
10
|
+
keywords = ["codecin", "stdlib", "hash"]
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Code CIN 官方标准库: 哈希 (hash)
|
|
2
|
+
// 用法: import "hash.cin"
|
|
3
|
+
//
|
|
4
|
+
// 全部为 64 位整数运算 (自然溢出即取模 2^64), 与 rand.cin 的
|
|
5
|
+
// 随机数配合可做简易哈希表 / 布隆过滤器的散列函数。
|
|
6
|
+
|
|
7
|
+
// djb2: h = h*33 + c
|
|
8
|
+
function hash_djb2(string s) -> int {
|
|
9
|
+
int h = 5381
|
|
10
|
+
int n = strlen(s)
|
|
11
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
12
|
+
h = h * 33 + s[i]
|
|
13
|
+
h = h & 0xFFFFFFFFFFFFFFFF
|
|
14
|
+
}
|
|
15
|
+
return h
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// FNV-1a (64 位)
|
|
19
|
+
function hash_fnv1a(string s) -> int {
|
|
20
|
+
int h = 0xCBF29CE484222325
|
|
21
|
+
int n = strlen(s)
|
|
22
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
23
|
+
h = h ^ s[i]
|
|
24
|
+
h = h * 0x100000001B3
|
|
25
|
+
h = h & 0xFFFFFFFFFFFFFFFF
|
|
26
|
+
}
|
|
27
|
+
return h
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// sdbm: h = c + (h<<6) + (h<<16) - h
|
|
31
|
+
function hash_sdbm(string s) -> int {
|
|
32
|
+
int h = 0
|
|
33
|
+
int n = strlen(s)
|
|
34
|
+
for (int i = 0; i < n; i = i + 1) {
|
|
35
|
+
h = s[i] + (h << 6) + (h << 16) - h
|
|
36
|
+
h = h & 0xFFFFFFFFFFFFFFFF
|
|
37
|
+
}
|
|
38
|
+
return h
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 整数位混合 (splitmix64 风格), 用于打散规律性输入
|
|
42
|
+
function hash_int(int x) -> int {
|
|
43
|
+
int z = x & 0xFFFFFFFFFFFFFFFF
|
|
44
|
+
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9
|
|
45
|
+
z = z & 0xFFFFFFFFFFFFFFFF
|
|
46
|
+
z = (z ^ (z >> 27)) * 0x94D049BB133111EB
|
|
47
|
+
z = z & 0xFFFFFFFFFFFFFFFF
|
|
48
|
+
return z ^ (z >> 31)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 组合两个哈希值
|
|
52
|
+
function hash_combine(int h1, int h2) -> int {
|
|
53
|
+
return hash_int(h1) ^ (h2 + 0x9E3779B97F4A7C15 + (h1 << 6) + (h1 >> 2))
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// 映射到桶下标 [0, buckets)
|
|
57
|
+
function hash_bucket(int h, int buckets) -> int {
|
|
58
|
+
if (buckets <= 0) { return 0 }
|
|
59
|
+
int m = h % buckets
|
|
60
|
+
if (m < 0) { m = m + buckets }
|
|
61
|
+
return m
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 字符串 -> 桶下标 (FNV-1a)
|
|
65
|
+
function hash_string_bucket(string s, int buckets) -> int {
|
|
66
|
+
return hash_bucket(hash_fnv1a(s), buckets)
|
|
67
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# 由 script/sync_stdlib_packages.py 生成,请勿手工编辑。
|
|
2
|
+
[package]
|
|
3
|
+
name = "heap"
|
|
4
|
+
version = "7.22.10"
|
|
5
|
+
description = "Code CIN 官方标准库: 定长二叉堆"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
authors = ["ByUsi Studio"]
|
|
8
|
+
entry = "src/heap.cin"
|
|
9
|
+
repo = "https://gitee.com/codecin/internal-libraries"
|
|
10
|
+
keywords = ["codecin", "stdlib", "heap"]
|