@shd101wyy/yo 0.1.26 → 0.1.27

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 (167) hide show
  1. package/.github/skills/yo-async-effects/SKILL.md +4 -4
  2. package/.github/skills/yo-async-effects/async-effects-recipes.md +34 -34
  3. package/.github/skills/yo-core-patterns/SKILL.md +1 -1
  4. package/.github/skills/yo-core-patterns/core-patterns-cheatsheet.md +26 -26
  5. package/.github/skills/yo-project-workflow/SKILL.md +6 -3
  6. package/.github/skills/yo-project-workflow/workflow-cheatsheet.md +34 -11
  7. package/.github/skills/yo-syntax/SKILL.md +7 -6
  8. package/.github/skills/yo-syntax/syntax-cheatsheet.md +73 -60
  9. package/.github/skills/yo-wasm-integration/wasm-integration-cheatsheet.md +3 -3
  10. package/README.md +10 -8
  11. package/out/cjs/index.cjs +456 -438
  12. package/out/cjs/yo-cli.cjs +576 -543
  13. package/out/cjs/yo-lsp.cjs +559 -532
  14. package/out/esm/index.mjs +281 -263
  15. package/out/types/src/formatter.d.ts +11 -0
  16. package/out/types/src/lsp/formatting.d.ts +2 -0
  17. package/out/types/src/tests/formatter.test.d.ts +1 -0
  18. package/out/types/tsconfig.tsbuildinfo +1 -1
  19. package/package.json +1 -1
  20. package/std/alg/hash.yo +13 -21
  21. package/std/allocator.yo +25 -40
  22. package/std/async.yo +3 -7
  23. package/std/build.yo +105 -151
  24. package/std/cli/arg_parser.yo +184 -169
  25. package/std/collections/array_list.yo +350 -314
  26. package/std/collections/btree_map.yo +142 -131
  27. package/std/collections/deque.yo +132 -128
  28. package/std/collections/hash_map.yo +542 -566
  29. package/std/collections/hash_set.yo +623 -687
  30. package/std/collections/linked_list.yo +275 -293
  31. package/std/collections/ordered_map.yo +113 -85
  32. package/std/collections/priority_queue.yo +73 -73
  33. package/std/crypto/md5.yo +191 -95
  34. package/std/crypto/random.yo +56 -64
  35. package/std/crypto/sha256.yo +151 -107
  36. package/std/encoding/base64.yo +87 -81
  37. package/std/encoding/hex.yo +43 -50
  38. package/std/encoding/html.yo +56 -81
  39. package/std/encoding/html_char_utils.yo +7 -13
  40. package/std/encoding/html_entities.yo +2248 -2253
  41. package/std/encoding/json.yo +316 -224
  42. package/std/encoding/punycode.yo +86 -116
  43. package/std/encoding/toml.yo +67 -66
  44. package/std/encoding/utf16.yo +37 -44
  45. package/std/env.yo +62 -91
  46. package/std/error.yo +7 -15
  47. package/std/fmt/display.yo +5 -9
  48. package/std/fmt/index.yo +8 -14
  49. package/std/fmt/to_string.yo +330 -315
  50. package/std/fmt/writer.yo +58 -87
  51. package/std/fs/dir.yo +83 -102
  52. package/std/fs/file.yo +147 -180
  53. package/std/fs/metadata.yo +45 -78
  54. package/std/fs/temp.yo +55 -65
  55. package/std/fs/types.yo +27 -40
  56. package/std/fs/walker.yo +53 -68
  57. package/std/gc.yo +5 -8
  58. package/std/glob.yo +30 -43
  59. package/std/http/client.yo +107 -120
  60. package/std/http/http.yo +106 -96
  61. package/std/http/index.yo +4 -6
  62. package/std/imm/list.yo +88 -93
  63. package/std/imm/map.yo +528 -464
  64. package/std/imm/set.yo +52 -57
  65. package/std/imm/sorted_map.yo +340 -286
  66. package/std/imm/sorted_set.yo +57 -63
  67. package/std/imm/string.yo +404 -345
  68. package/std/imm/vec.yo +173 -181
  69. package/std/io/reader.yo +3 -6
  70. package/std/io/writer.yo +4 -8
  71. package/std/libc/assert.yo +5 -9
  72. package/std/libc/ctype.yo +32 -22
  73. package/std/libc/dirent.yo +26 -25
  74. package/std/libc/errno.yo +164 -90
  75. package/std/libc/fcntl.yo +52 -45
  76. package/std/libc/float.yo +66 -44
  77. package/std/libc/limits.yo +42 -33
  78. package/std/libc/math.yo +53 -82
  79. package/std/libc/signal.yo +72 -47
  80. package/std/libc/stdatomic.yo +217 -188
  81. package/std/libc/stdint.yo +5 -29
  82. package/std/libc/stdio.yo +5 -29
  83. package/std/libc/stdlib.yo +32 -39
  84. package/std/libc/string.yo +5 -23
  85. package/std/libc/sys/stat.yo +58 -56
  86. package/std/libc/time.yo +5 -19
  87. package/std/libc/unistd.yo +5 -20
  88. package/std/libc/wctype.yo +6 -9
  89. package/std/libc/windows.yo +26 -30
  90. package/std/log.yo +41 -55
  91. package/std/net/addr.yo +102 -97
  92. package/std/net/dns.yo +27 -28
  93. package/std/net/errors.yo +50 -49
  94. package/std/net/tcp.yo +113 -124
  95. package/std/net/udp.yo +55 -66
  96. package/std/os/env.yo +35 -33
  97. package/std/os/signal.yo +15 -25
  98. package/std/path.yo +276 -311
  99. package/std/prelude.yo +6304 -4315
  100. package/std/process/command.yo +87 -103
  101. package/std/process/index.yo +12 -31
  102. package/std/regex/compiler.yo +196 -95
  103. package/std/regex/flags.yo +58 -39
  104. package/std/regex/index.yo +157 -173
  105. package/std/regex/match.yo +20 -31
  106. package/std/regex/node.yo +134 -152
  107. package/std/regex/parser.yo +283 -259
  108. package/std/regex/unicode.yo +172 -202
  109. package/std/regex/vm.yo +155 -171
  110. package/std/string/index.yo +5 -7
  111. package/std/string/rune.yo +45 -55
  112. package/std/string/string.yo +937 -964
  113. package/std/string/string_builder.yo +94 -104
  114. package/std/string/unicode.yo +46 -64
  115. package/std/sync/channel.yo +72 -73
  116. package/std/sync/cond.yo +31 -36
  117. package/std/sync/mutex.yo +30 -32
  118. package/std/sync/once.yo +13 -16
  119. package/std/sync/rwlock.yo +26 -31
  120. package/std/sync/waitgroup.yo +20 -25
  121. package/std/sys/advise.yo +16 -24
  122. package/std/sys/bufio/buf_reader.yo +77 -93
  123. package/std/sys/bufio/buf_writer.yo +52 -65
  124. package/std/sys/clock.yo +4 -9
  125. package/std/sys/constants.yo +77 -61
  126. package/std/sys/copy.yo +4 -10
  127. package/std/sys/dir.yo +26 -43
  128. package/std/sys/dns.yo +41 -61
  129. package/std/sys/errors.yo +95 -103
  130. package/std/sys/events.yo +45 -57
  131. package/std/sys/externs.yo +319 -267
  132. package/std/sys/fallocate.yo +7 -11
  133. package/std/sys/fcntl.yo +14 -22
  134. package/std/sys/file.yo +26 -40
  135. package/std/sys/future.yo +5 -8
  136. package/std/sys/iov.yo +12 -25
  137. package/std/sys/lock.yo +12 -13
  138. package/std/sys/mmap.yo +38 -43
  139. package/std/sys/path.yo +3 -8
  140. package/std/sys/perm.yo +7 -21
  141. package/std/sys/pipe.yo +5 -12
  142. package/std/sys/process.yo +23 -29
  143. package/std/sys/seek.yo +10 -12
  144. package/std/sys/signal.yo +7 -13
  145. package/std/sys/signals.yo +52 -35
  146. package/std/sys/socket.yo +63 -58
  147. package/std/sys/socketpair.yo +3 -6
  148. package/std/sys/sockinfo.yo +11 -20
  149. package/std/sys/statfs.yo +11 -34
  150. package/std/sys/statx.yo +25 -52
  151. package/std/sys/sysinfo.yo +15 -20
  152. package/std/sys/tcp.yo +62 -92
  153. package/std/sys/temp.yo +5 -9
  154. package/std/sys/time.yo +5 -15
  155. package/std/sys/timer.yo +6 -11
  156. package/std/sys/tty.yo +10 -18
  157. package/std/sys/udp.yo +22 -39
  158. package/std/sys/umask.yo +3 -6
  159. package/std/sys/unix.yo +33 -52
  160. package/std/testing/bench.yo +49 -52
  161. package/std/thread.yo +10 -15
  162. package/std/time/datetime.yo +105 -89
  163. package/std/time/duration.yo +43 -56
  164. package/std/time/instant.yo +13 -18
  165. package/std/time/sleep.yo +5 -9
  166. package/std/url/index.yo +184 -209
  167. package/std/worker.yo +6 -10
package/std/imm/map.yo CHANGED
@@ -17,363 +17,387 @@
17
17
  //! assert((m.get(i32(1)).unwrap() == i32(100)), "key 1 maps to 100");
