eliscript 0.0.1

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 (195) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +135 -0
  3. package/bin/eliscript +16 -0
  4. package/bin/eliscript-bootstrap +15 -0
  5. package/bin/eliscript-build +15 -0
  6. package/bin/eliscript-check +15 -0
  7. package/bin/eliscript-eval +15 -0
  8. package/bin/eliscript-format +15 -0
  9. package/bin/eliscript-org +10 -0
  10. package/bin/eliscript-portable +17 -0
  11. package/bin/eliscript-seed +10 -0
  12. package/bin/eliscript-seed-build +10 -0
  13. package/bin/eliscript-watch +15 -0
  14. package/bootstrap/README.md +100 -0
  15. package/bootstrap/compiler/analyzer.eli +1426 -0
  16. package/bootstrap/compiler/compiler.eli +472 -0
  17. package/bootstrap/compiler/emitter.eli +1745 -0
  18. package/bootstrap/compiler/expander.eli +2522 -0
  19. package/bootstrap/compiler/formatter.eli +359 -0
  20. package/bootstrap/compiler/ir.eli +297 -0
  21. package/bootstrap/compiler/lower.eli +661 -0
  22. package/bootstrap/compiler/project.eli +1120 -0
  23. package/bootstrap/compiler/reader.eli +321 -0
  24. package/bootstrap/compiler/source-map.eli +165 -0
  25. package/bootstrap/compiler/symbol.eli +92 -0
  26. package/bootstrap/compiler/syntax.eli +54 -0
  27. package/bootstrap/compiler/transient-analysis.eli +825 -0
  28. package/bootstrap/host/build.mjs +28 -0
  29. package/bootstrap/host/bun.mjs +282 -0
  30. package/bootstrap/host/check-cli.mjs +163 -0
  31. package/bootstrap/host/eliscript-cli.mjs +36 -0
  32. package/bootstrap/host/evaluation-cli.mjs +401 -0
  33. package/bootstrap/host/evaluation.mjs +560 -0
  34. package/bootstrap/host/format-cli.mjs +139 -0
  35. package/bootstrap/host/project-cli.mjs +295 -0
  36. package/bootstrap/host/project.mjs +1080 -0
  37. package/bootstrap/host/watch-cli.mjs +129 -0
  38. package/bootstrap/host/watch.mjs +267 -0
  39. package/browser/host.mjs +357 -0
  40. package/browser/worker.mjs +60 -0
  41. package/compiler/README.md +118 -0
  42. package/compiler/eliscript-analyzer.el +716 -0
  43. package/compiler/eliscript-cli.el +108 -0
  44. package/compiler/eliscript-diagnostic.el +158 -0
  45. package/compiler/eliscript-emitter.el +1351 -0
  46. package/compiler/eliscript-evaluation.el +259 -0
  47. package/compiler/eliscript-expander.el +1630 -0
  48. package/compiler/eliscript-form.el +79 -0
  49. package/compiler/eliscript-ir-emitter.el +1460 -0
  50. package/compiler/eliscript-ir.el +356 -0
  51. package/compiler/eliscript-lower.el +463 -0
  52. package/compiler/eliscript-macro-eval.el +811 -0
  53. package/compiler/eliscript-parameters.el +324 -0
  54. package/compiler/eliscript-portable.el +492 -0
  55. package/compiler/eliscript-project-cli.el +156 -0
  56. package/compiler/eliscript-project.el +1676 -0
  57. package/compiler/eliscript-reader.el +421 -0
  58. package/compiler/eliscript-source-map.el +192 -0
  59. package/compiler/eliscript-symbol.el +101 -0
  60. package/compiler/eliscript-transient-analysis.el +817 -0
  61. package/compiler/eliscript.el +239 -0
  62. package/dist/bootstrap/analyzer.mjs +968 -0
  63. package/dist/bootstrap/analyzer.mjs.map +1 -0
  64. package/dist/bootstrap/compiler.mjs +228 -0
  65. package/dist/bootstrap/compiler.mjs.map +1 -0
  66. package/dist/bootstrap/emitter.mjs +1173 -0
  67. package/dist/bootstrap/emitter.mjs.map +1 -0
  68. package/dist/bootstrap/expander.mjs +1469 -0
  69. package/dist/bootstrap/expander.mjs.map +1 -0
  70. package/dist/bootstrap/formatter.mjs +304 -0
  71. package/dist/bootstrap/formatter.mjs.map +1 -0
  72. package/dist/bootstrap/ir.mjs +193 -0
  73. package/dist/bootstrap/ir.mjs.map +1 -0
  74. package/dist/bootstrap/lower.mjs +344 -0
  75. package/dist/bootstrap/lower.mjs.map +1 -0
  76. package/dist/bootstrap/project.mjs +585 -0
  77. package/dist/bootstrap/project.mjs.map +1 -0
  78. package/dist/bootstrap/reader.mjs +259 -0
  79. package/dist/bootstrap/reader.mjs.map +1 -0
  80. package/dist/bootstrap/source-map.mjs +154 -0
  81. package/dist/bootstrap/source-map.mjs.map +1 -0
  82. package/dist/bootstrap/symbol.mjs +74 -0
  83. package/dist/bootstrap/symbol.mjs.map +1 -0
  84. package/dist/bootstrap/syntax.mjs +38 -0
  85. package/dist/bootstrap/syntax.mjs.map +1 -0
  86. package/dist/bootstrap/transient-analysis.mjs +660 -0
  87. package/dist/bootstrap/transient-analysis.mjs.map +1 -0
  88. package/dist/browser/compiler.js +6491 -0
  89. package/editor/README.md +83 -0
  90. package/editor/eliscript-mode.el +878 -0
  91. package/editor/eliscript-repl.el +501 -0
  92. package/package.json +163 -0
  93. package/platform/README.md +48 -0
  94. package/platform/browser.mjs +204 -0
  95. package/platform/worker.mjs +166 -0
  96. package/runtime/README.md +296 -0
  97. package/runtime/core/collection-internals.mjs +512 -0
  98. package/runtime/core/collection.mjs +645 -0
  99. package/runtime/core/data-text.mjs +532 -0
  100. package/runtime/core/data.mjs +268 -0
  101. package/runtime/core/identifier.mjs +314 -0
  102. package/runtime/core/lazy-sequence.mjs +244 -0
  103. package/runtime/core/list-internals.mjs +18 -0
  104. package/runtime/core/list.mjs +273 -0
  105. package/runtime/core/map-internals.mjs +793 -0
  106. package/runtime/core/map.mjs +394 -0
  107. package/runtime/core/metadata-internals.mjs +14 -0
  108. package/runtime/core/metadata.mjs +44 -0
  109. package/runtime/core/object-impl.mjs +65 -0
  110. package/runtime/core/object-impl.mjs.map +1 -0
  111. package/runtime/core/object.mjs +13 -0
  112. package/runtime/core/order.mjs +234 -0
  113. package/runtime/core/persistent-kind.mjs +43 -0
  114. package/runtime/core/protocol-error.mjs +36 -0
  115. package/runtime/core/protocol-impl.mjs +326 -0
  116. package/runtime/core/protocol-impl.mjs.map +1 -0
  117. package/runtime/core/protocol.mjs +14 -0
  118. package/runtime/core/queue-internals.mjs +27 -0
  119. package/runtime/core/queue.mjs +238 -0
  120. package/runtime/core/record.mjs +346 -0
  121. package/runtime/core/regex.mjs +184 -0
  122. package/runtime/core/sequence.mjs +632 -0
  123. package/runtime/core/set-algebra.mjs +287 -0
  124. package/runtime/core/set-internals.mjs +33 -0
  125. package/runtime/core/set.mjs +378 -0
  126. package/runtime/core/sorted-internals.mjs +215 -0
  127. package/runtime/core/sorted.mjs +598 -0
  128. package/runtime/core/text-impl.mjs +95 -0
  129. package/runtime/core/text-impl.mjs.map +1 -0
  130. package/runtime/core/text.mjs +15 -0
  131. package/runtime/core/transducer.mjs +668 -0
  132. package/runtime/core/transient-internals.mjs +38 -0
  133. package/runtime/core/transient.mjs +53 -0
  134. package/runtime/core/truth.mjs +3 -0
  135. package/runtime/core/type.mjs +262 -0
  136. package/runtime/core/value-internals.mjs +188 -0
  137. package/runtime/core/value.mjs +93 -0
  138. package/runtime/core/vector-internals.mjs +102 -0
  139. package/runtime/core/vector.mjs +951 -0
  140. package/runtime/literals.mjs +34 -0
  141. package/runtime/source-mapping.mjs +220 -0
  142. package/runtime/testing/list.mjs +52 -0
  143. package/runtime/testing/map.mjs +145 -0
  144. package/runtime/testing/queue.mjs +49 -0
  145. package/runtime/testing/set.mjs +61 -0
  146. package/runtime/testing/sorted.mjs +105 -0
  147. package/runtime/testing/value.mjs +17 -0
  148. package/runtime/testing/vector.mjs +102 -0
  149. package/runtime/worker-value-codec.mjs +565 -0
  150. package/runtime/worker-value-stream.mjs +786 -0
  151. package/runtime/worker.mjs +975 -0
  152. package/stdlib/README.md +774 -0
  153. package/stdlib/bit.eli +36 -0
  154. package/stdlib/core/collection.eli +134 -0
  155. package/stdlib/core/data-text.eli +16 -0
  156. package/stdlib/core/data.eli +306 -0
  157. package/stdlib/core/identifier.eli +32 -0
  158. package/stdlib/core/lazy-sequence.eli +63 -0
  159. package/stdlib/core/metadata.eli +20 -0
  160. package/stdlib/core/object.eli +89 -0
  161. package/stdlib/core/order.eli +133 -0
  162. package/stdlib/core/protocol.eli +387 -0
  163. package/stdlib/core/record.eli +16 -0
  164. package/stdlib/core/regex.eli +17 -0
  165. package/stdlib/core/seq.eli +701 -0
  166. package/stdlib/core/set.eli +312 -0
  167. package/stdlib/core/sorted.eli +53 -0
  168. package/stdlib/core/text.eli +103 -0
  169. package/stdlib/core/transducer.eli +59 -0
  170. package/stdlib/core/transient.eli +24 -0
  171. package/stdlib/core/type.eli +19 -0
  172. package/stdlib/core/walk.eli +195 -0
  173. package/stdlib/core/zipper.eli +508 -0
  174. package/stdlib/data-text.eli +1092 -0
  175. package/stdlib/data.eli +42 -0
  176. package/stdlib/deferred.eli +70 -0
  177. package/stdlib/function.eli +120 -0
  178. package/stdlib/hierarchy.eli +245 -0
  179. package/stdlib/identifier.eli +147 -0
  180. package/stdlib/interop/js.eli +616 -0
  181. package/stdlib/json.eli +870 -0
  182. package/stdlib/metadata.eli +56 -0
  183. package/stdlib/multimethod.eli +464 -0
  184. package/stdlib/numeric.eli +193 -0
  185. package/stdlib/object.eli +96 -0
  186. package/stdlib/persistent-list.eli +145 -0
  187. package/stdlib/persistent-map.eli +724 -0
  188. package/stdlib/persistent-queue.eli +32 -0
  189. package/stdlib/persistent-set.eli +236 -0
  190. package/stdlib/persistent-vector.eli +342 -0
  191. package/stdlib/result.eli +141 -0
  192. package/stdlib/sequence.eli +106 -0
  193. package/stdlib/state/atom.eli +265 -0
  194. package/stdlib/text.eli +88 -0
  195. package/stdlib/value.eli +325 -0
