@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.
- package/.github/skills/yo-async-effects/SKILL.md +4 -4
- package/.github/skills/yo-async-effects/async-effects-recipes.md +34 -34
- package/.github/skills/yo-core-patterns/SKILL.md +1 -1
- package/.github/skills/yo-core-patterns/core-patterns-cheatsheet.md +26 -26
- package/.github/skills/yo-project-workflow/SKILL.md +6 -3
- package/.github/skills/yo-project-workflow/workflow-cheatsheet.md +34 -11
- package/.github/skills/yo-syntax/SKILL.md +7 -6
- package/.github/skills/yo-syntax/syntax-cheatsheet.md +73 -60
- package/.github/skills/yo-wasm-integration/wasm-integration-cheatsheet.md +3 -3
- package/README.md +10 -8
- package/out/cjs/index.cjs +456 -438
- package/out/cjs/yo-cli.cjs +576 -543
- package/out/cjs/yo-lsp.cjs +559 -532
- package/out/esm/index.mjs +281 -263
- package/out/types/src/formatter.d.ts +11 -0
- package/out/types/src/lsp/formatting.d.ts +2 -0
- package/out/types/src/tests/formatter.test.d.ts +1 -0
- package/out/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/std/alg/hash.yo +13 -21
- package/std/allocator.yo +25 -40
- package/std/async.yo +3 -7
- package/std/build.yo +105 -151
- package/std/cli/arg_parser.yo +184 -169
- package/std/collections/array_list.yo +350 -314
- package/std/collections/btree_map.yo +142 -131
- package/std/collections/deque.yo +132 -128
- package/std/collections/hash_map.yo +542 -566
- package/std/collections/hash_set.yo +623 -687
- package/std/collections/linked_list.yo +275 -293
- package/std/collections/ordered_map.yo +113 -85
- package/std/collections/priority_queue.yo +73 -73
- package/std/crypto/md5.yo +191 -95
- package/std/crypto/random.yo +56 -64
- package/std/crypto/sha256.yo +151 -107
- package/std/encoding/base64.yo +87 -81
- package/std/encoding/hex.yo +43 -50
- package/std/encoding/html.yo +56 -81
- package/std/encoding/html_char_utils.yo +7 -13
- package/std/encoding/html_entities.yo +2248 -2253
- package/std/encoding/json.yo +316 -224
- package/std/encoding/punycode.yo +86 -116
- package/std/encoding/toml.yo +67 -66
- package/std/encoding/utf16.yo +37 -44
- package/std/env.yo +62 -91
- package/std/error.yo +7 -15
- package/std/fmt/display.yo +5 -9
- package/std/fmt/index.yo +8 -14
- package/std/fmt/to_string.yo +330 -315
- package/std/fmt/writer.yo +58 -87
- package/std/fs/dir.yo +83 -102
- package/std/fs/file.yo +147 -180
- package/std/fs/metadata.yo +45 -78
- package/std/fs/temp.yo +55 -65
- package/std/fs/types.yo +27 -40
- package/std/fs/walker.yo +53 -68
- package/std/gc.yo +5 -8
- package/std/glob.yo +30 -43
- package/std/http/client.yo +107 -120
- package/std/http/http.yo +106 -96
- package/std/http/index.yo +4 -6
- package/std/imm/list.yo +88 -93
- package/std/imm/map.yo +528 -464
- package/std/imm/set.yo +52 -57
- package/std/imm/sorted_map.yo +340 -286
- package/std/imm/sorted_set.yo +57 -63
- package/std/imm/string.yo +404 -345
- package/std/imm/vec.yo +173 -181
- package/std/io/reader.yo +3 -6
- package/std/io/writer.yo +4 -8
- package/std/libc/assert.yo +5 -9
- package/std/libc/ctype.yo +32 -22
- package/std/libc/dirent.yo +26 -25
- package/std/libc/errno.yo +164 -90
- package/std/libc/fcntl.yo +52 -45
- package/std/libc/float.yo +66 -44
- package/std/libc/limits.yo +42 -33
- package/std/libc/math.yo +53 -82
- package/std/libc/signal.yo +72 -47
- package/std/libc/stdatomic.yo +217 -188
- package/std/libc/stdint.yo +5 -29
- package/std/libc/stdio.yo +5 -29
- package/std/libc/stdlib.yo +32 -39
- package/std/libc/string.yo +5 -23
- package/std/libc/sys/stat.yo +58 -56
- package/std/libc/time.yo +5 -19
- package/std/libc/unistd.yo +5 -20
- package/std/libc/wctype.yo +6 -9
- package/std/libc/windows.yo +26 -30
- package/std/log.yo +41 -55
- package/std/net/addr.yo +102 -97
- package/std/net/dns.yo +27 -28
- package/std/net/errors.yo +50 -49
- package/std/net/tcp.yo +113 -124
- package/std/net/udp.yo +55 -66
- package/std/os/env.yo +35 -33
- package/std/os/signal.yo +15 -25
- package/std/path.yo +276 -311
- package/std/prelude.yo +6304 -4315
- package/std/process/command.yo +87 -103
- package/std/process/index.yo +12 -31
- package/std/regex/compiler.yo +196 -95
- package/std/regex/flags.yo +58 -39
- package/std/regex/index.yo +157 -173
- package/std/regex/match.yo +20 -31
- package/std/regex/node.yo +134 -152
- package/std/regex/parser.yo +283 -259
- package/std/regex/unicode.yo +172 -202
- package/std/regex/vm.yo +155 -171
- package/std/string/index.yo +5 -7
- package/std/string/rune.yo +45 -55
- package/std/string/string.yo +937 -964
- package/std/string/string_builder.yo +94 -104
- package/std/string/unicode.yo +46 -64
- package/std/sync/channel.yo +72 -73
- package/std/sync/cond.yo +31 -36
- package/std/sync/mutex.yo +30 -32
- package/std/sync/once.yo +13 -16
- package/std/sync/rwlock.yo +26 -31
- package/std/sync/waitgroup.yo +20 -25
- package/std/sys/advise.yo +16 -24
- package/std/sys/bufio/buf_reader.yo +77 -93
- package/std/sys/bufio/buf_writer.yo +52 -65
- package/std/sys/clock.yo +4 -9
- package/std/sys/constants.yo +77 -61
- package/std/sys/copy.yo +4 -10
- package/std/sys/dir.yo +26 -43
- package/std/sys/dns.yo +41 -61
- package/std/sys/errors.yo +95 -103
- package/std/sys/events.yo +45 -57
- package/std/sys/externs.yo +319 -267
- package/std/sys/fallocate.yo +7 -11
- package/std/sys/fcntl.yo +14 -22
- package/std/sys/file.yo +26 -40
- package/std/sys/future.yo +5 -8
- package/std/sys/iov.yo +12 -25
- package/std/sys/lock.yo +12 -13
- package/std/sys/mmap.yo +38 -43
- package/std/sys/path.yo +3 -8
- package/std/sys/perm.yo +7 -21
- package/std/sys/pipe.yo +5 -12
- package/std/sys/process.yo +23 -29
- package/std/sys/seek.yo +10 -12
- package/std/sys/signal.yo +7 -13
- package/std/sys/signals.yo +52 -35
- package/std/sys/socket.yo +63 -58
- package/std/sys/socketpair.yo +3 -6
- package/std/sys/sockinfo.yo +11 -20
- package/std/sys/statfs.yo +11 -34
- package/std/sys/statx.yo +25 -52
- package/std/sys/sysinfo.yo +15 -20
- package/std/sys/tcp.yo +62 -92
- package/std/sys/temp.yo +5 -9
- package/std/sys/time.yo +5 -15
- package/std/sys/timer.yo +6 -11
- package/std/sys/tty.yo +10 -18
- package/std/sys/udp.yo +22 -39
- package/std/sys/umask.yo +3 -6
- package/std/sys/unix.yo +33 -52
- package/std/testing/bench.yo +49 -52
- package/std/thread.yo +10 -15
- package/std/time/datetime.yo +105 -89
- package/std/time/duration.yo +43 -56
- package/std/time/instant.yo +13 -18
- package/std/time/sleep.yo +5 -9
- package/std/url/index.yo +184 -209
- 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 :: (
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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 :: (
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
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 :: (
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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 :: (
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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 :: (
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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(
|
|
147
|
+
match(
|
|
148
|
+
malloc(sz),
|
|
137
149
|
.Some(p) => p,
|
|
138
150
|
.None => panic("imm.Map: allocation failed")
|
|
139
151
|
)
|
|
140
152
|
});
|
|
141
|
-
|
|
142
|
-
|
|
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
|
-
|
|
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))(
|
|
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))(
|
|
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 :: (
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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
|
|
173
|
-
src_ptr := *(MapNode(K, V))(
|
|
174
|
-
dst_ptr := *(MapNode(K, V))(
|
|
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(
|
|
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))(
|
|
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(
|
|
191
|
-
_copy_children(K, V, new_ptr, branch._children_ptr,
|
|
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(
|
|
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(
|
|
214
|
-
_copy_children(K, V, new_ptr, branch._children_ptr, idx,
|
|
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(
|
|
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
|
|
232
|
-
if((
|
|
233
|
-
return
|
|
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 :: (
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
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
|
|
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
|
-
|
|
253
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
271
|
-
|
|
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(
|
|
274
|
-
return
|
|
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(
|
|
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(
|
|
283
|
-
_copy_pairs(K, V, new_ptr, coll._pairs_ptr, idx,
|
|
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
|
|
298
|
+
return(.Some(MapCollision(K, V)(hash : coll.hash, _pairs_ptr : new_ptr, _pairs_len : new_len)));
|
|
286
299
|
},
|
|
287
|
-
.None
|
|
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
|
-
|
|
298
|
-
(u32(1) << frag)
|
|
307
|
+
_bit_for :: (fn(frag : u32) -> u32)(
|
|
308
|
+
u32(1) << frag
|
|
299
309
|
);
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
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(
|
|
314
|
-
child := recur(K, V,
|
|
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
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
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(
|
|
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(
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
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
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
) -> InsertResult(K, V)
|
|
356
|
-
|
|
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(
|
|
359
|
-
if(
|
|
360
|
-
return
|
|
361
|
-
|
|
362
|
-
|
|
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
|
|
369
|
-
|
|
370
|
-
|
|
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
|
|
375
|
-
|
|
376
|
-
|
|
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,
|
|
386
|
-
return
|
|
387
|
-
|
|
388
|
-
|
|
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
|
|
393
|
-
|
|
394
|
-
|
|
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(
|
|
426
|
+
if(coll.hash == hash, {
|
|
399
427
|
found := _coll_find_key(K, V, coll, k);
|
|
400
|
-
return
|
|
401
|
-
|
|
402
|
-
|
|
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
|
|
408
|
-
|
|
409
|
-
|
|
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
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
) -> Option(V)
|
|
424
|
-
|
|
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))
|
|
427
|
-
true
|
|
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
|
|
467
|
+
return(.None);
|
|
434
468
|
});
|
|
435
469
|
idx := _branch_index_for(K, V, branch, bit);
|
|
436
|
-
return
|
|
470
|
+
return(recur(K, V, _branch_child_at(K, V, branch, idx), shift + BITS, hash, k));
|
|
437
471
|
},
|
|
438
472
|
.Collision(coll) => {
|
|
439
|
-
if(
|
|
440
|
-
return
|
|
473
|
+
if(coll.hash != hash, {
|
|
474
|
+
return(.None);
|
|
441
475
|
});
|
|
442
476
|
i := usize(0);
|
|
443
|
-
while
|
|
477
|
+
while(i < coll._pairs_len, i = (i + usize(1)), {
|
|
444
478
|
pair := (coll._pairs_ptr &+ i).*;
|
|
445
|
-
if(
|
|
446
|
-
return
|
|
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
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
) -> RemoveResult(K, V)
|
|
467
|
-
|
|
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
|
|
505
|
+
RemoveResult(K, V)(node :.None, removed : true),
|
|
471
506
|
true =>
|
|
472
|
-
RemoveResult(K, V)(node
|
|
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
|
|
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,
|
|
517
|
+
sub := recur(K, V, child, shift + BITS, hash, k);
|
|
483
518
|
if(!(sub.removed), {
|
|
484
|
-
return
|
|
519
|
+
return(RemoveResult(K, V)(node :.Some(node), removed : false));
|
|
485
520
|
});
|
|
486
|
-
match(
|
|
521
|
+
match(
|
|
522
|
+
sub.node,
|
|
487
523
|
.Some(new_child) => {
|
|
488
|
-
return
|
|
489
|
-
|
|
490
|
-
|
|
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(
|
|
495
|
-
return
|
|
532
|
+
if(branch._children_len == u8(1), {
|
|
533
|
+
return(RemoveResult(K, V)(node :.None, removed : true));
|
|
496
534
|
});
|
|
497
|
-
if(
|
|
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(
|
|
538
|
+
match(
|
|
539
|
+
remaining,
|
|
501
540
|
.Leaf(_) => {
|
|
502
|
-
return
|
|
541
|
+
return(RemoveResult(K, V)(node :.Some(remaining), removed : true));
|
|
503
542
|
},
|
|
504
543
|
_ => ()
|
|
505
544
|
);
|
|
506
545
|
});
|
|
507
|
-
return
|
|
508
|
-
|
|
509
|
-
|
|
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(
|
|
516
|
-
return
|
|
556
|
+
if(coll.hash != hash, {
|
|
557
|
+
return(RemoveResult(K, V)(node :.Some(node), removed : false));
|
|
517
558
|
});
|
|
518
|
-
match(
|
|
559
|
+
match(
|
|
560
|
+
_coll_without_key(K, V, coll, k),
|
|
519
561
|
.Some(new_coll) => {
|
|
520
|
-
if(
|
|
521
|
-
return
|
|
562
|
+
if(new_coll._pairs_len == coll._pairs_len, {
|
|
563
|
+
return(RemoveResult(K, V)(node :.Some(node), removed : false));
|
|
522
564
|
});
|
|
523
|
-
return
|
|
524
|
-
|
|
525
|
-
|
|
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
|
|
535
|
-
|
|
536
|
-
|
|
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
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
) -> Map(K, V)
|
|
557
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
) -> List(K)
|
|
586
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
) -> List(V)
|
|
614
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
) -> List(Pair(K, V))
|
|
642
|
-
|
|
643
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
) -> MapNode(K, U)
|
|
671
|
-
|
|
672
|
-
|
|
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
|
|
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(
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
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
|
|
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(
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
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
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
) -> Map(K, V)
|
|
710
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
) -> bool
|
|
744
|
-
|
|
745
|
-
|
|
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
|
|
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
|
|
824
|
+
while((i < coll._pairs_len) && ok, i = (i + usize(1)), {
|
|
763
825
|
pair := (coll._pairs_ptr &+ i).*;
|
|
764
|
-
match(
|
|
826
|
+
match(
|
|
827
|
+
target.get(pair.key),
|
|
765
828
|
.Some(tv) => {
|
|
766
|
-
if(
|
|
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
|
-
|
|
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
|
|
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)(
|
|
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(
|
|
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
|
|
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(
|
|
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
|
|
874
|
+
return(Self(_root :.Some(result.node), _len : new_len));
|
|
814
875
|
},
|
|
815
876
|
.None => Self(
|
|
816
|
-
_root
|
|
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(
|
|
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
|
|
889
|
+
return(self);
|
|
829
890
|
});
|
|
830
891
|
new_len := (self._len - usize(1));
|
|
831
|
-
return
|
|
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(
|
|
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(
|
|
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(
|
|
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(
|
|
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(
|
|
872
|
-
.
|
|
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(
|
|
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
|
|
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
|
-
|
|
903
|
-
(
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
))
|
|
916
|
-
|
|
917
|
-
|
|
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);
|