18
18
  //! assert((m.len() == usize(2)), "two entries");
19
19
  //! ```
20
-
21
- { GlobalAllocator } :: import "../allocator.yo";
20
+ { GlobalAllocator } :: import("../allocator.yo");
22
21
  { malloc, free } :: GlobalAllocator;
23
-
24
22
  /// Number of bits per HAMT level.
25
23
  BITS :: usize(5);
26
24
  /// Mask for extracting BITS-wide chunks from hash.
27
25
  MASK :: u64(31);
28
-
29
26
  /// Count the number of set bits in a u32 (popcount).
30
- popcount :: (fn(x: u32) -> u32)({
27
+ popcount :: (fn(x : u32) -> u32)({
31
28
  (v : u32) = x;
32
29
  v = (v - ((v >> u32(1)) & u32(1431655765)));
33
30
  v = ((v & u32(858993459)) + ((v >> u32(2)) & u32(858993459)));
34
31
  (((v + (v >> u32(4))) & u32(252645135)) * u32(16843009)) >> u32(24)
35
32
  });
36
-
37
33
  /// Key-value pair used in collision nodes and entries.
38
34
  Pair :: (fn(comptime(K) : Type, comptime(V) : Type) -> comptime(Type))(
39
35
  struct(key : K, value : V)
40
36
  );
41
-
42
37
  /// HAMT leaf node -- single key-value entry.
43
- MapLeaf :: (fn(
44
- comptime(K) : Type,
45
- comptime(V) : Type,
46
- where(K <: (Eq(K), Hash, Send), V <: Send)
47
- ) -> comptime(Type))(
48
- atomic object(
49
- hash : u64,
50
- key : K,
51
- value : V
38
+ MapLeaf :: (
39
+ fn(
40
+ comptime(K) : Type,
41
+ comptime(V) : Type,
42
+ where(K <: (Eq(K), Hash, Send), V <: Send)
43
+ ) -> comptime(Type)
44
+ )(
45
+ atomic(
46
+ object(
47
+ hash : u64,
48
+ key : K,
49
+ value : V
50
+ )
52
51
  )
53
52
  );
54
-
55
53
  /// HAMT collision node -- multiple entries sharing the same hash.
56
- MapCollision :: (fn(
57
- comptime(K) : Type,
58
- comptime(V) : Type,
59
- where(K <: (Eq(K), Hash, Send), V <: Send)
60
- ) -> comptime(Type))(
61
- atomic object(
62
- hash : u64,
63
- _pairs_ptr : *(Pair(K, V)),
64
- _pairs_len : usize
54
+ MapCollision :: (
55
+ fn(
56
+ comptime(K) : Type,
57
+ comptime(V) : Type,
58
+ where(K <: (Eq(K), Hash, Send), V <: Send)
59
+ ) -> comptime(Type)
60
+ )(
61
+ atomic(
62
+ object(
63
+ hash : u64,
64
+ _pairs_ptr : *(Pair(K, V)),
65
+ _pairs_len : usize
66
+ )
67
+ )
68
+ );
69
+ impl(
70
+ forall(K : Type, V : Type),
71
+ where(K <: (Eq(K), Hash, Send), V <: Send),
72
+ MapCollision(K, V),
73
+ Dispose(
74
+ dispose : (fn(self : Self) -> unit)({
75
+ i := usize(0);
76
+ while(i < self._pairs_len, i = (i + usize(1)), {
77
+ unsafe.drop((self._pairs_ptr &+ i).*);
78
+ });
79
+ free(.Some(*(void)(self._pairs_ptr)));
80
+ })
65
81
  )
66
82
  );
67
-
68
- impl(forall(K : Type, V : Type), where(K <: (Eq(K), Hash, Send), V <: Send),
69
- MapCollision(K, V), Dispose(
70
- dispose : (fn(self: Self) -> unit)({
71
- i := usize(0);
72
- while (i < self._pairs_len), (i = (i + usize(1))), {
73
- unsafe.drop((self._pairs_ptr &+ i).*);
74
- };
75
- free(.Some(*(void)(self._pairs_ptr)));
76
- })
77
- ));
78
-
79
83
  /// HAMT branch node -- bitmap + compact child array.
80
84
  ///
81
85
  /// `_children_ptr` is `*(void)` to break the circular type dependency with
82
86
  /// `MapNode`. It is cast to `*(MapNode(K, V))` in methods.
83
- MapBranch :: (fn(
84
- comptime(K) : Type,
85
- comptime(V) : Type,
86
- where(K <: (Eq(K), Hash, Send), V <: Send)
87
- ) -> comptime(Type))(
88
- atomic object(
89
- bitmap : u32,
90
- _children_ptr : *(void),
91
- _children_len : u8
87
+ MapBranch :: (
88
+ fn(
89
+ comptime(K) : Type,
90
+ comptime(V) : Type,
91
+ where(K <: (Eq(K), Hash, Send), V <: Send)
92
+ ) -> comptime(Type)
93
+ )(
94
+ atomic(
95
+ object(
96
+ bitmap : u32,
97
+ _children_ptr : *(void),
98
+ _children_len : u8
99
+ )
92
100
  )
93
101
  );
94
-
95
102
  /// Tagged union representing a single HAMT node.
96
- MapNode :: (fn(
97
- comptime(K) : Type,
98
- comptime(V) : Type,
99
- where(K <: (Eq(K), Hash, Send), V <: Send)
100
- ) -> comptime(Type))(
103
+ MapNode :: (
104
+ fn(
105
+ comptime(K) : Type,
106
+ comptime(V) : Type,
107
+ where(K <: (Eq(K), Hash, Send), V <: Send)
108
+ ) -> comptime(Type)
109
+ )(
101
110
  enum(
102
111
  Branch(node : MapBranch(K, V)),
103
112
  Leaf(node : MapLeaf(K, V)),
104
113
  Collision(node : MapCollision(K, V))
105
114
  )
106
115
  );
107
-
108
- impl(forall(K : Type, V : Type), where(K <: (Eq(K), Hash, Send), V <: Send),
109
- MapBranch(K, V), Dispose(
110
- dispose : (fn(self: Self) -> unit)({
111
- children := *(MapNode(K, V))(self._children_ptr);
112
- i := u8(0);
113
- while (i < self._children_len), (i = (i + u8(1))), {
114
- unsafe.drop((children &+ usize(i)).*);
115
- };
116
- free(.Some(self._children_ptr));
117
- })
118
- ));
119
-
116
+ impl(
117
+ forall(K : Type, V : Type),
118
+ where(K <: (Eq(K), Hash, Send), V <: Send),
119
+ MapBranch(K, V),
120
+ Dispose(
121
+ dispose : (fn(self : Self) -> unit)({
122
+ children := *(MapNode(K, V))(self._children_ptr);
123
+ i := u8(0);
124
+ while(i < self._children_len, i = (i + u8(1)), {
125
+ unsafe.drop((children &+ usize(i)).*);
126
+ });
127
+ free(.Some(self._children_ptr));
128
+ })
129
+ )
130
+ );
120
131
  /// Persistent immutable hash map.
121
- Map :: (fn(
122
- comptime(K) : Type,
123
- comptime(V) : Type,
124
- where(K <: (Eq(K), Hash, Send), V <: Send)
125
- ) -> comptime(Type))(
132
+ Map :: (
133
+ fn(
134
+ comptime(K) : Type,
135
+ comptime(V) : Type,
136
+ where(K <: (Eq(K), Hash, Send), V <: Send)
137
+ ) -> comptime(Type)
138
+ )(
126
139
  struct(
127
140
  _root : Option(MapNode(K, V)),
128
141
  _len : usize
129
142
  )
130
143
  );
131
-
132
144
  // -- MapBranch helpers --
133
-
134
- _alloc_map_children :: (fn(comptime(K) : Type, comptime(V) : Type, count: u8, where(K <: (Eq(K), Hash, Send), V <: Send)) -> *(void))({
145
+ _alloc_map_children :: (fn(comptime(K) : Type, comptime(V) : Type, count : u8, where(K <: (Eq(K), Hash, Send), V <: Send)) -> *(void))({
135
146
  sz := (sizeof(MapNode(K, V)) * usize(count));
136
- match(malloc(sz),
147
+ match(
148
+ malloc(sz),
137
149
  .Some(p) => p,
138
150
  .None => panic("imm.Map: allocation failed")
139
151
  )
140
152
  });
141
-
142
- _branch_index_for :: (fn(comptime(K) : Type, comptime(V) : Type, branch: MapBranch(K, V), bit: u32, where(K <: (Eq(K), Hash, Send), V <: Send)) -> u8)(
143
- u8(popcount((branch.bitmap & (bit - u32(1)))))
153
+ _branch_index_for :: (fn(comptime(K) : Type, comptime(V) : Type, branch : MapBranch(K, V), bit : u32, where(K <: (Eq(K), Hash, Send), V <: Send)) -> u8)(
154
+ u8(popcount(branch.bitmap & (bit - u32(1))))
144
155
  );
145
-
146
- _branch_has_child :: (fn(comptime(K) : Type, comptime(V) : Type, branch: MapBranch(K, V), bit: u32, where(K <: (Eq(K), Hash, Send), V <: Send)) -> bool)(
147
- ((branch.bitmap & bit) != u32(0))
156
+ _branch_has_child :: (fn(comptime(K) : Type, comptime(V) : Type, branch : MapBranch(K, V), bit : u32, where(K <: (Eq(K), Hash, Send), V <: Send)) -> bool)(
157
+ (branch.bitmap & bit) != u32(0)
148
158
  );
149
-
150
- _branch_child_at :: (fn(comptime(K) : Type, comptime(V) : Type, branch: MapBranch(K, V), idx: u8, where(K <: (Eq(K), Hash, Send), V <: Send)) -> MapNode(K, V))({
159
+ _branch_child_at :: (fn(comptime(K) : Type, comptime(V) : Type, branch : MapBranch(K, V), idx : u8, where(K <: (Eq(K), Hash, Send), V <: Send)) -> MapNode(K, V))({
151
160
  offset := (sizeof(MapNode(K, V)) * usize(idx));
152
- target := *(MapNode(K, V))((branch._children_ptr &+ offset));
161
+ target := *(MapNode(K, V))(branch._children_ptr &+ offset);
153
162
  target.*
154
163
  });
155
-
156
- _branch_set_child :: (fn(comptime(K) : Type, comptime(V) : Type, ptr: *(void), idx: u8, child: MapNode(K, V), where(K <: (Eq(K), Hash, Send), V <: Send)) -> unit)({
164
+ _branch_set_child :: (fn(comptime(K) : Type, comptime(V) : Type, ptr : *(void), idx : u8, child : MapNode(K, V), where(K <: (Eq(K), Hash, Send), V <: Send)) -> unit)({
157
165
  offset := (sizeof(MapNode(K, V)) * usize(idx));
158
- target := *(MapNode(K, V))((ptr &+ offset));
166
+ target := *(MapNode(K, V))(ptr &+ offset);
159
167
  target.* = child;
160
168
  });
161
-
162
169
  /// Copy children from source array to destination array element-by-element.
163
170
  /// This ensures proper RC increment for each child (memcpy would bypass RC).
164
- _copy_children :: (fn(
165
- comptime(K) : Type, comptime(V) : Type,
166
- dst: *(void), src: *(void),
167
- dst_offset: u8, src_offset: u8, count: u8,
168
- where(K <: (Eq(K), Hash, Send), V <: Send)
169
- ) -> unit)({
171
+ _copy_children :: (
172
+ fn(
173
+ comptime(K) : Type,
174
+ comptime(V) : Type,
175
+ dst : *(void),
176
+ src : *(void),
177
+ dst_offset : u8,
178
+ src_offset : u8,
179
+ count : u8,
180
+ where(K <: (Eq(K), Hash, Send), V <: Send)
181
+ ) -> unit
182
+ )({
170
183
  node_sz := sizeof(MapNode(K, V));
171
184
  i := u8(0);
172
- while (i < count), (i = (i + u8(1))), {
173
- src_ptr := *(MapNode(K, V))((src &+ (node_sz * usize((src_offset + i)))));
174
- dst_ptr := *(MapNode(K, V))((dst &+ (node_sz * usize((dst_offset + i)))));
185
+ while(i < count, i = (i + u8(1)), {
186
+ src_ptr := *(MapNode(K, V))(src &+ (node_sz * usize(src_offset + i)));
187
+ dst_ptr := *(MapNode(K, V))(dst &+ (node_sz * usize(dst_offset + i)));
175
188
  consume(dst_ptr.* = src_ptr.*);
176
- };
189
+ });
177
190
  });
178
-
179
- _branch_with_child :: (fn(comptime(K) : Type, comptime(V) : Type, branch: MapBranch(K, V), bit: u32, child: MapNode(K, V), where(K <: (Eq(K), Hash, Send), V <: Send)) -> MapBranch(K, V))({
191
+ _branch_with_child :: (fn(comptime(K) : Type, comptime(V) : Type, branch : MapBranch(K, V), bit : u32, child : MapNode(K, V), where(K <: (Eq(K), Hash, Send), V <: Send)) -> MapBranch(K, V))({
180
192
  idx := _branch_index_for(K, V, branch, bit);
181
193
  new_len := (branch._children_len + u8(1));
182
194
  new_ptr := _alloc_map_children(K, V, new_len);
183
- if((usize(idx) > usize(0)), {
195
+ if(usize(idx) > usize(0), {
184
196
  _copy_children(K, V, new_ptr, branch._children_ptr, u8(0), u8(0), idx);
185
197
  });
186
198
  child_offset := (sizeof(MapNode(K, V)) * usize(idx));
187
- child_target := *(MapNode(K, V))((new_ptr &+ child_offset));
199
+ child_target := *(MapNode(K, V))(new_ptr &+ child_offset);
188
200
  consume(child_target.* = child);
189
201
  remaining := (branch._children_len - idx);
190
- if((usize(remaining) > usize(0)), {
191
- _copy_children(K, V, new_ptr, branch._children_ptr, (idx + u8(1)), idx, remaining);
202
+ if(usize(remaining) > usize(0), {
203
+ _copy_children(K, V, new_ptr, branch._children_ptr, idx + u8(1), idx, remaining);
192
204
  });
193
- MapBranch(K, V)(bitmap: (branch.bitmap | bit), _children_ptr: new_ptr, _children_len: new_len)
205
+ MapBranch(K, V)(bitmap : (branch.bitmap | bit), _children_ptr : new_ptr, _children_len : new_len)
194
206
  });
195
-
196
- _branch_replace_child :: (fn(comptime(K) : Type, comptime(V) : Type, branch: MapBranch(K, V), bit: u32, child: MapNode(K, V), where(K <: (Eq(K), Hash, Send), V <: Send)) -> MapBranch(K, V))({
207
+ _branch_replace_child :: (fn(comptime(K) : Type, comptime(V) : Type, branch : MapBranch(K, V), bit : u32, child : MapNode(K, V), where(K <: (Eq(K), Hash, Send), V <: Send)) -> MapBranch(K, V))({
197
208
  idx := _branch_index_for(K, V, branch, bit);
198
209
  new_ptr := _alloc_map_children(K, V, branch._children_len);
199
210
  _copy_children(K, V, new_ptr, branch._children_ptr, u8(0), u8(0), branch._children_len);
200
211
  _branch_set_child(K, V, new_ptr, idx, child);
201
- MapBranch(K, V)(bitmap: branch.bitmap, _children_ptr: new_ptr, _children_len: branch._children_len)
212
+ MapBranch(K, V)(bitmap : branch.bitmap, _children_ptr : new_ptr, _children_len : branch._children_len)
202
213
  });
203
-
204
- _branch_without_child :: (fn(comptime(K) : Type, comptime(V) : Type, branch: MapBranch(K, V), bit: u32, where(K <: (Eq(K), Hash, Send), V <: Send)) -> MapBranch(K, V))({
214
+ _branch_without_child :: (fn(comptime(K) : Type, comptime(V) : Type, branch : MapBranch(K, V), bit : u32, where(K <: (Eq(K), Hash, Send), V <: Send)) -> MapBranch(K, V))({
205
215
  idx := _branch_index_for(K, V, branch, bit);
206
216
  new_len := (branch._children_len - u8(1));
207
217
  alloc_len := cond((new_len == u8(0)) => u8(1), true => new_len);
208
218
  new_ptr := _alloc_map_children(K, V, alloc_len);
209
- if((usize(idx) > usize(0)), {
219
+ if(usize(idx) > usize(0), {
210
220
  _copy_children(K, V, new_ptr, branch._children_ptr, u8(0), u8(0), idx);
211
221
  });
212
222
  remaining := (branch._children_len - (idx + u8(1)));
213
- if((usize(remaining) > usize(0)), {
214
- _copy_children(K, V, new_ptr, branch._children_ptr, idx, (idx + u8(1)), remaining);
223
+ if(usize(remaining) > usize(0), {
224
+ _copy_children(K, V, new_ptr, branch._children_ptr, idx, idx + u8(1), remaining);
215
225
  });
216
- MapBranch(K, V)(bitmap: (branch.bitmap & (~(bit))), _children_ptr: new_ptr, _children_len: new_len)
226
+ MapBranch(K, V)(bitmap : (branch.bitmap & (~(bit))), _children_ptr : new_ptr, _children_len : new_len)
217
227
  });
218
-
219
228
  // -- MapCollision helpers --
220
-
221
- _alloc_pairs :: (fn(comptime(K) : Type, comptime(V) : Type, count: usize, where(K <: (Eq(K), Hash, Send), V <: Send)) -> *(Pair(K, V)))({
229
+ _alloc_pairs :: (fn(comptime(K) : Type, comptime(V) : Type, count : usize, where(K <: (Eq(K), Hash, Send), V <: Send)) -> *(Pair(K, V)))({
222
230
  sz := (sizeof(Pair(K, V)) * count);
223
- match(malloc(sz),
231
+ match(
232
+ malloc(sz),
224
233
  .Some(p) => *(Pair(K, V))(p),
225
234
  .None => panic("imm.Map: collision allocation failed")
226
235
  )
227
236
  });
228
-
229
- _coll_find_key :: (fn(comptime(K) : Type, comptime(V) : Type, coll: MapCollision(K, V), k: K, where(K <: (Eq(K), Hash, Send), V <: Send)) -> Option(usize))({
237
+ _coll_find_key :: (fn(comptime(K) : Type, comptime(V) : Type, coll : MapCollision(K, V), k : K, where(K <: (Eq(K), Hash, Send), V <: Send)) -> Option(usize))({
230
238
  i := usize(0);
231
- while (i < coll._pairs_len), (i = (i + usize(1))), {
232
- if(((coll._pairs_ptr &+ i).*.key == k), {
233
- return .Some(i);
239
+ while(i < coll._pairs_len, i = (i + usize(1)), {
240
+ if((coll._pairs_ptr &+ i).*.key == k, {
241
+ return(.Some(i));
234
242
  });
235
- };
243
+ });
236
244
  .None
237
245
  });
238
-
239
246
  /// Copy pairs element-by-element to ensure proper RC for K/V.
240
- _copy_pairs :: (fn(
241
- comptime(K) : Type, comptime(V) : Type,
242
- dst: *(Pair(K, V)), src: *(Pair(K, V)),
243
- dst_offset: usize, src_offset: usize, count: usize,
244
- where(K <: (Eq(K), Hash, Send), V <: Send)
245
- ) -> unit)({
247
+ _copy_pairs :: (
248
+ fn(
249
+ comptime(K) : Type,
250
+ comptime(V) : Type,
251
+ dst : *(Pair(K, V)),
252
+ src : *(Pair(K, V)),
253
+ dst_offset : usize,
254
+ src_offset : usize,
255
+ count : usize,
256
+ where(K <: (Eq(K), Hash, Send), V <: Send)
257
+ ) -> unit
258
+ )({
246
259
  i := usize(0);
247
- while (i < count), (i = (i + usize(1))), {
260
+ while(i < count, i = (i + usize(1)), {
248
261
  consume((dst &+ (dst_offset + i)).* = (src &+ (src_offset + i)).*);
249
- };
262
+ });
250
263
  });
251
-
252
- _coll_with_pair :: (fn(comptime(K) : Type, comptime(V) : Type, coll: MapCollision(K, V), k: K, v: V, where(K <: (Eq(K), Hash, Send), V <: Send)) -> MapCollision(K, V))(
253
- match(_coll_find_key(K, V, coll, k),
264
+ _coll_with_pair :: (fn(comptime(K) : Type, comptime(V) : Type, coll : MapCollision(K, V), k : K, v : V, where(K <: (Eq(K), Hash, Send), V <: Send)) -> MapCollision(K, V))(
265
+ match(
266
+ _coll_find_key(K, V, coll, k),
254
267
  .Some(idx) => {
255
268
  new_ptr := _alloc_pairs(K, V, coll._pairs_len);
256
269
  _copy_pairs(K, V, new_ptr, coll._pairs_ptr, usize(0), usize(0), coll._pairs_len);
257
- (new_ptr &+ idx).* = Pair(K, V)(key: k, value: v);
258
- return MapCollision(K, V)(hash: coll.hash, _pairs_ptr: new_ptr, _pairs_len: coll._pairs_len);
270
+ (new_ptr &+ idx).* = Pair(K, V)(key : k, value : v);
271
+ return(MapCollision(K, V)(hash : coll.hash, _pairs_ptr : new_ptr, _pairs_len : coll._pairs_len));
259
272
  },
260
273
  .None => {
261
274
  new_len := (coll._pairs_len + usize(1));
262
275
  new_ptr := _alloc_pairs(K, V, new_len);
263
276
  _copy_pairs(K, V, new_ptr, coll._pairs_ptr, usize(0), usize(0), coll._pairs_len);
264
- consume((new_ptr &+ coll._pairs_len).* = Pair(K, V)(key: k, value: v));
265
- return MapCollision(K, V)(hash: coll.hash, _pairs_ptr: new_ptr, _pairs_len: new_len);
277
+ consume((new_ptr &+ coll._pairs_len).* = Pair(K, V)(key : k, value : v));
278
+ return(MapCollision(K, V)(hash : coll.hash, _pairs_ptr : new_ptr, _pairs_len : new_len));
266
279
  }
267
280
  )
268
281
  );
269
-
270
- _coll_without_key :: (fn(comptime(K) : Type, comptime(V) : Type, coll: MapCollision(K, V), k: K, where(K <: (Eq(K), Hash, Send), V <: Send)) -> Option(MapCollision(K, V)))(
271
- match(_coll_find_key(K, V, coll, k),
282
+ _coll_without_key :: (fn(comptime(K) : Type, comptime(V) : Type, coll : MapCollision(K, V), k : K, where(K <: (Eq(K), Hash, Send), V <: Send)) -> Option(MapCollision(K, V)))(
283
+ match(
284
+ _coll_find_key(K, V, coll, k),
272
285
  .Some(idx) => {
273
- if((coll._pairs_len <= usize(2)), {
274
- return .None;
286
+ if(coll._pairs_len <= usize(2), {
287
+ return(.None);
275
288
  });
276
289
  new_len := (coll._pairs_len - usize(1));
277
290
  new_ptr := _alloc_pairs(K, V, new_len);
278
- if((usize(idx) > usize(0)), {
291
+ if(usize(idx) > usize(0), {
279
292
  _copy_pairs(K, V, new_ptr, coll._pairs_ptr, usize(0), usize(0), idx);
280
293
  });
281
294
  remaining := (coll._pairs_len - (idx + usize(1)));
282
- if((usize(remaining) > usize(0)), {
283
- _copy_pairs(K, V, new_ptr, coll._pairs_ptr, idx, (idx + usize(1)), remaining);
295
+ if(usize(remaining) > usize(0), {
296
+ _copy_pairs(K, V, new_ptr, coll._pairs_ptr, idx, idx + usize(1), remaining);
284
297
  });
285
- return .Some(MapCollision(K, V)(hash: coll.hash, _pairs_ptr: new_ptr, _pairs_len: new_len));
298
+ return(.Some(MapCollision(K, V)(hash : coll.hash, _pairs_ptr : new_ptr, _pairs_len : new_len)));
286
299
  },
287
- .None => .Some(coll)
300
+ .None =>.Some(coll)
288
301
  )
289
302
  );
290
-
291
303
  // -- Core HAMT algorithms --
292
-
293
- _fragment :: (fn(hash: u64, shift: usize) -> u32)(
304
+ _fragment :: (fn(hash : u64, shift : usize) -> u32)(
294
305
  u32((hash >> u64(shift)) & MASK)
295
306
  );
296
-
297
- _bit_for :: (fn(frag: u32) -> u32)(
298
- (u32(1) << frag)
307
+ _bit_for :: (fn(frag : u32) -> u32)(
308
+ u32(1) << frag
299
309
  );
300
-
301
- _make_branch :: (fn(
302
- comptime(K) : Type,
303
- comptime(V) : Type,
304
- shift: usize,
305
- node1: MapNode(K, V),
306
- hash1: u64,
307
- node2: MapNode(K, V),
308
- hash2: u64,
309
- where(K <: (Eq(K), Hash, Send), V <: Send)
310
- ) -> MapNode(K, V))({
310
+ _make_branch :: (
311
+ fn(
312
+ comptime(K) : Type,
313
+ comptime(V) : Type,
314
+ shift : usize,
315
+ node1 : MapNode(K, V),
316
+ hash1 : u64,
317
+ node2 : MapNode(K, V),
318
+ hash2 : u64,
319
+ where(K <: (Eq(K), Hash, Send), V <: Send)
320
+ ) -> MapNode(K, V)
321
+ )({
311
322
  frag1 := _fragment(hash1, shift);
312
323
  frag2 := _fragment(hash2, shift);
313
- if((frag1 == frag2), {
314
- child := recur(K, V, (shift + BITS), node1, hash1, node2, hash2);
324
+ if(frag1 == frag2, {
325
+ child := recur(K, V, shift + BITS, node1, hash1, node2, hash2);
315
326
  ptr := _alloc_map_children(K, V, u8(1));
316
327
  consume((*(MapNode(K, V))(ptr)).* = child);
317
- return .Branch(MapBranch(K, V)(
318
- bitmap: _bit_for(frag1),
319
- _children_ptr: ptr,
320
- _children_len: u8(1)
321
- ));
328
+ return(
329
+ .Branch(
330
+ MapBranch(K, V)(
331
+ bitmap : _bit_for(frag1),
332
+ _children_ptr : ptr,
333
+ _children_len : u8(1)
334
+ )
335
+ )
336
+ );
322
337
  });
323
338
  bit1 := _bit_for(frag1);
324
339
  bit2 := _bit_for(frag2);
325
340
  bitmap := (bit1 | bit2);
326
341
  ptr := _alloc_map_children(K, V, u8(2));
327
342
  children := *(MapNode(K, V))(ptr);
328
- if((frag1 < frag2), {
343
+ if(frag1 < frag2, {
329
344
  consume(children.* = node1);
330
345
  consume((children &+ usize(1)).* = node2);
331
346
  }, {
332
347
  consume(children.* = node2);
333
348
  consume((children &+ usize(1)).* = node1);
334
349
  });
335
- .Branch(MapBranch(K, V)(
336
- bitmap: bitmap,
337
- _children_ptr: ptr,
338
- _children_len: u8(2)
339
- ))
350
+ .Branch(
351
+ MapBranch(K, V)(
352
+ bitmap : bitmap,
353
+ _children_ptr : ptr,
354
+ _children_len : u8(2)
355
+ )
356
+ )
340
357
  });
341
-
342
358
  InsertResult :: (fn(comptime(K) : Type, comptime(V) : Type, where(K <: (Eq(K), Hash, Send), V <: Send)) -> comptime(Type))(
343
- struct(node: MapNode(K, V), added: bool)
359
+ struct(node : MapNode(K, V), added : bool)
344
360
  );
345
-
346
- _node_insert :: (fn(
347
- comptime(K) : Type,
348
- comptime(V) : Type,
349
- node: MapNode(K, V),
350
- shift: usize,
351
- hash: u64,
352
- k: K,
353
- v: V,
354
- where(K <: (Eq(K), Hash, Send), V <: Send)
355
- ) -> InsertResult(K, V))(
356
- match(node,
361
+ _node_insert :: (
362
+ fn(
363
+ comptime(K) : Type,
364
+ comptime(V) : Type,
365
+ node : MapNode(K, V),
366
+ shift : usize,
367
+ hash : u64,
368
+ k : K,
369
+ v : V,
370
+ where(K <: (Eq(K), Hash, Send), V <: Send)
371
+ ) -> InsertResult(K, V)
372
+ )(
373
+ match(
374
+ node,
357
375
  .Leaf(leaf) => {
358
- if((leaf.hash == hash), {
359
- if((leaf.key == k), {
360
- return InsertResult(K, V)(
361
- node: .Leaf(MapLeaf(K, V)(hash: hash, key: k, value: v)),
362
- added: false
376
+ if(leaf.hash == hash, {
377
+ if(leaf.key == k, {
378
+ return(
379
+ InsertResult(K, V)(
380
+ node :.Leaf(MapLeaf(K, V)(hash : hash, key : k, value : v)),
381
+ added : false
382
+ )
363
383
  );
364
384
  });
365
385
  pairs_ptr := _alloc_pairs(K, V, usize(2));
366
- consume(pairs_ptr.* = Pair(K, V)(key: leaf.key, value: leaf.value));
367
- consume((pairs_ptr &+ usize(1)).* = Pair(K, V)(key: k, value: v));
368
- return InsertResult(K, V)(
369
- node: .Collision(MapCollision(K, V)(hash: hash, _pairs_ptr: pairs_ptr, _pairs_len: usize(2))),
370
- added: true
386
+ consume(pairs_ptr.* = Pair(K, V)(key : leaf.key, value : leaf.value));
387
+ consume((pairs_ptr &+ usize(1)).* = Pair(K, V)(key : k, value : v));
388
+ return(
389
+ InsertResult(K, V)(
390
+ node :.Collision(MapCollision(K, V)(hash : hash, _pairs_ptr : pairs_ptr, _pairs_len : usize(2))),
391
+ added : true
392
+ )
371
393
  );
372
394
  });
373
- new_leaf := MapNode(K, V).Leaf(MapLeaf(K, V)(hash: hash, key: k, value: v));
374
- return InsertResult(K, V)(
375
- node: _make_branch(K, V, shift, node, leaf.hash, new_leaf, hash),
376
- added: true
395
+ new_leaf := MapNode(K, V).Leaf(MapLeaf(K, V)(hash : hash, key : k, value : v));
396
+ return(
397
+ InsertResult(K, V)(
398
+ node : _make_branch(K, V, shift, node, leaf.hash, new_leaf, hash),
399
+ added : true
400
+ )
377
401
  );
378
402
  },
379
403
  .Branch(branch) => {
@@ -382,147 +406,167 @@ _node_insert :: (fn(
382
406
  if(_branch_has_child(K, V, branch, bit), {
383
407
  idx := _branch_index_for(K, V, branch, bit);
384
408
  child := _branch_child_at(K, V, branch, idx);
385
- result := recur(K, V, child, (shift + BITS), hash, k, v);
386
- return InsertResult(K, V)(
387
- node: .Branch(_branch_replace_child(K, V, branch, bit, result.node)),
388
- added: result.added
409
+ result := recur(K, V, child, shift + BITS, hash, k, v);
410
+ return(
411
+ InsertResult(K, V)(
412
+ node :.Branch(_branch_replace_child(K, V, branch, bit, result.node)),
413
+ added : result.added
414
+ )
389
415
  );
390
416
  });
391
- new_leaf_node := MapNode(K, V).Leaf(MapLeaf(K, V)(hash: hash, key: k, value: v));
392
- return InsertResult(K, V)(
393
- node: .Branch(_branch_with_child(K, V, branch, bit, new_leaf_node)),
394
- added: true
417
+ new_leaf_node := MapNode(K, V).Leaf(MapLeaf(K, V)(hash : hash, key : k, value : v));
418
+ return(
419
+ InsertResult(K, V)(
420
+ node :.Branch(_branch_with_child(K, V, branch, bit, new_leaf_node)),
421
+ added : true
422
+ )
395
423
  );
396
424
  },
397
425
  .Collision(coll) => {
398
- if((coll.hash == hash), {
426
+ if(coll.hash == hash, {
399
427
  found := _coll_find_key(K, V, coll, k);
400
- return InsertResult(K, V)(
401
- node: .Collision(_coll_with_pair(K, V, coll, k, v)),
402
- added: found.is_none()
428
+ return(
429
+ InsertResult(K, V)(
430
+ node :.Collision(_coll_with_pair(K, V, coll, k, v)),
431
+ added : found.is_none()
432
+ )
403
433
  );
404
434
  });
405
435
  coll_node := MapNode(K, V).Collision(coll);
406
- new_leaf := MapNode(K, V).Leaf(MapLeaf(K, V)(hash: hash, key: k, value: v));
407
- return InsertResult(K, V)(
408
- node: _make_branch(K, V, shift, coll_node, coll.hash, new_leaf, hash),
409
- added: true
436
+ new_leaf := MapNode(K, V).Leaf(MapLeaf(K, V)(hash : hash, key : k, value : v));
437
+ return(
438
+ InsertResult(K, V)(
439
+ node : _make_branch(K, V, shift, coll_node, coll.hash, new_leaf, hash),
440
+ added : true
441
+ )
410
442
  );
411
443
  }
412
444
  )
413
445
  );
414
-
415
- _node_get :: (fn(
416
- comptime(K) : Type,
417
- comptime(V) : Type,
418
- node: MapNode(K, V),
419
- shift: usize,
420
- hash: u64,
421
- k: K,
422
- where(K <: (Eq(K), Hash, Send), V <: Send)
423
- ) -> Option(V))(
424
- match(node,
446
+ _node_get :: (
447
+ fn(
448
+ comptime(K) : Type,
449
+ comptime(V) : Type,
450
+ node : MapNode(K, V),
451
+ shift : usize,
452
+ hash : u64,
453
+ k : K,
454
+ where(K <: (Eq(K), Hash, Send), V <: Send)
455
+ ) -> Option(V)
456
+ )(
457
+ match(
458
+ node,
425
459
  .Leaf(leaf) => cond(
426
- ((leaf.hash == hash) && (leaf.key == k)) => .Some(leaf.value),
427
- true => .None
460
+ ((leaf.hash == hash) && (leaf.key == k)) =>.Some(leaf.value),
461
+ true =>.None
428
462
  ),
429
463
  .Branch(branch) => {
430
464
  frag := _fragment(hash, shift);
431
465
  bit := _bit_for(frag);
432
466
  if(!(_branch_has_child(K, V, branch, bit)), {
433
- return .None;
467
+ return(.None);
434
468
  });
435
469
  idx := _branch_index_for(K, V, branch, bit);
436
- return recur(K, V, _branch_child_at(K, V, branch, idx), (shift + BITS), hash, k);
470
+ return(recur(K, V, _branch_child_at(K, V, branch, idx), shift + BITS, hash, k));
437
471
  },
438
472
  .Collision(coll) => {
439
- if((coll.hash != hash), {
440
- return .None;
473
+ if(coll.hash != hash, {
474
+ return(.None);
441
475
  });
442
476
  i := usize(0);
443
- while (i < coll._pairs_len), (i = (i + usize(1))), {
477
+ while(i < coll._pairs_len, i = (i + usize(1)), {
444
478
  pair := (coll._pairs_ptr &+ i).*;
445
- if((pair.key == k), {
446
- return .Some(pair.value);
479
+ if(pair.key == k, {
480
+ return(.Some(pair.value));
447
481
  });
448
- };
482
+ });
449
483
  .None
450
484
  }
451
485
  )
452
486
  );
453
-
454
487
  RemoveResult :: (fn(comptime(K) : Type, comptime(V) : Type, where(K <: (Eq(K), Hash, Send), V <: Send)) -> comptime(Type))(
455
- struct(node: Option(MapNode(K, V)), removed: bool)
488
+ struct(node : Option(MapNode(K, V)), removed : bool)
456
489
  );
457
-
458
- _node_remove :: (fn(
459
- comptime(K) : Type,
460
- comptime(V) : Type,
461
- node: MapNode(K, V),
462
- shift: usize,
463
- hash: u64,
464
- k: K,
465
- where(K <: (Eq(K), Hash, Send), V <: Send)
466
- ) -> RemoveResult(K, V))(
467
- match(node,
490
+ _node_remove :: (
491
+ fn(
492
+ comptime(K) : Type,
493
+ comptime(V) : Type,
494
+ node : MapNode(K, V),
495
+ shift : usize,
496
+ hash : u64,
497
+ k : K,
498
+ where(K <: (Eq(K), Hash, Send), V <: Send)
499
+ ) -> RemoveResult(K, V)
500
+ )(
501
+ match(
502
+ node,
468
503
  .Leaf(leaf) => cond(
469
504
  ((leaf.hash == hash) && (leaf.key == k)) =>
470
- RemoveResult(K, V)(node: .None, removed: true),
505
+ RemoveResult(K, V)(node :.None, removed : true),
471
506
  true =>
472
- RemoveResult(K, V)(node: .Some(node), removed: false)
507
+ RemoveResult(K, V)(node :.Some(node), removed : false)
473
508
  ),
474
509
  .Branch(branch) => {
475
510
  frag := _fragment(hash, shift);
476
511
  bit := _bit_for(frag);
477
512
  if(!(_branch_has_child(K, V, branch, bit)), {
478
- return RemoveResult(K, V)(node: .Some(node), removed: false);
513
+ return(RemoveResult(K, V)(node :.Some(node), removed : false));
479
514
  });
480
515
  idx := _branch_index_for(K, V, branch, bit);
481
516
  child := _branch_child_at(K, V, branch, idx);
482
- sub := recur(K, V, child, (shift + BITS), hash, k);
517
+ sub := recur(K, V, child, shift + BITS, hash, k);
483
518
  if(!(sub.removed), {
484
- return RemoveResult(K, V)(node: .Some(node), removed: false);
519
+ return(RemoveResult(K, V)(node :.Some(node), removed : false));
485
520
  });
486
- match(sub.node,
521
+ match(
522
+ sub.node,
487
523
  .Some(new_child) => {
488
- return RemoveResult(K, V)(
489
- node: .Some(.Branch(_branch_replace_child(K, V, branch, bit, new_child))),
490
- removed: true
524
+ return(
525
+ RemoveResult(K, V)(
526
+ node :.Some(.Branch(_branch_replace_child(K, V, branch, bit, new_child))),
527
+ removed : true
528
+ )
491
529
  );
492
530
  },
493
531
  .None => {
494
- if((branch._children_len == u8(1)), {
495
- return RemoveResult(K, V)(node: .None, removed: true);
532
+ if(branch._children_len == u8(1), {
533
+ return(RemoveResult(K, V)(node :.None, removed : true));
496
534
  });
497
- if((branch._children_len == u8(2)), {
535
+ if(branch._children_len == u8(2), {
498
536
  other_idx := cond((idx == u8(0)) => u8(1), true => u8(0));
499
537
  remaining := _branch_child_at(K, V, branch, other_idx);
500
- match(remaining,
538
+ match(
539
+ remaining,
501
540
  .Leaf(_) => {
502
- return RemoveResult(K, V)(node: .Some(remaining), removed: true);
541
+ return(RemoveResult(K, V)(node :.Some(remaining), removed : true));
503
542
  },
504
543
  _ => ()
505
544
  );
506
545
  });
507
- return RemoveResult(K, V)(
508
- node: .Some(.Branch(_branch_without_child(K, V, branch, bit))),
509
- removed: true
546
+ return(
547
+ RemoveResult(K, V)(
548
+ node :.Some(.Branch(_branch_without_child(K, V, branch, bit))),
549
+ removed : true
550
+ )
510
551
  );
511
552
  }
512
553
  );
513
554
  },
514
555
  .Collision(coll) => {
515
- if((coll.hash != hash), {
516
- return RemoveResult(K, V)(node: .Some(node), removed: false);
556
+ if(coll.hash != hash, {
557
+ return(RemoveResult(K, V)(node :.Some(node), removed : false));
517
558
  });
518
- match(_coll_without_key(K, V, coll, k),
559
+ match(
560
+ _coll_without_key(K, V, coll, k),
519
561
  .Some(new_coll) => {
520
- if((new_coll._pairs_len == coll._pairs_len), {
521
- return RemoveResult(K, V)(node: .Some(node), removed: false);
562
+ if(new_coll._pairs_len == coll._pairs_len, {
563
+ return(RemoveResult(K, V)(node :.Some(node), removed : false));
522
564
  });
523
- return RemoveResult(K, V)(
524
- node: .Some(.Collision(new_coll)),
525
- removed: true
565
+ return(
566
+ RemoveResult(K, V)(
567
+ node :.Some(.Collision(new_coll)),
568
+ removed : true
569
+ )
526
570
  );
527
571
  },
528
572
  .None => {
@@ -531,183 +575,198 @@ _node_remove :: (fn(
531
575
  true => usize(0)
532
576
  );
533
577
  remaining_pair := (coll._pairs_ptr &+ remaining_idx).*;
534
- return RemoveResult(K, V)(
535
- node: .Some(.Leaf(MapLeaf(K, V)(hash: coll.hash, key: remaining_pair.key, value: remaining_pair.value))),
536
- removed: true
578
+ return(
579
+ RemoveResult(K, V)(
580
+ node :.Some(.Leaf(MapLeaf(K, V)(hash : coll.hash, key : remaining_pair.key, value : remaining_pair.value))),
581
+ removed : true
582
+ )
537
583
  );
538
584
  }
539
585
  );
540
586
  }
541
587
  )
542
588
  );
543
-
544
589
  // -- Collection helpers --
545
-
546
- { List } :: import "./list.yo";
547
-
590
+ { List } :: import("./list.yo");
548
591
  // -- Eq trait --
549
-
550
- _merge_from_node :: (fn(
551
- comptime(K) : Type,
552
- comptime(V) : Type,
553
- acc: Map(K, V),
554
- node: MapNode(K, V),
555
- where(K <: (Eq(K), Hash, Send), V <: Send)
556
- ) -> Map(K, V))(
557
- match(node,
592
+ _merge_from_node :: (
593
+ fn(
594
+ comptime(K) : Type,
595
+ comptime(V) : Type,
596
+ acc : Map(K, V),
597
+ node : MapNode(K, V),
598
+ where(K <: (Eq(K), Hash, Send), V <: Send)
599
+ ) -> Map(K, V)
600
+ )(
601
+ match(
602
+ node,
558
603
  .Leaf(leaf) => acc.insert(leaf.key, leaf.value),
559
604
  .Branch(branch) => {
560
605
  (result : Map(K, V)) = acc;
561
606
  i := u8(0);
562
- while (i < branch._children_len), (i = (i + u8(1))), {
607
+ while(i < branch._children_len, i = (i + u8(1)), {
563
608
  result = recur(K, V, result, _branch_child_at(K, V, branch, i));
564
- };
609
+ });
565
610
  result
566
611
  },
567
612
  .Collision(coll) => {
568
613
  (result : Map(K, V)) = acc;
569
614
  i := usize(0);
570
- while (i < coll._pairs_len), (i = (i + usize(1))), {
615
+ while(i < coll._pairs_len, i = (i + usize(1)), {
571
616
  pair := (coll._pairs_ptr &+ i).*;
572
617
  result = result.insert(pair.key, pair.value);
573
- };
618
+ });
574
619
  result
575
620
  }
576
621
  )
577
622
  );
578
-
579
- _collect_keys :: (fn(
580
- comptime(K) : Type,
581
- comptime(V) : Type,
582
- node: MapNode(K, V),
583
- acc: List(K),
584
- where(K <: (Eq(K), Hash, Send), V <: Send)
585
- ) -> List(K))(
586
- match(node,
623
+ _collect_keys :: (
624
+ fn(
625
+ comptime(K) : Type,
626
+ comptime(V) : Type,
627
+ node : MapNode(K, V),
628
+ acc : List(K),
629
+ where(K <: (Eq(K), Hash, Send), V <: Send)
630
+ ) -> List(K)
631
+ )(
632
+ match(
633
+ node,
587
634
  .Leaf(leaf) => acc.prepend(leaf.key),
588
635
  .Branch(branch) => {
589
636
  (result : List(K)) = acc;
590
637
  i := u8(0);
591
- while (i < branch._children_len), (i = (i + u8(1))), {
638
+ while(i < branch._children_len, i = (i + u8(1)), {
592
639
  result = recur(K, V, _branch_child_at(K, V, branch, i), result);
593
- };
640
+ });
594
641
  result
595
642
  },
596
643
  .Collision(coll) => {
597
644
  (result : List(K)) = acc;
598
645
  i := usize(0);
599
- while (i < coll._pairs_len), (i = (i + usize(1))), {
646
+ while(i < coll._pairs_len, i = (i + usize(1)), {
600
647
  result = result.prepend((coll._pairs_ptr &+ i).*.key);
601
- };
648
+ });
602
649
  result
603
650
  }
604
651
  )
605
652
  );
606
-
607
- _collect_values :: (fn(
608
- comptime(K) : Type,
609
- comptime(V) : Type,
610
- node: MapNode(K, V),
611
- acc: List(V),
612
- where(K <: (Eq(K), Hash, Send), V <: Send)
613
- ) -> List(V))(
614
- match(node,
653
+ _collect_values :: (
654
+ fn(
655
+ comptime(K) : Type,
656
+ comptime(V) : Type,
657
+ node : MapNode(K, V),
658
+ acc : List(V),
659
+ where(K <: (Eq(K), Hash, Send), V <: Send)
660
+ ) -> List(V)
661
+ )(
662
+ match(
663
+ node,
615
664
  .Leaf(leaf) => acc.prepend(leaf.value),
616
665
  .Branch(branch) => {
617
666
  (result : List(V)) = acc;
618
667
  i := u8(0);
619
- while (i < branch._children_len), (i = (i + u8(1))), {
668
+ while(i < branch._children_len, i = (i + u8(1)), {
620
669
  result = recur(K, V, _branch_child_at(K, V, branch, i), result);
621
- };
670
+ });
622
671
  result
623
672
  },
624
673
  .Collision(coll) => {
625
674
  (result : List(V)) = acc;
626
675
  i := usize(0);
627
- while (i < coll._pairs_len), (i = (i + usize(1))), {
676
+ while(i < coll._pairs_len, i = (i + usize(1)), {
628
677
  result = result.prepend((coll._pairs_ptr &+ i).*.value);
629
- };
678
+ });
630
679
  result
631
680
  }
632
681
  )
633
682
  );
634
-
635
- _collect_entries :: (fn(
636
- comptime(K) : Type,
637
- comptime(V) : Type,
638
- node: MapNode(K, V),
639
- acc: List(Pair(K, V)),
640
- where(K <: (Eq(K), Hash, Send), V <: Send)
641
- ) -> List(Pair(K, V)))(
642
- match(node,
643
- .Leaf(leaf) => acc.prepend(Pair(K, V)(key: leaf.key, value: leaf.value)),
683
+ _collect_entries :: (
684
+ fn(
685
+ comptime(K) : Type,
686
+ comptime(V) : Type,
687
+ node : MapNode(K, V),
688
+ acc : List(Pair(K, V)),
689
+ where(K <: (Eq(K), Hash, Send), V <: Send)
690
+ ) -> List(Pair(K, V))
691
+ )(
692
+ match(
693
+ node,
694
+ .Leaf(leaf) => acc.prepend(Pair(K, V)(key : leaf.key, value : leaf.value)),
644
695
  .Branch(branch) => {
645
696
  (result : List(Pair(K, V))) = acc;
646
697
  i := u8(0);
647
- while (i < branch._children_len), (i = (i + u8(1))), {
698
+ while(i < branch._children_len, i = (i + u8(1)), {
648
699
  result = recur(K, V, _branch_child_at(K, V, branch, i), result);
649
- };
700
+ });
650
701
  result
651
702
  },
652
703
  .Collision(coll) => {
653
704
  (result : List(Pair(K, V))) = acc;
654
705
  i := usize(0);
655
- while (i < coll._pairs_len), (i = (i + usize(1))), {
706
+ while(i < coll._pairs_len, i = (i + usize(1)), {
656
707
  result = result.prepend((coll._pairs_ptr &+ i).*);
657
- };
708
+ });
658
709
  result
659
710
  }
660
711
  )
661
712
  );
662
-
663
- _map_values_node :: (fn(
664
- comptime(K) : Type,
665
- comptime(V) : Type,
666
- comptime(U) : Type,
667
- node: MapNode(K, V),
668
- f: Impl(Fn(a: V) -> U),
669
- where(K <: (Eq(K), Hash, Send), V <: Send, U <: Send)
670
- ) -> MapNode(K, U))(
671
- match(node,
672
- .Leaf(leaf) => .Leaf(MapLeaf(K, U)(hash: leaf.hash, key: leaf.key, value: f(leaf.value))),
713
+ _map_values_node :: (
714
+ fn(
715
+ comptime(K) : Type,
716
+ comptime(V) : Type,
717
+ comptime(U) : Type,
718
+ node : MapNode(K, V),
719
+ f : Impl(Fn(a : V) -> U),
720
+ where(K <: (Eq(K), Hash, Send), V <: Send, U <: Send)
721
+ ) -> MapNode(K, U)
722
+ )(
723
+ match(
724
+ node,
725
+ .Leaf(leaf) =>.Leaf(MapLeaf(K, U)(hash : leaf.hash, key : leaf.key, value : f(leaf.value))),
673
726
  .Branch(branch) => {
674
727
  new_ptr := _alloc_map_children(K, U, branch._children_len);
675
728
  new_children := *(MapNode(K, U))(new_ptr);
676
729
  i := u8(0);
677
- while (i < branch._children_len), (i = (i + u8(1))), {
730
+ while(i < branch._children_len, i = (i + u8(1)), {
678
731
  consume((new_children &+ usize(i)).* = recur(K, V, U, _branch_child_at(K, V, branch, i), f));
679
- };
680
- .Branch(MapBranch(K, U)(
681
- bitmap: branch.bitmap,
682
- _children_ptr: new_ptr,
683
- _children_len: branch._children_len
684
- ))
732
+ });
733
+ .Branch(
734
+ MapBranch(K, U)(
735
+ bitmap : branch.bitmap,
736
+ _children_ptr : new_ptr,
737
+ _children_len : branch._children_len
738
+ )
739
+ )
685
740
  },
686
741
  .Collision(coll) => {
687
742
  new_ptr := _alloc_pairs(K, U, coll._pairs_len);
688
743
  i := usize(0);
689
- while (i < coll._pairs_len), (i = (i + usize(1))), {
744
+ while(i < coll._pairs_len, i = (i + usize(1)), {
690
745
  old_pair := (coll._pairs_ptr &+ i).*;
691
- consume((new_ptr &+ i).* = Pair(K, U)(key: old_pair.key, value: f(old_pair.value)));
692
- };
693
- .Collision(MapCollision(K, U)(
694
- hash: coll.hash,
695
- _pairs_ptr: new_ptr,
696
- _pairs_len: coll._pairs_len
697
- ))
746
+ consume((new_ptr &+ i).* = Pair(K, U)(key : old_pair.key, value : f(old_pair.value)));
747
+ });
748
+ .Collision(
749
+ MapCollision(K, U)(
750
+ hash : coll.hash,
751
+ _pairs_ptr : new_ptr,
752
+ _pairs_len : coll._pairs_len
753
+ )
754
+ )
698
755
  }
699
756
  )
700
757
  );
701
-
702
- _filter_from_node :: (fn(
703
- comptime(K) : Type,
704
- comptime(V) : Type,
705
- acc: Map(K, V),
706
- node: MapNode(K, V),
707
- f: Impl(Fn(k: K, v: V) -> bool),
708
- where(K <: (Eq(K), Hash, Send), V <: Send)
709
- ) -> Map(K, V))(
710
- match(node,
758
+ _filter_from_node :: (
759
+ fn(
760
+ comptime(K) : Type,
761
+ comptime(V) : Type,
762
+ acc : Map(K, V),
763
+ node : MapNode(K, V),
764
+ f : Impl(Fn(k : K, v : V) -> bool),
765
+ where(K <: (Eq(K), Hash, Send), V <: Send)
766
+ ) -> Map(K, V)
767
+ )(
768
+ match(
769
+ node,
711
770
  .Leaf(leaf) => cond(
712
771
  f(leaf.key, leaf.value) => acc.insert(leaf.key, leaf.value),
713
772
  true => acc
@@ -715,55 +774,59 @@ _filter_from_node :: (fn(
715
774
  .Branch(branch) => {
716
775
  (result : Map(K, V)) = acc;
717
776
  i := u8(0);
718
- while (i < branch._children_len), (i = (i + u8(1))), {
777
+ while(i < branch._children_len, i = (i + u8(1)), {
719
778
  result = recur(K, V, result, _branch_child_at(K, V, branch, i), f);
720
- };
779
+ });
721
780
  result
722
781
  },
723
782
  .Collision(coll) => {
724
783
  (result : Map(K, V)) = acc;
725
784
  i := usize(0);
726
- while (i < coll._pairs_len), (i = (i + usize(1))), {
785
+ while(i < coll._pairs_len, i = (i + usize(1)), {
727
786
  pair := (coll._pairs_ptr &+ i).*;
728
787
  if(f(pair.key, pair.value), {
729
788
  result = result.insert(pair.key, pair.value);
730
789
  });
731
- };
790
+ });
732
791
  result
733
792
  }
734
793
  )
735
794
  );
736
-
737
- _all_entries_match :: (fn(
738
- comptime(K) : Type,
739
- comptime(V) : Type,
740
- node: MapNode(K, V),
741
- target: Map(K, V),
742
- where(K <: (Eq(K), Hash, Send), V <: (Send, Eq(V)))
743
- ) -> bool)(
744
- match(node,
745
- .Leaf(leaf) => match(target.get(leaf.key),
795
+ _all_entries_match :: (
796
+ fn(
797
+ comptime(K) : Type,
798
+ comptime(V) : Type,
799
+ node : MapNode(K, V),
800
+ target : Map(K, V),
801
+ where(K <: (Eq(K), Hash, Send), V <: (Send, Eq(V)))
802
+ ) -> bool
803
+ )(
804
+ match(
805
+ node,
806
+ .Leaf(leaf) => match(
807
+ target.get(leaf.key),
746
808
  .Some(tv) => (tv == leaf.value),
747
809
  .None => false
748
810
  ),
749
811
  .Branch(branch) => {
750
812
  i := u8(0);
751
813
  (ok : bool) = true;
752
- while ((i < branch._children_len) && ok), (i = (i + u8(1))), {
814
+ while((i < branch._children_len) && ok, i = (i + u8(1)), {
753
815
  if(!(recur(K, V, _branch_child_at(K, V, branch, i), target)), {
754
816
  ok = false;
755
817
  });
756
- };
818
+ });
757
819
  ok
758
820
  },
759
821
  .Collision(coll) => {
760
822
  i := usize(0);
761
823
  (ok : bool) = true;
762
- while ((i < coll._pairs_len) && ok), (i = (i + usize(1))), {
824
+ while((i < coll._pairs_len) && ok, i = (i + usize(1)), {
763
825
  pair := (coll._pairs_ptr &+ i).*;
764
- match(target.get(pair.key),
826
+ match(
827
+ target.get(pair.key),
765
828
  .Some(tv) => {
766
- if((tv != pair.value), {
829
+ if(tv != pair.value, {
767
830
  ok = false;
768
831
  });
769
832
  },
@@ -771,113 +834,111 @@ _all_entries_match :: (fn(
771
834
  ok = false;
772
835
  }
773
836
  );
774
- };
837
+ });
775
838
  ok
776
839
  }
777
840
  )
778
841
  );
779
-
780
842
  // -- Public Map API --
781
-
782
- impl(forall(K : Type, V : Type), where(K <: (Eq(K), Hash, Send), V <: Send), Map(K, V),
843
+ impl(
844
+ forall(K : Type, V : Type),
845
+ where(K <: (Eq(K), Hash, Send), V <: Send),
846
+ Map(K, V),
783
847
  /// Create an empty map.
784
848
  new : (fn() -> Self)(
785
- Self(_root: .None, _len: usize(0))
849
+ Self(_root :.None, _len : usize(0))
786
850
  ),
787
-
788
851
  /// Number of key-value entries.
789
- len : (fn(self: Self) -> usize)(self._len),
790
-
852
+ len : (fn(self : Self) -> usize)(self._len),
791
853
  /// Check if the map is empty.
792
- is_empty : (fn(self: Self) -> bool)((self._len == usize(0))),
793
-
854
+ is_empty : (fn(self : Self) -> bool)(self._len == usize(0)),
794
855
  /// Look up the value associated with `key`.
795
- get : (fn(self: Self, key: K) -> Option(V))(
796
- match(self._root,
856
+ get : (fn(self : Self, key : K) -> Option(V))(
857
+ match(
858
+ self._root,
797
859
  .Some(root) => _node_get(K, V, root, usize(0), key.hash(), key),
798
- .None => .None
860
+ .None =>.None
799
861
  )
800
862
  ),
801
-
802
863
  /// Check if the map contains `key`.
803
- contains_key : (fn(self: Self, key: K) -> bool)(
864
+ contains_key : (fn(self : Self, key : K) -> bool)(
804
865
  self.get(key).is_some()
805
866
  ),
806
-
807
867
  /// Return a new map with `key` mapped to `value`.
808
- insert : (fn(self: Self, key: K, value: V) -> Self)(
809
- match(self._root,
868
+ insert : (fn(self : Self, key : K, value : V) -> Self)(
869
+ match(
870
+ self._root,
810
871
  .Some(root) => {
811
872
  result := _node_insert(K, V, root, usize(0), key.hash(), key, value);
812
873
  new_len := cond(result.added => (self._len + usize(1)), true => self._len);
813
- return Self(_root: .Some(result.node), _len: new_len);
874
+ return(Self(_root :.Some(result.node), _len : new_len));
814
875
  },
815
876
  .None => Self(
816
- _root: .Some(.Leaf(MapLeaf(K, V)(hash: key.hash(), key: key, value: value))),
817
- _len: usize(1)
877
+ _root :.Some(.Leaf(MapLeaf(K, V)(hash : key.hash(), key : key, value : value))),
878
+ _len : usize(1)
818
879
  )
819
880
  )
820
881
  ),
821
-
822
882
  /// Return a new map without `key`.
823
- remove : (fn(self: Self, key: K) -> Self)(
824
- match(self._root,
883
+ remove : (fn(self : Self, key : K) -> Self)(
884
+ match(
885
+ self._root,
825
886
  .Some(root) => {
826
887
  result := _node_remove(K, V, root, usize(0), key.hash(), key);
827
888
  if(!(result.removed), {
828
- return self;
889
+ return(self);
829
890
  });
830
891
  new_len := (self._len - usize(1));
831
- return Self(_root: result.node, _len: new_len);
892
+ return(Self(_root : result.node, _len : new_len));
832
893
  },
833
894
  .None => self
834
895
  )
835
896
  ),
836
-
837
897
  /// Merge another map into this one. Keys from `other` overwrite `self`.
838
- merge : (fn(self: Self, other: Self) -> Self)(
839
- match(other._root,
898
+ merge : (fn(self : Self, other : Self) -> Self)(
899
+ match(
900
+ other._root,
840
901
  .Some(root) => _merge_from_node(K, V, self, root),
841
902
  .None => self
842
903
  )
843
904
  ),
844
-
845
905
  /// Collect all keys into a list.
846
- keys : (fn(self: Self) -> List(K))(
847
- match(self._root,
906
+ keys : (fn(self : Self) -> List(K))(
907
+ match(
908
+ self._root,
848
909
  .Some(root) => _collect_keys(K, V, root, List(K).new()),
849
910
  .None => List(K).new()
850
911
  )
851
912
  ),
852
-
853
913
  /// Collect all values into a list.
854
- values : (fn(self: Self) -> List(V))(
855
- match(self._root,
914
+ values : (fn(self : Self) -> List(V))(
915
+ match(
916
+ self._root,
856
917
  .Some(root) => _collect_values(K, V, root, List(V).new()),
857
918
  .None => List(V).new()
858
919
  )
859
920
  ),
860
-
861
921
  /// Collect all entries into a list of pairs.
862
- entries : (fn(self: Self) -> List(Pair(K, V)))(
863
- match(self._root,
922
+ entries : (fn(self : Self) -> List(Pair(K, V)))(
923
+ match(
924
+ self._root,
864
925
  .Some(root) => _collect_entries(K, V, root, List(Pair(K, V)).new()),
865
926
  .None => List(Pair(K, V)).new()
866
927
  )
867
928
  ),
868
-
869
929
  /// Apply a function to each value, producing a new map.
870
- map_values : (fn(forall(U : Type), self: Self, f: Impl(Fn(a: V) -> U), where(U <: Send)) -> Map(K, U))(
871
- match(self._root,
872
- .Some(root) => Map(K, U)(_root: .Some(_map_values_node(K, V, U, root, f)), _len: self._len),
930
+ map_values : (fn(forall(U : Type), self : Self, f : Impl(Fn(a : V) -> U), where(U <: Send)) -> Map(K, U))(
931
+ match(
932
+ self._root,
933
+ .Some(root) => Map(K, U)(_root :.Some(_map_values_node(K, V, U, root, f)), _len : self._len),
873
934
  .None => Map(K, U).new()
874
935
  )
875
936
  ),
876
-
877
937
  /// Keep only entries where the predicate returns true.
878
- filter : (fn(self: Self, f: Impl(Fn(k: K, v: V) -> bool)) -> Self)({
938
+ filter : (fn(self : Self, f : Impl(Fn(k : K, v : V) -> bool)) -> Self)({
879
939
  (result : Self) = Self.new();
880
- match(self._root,
940
+ match(
941
+ self._root,
881
942
  .Some(root) => {
882
943
  result = _filter_from_node(K, V, result, root, f);
883
944
  },
@@ -885,33 +946,36 @@ impl(forall(K : Type, V : Type), where(K <: (Eq(K), Hash, Send), V <: Send), Map
885
946
  );
886
947
  result
887
948
  }),
888
-
889
949
  /// Create a map from a slice of key-value pairs.
890
- from_entries : (fn(pairs: Slice(Pair(K, V))) -> Self)({
950
+ from_entries : (fn(pairs : Slice(Pair(K, V))) -> Self)({
891
951
  (result : Self) = Self.new();
892
952
  i := usize(0);
893
953
  plen := pairs.len();
894
- while (i < plen), (i = (i + usize(1))), {
954
+ while(i < plen, i = (i + usize(1)), {
895
955
  p := (pairs.ptr() &+ i).*;
896
956
  result = result.insert(p.key, p.value);
897
- };
957
+ });
898
958
  result
899
959
  })
900
960
  );
901
-
902
- impl(forall(K : Type, V : Type), where(K <: (Eq(K), Hash, Send), V <: (Send, Eq(V))), Map(K, V), Eq(Map(K, V))(
903
- (==) : (fn(lhs: Self, rhs: Self) -> bool)({
904
- if((lhs._len != rhs._len), {
905
- return false;
906
- });
907
- if(((lhs._len == usize(0)) && (rhs._len == usize(0))), {
908
- return true;
909
- });
910
- match(lhs._root,
911
- .Some(root) => _all_entries_match(K, V, root, rhs),
912
- .None => true
913
- )
914
- })
915
- ));
916
-
917
- export Map, MapNode, MapBranch, MapLeaf, MapCollision, Pair, InsertResult, RemoveResult;
961
+ impl(
962
+ forall(K : Type, V : Type),
963
+ where(K <: (Eq(K), Hash, Send), V <: (Send, Eq(V))),
964
+ Map(K, V),
965
+ Eq(Map(K, V))(
966
+ (==) : (fn(lhs : Self, rhs : Self) -> bool)({
967
+ if(lhs._len != rhs._len, {
968
+ return(false);
969
+ });
970
+ if((lhs._len == usize(0)) && (rhs._len == usize(0)), {
971
+ return(true);
972
+ });
973
+ match(
974
+ lhs._root,
975
+ .Some(root) => _all_entries_match(K, V, root, rhs),
976
+ .None => true
977
+ )
978
+ })
979
+ )
980
+ );
981
+ export(Map, MapNode, MapBranch, MapLeaf, MapCollision, Pair, InsertResult, RemoveResult);