@@ -0,0 +1,793 @@
1
+ import { equalValues, hashValue } from "./value.mjs";
2
+
3
+ export const MAP_STATE = Symbol("eliscript.map.state");
4
+ export const MAP_CONSTRUCTOR_TOKEN = Symbol("eliscript.map.constructor");
5
+ export const MAP_NOT_FOUND = Symbol("eliscript.map.not-found");
6
+ export const TRANSIENT_MAP_STATE = Symbol("eliscript.map.transient-state");
7
+ export const TRANSIENT_MAP_CONSTRUCTOR_TOKEN = Symbol(
8
+ "eliscript.map.transient-constructor",
9
+ );
10
+
11
+ export const ARRAY_NODE_THRESHOLD = 32;
12
+ export const BITMAP_NODE_THRESHOLD = 24;
13
+
14
+ const BRANCH_MASK = 31;
15
+
16
+ const metrics = {
17
+ nodeAllocations: 0,
18
+ nodeVisits: 0,
19
+ entryAllocations: 0,
20
+ keyEqualityChecks: 0,
21
+ promotions: 0,
22
+ demotions: 0,
23
+ };
24
+
25
+ const transientMetrics = {
26
+ nodeClones: 0,
27
+ nodeMutations: 0,
28
+ persistentCalls: 0,
29
+ invalidCalls: 0,
30
+ };
31
+
32
+ export class MapEntry {
33
+ constructor(key, value, hash) {
34
+ metrics.entryAllocations += 1;
35
+ this.key = key;
36
+ this.value = value;
37
+ this.hash = hash >>> 0;
38
+ Object.freeze(this);
39
+ }
40
+ }
41
+
42
+ export class BitmapIndexedNode {
43
+ constructor(bitmap = 0, items = [], owner = null) {
44
+ metrics.nodeAllocations += 1;
45
+ this.bitmap = bitmap >>> 0;
46
+ this.owner = owner;
47
+ this.items = owner === null ? Object.freeze(items) : items;
48
+ if (owner === null) {
49
+ Object.freeze(this);
50
+ }
51
+ }
52
+ }
53
+
54
+ export class ArrayNode {
55
+ constructor(count, children, owner = null) {
56
+ metrics.nodeAllocations += 1;
57
+ this.count = count;
58
+ this.owner = owner;
59
+ this.children = owner === null ? Object.freeze(children) : children;
60
+ if (owner === null) {
61
+ Object.freeze(this);
62
+ }
63
+ }
64
+ }
65
+
66
+ export class HashCollisionNode {
67
+ constructor(hash, entries, owner = null) {
68
+ metrics.nodeAllocations += 1;
69
+ this.hash = hash >>> 0;
70
+ this.owner = owner;
71
+ this.entries = owner === null ? Object.freeze(entries) : entries;
72
+ if (owner === null) {
73
+ Object.freeze(this);
74
+ }
75
+ }
76
+ }
77
+
78
+ function editableMapNode(node, owner) {
79
+ if (node.owner === owner) {
80
+ return node;
81
+ }
82
+ transientMetrics.nodeClones += 1;
83
+ if (node instanceof BitmapIndexedNode) {
84
+ return new BitmapIndexedNode(node.bitmap, node.items.slice(), owner);
85
+ }
86
+ if (node instanceof ArrayNode) {
87
+ return new ArrayNode(node.count, node.children.slice(), owner);
88
+ }
89
+ return new HashCollisionNode(node.hash, node.entries.slice(), owner);
90
+ }
91
+
92
+ function recordTransientNodeMutation() {
93
+ transientMetrics.nodeMutations += 1;
94
+ }
95
+
96
+ function visitNode(node) {
97
+ metrics.nodeVisits += 1;
98
+ return node;
99
+ }
100
+
101
+ function keysEqual(left, right) {
102
+ metrics.keyEqualityChecks += 1;
103
+ return equalValues(left, right);
104
+ }
105
+
106
+ function branchIndex(hash, shift) {
107
+ return (hash >>> shift) & BRANCH_MASK;
108
+ }
109
+
110
+ function bitPosition(hash, shift) {
111
+ return (1 << branchIndex(hash, shift)) >>> 0;
112
+ }
113
+
114
+ function popcount(value) {
115
+ let bits = value >>> 0;
116
+ bits -= (bits >>> 1) & 0x5555_5555;
117
+ bits = (bits & 0x3333_3333) + ((bits >>> 2) & 0x3333_3333);
118
+ return Math.imul((bits + (bits >>> 4)) & 0x0f0f_0f0f, 0x0101_0101) >>> 24;
119
+ }
120
+
121
+ function packedIndex(bitmap, bit) {
122
+ return popcount((bitmap & ((bit - 1) >>> 0)) >>> 0);
123
+ }
124
+
125
+ function sameEntry(entry, hash, key) {
126
+ return entry.hash === hash && keysEqual(entry.key, key);
127
+ }
128
+
129
+ function mergeEntries(shift, left, right) {
130
+ if (left.hash === right.hash) {
131
+ return new HashCollisionNode(left.hash, [left, right]);
132
+ }
133
+ const leftIndex = branchIndex(left.hash, shift);
134
+ const rightIndex = branchIndex(right.hash, shift);
135
+ if (leftIndex === rightIndex) {
136
+ return new BitmapIndexedNode(
137
+ (1 << leftIndex) >>> 0,
138
+ [mergeEntries(shift + 5, left, right)],
139
+ );
140
+ }
141
+ const bitmap = ((1 << leftIndex) | (1 << rightIndex)) >>> 0;
142
+ return new BitmapIndexedNode(
143
+ bitmap,
144
+ leftIndex < rightIndex ? [left, right] : [right, left],
145
+ );
146
+ }
147
+
148
+ function mergeCollisionAndEntry(shift, node, entry) {
149
+ const nodeIndex = branchIndex(node.hash, shift);
150
+ const entryIndex = branchIndex(entry.hash, shift);
151
+ if (nodeIndex === entryIndex) {
152
+ return new BitmapIndexedNode(
153
+ (1 << nodeIndex) >>> 0,
154
+ [mergeCollisionAndEntry(shift + 5, node, entry)],
155
+ );
156
+ }
157
+ const bitmap = ((1 << nodeIndex) | (1 << entryIndex)) >>> 0;
158
+ return new BitmapIndexedNode(
159
+ bitmap,
160
+ nodeIndex < entryIndex ? [node, entry] : [entry, node],
161
+ );
162
+ }
163
+
164
+ function assocEntry(entry, shift, hash, key, value) {
165
+ if (sameEntry(entry, hash, key)) {
166
+ if (equalValues(entry.value, value)) {
167
+ return { item: entry, added: false, changed: false };
168
+ }
169
+ return {
170
+ item: new MapEntry(entry.key, value, entry.hash),
171
+ added: false,
172
+ changed: true,
173
+ };
174
+ }
175
+ return {
176
+ item: mergeEntries(shift, entry, new MapEntry(key, value, hash)),
177
+ added: true,
178
+ changed: true,
179
+ };
180
+ }
181
+
182
+ function findEntry(entry, hash, key, notFound) {
183
+ return sameEntry(entry, hash, key) ? entry.value : notFound;
184
+ }
185
+
186
+ function promoteBitmapNode(node, shift, hash, key, value) {
187
+ const children = Array(32).fill(undefined);
188
+ let packed = 0;
189
+ for (let index = 0; index < 32; index += 1) {
190
+ const bit = (1 << index) >>> 0;
191
+ if ((node.bitmap & bit) !== 0) {
192
+ children[index] = node.items[packed];
193
+ packed += 1;
194
+ }
195
+ }
196
+ children[branchIndex(hash, shift)] = new MapEntry(key, value, hash);
197
+ metrics.promotions += 1;
198
+ return new ArrayNode(node.items.length + 1, children);
199
+ }
200
+
201
+ function packArrayNode(node) {
202
+ let bitmap = 0;
203
+ const items = [];
204
+ for (let index = 0; index < 32; index += 1) {
205
+ const child = node.children[index];
206
+ if (child !== undefined) {
207
+ bitmap = (bitmap | (1 << index)) >>> 0;
208
+ items.push(child);
209
+ }
210
+ }
211
+ metrics.demotions += 1;
212
+ return new BitmapIndexedNode(bitmap, items);
213
+ }
214
+
215
+ function findInNode(node, shift, hash, key, notFound) {
216
+ visitNode(node);
217
+ if (node instanceof BitmapIndexedNode) {
218
+ const bit = bitPosition(hash, shift);
219
+ if ((node.bitmap & bit) === 0) {
220
+ return notFound;
221
+ }
222
+ const item = node.items[packedIndex(node.bitmap, bit)];
223
+ return item instanceof MapEntry
224
+ ? findEntry(item, hash, key, notFound)
225
+ : findInNode(item, shift + 5, hash, key, notFound);
226
+ }
227
+ if (node instanceof ArrayNode) {
228
+ const item = node.children[branchIndex(hash, shift)];
229
+ if (item === undefined) {
230
+ return notFound;
231
+ }
232
+ return item instanceof MapEntry
233
+ ? findEntry(item, hash, key, notFound)
234
+ : findInNode(item, shift + 5, hash, key, notFound);
235
+ }
236
+ if (node.hash !== hash) {
237
+ return notFound;
238
+ }
239
+ for (const entry of node.entries) {
240
+ if (keysEqual(entry.key, key)) {
241
+ return entry.value;
242
+ }
243
+ }
244
+ return notFound;
245
+ }
246
+
247
+ function assocNode(node, shift, hash, key, value) {
248
+ visitNode(node);
249
+ if (node instanceof BitmapIndexedNode) {
250
+ const bit = bitPosition(hash, shift);
251
+ const index = packedIndex(node.bitmap, bit);
252
+ if ((node.bitmap & bit) === 0) {
253
+ if (node.items.length + 1 >= ARRAY_NODE_THRESHOLD) {
254
+ return {
255
+ item: promoteBitmapNode(node, shift, hash, key, value),
256
+ added: true,
257
+ changed: true,
258
+ };
259
+ }
260
+ const items = node.items.slice();
261
+ items.splice(index, 0, new MapEntry(key, value, hash));
262
+ return {
263
+ item: new BitmapIndexedNode((node.bitmap | bit) >>> 0, items),
264
+ added: true,
265
+ changed: true,
266
+ };
267
+ }
268
+
269
+ const existing = node.items[index];
270
+ const result = existing instanceof MapEntry
271
+ ? assocEntry(existing, shift + 5, hash, key, value)
272
+ : assocNode(existing, shift + 5, hash, key, value);
273
+ if (!result.changed) {
274
+ return { item: node, added: false, changed: false };
275
+ }
276
+ const items = node.items.slice();
277
+ items[index] = result.item;
278
+ return {
279
+ item: new BitmapIndexedNode(node.bitmap, items),
280
+ added: result.added,
281
+ changed: true,
282
+ };
283
+ }
284
+
285
+ if (node instanceof ArrayNode) {
286
+ const index = branchIndex(hash, shift);
287
+ const existing = node.children[index];
288
+ if (existing === undefined) {
289
+ const children = node.children.slice();
290
+ children[index] = new MapEntry(key, value, hash);
291
+ return {
292
+ item: new ArrayNode(node.count + 1, children),
293
+ added: true,
294
+ changed: true,
295
+ };
296
+ }
297
+ const result = existing instanceof MapEntry
298
+ ? assocEntry(existing, shift + 5, hash, key, value)
299
+ : assocNode(existing, shift + 5, hash, key, value);
300
+ if (!result.changed) {
301
+ return { item: node, added: false, changed: false };
302
+ }
303
+ const children = node.children.slice();
304
+ children[index] = result.item;
305
+ return {
306
+ item: new ArrayNode(node.count, children),
307
+ added: result.added,
308
+ changed: true,
309
+ };
310
+ }
311
+
312
+ if (node.hash !== hash) {
313
+ return {
314
+ item: mergeCollisionAndEntry(
315
+ shift,
316
+ node,
317
+ new MapEntry(key, value, hash),
318
+ ),
319
+ added: true,
320
+ changed: true,
321
+ };
322
+ }
323
+ const index = node.entries.findIndex((entry) => keysEqual(entry.key, key));
324
+ if (index >= 0) {
325
+ const existing = node.entries[index];
326
+ if (equalValues(existing.value, value)) {
327
+ return { item: node, added: false, changed: false };
328
+ }
329
+ const entries = node.entries.slice();
330
+ entries[index] = new MapEntry(existing.key, value, hash);
331
+ return {
332
+ item: new HashCollisionNode(hash, entries),
333
+ added: false,
334
+ changed: true,
335
+ };
336
+ }
337
+ return {
338
+ item: new HashCollisionNode(
339
+ hash,
340
+ [...node.entries, new MapEntry(key, value, hash)],
341
+ ),
342
+ added: true,
343
+ changed: true,
344
+ };
345
+ }
346
+
347
+ function removePackedItem(node, bit, index) {
348
+ if (node.items.length === 1) {
349
+ return undefined;
350
+ }
351
+ const items = node.items.slice();
352
+ items.splice(index, 1);
353
+ return new BitmapIndexedNode((node.bitmap ^ bit) >>> 0, items);
354
+ }
355
+
356
+ function dissocNode(node, shift, hash, key) {
357
+ visitNode(node);
358
+ if (node instanceof BitmapIndexedNode) {
359
+ const bit = bitPosition(hash, shift);
360
+ if ((node.bitmap & bit) === 0) {
361
+ return { item: node, removed: false };
362
+ }
363
+ const index = packedIndex(node.bitmap, bit);
364
+ const existing = node.items[index];
365
+ if (existing instanceof MapEntry) {
366
+ if (!sameEntry(existing, hash, key)) {
367
+ return { item: node, removed: false };
368
+ }
369
+ return {
370
+ item: removePackedItem(node, bit, index),
371
+ removed: true,
372
+ };
373
+ }
374
+ const result = dissocNode(existing, shift + 5, hash, key);
375
+ if (!result.removed) {
376
+ return { item: node, removed: false };
377
+ }
378
+ if (result.item === undefined) {
379
+ return {
380
+ item: removePackedItem(node, bit, index),
381
+ removed: true,
382
+ };
383
+ }
384
+ const items = node.items.slice();
385
+ items[index] = result.item;
386
+ return {
387
+ item: new BitmapIndexedNode(node.bitmap, items),
388
+ removed: true,
389
+ };
390
+ }
391
+
392
+ if (node instanceof ArrayNode) {
393
+ const index = branchIndex(hash, shift);
394
+ const existing = node.children[index];
395
+ if (existing === undefined) {
396
+ return { item: node, removed: false };
397
+ }
398
+ let result;
399
+ if (existing instanceof MapEntry) {
400
+ result = sameEntry(existing, hash, key)
401
+ ? { item: undefined, removed: true }
402
+ : { item: existing, removed: false };
403
+ } else {
404
+ result = dissocNode(existing, shift + 5, hash, key);
405
+ }
406
+ if (!result.removed) {
407
+ return { item: node, removed: false };
408
+ }
409
+ const children = node.children.slice();
410
+ children[index] = result.item;
411
+ const count = result.item === undefined ? node.count - 1 : node.count;
412
+ const updated = new ArrayNode(count, children);
413
+ return {
414
+ item: count <= BITMAP_NODE_THRESHOLD ? packArrayNode(updated) : updated,
415
+ removed: true,
416
+ };
417
+ }
418
+
419
+ if (node.hash !== hash) {
420
+ return { item: node, removed: false };
421
+ }
422
+ const index = node.entries.findIndex((entry) => keysEqual(entry.key, key));
423
+ if (index < 0) {
424
+ return { item: node, removed: false };
425
+ }
426
+ if (node.entries.length === 2) {
427
+ return { item: node.entries[index === 0 ? 1 : 0], removed: true };
428
+ }
429
+ const entries = node.entries.slice();
430
+ entries.splice(index, 1);
431
+ return {
432
+ item: new HashCollisionNode(hash, entries),
433
+ removed: true,
434
+ };
435
+ }
436
+
437
+ function transientMergeEntries(shift, left, right, owner) {
438
+ if (left.hash === right.hash) {
439
+ return new HashCollisionNode(left.hash, [left, right], owner);
440
+ }
441
+ const leftIndex = branchIndex(left.hash, shift);
442
+ const rightIndex = branchIndex(right.hash, shift);
443
+ if (leftIndex === rightIndex) {
444
+ return new BitmapIndexedNode(
445
+ (1 << leftIndex) >>> 0,
446
+ [transientMergeEntries(shift + 5, left, right, owner)],
447
+ owner,
448
+ );
449
+ }
450
+ const bitmap = ((1 << leftIndex) | (1 << rightIndex)) >>> 0;
451
+ return new BitmapIndexedNode(
452
+ bitmap,
453
+ leftIndex < rightIndex ? [left, right] : [right, left],
454
+ owner,
455
+ );
456
+ }
457
+
458
+ function transientMergeCollisionAndEntry(shift, node, entry, owner) {
459
+ const nodeIndex = branchIndex(node.hash, shift);
460
+ const entryIndex = branchIndex(entry.hash, shift);
461
+ if (nodeIndex === entryIndex) {
462
+ return new BitmapIndexedNode(
463
+ (1 << nodeIndex) >>> 0,
464
+ [transientMergeCollisionAndEntry(shift + 5, node, entry, owner)],
465
+ owner,
466
+ );
467
+ }
468
+ const bitmap = ((1 << nodeIndex) | (1 << entryIndex)) >>> 0;
469
+ return new BitmapIndexedNode(
470
+ bitmap,
471
+ nodeIndex < entryIndex ? [node, entry] : [entry, node],
472
+ owner,
473
+ );
474
+ }
475
+
476
+ function transientAssocEntry(entry, shift, hash, key, value, owner) {
477
+ if (sameEntry(entry, hash, key)) {
478
+ if (equalValues(entry.value, value)) {
479
+ return { item: entry, added: false, changed: false };
480
+ }
481
+ return {
482
+ item: new MapEntry(entry.key, value, entry.hash),
483
+ added: false,
484
+ changed: true,
485
+ };
486
+ }
487
+ return {
488
+ item: transientMergeEntries(
489
+ shift,
490
+ entry,
491
+ new MapEntry(key, value, hash),
492
+ owner,
493
+ ),
494
+ added: true,
495
+ changed: true,
496
+ };
497
+ }
498
+
499
+ function transientPromoteBitmapNode(node, shift, hash, key, value, owner) {
500
+ const children = Array(32).fill(undefined);
501
+ let packed = 0;
502
+ for (let index = 0; index < 32; index += 1) {
503
+ const bit = (1 << index) >>> 0;
504
+ if ((node.bitmap & bit) !== 0) {
505
+ children[index] = node.items[packed];
506
+ packed += 1;
507
+ }
508
+ }
509
+ children[branchIndex(hash, shift)] = new MapEntry(key, value, hash);
510
+ metrics.promotions += 1;
511
+ return new ArrayNode(node.items.length + 1, children, owner);
512
+ }
513
+
514
+ function transientPackArrayNode(node, removedIndex, owner) {
515
+ let bitmap = 0;
516
+ const items = [];
517
+ for (let index = 0; index < 32; index += 1) {
518
+ if (index === removedIndex) {
519
+ continue;
520
+ }
521
+ const child = node.children[index];
522
+ if (child !== undefined) {
523
+ bitmap = (bitmap | (1 << index)) >>> 0;
524
+ items.push(child);
525
+ }
526
+ }
527
+ metrics.demotions += 1;
528
+ return new BitmapIndexedNode(bitmap, items, owner);
529
+ }
530
+
531
+ function transientAssocNode(node, shift, hash, key, value, owner) {
532
+ visitNode(node);
533
+ if (node instanceof BitmapIndexedNode) {
534
+ const bit = bitPosition(hash, shift);
535
+ const index = packedIndex(node.bitmap, bit);
536
+ if ((node.bitmap & bit) === 0) {
537
+ if (node.items.length + 1 >= ARRAY_NODE_THRESHOLD) {
538
+ return {
539
+ item: transientPromoteBitmapNode(
540
+ node,
541
+ shift,
542
+ hash,
543
+ key,
544
+ value,
545
+ owner,
546
+ ),
547
+ added: true,
548
+ changed: true,
549
+ };
550
+ }
551
+ const editable = editableMapNode(node, owner);
552
+ editable.bitmap = (editable.bitmap | bit) >>> 0;
553
+ editable.items.splice(index, 0, new MapEntry(key, value, hash));
554
+ recordTransientNodeMutation();
555
+ return { item: editable, added: true, changed: true };
556
+ }
557
+
558
+ const existing = node.items[index];
559
+ const result = existing instanceof MapEntry
560
+ ? transientAssocEntry(existing, shift + 5, hash, key, value, owner)
561
+ : transientAssocNode(existing, shift + 5, hash, key, value, owner);
562
+ if (!result.changed) {
563
+ return { item: node, added: false, changed: false };
564
+ }
565
+ const editable = editableMapNode(node, owner);
566
+ editable.items[index] = result.item;
567
+ recordTransientNodeMutation();
568
+ return { item: editable, added: result.added, changed: true };
569
+ }
570
+
571
+ if (node instanceof ArrayNode) {
572
+ const index = branchIndex(hash, shift);
573
+ const existing = node.children[index];
574
+ if (existing === undefined) {
575
+ const editable = editableMapNode(node, owner);
576
+ editable.children[index] = new MapEntry(key, value, hash);
577
+ editable.count += 1;
578
+ recordTransientNodeMutation();
579
+ return { item: editable, added: true, changed: true };
580
+ }
581
+ const result = existing instanceof MapEntry
582
+ ? transientAssocEntry(existing, shift + 5, hash, key, value, owner)
583
+ : transientAssocNode(existing, shift + 5, hash, key, value, owner);
584
+ if (!result.changed) {
585
+ return { item: node, added: false, changed: false };
586
+ }
587
+ const editable = editableMapNode(node, owner);
588
+ editable.children[index] = result.item;
589
+ recordTransientNodeMutation();
590
+ return { item: editable, added: result.added, changed: true };
591
+ }
592
+
593
+ if (node.hash !== hash) {
594
+ return {
595
+ item: transientMergeCollisionAndEntry(
596
+ shift,
597
+ node,
598
+ new MapEntry(key, value, hash),
599
+ owner,
600
+ ),
601
+ added: true,
602
+ changed: true,
603
+ };
604
+ }
605
+ const index = node.entries.findIndex((entry) => keysEqual(entry.key, key));
606
+ if (index >= 0) {
607
+ const existing = node.entries[index];
608
+ if (equalValues(existing.value, value)) {
609
+ return { item: node, added: false, changed: false };
610
+ }
611
+ const editable = editableMapNode(node, owner);
612
+ editable.entries[index] = new MapEntry(existing.key, value, hash);
613
+ recordTransientNodeMutation();
614
+ return { item: editable, added: false, changed: true };
615
+ }
616
+ const editable = editableMapNode(node, owner);
617
+ editable.entries.push(new MapEntry(key, value, hash));
618
+ recordTransientNodeMutation();
619
+ return { item: editable, added: true, changed: true };
620
+ }
621
+
622
+ function transientRemovePackedItem(node, bit, index, owner) {
623
+ if (node.items.length === 1) {
624
+ return undefined;
625
+ }
626
+ const editable = editableMapNode(node, owner);
627
+ editable.bitmap = (editable.bitmap ^ bit) >>> 0;
628
+ editable.items.splice(index, 1);
629
+ recordTransientNodeMutation();
630
+ return editable;
631
+ }
632
+
633
+ function transientDissocNode(node, shift, hash, key, owner) {
634
+ visitNode(node);
635
+ if (node instanceof BitmapIndexedNode) {
636
+ const bit = bitPosition(hash, shift);
637
+ if ((node.bitmap & bit) === 0) {
638
+ return { item: node, removed: false };
639
+ }
640
+ const index = packedIndex(node.bitmap, bit);
641
+ const existing = node.items[index];
642
+ if (existing instanceof MapEntry) {
643
+ if (!sameEntry(existing, hash, key)) {
644
+ return { item: node, removed: false };
645
+ }
646
+ return {
647
+ item: transientRemovePackedItem(node, bit, index, owner),
648
+ removed: true,
649
+ };
650
+ }
651
+ const result = transientDissocNode(
652
+ existing,
653
+ shift + 5,
654
+ hash,
655
+ key,
656
+ owner,
657
+ );
658
+ if (!result.removed) {
659
+ return { item: node, removed: false };
660
+ }
661
+ if (result.item === undefined) {
662
+ return {
663
+ item: transientRemovePackedItem(node, bit, index, owner),
664
+ removed: true,
665
+ };
666
+ }
667
+ const editable = editableMapNode(node, owner);
668
+ editable.items[index] = result.item;
669
+ recordTransientNodeMutation();
670
+ return { item: editable, removed: true };
671
+ }
672
+
673
+ if (node instanceof ArrayNode) {
674
+ const index = branchIndex(hash, shift);
675
+ const existing = node.children[index];
676
+ if (existing === undefined) {
677
+ return { item: node, removed: false };
678
+ }
679
+ const result = existing instanceof MapEntry
680
+ ? (sameEntry(existing, hash, key)
681
+ ? { item: undefined, removed: true }
682
+ : { item: existing, removed: false })
683
+ : transientDissocNode(existing, shift + 5, hash, key, owner);
684
+ if (!result.removed) {
685
+ return { item: node, removed: false };
686
+ }
687
+ const count = result.item === undefined ? node.count - 1 : node.count;
688
+ if (result.item === undefined && count <= BITMAP_NODE_THRESHOLD) {
689
+ return {
690
+ item: transientPackArrayNode(node, index, owner),
691
+ removed: true,
692
+ };
693
+ }
694
+ const editable = editableMapNode(node, owner);
695
+ editable.children[index] = result.item;
696
+ editable.count = count;
697
+ recordTransientNodeMutation();
698
+ return { item: editable, removed: true };
699
+ }
700
+
701
+ if (node.hash !== hash) {
702
+ return { item: node, removed: false };
703
+ }
704
+ const index = node.entries.findIndex((entry) => keysEqual(entry.key, key));
705
+ if (index < 0) {
706
+ return { item: node, removed: false };
707
+ }
708
+ if (node.entries.length === 2) {
709
+ return { item: node.entries[index === 0 ? 1 : 0], removed: true };
710
+ }
711
+ const editable = editableMapNode(node, owner);
712
+ editable.entries.splice(index, 1);
713
+ recordTransientNodeMutation();
714
+ return { item: editable, removed: true };
715
+ }
716
+
717
+ export function mapFind(root, hash, key, notFound) {
718
+ return findInNode(root, 0, hash >>> 0, key, notFound);
719
+ }
720
+
721
+ export function mapAssoc(root, hash, key, value) {
722
+ return assocNode(root, 0, hash >>> 0, key, value);
723
+ }
724
+
725
+ export function mapDissoc(root, hash, key) {
726
+ return dissocNode(root, 0, hash >>> 0, key);
727
+ }
728
+
729
+ export function mapAssocTransient(root, owner, hash, key, value) {
730
+ return transientAssocNode(root, 0, hash >>> 0, key, value, owner);
731
+ }
732
+
733
+ export function mapDissocTransient(root, owner, hash, key) {
734
+ return transientDissocNode(root, 0, hash >>> 0, key, owner);
735
+ }
736
+
737
+ export function* mapEntries(item) {
738
+ if (item instanceof MapEntry) {
739
+ yield item;
740
+ return;
741
+ }
742
+ if (item instanceof BitmapIndexedNode) {
743
+ for (const child of item.items) {
744
+ yield* mapEntries(child);
745
+ }
746
+ return;
747
+ }
748
+ if (item instanceof ArrayNode) {
749
+ for (const child of item.children) {
750
+ if (child !== undefined) {
751
+ yield* mapEntries(child);
752
+ }
753
+ }
754
+ return;
755
+ }
756
+ for (const entry of item.entries) {
757
+ yield entry;
758
+ }
759
+ }
760
+
761
+ export function mapHash(key) {
762
+ return hashValue(key);
763
+ }
764
+
765
+ export function resetMapMetrics() {
766
+ for (const key of Object.keys(metrics)) {
767
+ metrics[key] = 0;
768
+ }
769
+ }
770
+
771
+ export function readMapMetrics() {
772
+ return Object.freeze({ ...metrics });
773
+ }
774
+
775
+ export function resetTransientMapMetrics() {
776
+ for (const key of Object.keys(transientMetrics)) {
777
+ transientMetrics[key] = 0;
778
+ }
779
+ }
780
+
781
+ export function readTransientMapMetrics() {
782
+ return Object.freeze({ ...transientMetrics });
783
+ }
784
+
785
+ export function recordTransientMapPersistent() {
786
+ transientMetrics.persistentCalls += 1;
787
+ }
788
+
789
+ export function recordInvalidTransientMapCall() {
790
+ transientMetrics.invalidCalls += 1;
791
+ }
792
+
793
+ export const EMPTY_BITMAP_NODE = new BitmapIndexedNode();