mt-lang 0.2.0 → 0.2.2
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.
- checksums.yaml +4 -4
- data/README.md +12 -0
- data/Rakefile +1 -1
- data/docs/index.html +13 -3
- data/lib/milk_tea/base.rb +7 -2
- data/std/asset_pack.mt +267 -0
- data/std/async/libuv_runtime.mt +539 -0
- data/std/async/mailbox.mt +191 -0
- data/std/async/runtime.mt +87 -0
- data/std/async.mt +87 -0
- data/std/base64.mt +185 -0
- data/std/behavior_tree.mt +396 -0
- data/std/binary.mt +314 -0
- data/std/binary_heap.mt +157 -0
- data/std/bitset.mt +239 -0
- data/std/box2d.mt +535 -0
- data/std/bytes.mt +46 -0
- data/std/c/box2d.mt +1254 -0
- data/std/c/cgltf.mt +647 -0
- data/std/c/cjson.mt +106 -0
- data/std/c/crypto.mt +11 -0
- data/std/c/crypto_support.h +51 -0
- data/std/c/ctype.mt +19 -0
- data/std/c/ctype_bindgen.h +29 -0
- data/std/c/curl.mt +1223 -0
- data/std/c/enet.mt +473 -0
- data/std/c/errno.mt +41 -0
- data/std/c/errno_bindgen.h +53 -0
- data/std/c/flecs.mt +2945 -0
- data/std/c/fs.linux.mt +43 -0
- data/std/c/fs.windows.mt +43 -0
- data/std/c/fs_support.h +954 -0
- data/std/c/gl.mt +2065 -0
- data/std/c/gl_registry_helpers.h +10748 -0
- data/std/c/glfw.mt +499 -0
- data/std/c/libc.mt +167 -0
- data/std/c/libuv.mt +1491 -0
- data/std/c/math.mt +22 -0
- data/std/c/math_bindgen.h +31 -0
- data/std/c/miniaudio.mt +3420 -0
- data/std/c/pcre2.mt +735 -0
- data/std/c/process.mt +58 -0
- data/std/c/process_support.h +1147 -0
- data/std/c/raygui.mt +1602 -0
- data/std/c/raylib.mt +1245 -0
- data/std/c/raymath.mt +163 -0
- data/std/c/rlgl.mt +434 -0
- data/std/c/rpng.mt +60 -0
- data/std/c/rres.mt +187 -0
- data/std/c/sdl3.mt +4132 -0
- data/std/c/sqlite3.mt +951 -0
- data/std/c/stb_image.mt +52 -0
- data/std/c/stb_image_resize2.mt +128 -0
- data/std/c/stb_image_write.mt +19 -0
- data/std/c/stb_rect_pack.mt +38 -0
- data/std/c/stb_truetype.mt +175 -0
- data/std/c/stb_vorbis.mt +31 -0
- data/std/c/stdio.mt +90 -0
- data/std/c/steamworks.h +5769 -0
- data/std/c/steamworks.mt +4771 -0
- data/std/c/string.mt +16 -0
- data/std/c/string_bindgen.h +36 -0
- data/std/c/sync.mt +33 -0
- data/std/c/sync_support.h +219 -0
- data/std/c/terminal.mt +24 -0
- data/std/c/terminal_support.h +408 -0
- data/std/c/time.mt +26 -0
- data/std/c/tls.mt +24 -0
- data/std/c/tls_support.h +626 -0
- data/std/c/tracy.mt +53 -0
- data/std/c/zlib.mt +20 -0
- data/std/c/zlib_support.h +239 -0
- data/std/c/zstd.mt +231 -0
- data/std/cell.mt +60 -0
- data/std/cgltf.mt +115 -0
- data/std/cjson.mt +90 -0
- data/std/cli.mt +768 -0
- data/std/cookie.mt +198 -0
- data/std/counter.mt +160 -0
- data/std/crypto.mt +74 -0
- data/std/cstring.mt +13 -0
- data/std/ctype.mt +52 -0
- data/std/curl/runtime.mt +261 -0
- data/std/curl.mt +38 -0
- data/std/deque.mt +348 -0
- data/std/encoding.mt +93 -0
- data/std/enet.mt +133 -0
- data/std/env.mt +41 -0
- data/std/errno.mt +47 -0
- data/std/flecs.mt +978 -0
- data/std/fmt.mt +271 -0
- data/std/fs.linux.mt +696 -0
- data/std/fs.windows.mt +711 -0
- data/std/fsm.mt +267 -0
- data/std/gl.mt +2062 -0
- data/std/glfw.mt +471 -0
- data/std/goap.mt +342 -0
- data/std/graph.mt +521 -0
- data/std/gzip.mt +77 -0
- data/std/hash.mt +429 -0
- data/std/http/server.mt +810 -0
- data/std/http.mt +1227 -0
- data/std/intern.mt +53 -0
- data/std/jobs.mt +288 -0
- data/std/json.mt +627 -0
- data/std/libc.mt +34 -0
- data/std/libuv.mt +554 -0
- data/std/linear_algebra.mt +243 -0
- data/std/linked_map.mt +372 -0
- data/std/linked_map_view.mt +49 -0
- data/std/linked_set.mt +117 -0
- data/std/log.mt +69 -0
- data/std/map.mt +418 -0
- data/std/math.mt +24 -0
- data/std/mem/arena.mt +137 -0
- data/std/mem/endian.mt +51 -0
- data/std/mem/heap.mt +298 -0
- data/std/mem/pool.mt +164 -0
- data/std/mem/stack.mt +47 -0
- data/std/mem/tracking.mt +119 -0
- data/std/miniaudio.mt +1299 -0
- data/std/multiset.mt +191 -0
- data/std/net/channel.mt +727 -0
- data/std/net/clock.mt +237 -0
- data/std/net/discovery.mt +277 -0
- data/std/net/lobby.mt +813 -0
- data/std/net/manager.mt +539 -0
- data/std/net/mux.mt +839 -0
- data/std/net/nat.mt +122 -0
- data/std/net/packet.mt +219 -0
- data/std/net/punch.mt +162 -0
- data/std/net/rpc.mt +119 -0
- data/std/net/session.mt +1157 -0
- data/std/net/stun.mt +252 -0
- data/std/net/sync.mt +183 -0
- data/std/net/turn.mt +459 -0
- data/std/net.mt +2944 -0
- data/std/oauth2.mt +408 -0
- data/std/option.mt +51 -0
- data/std/ordered_map.mt +497 -0
- data/std/ordered_set.mt +348 -0
- data/std/path.mt +368 -0
- data/std/pcre2/runtime.mt +35 -0
- data/std/pcre2.mt +78 -0
- data/std/priority_queue.mt +55 -0
- data/std/process.mt +837 -0
- data/std/queue.mt +55 -0
- data/std/random.mt +146 -0
- data/std/raygui.mt +99 -0
- data/std/raylib/debug_console.mt +71 -0
- data/std/raylib/easing.mt +388 -0
- data/std/raylib/packed_assets.mt +255 -0
- data/std/raylib/runtime.mt +30 -0
- data/std/raylib/tracy_gpu.mt +37 -0
- data/std/raylib.mt +1510 -0
- data/std/raymath.mt +153 -0
- data/std/result.mt +89 -0
- data/std/rlgl.mt +268 -0
- data/std/rpng.mt +46 -0
- data/std/rres.mt +36 -0
- data/std/sdl3/runtime.mt +54 -0
- data/std/sdl3.mt +1731 -0
- data/std/serialize.mt +68 -0
- data/std/set.mt +124 -0
- data/std/spatial.mt +176 -0
- data/std/sqlite3.mt +151 -0
- data/std/stack.mt +55 -0
- data/std/stb_image.mt +47 -0
- data/std/stb_image_resize2.mt +41 -0
- data/std/stb_image_write.mt +17 -0
- data/std/stb_rect_pack.mt +15 -0
- data/std/stb_truetype.mt +77 -0
- data/std/stb_vorbis.mt +16 -0
- data/std/stdio.mt +88 -0
- data/std/steamworks.mt +1542 -0
- data/std/str.mt +293 -0
- data/std/string.mt +234 -0
- data/std/sync.mt +194 -0
- data/std/tar.mt +704 -0
- data/std/terminal.mt +1002 -0
- data/std/testing.mt +266 -0
- data/std/thread.mt +120 -0
- data/std/time.mt +105 -0
- data/std/tls.mt +616 -0
- data/std/toml.mt +1310 -0
- data/std/tracy.mt +42 -0
- data/std/uri.mt +118 -0
- data/std/url.mt +372 -0
- data/std/vec.mt +433 -0
- data/std/zstd.mt +94 -0
- metadata +187 -2
data/std/hash.mt
ADDED
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
## Canonical hash, equal, and order implementations for common primitive types.
|
|
2
|
+
## Import this module to use primitive types as keys in hash-based and
|
|
3
|
+
## order-based collections (Map, Set, BinaryHeap, OrderedMap, etc.).
|
|
4
|
+
|
|
5
|
+
import std.str
|
|
6
|
+
|
|
7
|
+
# ---------------------------------------------------------------------------
|
|
8
|
+
# int
|
|
9
|
+
# ---------------------------------------------------------------------------
|
|
10
|
+
|
|
11
|
+
extending int:
|
|
12
|
+
public static function hash(value: const_ptr[int]) -> uint:
|
|
13
|
+
unsafe:
|
|
14
|
+
let v = read(ptr[int]<-value)
|
|
15
|
+
let as_uint: uint = uint<-v
|
|
16
|
+
let fnv: uint = 0x811C9DC5
|
|
17
|
+
let prime: uint = 0x01000193
|
|
18
|
+
var h = fnv
|
|
19
|
+
h = (h ^ (as_uint & uint<-(0xFF))) * prime
|
|
20
|
+
h = (h ^ ((as_uint >> uint<-(8)) & uint<-(0xFF))) * prime
|
|
21
|
+
h = (h ^ ((as_uint >> uint<-(16)) & uint<-(0xFF))) * prime
|
|
22
|
+
h = (h ^ ((as_uint >> uint<-(24)) & uint<-(0xFF))) * prime
|
|
23
|
+
return h
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
public static function equal(a: const_ptr[int], b: const_ptr[int]) -> bool:
|
|
27
|
+
unsafe:
|
|
28
|
+
return read(ptr[int]<-a) == read(ptr[int]<-b)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
public static function order(a: const_ptr[int], b: const_ptr[int]) -> int:
|
|
32
|
+
unsafe:
|
|
33
|
+
let av = read(ptr[int]<-a)
|
|
34
|
+
let bv = read(ptr[int]<-b)
|
|
35
|
+
if av < bv:
|
|
36
|
+
return -1
|
|
37
|
+
else if av > bv:
|
|
38
|
+
return 1
|
|
39
|
+
return 0
|
|
40
|
+
|
|
41
|
+
# ---------------------------------------------------------------------------
|
|
42
|
+
# uint
|
|
43
|
+
# ---------------------------------------------------------------------------
|
|
44
|
+
|
|
45
|
+
extending uint:
|
|
46
|
+
public static function hash(value: const_ptr[uint]) -> uint:
|
|
47
|
+
unsafe:
|
|
48
|
+
let v = read(ptr[uint]<-value)
|
|
49
|
+
let fnv: uint = 0x811C9DC5
|
|
50
|
+
let prime: uint = 0x01000193
|
|
51
|
+
var h = fnv
|
|
52
|
+
h = (h ^ (uint<-(ubyte<-(v & uint<-(0xFF))))) * prime
|
|
53
|
+
h = (h ^ (uint<-(ubyte<-((v >> uint<-(8)) & uint<-(0xFF))))) * prime
|
|
54
|
+
h = (h ^ (uint<-(ubyte<-((v >> uint<-(16)) & uint<-(0xFF))))) * prime
|
|
55
|
+
h = (h ^ (uint<-(ubyte<-((v >> uint<-(24)) & uint<-(0xFF))))) * prime
|
|
56
|
+
return h
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
public static function equal(a: const_ptr[uint], b: const_ptr[uint]) -> bool:
|
|
60
|
+
unsafe:
|
|
61
|
+
return read(ptr[uint]<-a) == read(ptr[uint]<-b)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
public static function order(a: const_ptr[uint], b: const_ptr[uint]) -> int:
|
|
65
|
+
unsafe:
|
|
66
|
+
let av = read(ptr[uint]<-a)
|
|
67
|
+
let bv = read(ptr[uint]<-b)
|
|
68
|
+
if av < bv:
|
|
69
|
+
return -1
|
|
70
|
+
else if av > bv:
|
|
71
|
+
return 1
|
|
72
|
+
return 0
|
|
73
|
+
|
|
74
|
+
# ---------------------------------------------------------------------------
|
|
75
|
+
# bool
|
|
76
|
+
# ---------------------------------------------------------------------------
|
|
77
|
+
|
|
78
|
+
extending bool:
|
|
79
|
+
public static function hash(value: const_ptr[bool]) -> uint:
|
|
80
|
+
unsafe:
|
|
81
|
+
if read(ptr[bool]<-value):
|
|
82
|
+
return 1
|
|
83
|
+
return 0
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
public static function equal(a: const_ptr[bool], b: const_ptr[bool]) -> bool:
|
|
87
|
+
unsafe:
|
|
88
|
+
return read(ptr[bool]<-a) == read(ptr[bool]<-b)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
public static function order(a: const_ptr[bool], b: const_ptr[bool]) -> int:
|
|
92
|
+
unsafe:
|
|
93
|
+
let av = read(ptr[bool]<-a)
|
|
94
|
+
let bv = read(ptr[bool]<-b)
|
|
95
|
+
if av == bv:
|
|
96
|
+
return 0
|
|
97
|
+
else if av:
|
|
98
|
+
return 1
|
|
99
|
+
return -1
|
|
100
|
+
|
|
101
|
+
# ---------------------------------------------------------------------------
|
|
102
|
+
# float — bitwise hash, exact equal
|
|
103
|
+
# ---------------------------------------------------------------------------
|
|
104
|
+
|
|
105
|
+
extending float:
|
|
106
|
+
public static function hash(value: const_ptr[float]) -> uint:
|
|
107
|
+
unsafe:
|
|
108
|
+
let v = read(ptr[float]<-value)
|
|
109
|
+
# `+0.0` and `-0.0` compare equal but have distinct bit patterns;
|
|
110
|
+
# collapse both to one hash so hash stays consistent with `equal`.
|
|
111
|
+
if v == 0.0:
|
|
112
|
+
return 0
|
|
113
|
+
return reinterpret[uint](v)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
public static function equal(a: const_ptr[float], b: const_ptr[float]) -> bool:
|
|
117
|
+
unsafe:
|
|
118
|
+
return read(ptr[float]<-a) == read(ptr[float]<-b)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
public static function order(a: const_ptr[float], b: const_ptr[float]) -> int:
|
|
122
|
+
unsafe:
|
|
123
|
+
let av = read(ptr[float]<-a)
|
|
124
|
+
let bv = read(ptr[float]<-b)
|
|
125
|
+
if av < bv:
|
|
126
|
+
return -1
|
|
127
|
+
else if av > bv:
|
|
128
|
+
return 1
|
|
129
|
+
return 0
|
|
130
|
+
|
|
131
|
+
# ---------------------------------------------------------------------------
|
|
132
|
+
# double — bitwise hash, exact equal
|
|
133
|
+
# ---------------------------------------------------------------------------
|
|
134
|
+
|
|
135
|
+
extending double:
|
|
136
|
+
public static function hash(value: const_ptr[double]) -> uint:
|
|
137
|
+
unsafe:
|
|
138
|
+
let v = read(ptr[double]<-value)
|
|
139
|
+
# `+0.0` and `-0.0` compare equal but have distinct bit patterns;
|
|
140
|
+
# collapse both to one hash so hash stays consistent with `equal`.
|
|
141
|
+
if v == 0.0:
|
|
142
|
+
return 0
|
|
143
|
+
let bits: ulong = reinterpret[ulong](v)
|
|
144
|
+
return uint<-((bits >> uint<-(32)) ^ bits)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
public static function equal(a: const_ptr[double], b: const_ptr[double]) -> bool:
|
|
148
|
+
unsafe:
|
|
149
|
+
return read(ptr[double]<-a) == read(ptr[double]<-b)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
public static function order(a: const_ptr[double], b: const_ptr[double]) -> int:
|
|
153
|
+
unsafe:
|
|
154
|
+
let av = read(ptr[double]<-a)
|
|
155
|
+
let bv = read(ptr[double]<-b)
|
|
156
|
+
if av < bv:
|
|
157
|
+
return -1
|
|
158
|
+
else if av > bv:
|
|
159
|
+
return 1
|
|
160
|
+
return 0
|
|
161
|
+
|
|
162
|
+
# ---------------------------------------------------------------------------
|
|
163
|
+
# char
|
|
164
|
+
# ---------------------------------------------------------------------------
|
|
165
|
+
|
|
166
|
+
extending char:
|
|
167
|
+
public static function hash(value: const_ptr[char]) -> uint:
|
|
168
|
+
unsafe:
|
|
169
|
+
return uint<-read(ptr[char]<-value)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
public static function equal(a: const_ptr[char], b: const_ptr[char]) -> bool:
|
|
173
|
+
unsafe:
|
|
174
|
+
return read(ptr[char]<-a) == read(ptr[char]<-b)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
public static function order(a: const_ptr[char], b: const_ptr[char]) -> int:
|
|
178
|
+
unsafe:
|
|
179
|
+
let av = int<-read(ptr[char]<-a)
|
|
180
|
+
let bv = int<-read(ptr[char]<-b)
|
|
181
|
+
if av < bv:
|
|
182
|
+
return -1
|
|
183
|
+
else if av > bv:
|
|
184
|
+
return 1
|
|
185
|
+
return 0
|
|
186
|
+
|
|
187
|
+
# ---------------------------------------------------------------------------
|
|
188
|
+
# Remaining integer widths — byte/short/long and unsigned variants, ptr_int.
|
|
189
|
+
# hash mixes the value's bytes (FNV-1a); equal/order use native operators.
|
|
190
|
+
# ---------------------------------------------------------------------------
|
|
191
|
+
|
|
192
|
+
function hash_u32(value: uint) -> uint:
|
|
193
|
+
let fnv: uint = 0x811C9DC5
|
|
194
|
+
let prime: uint = 0x01000193
|
|
195
|
+
var h = fnv
|
|
196
|
+
h = (h ^ (value & uint<-(0xFF))) * prime
|
|
197
|
+
h = (h ^ ((value >> uint<-(8)) & uint<-(0xFF))) * prime
|
|
198
|
+
h = (h ^ ((value >> uint<-(16)) & uint<-(0xFF))) * prime
|
|
199
|
+
h = (h ^ ((value >> uint<-(24)) & uint<-(0xFF))) * prime
|
|
200
|
+
return h
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
function hash_u64(value: ulong) -> uint:
|
|
204
|
+
return hash_u32(uint<-((value >> uint<-(32)) ^ value))
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
extending byte:
|
|
208
|
+
public static function hash(value: const_ptr[byte]) -> uint:
|
|
209
|
+
unsafe:
|
|
210
|
+
return hash_u32(uint<-(int<-read(ptr[byte]<-value)))
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
public static function equal(a: const_ptr[byte], b: const_ptr[byte]) -> bool:
|
|
214
|
+
unsafe:
|
|
215
|
+
return read(ptr[byte]<-a) == read(ptr[byte]<-b)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
public static function order(a: const_ptr[byte], b: const_ptr[byte]) -> int:
|
|
219
|
+
unsafe:
|
|
220
|
+
let av = read(ptr[byte]<-a)
|
|
221
|
+
let bv = read(ptr[byte]<-b)
|
|
222
|
+
if av < bv:
|
|
223
|
+
return -1
|
|
224
|
+
else if av > bv:
|
|
225
|
+
return 1
|
|
226
|
+
return 0
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
extending ubyte:
|
|
230
|
+
public static function hash(value: const_ptr[ubyte]) -> uint:
|
|
231
|
+
unsafe:
|
|
232
|
+
return hash_u32(uint<-read(ptr[ubyte]<-value))
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
public static function equal(a: const_ptr[ubyte], b: const_ptr[ubyte]) -> bool:
|
|
236
|
+
unsafe:
|
|
237
|
+
return read(ptr[ubyte]<-a) == read(ptr[ubyte]<-b)
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
public static function order(a: const_ptr[ubyte], b: const_ptr[ubyte]) -> int:
|
|
241
|
+
unsafe:
|
|
242
|
+
let av = read(ptr[ubyte]<-a)
|
|
243
|
+
let bv = read(ptr[ubyte]<-b)
|
|
244
|
+
if av < bv:
|
|
245
|
+
return -1
|
|
246
|
+
else if av > bv:
|
|
247
|
+
return 1
|
|
248
|
+
return 0
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
extending short:
|
|
252
|
+
public static function hash(value: const_ptr[short]) -> uint:
|
|
253
|
+
unsafe:
|
|
254
|
+
return hash_u32(uint<-(int<-read(ptr[short]<-value)))
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
public static function equal(a: const_ptr[short], b: const_ptr[short]) -> bool:
|
|
258
|
+
unsafe:
|
|
259
|
+
return read(ptr[short]<-a) == read(ptr[short]<-b)
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
public static function order(a: const_ptr[short], b: const_ptr[short]) -> int:
|
|
263
|
+
unsafe:
|
|
264
|
+
let av = read(ptr[short]<-a)
|
|
265
|
+
let bv = read(ptr[short]<-b)
|
|
266
|
+
if av < bv:
|
|
267
|
+
return -1
|
|
268
|
+
else if av > bv:
|
|
269
|
+
return 1
|
|
270
|
+
return 0
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
extending ushort:
|
|
274
|
+
public static function hash(value: const_ptr[ushort]) -> uint:
|
|
275
|
+
unsafe:
|
|
276
|
+
return hash_u32(uint<-read(ptr[ushort]<-value))
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
public static function equal(a: const_ptr[ushort], b: const_ptr[ushort]) -> bool:
|
|
280
|
+
unsafe:
|
|
281
|
+
return read(ptr[ushort]<-a) == read(ptr[ushort]<-b)
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
public static function order(a: const_ptr[ushort], b: const_ptr[ushort]) -> int:
|
|
285
|
+
unsafe:
|
|
286
|
+
let av = read(ptr[ushort]<-a)
|
|
287
|
+
let bv = read(ptr[ushort]<-b)
|
|
288
|
+
if av < bv:
|
|
289
|
+
return -1
|
|
290
|
+
else if av > bv:
|
|
291
|
+
return 1
|
|
292
|
+
return 0
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
extending long:
|
|
296
|
+
public static function hash(value: const_ptr[long]) -> uint:
|
|
297
|
+
unsafe:
|
|
298
|
+
return hash_u64(ulong<-read(ptr[long]<-value))
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
public static function equal(a: const_ptr[long], b: const_ptr[long]) -> bool:
|
|
302
|
+
unsafe:
|
|
303
|
+
return read(ptr[long]<-a) == read(ptr[long]<-b)
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
public static function order(a: const_ptr[long], b: const_ptr[long]) -> int:
|
|
307
|
+
unsafe:
|
|
308
|
+
let av = read(ptr[long]<-a)
|
|
309
|
+
let bv = read(ptr[long]<-b)
|
|
310
|
+
if av < bv:
|
|
311
|
+
return -1
|
|
312
|
+
else if av > bv:
|
|
313
|
+
return 1
|
|
314
|
+
return 0
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
extending ulong:
|
|
318
|
+
public static function hash(value: const_ptr[ulong]) -> uint:
|
|
319
|
+
unsafe:
|
|
320
|
+
return hash_u64(read(ptr[ulong]<-value))
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
public static function equal(a: const_ptr[ulong], b: const_ptr[ulong]) -> bool:
|
|
324
|
+
unsafe:
|
|
325
|
+
return read(ptr[ulong]<-a) == read(ptr[ulong]<-b)
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
public static function order(a: const_ptr[ulong], b: const_ptr[ulong]) -> int:
|
|
329
|
+
unsafe:
|
|
330
|
+
let av = read(ptr[ulong]<-a)
|
|
331
|
+
let bv = read(ptr[ulong]<-b)
|
|
332
|
+
if av < bv:
|
|
333
|
+
return -1
|
|
334
|
+
else if av > bv:
|
|
335
|
+
return 1
|
|
336
|
+
return 0
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
extending ptr_int:
|
|
340
|
+
public static function hash(value: const_ptr[ptr_int]) -> uint:
|
|
341
|
+
unsafe:
|
|
342
|
+
return hash_u64(ulong<-read(ptr[ptr_int]<-value))
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
public static function equal(a: const_ptr[ptr_int], b: const_ptr[ptr_int]) -> bool:
|
|
346
|
+
unsafe:
|
|
347
|
+
return read(ptr[ptr_int]<-a) == read(ptr[ptr_int]<-b)
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
public static function order(a: const_ptr[ptr_int], b: const_ptr[ptr_int]) -> int:
|
|
351
|
+
unsafe:
|
|
352
|
+
let av = read(ptr[ptr_int]<-a)
|
|
353
|
+
let bv = read(ptr[ptr_int]<-b)
|
|
354
|
+
if av < bv:
|
|
355
|
+
return -1
|
|
356
|
+
else if av > bv:
|
|
357
|
+
return 1
|
|
358
|
+
return 0
|
|
359
|
+
|
|
360
|
+
# ---------------------------------------------------------------------------
|
|
361
|
+
# Generic struct helpers — per-field reflection-based hash/equal/order.
|
|
362
|
+
# ---------------------------------------------------------------------------
|
|
363
|
+
|
|
364
|
+
const fnv_offset: uint = 0x811C9DC5
|
|
365
|
+
const fnv_prime: uint = 0x01000193
|
|
366
|
+
|
|
367
|
+
# Each field is hashed/compared/ordered through its own canonical hook
|
|
368
|
+
# (`hash[field.type]` / `equal[field.type]` / `order[field.type]`), so fields
|
|
369
|
+
# with reference semantics (e.g. `str`, nested structs) are handled by content
|
|
370
|
+
# rather than by raw bytes. A type delegates by defining `hash`/`equal`/`order`
|
|
371
|
+
# that call these helpers; nested struct fields must likewise provide the hooks.
|
|
372
|
+
|
|
373
|
+
public function hash_struct[T](value: const_ptr[T]) -> uint:
|
|
374
|
+
var h = fnv_offset
|
|
375
|
+
inline for field in fields_of(T):
|
|
376
|
+
let offset = offset_of(T, field)
|
|
377
|
+
unsafe:
|
|
378
|
+
let field_ptr = const_ptr[field.type]<-(ptr[ubyte]<-value + offset)
|
|
379
|
+
h = (h ^ hash[field.type](field_ptr)) * fnv_prime
|
|
380
|
+
return h
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
public function equal_struct[T](a: const_ptr[T], b: const_ptr[T]) -> bool:
|
|
384
|
+
inline for field in fields_of(T):
|
|
385
|
+
let offset = offset_of(T, field)
|
|
386
|
+
unsafe:
|
|
387
|
+
let pa = const_ptr[field.type]<-(ptr[ubyte]<-a + offset)
|
|
388
|
+
let pb = const_ptr[field.type]<-(ptr[ubyte]<-b + offset)
|
|
389
|
+
if not equal[field.type](pa, pb):
|
|
390
|
+
return false
|
|
391
|
+
return true
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
public function order_struct[T](a: const_ptr[T], b: const_ptr[T]) -> int:
|
|
395
|
+
inline for field in fields_of(T):
|
|
396
|
+
let offset = offset_of(T, field)
|
|
397
|
+
unsafe:
|
|
398
|
+
let pa = const_ptr[field.type]<-(ptr[ubyte]<-a + offset)
|
|
399
|
+
let pb = const_ptr[field.type]<-(ptr[ubyte]<-b + offset)
|
|
400
|
+
let result = order[field.type](pa, pb)
|
|
401
|
+
if result != 0:
|
|
402
|
+
return result
|
|
403
|
+
return 0
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
# ---------------------------------------------------------------------------
|
|
407
|
+
# ptr_uint (for map keys)
|
|
408
|
+
# ---------------------------------------------------------------------------
|
|
409
|
+
|
|
410
|
+
extending ptr_uint:
|
|
411
|
+
public static function hash(value: const_ptr[ptr_uint]) -> uint:
|
|
412
|
+
unsafe:
|
|
413
|
+
return hash_u64(ulong<-read(ptr[ptr_uint]<-value))
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
public static function equal(a: const_ptr[ptr_uint], b: const_ptr[ptr_uint]) -> bool:
|
|
417
|
+
unsafe:
|
|
418
|
+
return read(ptr[ptr_uint]<-a) == read(ptr[ptr_uint]<-b)
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
public static function order(a: const_ptr[ptr_uint], b: const_ptr[ptr_uint]) -> int:
|
|
422
|
+
unsafe:
|
|
423
|
+
let av = read(ptr[ptr_uint]<-a)
|
|
424
|
+
let bv = read(ptr[ptr_uint]<-b)
|
|
425
|
+
if av < bv:
|
|
426
|
+
return -1
|
|
427
|
+
else if av > bv:
|
|
428
|
+
return 1
|
|
429
|
+
return 0
|