ic-mops 0.8.4 → 0.8.5

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 (99) hide show
  1. package/.mops/base@0.7.4/LICENSE +208 -0
  2. package/.mops/base@0.7.4/README.md +64 -0
  3. package/.mops/base@0.7.4/mops.toml +5 -0
  4. package/.mops/base@0.7.4/src/Array.mo +686 -0
  5. package/.mops/base@0.7.4/src/AssocList.mo +203 -0
  6. package/.mops/base@0.7.4/src/Blob.mo +55 -0
  7. package/.mops/base@0.7.4/src/Bool.mo +44 -0
  8. package/.mops/base@0.7.4/src/Buffer.mo +1937 -0
  9. package/.mops/base@0.7.4/src/CertifiedData.mo +29 -0
  10. package/.mops/base@0.7.4/src/Char.mo +67 -0
  11. package/.mops/base@0.7.4/src/Debug.mo +15 -0
  12. package/.mops/base@0.7.4/src/Deque.mo +75 -0
  13. package/.mops/base@0.7.4/src/Error.mo +41 -0
  14. package/.mops/base@0.7.4/src/ExperimentalCycles.mo +51 -0
  15. package/.mops/base@0.7.4/src/ExperimentalInternetComputer.mo +36 -0
  16. package/.mops/base@0.7.4/src/ExperimentalStableMemory.mo +121 -0
  17. package/.mops/base@0.7.4/src/Float.mo +150 -0
  18. package/.mops/base@0.7.4/src/Func.mo +38 -0
  19. package/.mops/base@0.7.4/src/Hash.mo +83 -0
  20. package/.mops/base@0.7.4/src/HashMap.mo +229 -0
  21. package/.mops/base@0.7.4/src/Heap.mo +113 -0
  22. package/.mops/base@0.7.4/src/Int.mo +150 -0
  23. package/.mops/base@0.7.4/src/Int16.mo +159 -0
  24. package/.mops/base@0.7.4/src/Int32.mo +160 -0
  25. package/.mops/base@0.7.4/src/Int64.mo +161 -0
  26. package/.mops/base@0.7.4/src/Int8.mo +160 -0
  27. package/.mops/base@0.7.4/src/Iter.mo +220 -0
  28. package/.mops/base@0.7.4/src/IterType.mo +7 -0
  29. package/.mops/base@0.7.4/src/List.mo +433 -0
  30. package/.mops/base@0.7.4/src/Nat.mo +75 -0
  31. package/.mops/base@0.7.4/src/Nat16.mo +146 -0
  32. package/.mops/base@0.7.4/src/Nat32.mo +146 -0
  33. package/.mops/base@0.7.4/src/Nat64.mo +146 -0
  34. package/.mops/base@0.7.4/src/Nat8.mo +146 -0
  35. package/.mops/base@0.7.4/src/None.mo +19 -0
  36. package/.mops/base@0.7.4/src/Option.mo +160 -0
  37. package/.mops/base@0.7.4/src/Order.mo +46 -0
  38. package/.mops/base@0.7.4/src/Prelude.mo +33 -0
  39. package/.mops/base@0.7.4/src/Principal.mo +58 -0
  40. package/.mops/base@0.7.4/src/RBTree.mo +218 -0
  41. package/.mops/base@0.7.4/src/Random.mo +188 -0
  42. package/.mops/base@0.7.4/src/Result.mo +210 -0
  43. package/.mops/base@0.7.4/src/Stack.mo +40 -0
  44. package/.mops/base@0.7.4/src/Text.mo +615 -0
  45. package/.mops/base@0.7.4/src/Time.mo +37 -0
  46. package/.mops/base@0.7.4/src/Trie.mo +1200 -0
  47. package/.mops/base@0.7.4/src/TrieMap.mo +180 -0
  48. package/.mops/base@0.7.4/src/TrieSet.mo +97 -0
  49. package/.mops/base@0.8.3/LICENSE +208 -0
  50. package/.mops/base@0.8.3/README.md +64 -0
  51. package/.mops/base@0.8.3/mops.toml +6 -0
  52. package/.mops/base@0.8.3/src/Array.mo +717 -0
  53. package/.mops/base@0.8.3/src/AssocList.mo +404 -0
  54. package/.mops/base@0.8.3/src/Blob.mo +212 -0
  55. package/.mops/base@0.8.3/src/Bool.mo +44 -0
  56. package/.mops/base@0.8.3/src/Buffer.mo +2660 -0
  57. package/.mops/base@0.8.3/src/CertifiedData.mo +53 -0
  58. package/.mops/base@0.8.3/src/Char.mo +65 -0
  59. package/.mops/base@0.8.3/src/Debug.mo +56 -0
  60. package/.mops/base@0.8.3/src/Deque.mo +243 -0
  61. package/.mops/base@0.8.3/src/Error.mo +68 -0
  62. package/.mops/base@0.8.3/src/ExperimentalCycles.mo +151 -0
  63. package/.mops/base@0.8.3/src/ExperimentalInternetComputer.mo +60 -0
  64. package/.mops/base@0.8.3/src/ExperimentalStableMemory.mo +348 -0
  65. package/.mops/base@0.8.3/src/Float.mo +843 -0
  66. package/.mops/base@0.8.3/src/Func.mo +46 -0
  67. package/.mops/base@0.8.3/src/Hash.mo +82 -0
  68. package/.mops/base@0.8.3/src/HashMap.mo +457 -0
  69. package/.mops/base@0.8.3/src/Heap.mo +233 -0
  70. package/.mops/base@0.8.3/src/Int.mo +365 -0
  71. package/.mops/base@0.8.3/src/Int16.mo +521 -0
  72. package/.mops/base@0.8.3/src/Int32.mo +522 -0
  73. package/.mops/base@0.8.3/src/Int64.mo +522 -0
  74. package/.mops/base@0.8.3/src/Int8.mo +522 -0
  75. package/.mops/base@0.8.3/src/Iter.mo +227 -0
  76. package/.mops/base@0.8.3/src/IterType.mo +7 -0
  77. package/.mops/base@0.8.3/src/List.mo +930 -0
  78. package/.mops/base@0.8.3/src/Nat.mo +305 -0
  79. package/.mops/base@0.8.3/src/Nat16.mo +144 -0
  80. package/.mops/base@0.8.3/src/Nat32.mo +144 -0
  81. package/.mops/base@0.8.3/src/Nat64.mo +144 -0
  82. package/.mops/base@0.8.3/src/Nat8.mo +144 -0
  83. package/.mops/base@0.8.3/src/None.mo +19 -0
  84. package/.mops/base@0.8.3/src/Option.mo +154 -0
  85. package/.mops/base@0.8.3/src/Order.mo +46 -0
  86. package/.mops/base@0.8.3/src/Prelude.mo +33 -0
  87. package/.mops/base@0.8.3/src/Principal.mo +249 -0
  88. package/.mops/base@0.8.3/src/RBTree.mo +681 -0
  89. package/.mops/base@0.8.3/src/Random.mo +270 -0
  90. package/.mops/base@0.8.3/src/Result.mo +209 -0
  91. package/.mops/base@0.8.3/src/Stack.mo +93 -0
  92. package/.mops/base@0.8.3/src/Text.mo +761 -0
  93. package/.mops/base@0.8.3/src/Time.mo +36 -0
  94. package/.mops/base@0.8.3/src/Timer.mo +62 -0
  95. package/.mops/base@0.8.3/src/Trie.mo +1603 -0
  96. package/.mops/base@0.8.3/src/TrieMap.mo +392 -0
  97. package/.mops/base@0.8.3/src/TrieSet.mo +148 -0
  98. package/network.txt +1 -0
  99. package/package.json +2 -2
@@ -0,0 +1,1200 @@
1
+ /// Functional key-value hash maps.
2
+ ///
3
+ /// Functional maps (and sets) whose representation is "canonical", and
4
+ /// independent of operation history (unlike other popular search trees).
5
+ ///
6
+ /// The representation we use here comes from Section 6 of ["Incremental computation via function caching", Pugh & Teitelbaum](https://dl.acm.org/citation.cfm?id=75305).
7
+ ///
8
+ /// ## User's overview
9
+ ///
10
+ /// This module provides an applicative (functional) hash map.
11
+ /// Notably, each `put` produces a **new trie _and value being replaced, if any_**.
12
+ ///
13
+ /// Those looking for a more familiar (imperative,
14
+ /// object-oriented) hash map should consider `TrieMap` or `HashMap` instead.
15
+ ///
16
+ /// The basic `Trie` operations consist of:
17
+ /// - `put` - put a key-value into the trie, producing a new version.
18
+ /// - `get` - get a key's value from the trie, or `null` if none.
19
+ /// - `iter` - visit every key-value in the trie.
20
+ ///
21
+ /// The `put` and `get` operations work over `Key` records,
22
+ /// which group the hash of the key with its non-hash key value.
23
+ ///
24
+ /// ```motoko
25
+ /// import Trie "mo:base/Trie";
26
+ /// import Text "mo:base/Text";
27
+ ///
28
+ /// type Trie<K, V> = Trie.Trie<K, V>;
29
+ /// type Key<K> = Trie.Key<K>;
30
+ ///
31
+ /// func key(t: Text) : Key<Text> { { key = t; hash = Text.hash t } };
32
+ ///
33
+ /// let t0 : Trie<Text, Nat> = Trie.empty();
34
+ /// let t1 : Trie<Text, Nat> = Trie.put(t0, key "hello", Text.equal, 42).0;
35
+ /// let t2 : Trie<Text, Nat> = Trie.put(t1, key "world", Text.equal, 24).0;
36
+ /// let n : ?Nat = Trie.put(t1, key "hello", Text.equal, 0).1;
37
+ /// assert (n == ?42);
38
+ /// ```
39
+ ///
40
+
41
+ // ## Implementation overview
42
+ //
43
+ // A (hash) trie is a binary tree container for key-value pairs that
44
+ // consists of leaf and branch nodes.
45
+ //
46
+ // Each internal **branch node**
47
+ // represents having distinguished its key-value pairs on a single bit of
48
+ // the keys.
49
+ // By following paths in the trie, we determine an increasingly smaller
50
+ // and smaller subset of the keys.
51
+ //
52
+ // Each **leaf node** consists of an association list of key-value pairs.
53
+ //
54
+ // Each non-empty trie node stores a size; we discuss that more below.
55
+ //
56
+ // ### Adaptive depth
57
+ //
58
+ // We say that a leaf is valid if it contains no more than `MAX_LEAF_SIZE`
59
+ // key-value pairs. When a leaf node grows too large, the
60
+ // binary tree produces a new internal binary node, and splits the leaf into
61
+ // a pair of leaves using an additional bit of their keys' hash strings.
62
+ //
63
+ // For small mappings, the trie structure consists of a single
64
+ // leaf, which contains up to MAX_LEAF_SIZE key-value pairs.
65
+ //
66
+ // ### Cached sizes
67
+ //
68
+ // At each branch and leaf, we use a stored size to support a
69
+ // memory-efficient `toArray` function, which itself relies on
70
+ // per-element projection via `nth`; in turn, `nth` directly uses the
71
+ // O(1)-time function `size` for achieving an acceptable level of
72
+ // algorithmic efficiency. Notably, leaves are generally lists of
73
+ // key-value pairs, and we do not store a size for each Cons cell in the
74
+ // list.
75
+ //
76
+
77
+ import Debug "mo:base/Debug";
78
+
79
+ import Prim "mo:⛔";
80
+ import P "Prelude";
81
+ import Option "Option";
82
+ import Hash "Hash";
83
+ import A "Array";
84
+
85
+ import List "List";
86
+ import AssocList "AssocList";
87
+ import I "Iter";
88
+
89
+ module {
90
+
91
+ let MAX_LEAF_SIZE = 8; // to do -- further profiling and tuning
92
+
93
+ /// Binary hash tries: either empty, a leaf node, or a branch node
94
+ public type Trie<K, V> = {
95
+ #empty;
96
+ #leaf : Leaf<K, V>;
97
+ #branch : Branch<K, V>;
98
+ };
99
+
100
+ /// Leaf nodes of trie consist of key-value pairs as a list.
101
+ public type Leaf<K, V> = {
102
+ size : Nat ;
103
+ keyvals : AssocList<Key<K>, V> ;
104
+ };
105
+
106
+ /// Branch nodes of the trie discriminate on a bit position of the keys' hashes.
107
+ /// we never store this bitpos; rather,
108
+ /// we enforce a style where this position is always known from context.
109
+ public type Branch<K, V> = {
110
+ size : Nat;
111
+ left : Trie<K, V>;
112
+ right : Trie<K, V>;
113
+ };
114
+
115
+ public type AssocList<K, V> = AssocList.AssocList<K, V>;
116
+
117
+ //// A `Key` for the trie has an associated hash value
118
+ public type Key<K> = {
119
+ /// `hash` permits fast inequality checks, and permits collisions
120
+ hash: Hash.Hash;
121
+ /// `key` permits precise equality checks, but only used after equal hashes.
122
+ key: K;
123
+ };
124
+
125
+ type List<T> = List.List<T>;
126
+
127
+ /// Equality function for two `Key<K>`s, in terms of equality of `K`'s.
128
+ public func equalKey<K>(keq : (K, K) -> Bool) : ((Key<K>, Key<K>) -> Bool) {
129
+ func (key1 : Key<K>, key2 : Key<K>) : Bool {
130
+ Hash.equal(key1.hash, key2.hash) and keq(key1.key, key2.key)
131
+ }
132
+ };
133
+
134
+ /// @deprecated `isValid` is an internal predicate and will be removed in future.
135
+ public func isValid<K, V>(t : Trie<K, V>, _enforceNormal : Bool) : Bool {
136
+ func rec(t : Trie<K, V>, bitpos : ?Hash.Hash, bits : Hash.Hash, mask : Hash.Hash) : Bool {
137
+ switch t {
138
+ case (#empty) {
139
+ true;
140
+ };
141
+ case (#leaf(l)) {
142
+ let len = List.size(l.keyvals);
143
+ len <= MAX_LEAF_SIZE
144
+ and
145
+ len == l.size
146
+ and
147
+ List.all(
148
+ l.keyvals,
149
+ func ((k : Key<K>, v : V)) : Bool {
150
+ ((k.hash & mask) == bits)
151
+ }
152
+ )
153
+ };
154
+ case (#branch(b)) {
155
+ let bitpos1 =
156
+ switch bitpos {
157
+ case null {Prim.natToNat32(0)};
158
+ case (?bp) {Prim.natToNat32(Prim.nat32ToNat(bp) + 1)}
159
+ };
160
+ let mask1 = mask | (Prim.natToNat32(1) << bitpos1);
161
+ let bits1 = bits | (Prim.natToNat32(1) << bitpos1);
162
+ let sum = size(b.left) + size(b.right);
163
+ (b.size == sum) and
164
+ rec(b.left, ?bitpos1, bits, mask1) and
165
+ rec(b.right, ?bitpos1, bits1, mask1)
166
+ };
167
+ }
168
+ };
169
+ rec(t, null, 0, 0)
170
+ };
171
+
172
+
173
+ /// A 2D trie maps dimension-1 keys to another
174
+ /// layer of tries, each keyed on the dimension-2 keys.
175
+ public type Trie2D<K1, K2, V> = Trie<K1, Trie<K2, V>>;
176
+
177
+ /// A 3D trie maps dimension-1 keys to another
178
+ /// layer of 2D tries, each keyed on the dimension-2 and dimension-3 keys.
179
+ public type Trie3D<K1, K2, K3, V> = Trie<K1, Trie2D<K2, K3, V> >;
180
+
181
+ /// An empty trie.
182
+ public func empty<K, V>() : Trie<K, V> { #empty; };
183
+
184
+ /// Get the number of key-value pairs in the trie, in constant time.
185
+
186
+ /// Get size in O(1) time.
187
+ public func size<K, V>(t : Trie<K, V>) : Nat {
188
+ switch t {
189
+ case (#empty) { 0 };
190
+ case (#leaf(l)) { l.size };
191
+ case (#branch(b)) { b.size };
192
+ }
193
+ };
194
+
195
+ /// Construct a branch node, computing the size stored there.
196
+ public func branch<K, V>(l : Trie<K, V>, r : Trie<K, V>) : Trie<K, V> {
197
+ let sum = size(l) + size(r);
198
+ #branch {
199
+ size = sum;
200
+ left = l;
201
+ right = r
202
+ };
203
+ };
204
+
205
+ /// Construct a leaf node, computing the size stored there.
206
+ ///
207
+ /// This helper function automatically enforces the MAX_LEAF_SIZE
208
+ /// by constructing branches as necessary; to do so, it also needs the bitpos
209
+ /// of the leaf.
210
+ public func leaf<K, V>(kvs : AssocList<Key<K>, V>, bitpos : Nat) : Trie<K, V> {
211
+ fromList(null, kvs, bitpos)
212
+ };
213
+
214
+ module ListUtil {
215
+ /* Deprecated: List.lenClamp */
216
+ /// Return the list length unless the number of items in the list exceeds
217
+ /// a maximum value. If the list length exceed the maximum, the function
218
+ /// returns `null`.
219
+ public func lenClamp<T>(l : List<T>, max : Nat) : ?Nat {
220
+ func rec(l : List<T>, max : Nat, i : Nat) : ?Nat {
221
+ switch l {
222
+ case null { ?i };
223
+ case (?(_, t)) {
224
+ if ( i >= max ) { null }
225
+ else { rec(t, max, i + 1) }
226
+ };
227
+ }
228
+ };
229
+ rec(l, max, 0)
230
+ };
231
+ };
232
+
233
+ /// Transform a list into a trie, splitting input list into small (leaf) lists, if necessary.
234
+ public func fromList<K, V>(kvc : ?Nat, kvs : AssocList<Key<K>, V>, bitpos : Nat) : Trie<K, V> {
235
+ func rec(kvc : ?Nat, kvs : AssocList<Key<K>, V>, bitpos : Nat) : Trie<K, V> {
236
+ switch kvc {
237
+ case null {
238
+ switch (ListUtil.lenClamp(kvs, MAX_LEAF_SIZE)) {
239
+ case null {} /* fall through to branch case. */;
240
+ case (?len) {
241
+ return #leaf({ size = len; keyvals = kvs })
242
+ };
243
+ }
244
+ };
245
+ case (?c) {
246
+ if ( c == 0 ) {
247
+ return #empty
248
+ } else if ( c <= MAX_LEAF_SIZE ) {
249
+ return #leaf({ size = c; keyvals = kvs })
250
+ } else {
251
+ /* fall through to branch case */
252
+ }
253
+ };
254
+ };
255
+ let (ls, l, rs, r) = splitList(kvs, bitpos);
256
+ if ( ls == 0 and rs == 0 ) {
257
+ #empty
258
+ } else if (rs == 0 and ls <= MAX_LEAF_SIZE) {
259
+ #leaf({ size = ls; keyvals = l })
260
+ } else if (ls == 0 and rs <= MAX_LEAF_SIZE) {
261
+ #leaf({ size = rs; keyvals = r })
262
+ } else {
263
+ branch(rec(?ls, l, bitpos + 1), rec(?rs, r, bitpos + 1))
264
+ }
265
+ };
266
+ rec(kvc, kvs, bitpos)
267
+ };
268
+
269
+ /// Clone the trie efficiently, via sharing.
270
+ ///
271
+ /// Purely-functional representation permits _O(1)_ copy, via persistent sharing.
272
+ public func clone<K, V>(t : Trie<K, V>) : Trie<K, V> = t;
273
+
274
+ /// Replace the given key's value option with the given one, returning the previous one
275
+ public func replace<K, V>(t : Trie<K, V>, k : Key<K>, k_eq : (K, K) -> Bool, v : ?V) : (Trie<K, V>, ?V) {
276
+ let key_eq = equalKey(k_eq);
277
+
278
+ func rec(t : Trie<K, V>, bitpos : Nat) : (Trie<K, V>, ?V) {
279
+ switch t {
280
+ case (#empty) {
281
+ let (kvs, _) = AssocList.replace(null, k, key_eq, v);
282
+ (leaf(kvs, bitpos), null)
283
+ };
284
+ case (#branch(b)) {
285
+ let bit = Hash.bit(k.hash, bitpos);
286
+ // rebuild either the left or right path with the (k, v) pair
287
+ if (not bit) {
288
+ let (l, v_) = rec(b.left, bitpos + 1);
289
+ (branch(l, b.right), v_)
290
+ }
291
+ else {
292
+ let (r, v_) = rec(b.right, bitpos + 1);
293
+ (branch(b.left, r), v_)
294
+ }
295
+ };
296
+ case (#leaf(l)) {
297
+ let (kvs2, old_val) =
298
+ AssocList.replace(l.keyvals, k, key_eq, v);
299
+ (leaf(kvs2, bitpos), old_val)
300
+ };
301
+ }
302
+ };
303
+ let (to, vo) = rec(t, 0);
304
+ //assert(isValid<K, V>(to, false));
305
+ (to, vo)
306
+ };
307
+
308
+ /// Put the given key's value in the trie; return the new trie, and the previous value associated with the key, if any
309
+ public func put<K, V>(t : Trie<K, V>, k : Key<K>, k_eq : (K, K) -> Bool, v : V) : (Trie<K, V>, ?V) {
310
+ replace(t, k, k_eq, ?v)
311
+ };
312
+
313
+ /// Get the value of the given key in the trie, or return null if nonexistent
314
+ public func get<K, V>(t : Trie<K, V>, k : Key<K>, k_eq : (K, K) -> Bool) : ?V =
315
+ find(t, k, k_eq);
316
+
317
+ /// Find the given key's value in the trie, or return null if nonexistent
318
+ public func find<K, V>(t : Trie<K, V>, k : Key<K>, k_eq : (K, K) -> Bool) : ?V {
319
+ let key_eq = equalKey(k_eq);
320
+ func rec(t : Trie<K, V>, bitpos : Nat) : ?V {
321
+ switch t {
322
+ case (#empty) { null };
323
+ case (#leaf(l)) {
324
+ AssocList.find(l.keyvals, k, key_eq)
325
+ };
326
+ case (#branch(b)) {
327
+ let bit = Hash.bit(k.hash, bitpos);
328
+ if (not bit) {
329
+ rec(b.left, bitpos + 1)
330
+ }
331
+ else {
332
+ rec(b.right, bitpos + 1)
333
+ }
334
+ };
335
+ }
336
+ };
337
+ rec(t, 0)
338
+ };
339
+
340
+
341
+
342
+ func splitAssocList<K, V>(al : AssocList<Key<K>, V>, bitpos : Nat)
343
+ : (AssocList<Key<K>, V>, AssocList<Key<K>, V>)
344
+ {
345
+ List.partition(
346
+ al,
347
+ func ((k : Key<K>, v : V)) : Bool {
348
+ not Hash.bit(k.hash, bitpos)
349
+ }
350
+ )
351
+ };
352
+
353
+ func splitList<K, V>(l : AssocList<Key<K>, V>, bitpos : Nat)
354
+ : (Nat, AssocList<Key<K>, V>, Nat, AssocList<Key<K>, V>)
355
+ {
356
+ func rec(l : AssocList<Key<K>, V>) : (Nat, AssocList<Key<K>, V>, Nat, AssocList<Key<K>, V>) {
357
+ switch l {
358
+ case null { (0, null, 0, null) };
359
+ case (?((k, v), t)) {
360
+ let (cl, l, cr, r) = rec(t) ;
361
+ if (not Hash.bit(k.hash, bitpos)){
362
+ (cl + 1, ?((k, v), l), cr, r)
363
+ } else {
364
+ (cl, l, cr + 1, ?((k, v), r))
365
+ }
366
+ };
367
+ }
368
+ };
369
+ rec(l)
370
+ };
371
+
372
+ /// Merge tries, preferring the left trie where there are collisions
373
+ /// in common keys.
374
+ ///
375
+ /// note: the `disj` operation generalizes this `merge`
376
+ /// operation in various ways, and does not (in general) lose
377
+ /// information; this operation is a simpler, special case.
378
+ public func merge<K, V>(tl : Trie<K, V>, tr : Trie<K, V>, k_eq : (K, K) -> Bool) : Trie<K, V> {
379
+ let key_eq = equalKey(k_eq);
380
+ func rec(bitpos : Nat, tl : Trie<K, V>, tr : Trie<K, V>) : Trie<K, V> {
381
+ switch (tl, tr) {
382
+ case (#empty, _) { return tr };
383
+ case (_, #empty) { return tl };
384
+ case (#leaf(l1), #leaf(l2)) {
385
+ leaf(
386
+ AssocList.disj(
387
+ l1.keyvals, l2.keyvals,
388
+ key_eq,
389
+ func (x : ?V, y : ?V) : V {
390
+ switch (x, y) {
391
+ case (null, null) { P.unreachable() };
392
+ case (null, ?v) { v };
393
+ case (?v, _) { v };
394
+ }
395
+ }
396
+ ),
397
+ bitpos)
398
+ };
399
+ case (#leaf(l), _) {
400
+ let (ll, lr) = splitAssocList(l.keyvals, bitpos);
401
+ rec(bitpos, branch(leaf(ll, bitpos), leaf(lr, bitpos)), tr)
402
+ };
403
+ case (_, #leaf(l)) {
404
+ let (ll, lr) = splitAssocList(l.keyvals, bitpos);
405
+ rec(bitpos, tl, branch(leaf(ll, bitpos), leaf(lr, bitpos)))
406
+ };
407
+ case (#branch(b1), #branch(b2)) {
408
+ branch(rec(bitpos + 1, b1.left, b2.left),
409
+ rec(bitpos + 1, b1.right, b2.right))
410
+ };
411
+ }
412
+ };
413
+ rec(0, tl, tr)
414
+ };
415
+
416
+ /// Merge tries like `merge`, except signals a
417
+ /// dynamic error if there are collisions in common keys between the
418
+ /// left and right inputs.
419
+ public func mergeDisjoint<K, V>(tl : Trie<K, V>, tr : Trie<K, V>, k_eq : (K, K) -> Bool) : Trie<K, V> {
420
+ let key_eq = equalKey(k_eq);
421
+
422
+ func rec(bitpos : Nat, tl : Trie<K, V>, tr : Trie<K, V>) : Trie<K, V> {
423
+ switch (tl, tr) {
424
+ case (#empty, _) { return tr };
425
+ case (_, #empty) { return tl };
426
+ case (#leaf(l1), #leaf(l2)) {
427
+ leaf(
428
+ AssocList.disjDisjoint(
429
+ l1.keyvals, l2.keyvals,
430
+ func (x : ?V, y : ?V) : V {
431
+ switch (x, y) {
432
+ case (null, ?v) { v };
433
+ case (?v, null) { v };
434
+ case (_, _) { P.unreachable() };
435
+ }
436
+ }
437
+ ),
438
+ bitpos
439
+ )
440
+ };
441
+ case (#leaf(l), _) {
442
+ let (ll, lr) = splitAssocList(l.keyvals, bitpos);
443
+ rec(bitpos, branch(leaf(ll, bitpos), leaf(lr, bitpos)), tr)
444
+ };
445
+ case (_, #leaf(l)) {
446
+ let (ll, lr) = splitAssocList(l.keyvals, bitpos);
447
+ rec(bitpos, tl, branch(leaf(ll, bitpos), leaf(lr, bitpos)))
448
+ };
449
+ case (#branch(b1), #branch(b2)) {
450
+ branch(
451
+ rec(bitpos + 1, b1.left, b2.left),
452
+ rec(bitpos + 1, b1.right, b2.right)
453
+ )
454
+ };
455
+ }
456
+ };
457
+ rec(0, tl, tr)
458
+ };
459
+
460
+ /// Difference of tries. The output consists are pairs of
461
+ /// the left trie whose keys are not present in the right trie; the
462
+ /// values of the right trie are irrelevant.
463
+ public func diff<K, V, W>(tl : Trie<K, V>, tr : Trie<K, W>, k_eq : ( K, K) -> Bool) : Trie<K, V> {
464
+ let key_eq = equalKey(k_eq);
465
+
466
+ func rec(bitpos : Nat, tl : Trie<K, V>, tr : Trie<K, W>) : Trie<K, V> {
467
+ switch (tl, tr) {
468
+ case (#empty, _) { return #empty };
469
+ case (_, #empty) { return tl };
470
+ case (#leaf(l1), #leaf(l2)) {
471
+ leaf(
472
+ AssocList.diff(
473
+ l1.keyvals, l2.keyvals,
474
+ key_eq,
475
+ ),
476
+ bitpos
477
+ )
478
+ };
479
+ case (#leaf(l), _) {
480
+ let (ll, lr) = splitAssocList(l.keyvals, bitpos);
481
+ rec(bitpos, branch(leaf(ll, bitpos), leaf(lr, bitpos)), tr)
482
+ };
483
+ case (_, #leaf(l)) {
484
+ let (ll, lr) = splitAssocList(l.keyvals, bitpos);
485
+ rec(bitpos, tl, branch(leaf(ll, bitpos), leaf(lr, bitpos)))
486
+ };
487
+ case (#branch(b1), #branch(b2)) {
488
+ branch(rec(bitpos + 1, b1.left, b2.left),
489
+ rec(bitpos + 1, b1.right, b2.right))
490
+ };
491
+ }
492
+ };
493
+ rec(0, tl, tr)
494
+ };
495
+
496
+ /// Map disjunction.
497
+ ///
498
+ /// This operation generalizes the notion of "set union" to finite maps.
499
+ ///
500
+ /// Produces a "disjunctive image" of the two tries, where the values of
501
+ /// matching keys are combined with the given binary operator.
502
+ ///
503
+ /// For unmatched key-value pairs, the operator is still applied to
504
+ /// create the value in the image. To accomodate these various
505
+ /// situations, the operator accepts optional values, but is never
506
+ /// applied to (null, null).
507
+ ///
508
+ /// Implements the database idea of an ["outer join"](https://stackoverflow.com/questions/38549/what-is-the-difference-between-inner-join-and-outer-join).
509
+ ///
510
+ public func disj<K, V, W, X>(
511
+ tl : Trie<K, V>,
512
+ tr : Trie<K, W>,
513
+ k_eq : (K, K) -> Bool,
514
+ vbin : (?V, ?W) -> X
515
+ ) : Trie<K, X> {
516
+ let key_eq = equalKey(k_eq);
517
+
518
+ /* empty right case; build from left only: */
519
+ func recL(t : Trie<K, V>, bitpos : Nat) : Trie<K, X> {
520
+ switch t {
521
+ case (#empty) { #empty };
522
+ case (#leaf(l)) {
523
+ leaf(AssocList.disj(l.keyvals, null, key_eq, vbin), bitpos)
524
+ };
525
+ case (#branch(b)) {
526
+ branch(recL(b.left, bitpos + 1),
527
+ recL(b.right, bitpos + 1)) };
528
+ }
529
+ };
530
+
531
+ /* empty left case; build from right only: */
532
+ func recR(t : Trie<K, W>, bitpos : Nat) : Trie<K, X> {
533
+ switch t {
534
+ case (#empty) { #empty };
535
+ case (#leaf(l)) {
536
+ leaf(AssocList.disj(null, l.keyvals, key_eq, vbin), bitpos)
537
+ };
538
+ case (#branch(b)) {
539
+ branch(recR(b.left, bitpos + 1),
540
+ recR(b.right, bitpos + 1)) };
541
+ }
542
+ };
543
+
544
+ /* main recursion */
545
+ func rec(bitpos : Nat, tl : Trie<K, V>, tr : Trie<K, W>) : Trie<K, X> {
546
+ switch (tl, tr) {
547
+ case (#empty, #empty) { #empty };
548
+ case (#empty, _) { recR(tr, bitpos) };
549
+ case (_, #empty) { recL(tl, bitpos) };
550
+ case (#leaf(l1), #leaf(l2)) {
551
+ leaf(AssocList.disj(l1.keyvals, l2.keyvals, key_eq, vbin), bitpos)
552
+ };
553
+ case (#leaf(l), _) {
554
+ let (ll, lr) = splitAssocList(l.keyvals, bitpos);
555
+ rec(bitpos, branch(leaf(ll, bitpos), leaf(lr, bitpos)), tr)
556
+ };
557
+ case (_, #leaf(l)) {
558
+ let (ll, lr) = splitAssocList(l.keyvals, bitpos);
559
+ rec(bitpos, tl, branch(leaf(ll, bitpos), leaf(lr, bitpos)))
560
+ };
561
+ case (#branch(b1), #branch(b2)) {
562
+ branch(rec(bitpos + 1, b1.left, b2.left),
563
+ rec(bitpos + 1, b1.right, b2.right))
564
+ };
565
+ }
566
+ };
567
+
568
+ rec(0, tl, tr)
569
+ };
570
+
571
+ /// Map join.
572
+ ///
573
+ /// Implements the database idea of an ["inner join"](https://stackoverflow.com/questions/38549/what-is-the-difference-between-inner-join-and-outer-join).
574
+ ///
575
+ /// This operation generalizes the notion of "set intersection" to
576
+ /// finite maps. The values of matching keys are combined with the given binary
577
+ /// operator, and unmatched key-value pairs are not present in the output.
578
+ ///
579
+ public func join<K, V, W, X>(
580
+ tl : Trie<K, V>,
581
+ tr : Trie<K, W>,
582
+ k_eq : (K, K) -> Bool,
583
+ vbin : (V, W) -> X
584
+ ) : Trie<K, X> {
585
+ let key_eq = equalKey(k_eq);
586
+
587
+ func rec(bitpos : Nat, tl : Trie<K, V>, tr : Trie<K, W>) : Trie<K, X> {
588
+ switch (tl, tr) {
589
+ case (#empty, _) { #empty };
590
+ case (_, #empty) { #empty };
591
+ case (#leaf(l1), #leaf(l2)) {
592
+ leaf(AssocList.join(l1.keyvals, l2.keyvals, key_eq, vbin), bitpos)
593
+ };
594
+ case (#leaf(l), _) {
595
+ let (ll, lr) = splitAssocList(l.keyvals, bitpos);
596
+ rec(bitpos, branch(leaf(ll, bitpos), leaf(lr, bitpos)), tr)
597
+ };
598
+ case (_, #leaf(l)) {
599
+ let (ll, lr) = splitAssocList(l.keyvals, bitpos);
600
+ rec(bitpos, tl, branch(leaf(ll, bitpos), leaf(lr, bitpos)))
601
+ };
602
+ case (#branch(b1), #branch(b2)) {
603
+ branch(rec(bitpos + 1, b1.left, b2.left),
604
+ rec(bitpos + 1, b1.right, b2.right))
605
+ };
606
+ }
607
+ };
608
+
609
+ rec(0, tl, tr)
610
+ };
611
+
612
+ /// This operation gives a recursor for the internal structure of
613
+ /// tries. Many common operations are instantiations of this function,
614
+ /// either as clients, or as hand-specialized versions (e.g., see , map,
615
+ /// mapFilter, some and all below).
616
+ public func foldUp<K, V, X>(t : Trie<K, V>, bin : (X, X) -> X, leaf : (K, V) -> X, empty : X) : X {
617
+ func rec(t : Trie<K, V>) : X {
618
+ switch t {
619
+ case (#empty) { empty };
620
+ case (#leaf(l)) {
621
+ AssocList.fold(
622
+ l.keyvals, empty,
623
+ func (k : Key<K>, v : V, x : X) : X { bin(leaf(k.key, v), x) }
624
+ )
625
+ };
626
+ case (#branch(b)) { bin(rec(b.left), rec(b.right)) };
627
+ }
628
+ };
629
+ rec(t)
630
+ };
631
+
632
+
633
+ /// Map product.
634
+ ///
635
+ /// Conditional _catesian product_, where the given
636
+ /// operation `op` _conditionally_ creates output elements in the
637
+ /// resulting trie.
638
+ ///
639
+ /// The keyed structure of the input tries are not relevant for this
640
+ /// operation: all pairs are considered, regardless of keys matching or
641
+ /// not. Moreover, the resulting trie may use keys that are unrelated to
642
+ /// these input keys.
643
+ ///
644
+ public func prod<K1, V1, K2, V2, K3, V3>(
645
+ tl : Trie<K1, V1>,
646
+ tr : Trie<K2, V2>,
647
+ op : (K1, V1, K2, V2) -> ?(Key<K3>, V3),
648
+ k3_eq : (K3, K3) -> Bool
649
+ ) : Trie<K3, V3> {
650
+
651
+ /*- binary case: merge disjoint results: */
652
+ func merge (a : Trie<K3, V3>, b : Trie<K3, V3>) : Trie<K3, V3> =
653
+ mergeDisjoint(a, b, k3_eq);
654
+
655
+ /*- "`foldUp` squared" (imagine two nested loops): */
656
+ foldUp(
657
+ tl, merge,
658
+ func (k1 : K1, v1 : V1) : Trie<K3, V3> {
659
+ foldUp(
660
+ tr, merge,
661
+ func (k2 : K2, v2 : V2) : Trie<K3, V3> {
662
+ switch (op(k1, v1, k2, v2)) {
663
+ case null { #empty };
664
+ case (?(k3, v3)) { (put(#empty, k3, k3_eq, v3)).0 };
665
+ }
666
+ },
667
+ #empty
668
+ )
669
+ },
670
+ #empty
671
+ )
672
+ };
673
+
674
+ /// Returns an `Iter` over the key-value entries of the trie.
675
+ ///
676
+ /// Each iterator gets a _persistent view_ of the mapping, independent of concurrent updates to the iterated map.
677
+ public func iter<K, V>(t : Trie<K, V>) : I.Iter<(K, V)> {
678
+ object {
679
+ var stack = ?(t, null) : List.List<Trie<K, V>>;
680
+ public func next() : ?(K, V) {
681
+ switch stack {
682
+ case null { null };
683
+ case (?(trie, stack2)) {
684
+ switch trie {
685
+ case (#empty) {
686
+ stack := stack2;
687
+ next()
688
+ };
689
+ case (#leaf({ keyvals = null })) {
690
+ stack := stack2;
691
+ next()
692
+ };
693
+ case (#leaf({ size = c; keyvals = ?((k, v), kvs) })) {
694
+ stack := ?(#leaf({ size = c-1; keyvals = kvs }), stack2);
695
+ ?(k.key, v)
696
+ };
697
+ case (#branch(br)) {
698
+ stack := ?(br.left, ?(br.right, stack2));
699
+ next()
700
+ };
701
+ }
702
+ }
703
+ }
704
+ }
705
+ }
706
+ };
707
+
708
+ /// Represent the construction of tries as data.
709
+ ///
710
+ /// This module provides optimized variants of normal tries, for
711
+ /// more efficient join queries.
712
+ ///
713
+ /// The central insight is that for (unmaterialized) join query results, we
714
+ /// do not need to actually build any resulting trie of the resulting
715
+ /// data, but rather, just need a collection of what would be in that
716
+ /// trie. Since query results can be large (quadratic in the DB size),
717
+ /// avoiding the construction of this trie provides a considerable savings.
718
+ ///
719
+ /// To get this savings, we use an ADT for the operations that _would_ build this trie,
720
+ /// if evaluated. This structure specializes a rope: a balanced tree representing a
721
+ /// sequence. It is only as balanced as the tries from which we generate
722
+ /// these build ASTs. They have no intrinsic balance properties of their
723
+ /// own.
724
+ ///
725
+ public module Build {
726
+ /// The build of a trie, as an AST for a simple DSL.
727
+ public type Build<K, V> = {
728
+ #skip ;
729
+ #put : (K, ?Hash.Hash, V) ;
730
+ #seq : {
731
+ size : Nat;
732
+ left : Build<K, V>;
733
+ right : Build<K, V>;
734
+ } ;
735
+ };
736
+
737
+ /// Size of the build, measured in `#put` operations
738
+ public func size<K, V>(tb : Build<K, V>) : Nat {
739
+ switch tb {
740
+ case (#skip) { 0 };
741
+ case (#put(_, _, _)) { 1 };
742
+ case (#seq(seq)) { seq.size };
743
+ }
744
+ };
745
+
746
+ /// Build sequence of two sub-builds
747
+ public func seq<K, V>(l : Build<K, V>, r : Build<K, V>) : Build<K, V> {
748
+ let sum = size(l) + size(r);
749
+ #seq({ size = sum; left = l; right = r })
750
+ };
751
+
752
+ /// Like [`prod`](#prod), except do not actually do the put calls, just
753
+ /// record them, as a (binary tree) data structure, isomorphic to the
754
+ /// recursion of this function (which is balanced, in expectation).
755
+ public func prod<K1, V1, K2, V2, K3, V3>(
756
+ tl : Trie<K1, V1>,
757
+ tr : Trie<K2, V2>,
758
+ op : (K1, V1, K2, V2) -> ?(K3, V3),
759
+ k3_eq : (K3, K3) -> Bool
760
+ ) : Build<K3, V3> {
761
+
762
+ func outer_bin (a : Build<K3, V3>, b : Build<K3, V3>)
763
+ : Build<K3, V3> {
764
+ seq(a, b)
765
+ };
766
+
767
+ func inner_bin (a : Build<K3, V3>, b : Build<K3, V3>)
768
+ : Build<K3, V3> {
769
+ seq(a, b)
770
+ };
771
+
772
+ /// double-nested folds
773
+ foldUp(
774
+ tl, outer_bin,
775
+ func (k1 : K1, v1 : V1) : Build<K3, V3> {
776
+ foldUp(
777
+ tr, inner_bin,
778
+ func (k2 : K2, v2 : V2) : Build<K3, V3> {
779
+ switch (op(k1, v1, k2, v2)) {
780
+ case null { #skip };
781
+ case (?(k3, v3)) { #put(k3, null, v3) };
782
+ }
783
+ },
784
+ #skip
785
+ )
786
+ },
787
+ #skip
788
+ )
789
+ };
790
+
791
+ /// Project the nth key-value pair from the trie build.
792
+ ///
793
+ /// This position is meaningful only when the build contains multiple uses of one or more keys, otherwise it is not.
794
+ public func nth<K, V>(tb : Build<K, V>, i : Nat) : ?(K, ?Hash.Hash, V) {
795
+ func rec(tb : Build<K, V>, i : Nat) : ?(K, ?Hash.Hash, V) {
796
+ switch tb {
797
+ case (#skip) { P.unreachable() };
798
+ case (#put(k, h, v)) {
799
+ assert(i == 0);
800
+ ?(k, h, v)
801
+ };
802
+ case (#seq(s)) {
803
+ let size_left = size(s.left);
804
+ if (i < size_left) { rec(s.left, i) }
805
+ else { rec(s.right, i - size_left) }
806
+ };
807
+ }
808
+ };
809
+
810
+ if (i >= size(tb)) {
811
+ return null
812
+ };
813
+ rec(tb, i)
814
+ };
815
+
816
+ /// Like [`mergeDisjoint`](#mergedisjoint), except that it avoids the
817
+ /// work of actually merging any tries; rather, just record the work for
818
+ /// latter (if ever).
819
+ public func projectInner<K1, K2, V>(t : Trie<K1, Build<K2, V>>)
820
+ : Build<K2, V> {
821
+ foldUp(
822
+ t,
823
+ func (t1 : Build<K2, V>, t2 : Build<K2, V>) : Build<K2, V> { seq(t1, t2) },
824
+ func (_ : K1, t : Build<K2, V>) : Build<K2, V> { t },
825
+ #skip
826
+ )
827
+ };
828
+
829
+ /// Gather the collection of key-value pairs into an array of a (possibly-distinct) type.
830
+ public func toArray<K, V, W>(tb : Build<K, V>, f: (K, V) -> W) : [W] {
831
+ let c = size(tb);
832
+ let a = A.init<?W>(c, null);
833
+ var i = 0;
834
+ func rec(tb : Build<K, V>) {
835
+ switch tb {
836
+ case (#skip) {};
837
+ case (#put(k, _, v)) { a[i] := ?f(k, v); i := i + 1 };
838
+ case (#seq(s)) { rec(s.left); rec(s.right) };
839
+ }
840
+ };
841
+ rec(tb);
842
+ A.tabulate(c, func(i : Nat) : W {
843
+ switch (a[i]) {
844
+ case null { P.unreachable() };
845
+ case (?x) { x }
846
+ }})
847
+ };
848
+
849
+ };
850
+
851
+ /// Fold over the key-value pairs of the trie, using an accumulator.
852
+ /// The key-value pairs have no reliable or meaningful ordering.
853
+ public func fold<K, V, X>(t : Trie<K, V>, f : (K, V, X) -> X, x : X) : X {
854
+ func rec(t : Trie<K, V>, x : X) : X {
855
+ switch t {
856
+ case (#empty) { x };
857
+ case (#leaf(l)) {
858
+ AssocList.fold(
859
+ l.keyvals, x,
860
+ func (k : Key<K>, v : V, x : X) : X = f(k.key, v, x)
861
+ )
862
+ };
863
+ case (#branch(b)) { rec(b.left, rec(b.right, x)) };
864
+ };
865
+ };
866
+ rec(t, x)
867
+ };
868
+
869
+ /// Test whether a given key-value pair is present, or not.
870
+ public func some<K, V>(t : Trie<K, V>, f : (K, V) -> Bool) : Bool {
871
+ func rec(t : Trie<K, V>) : Bool {
872
+ switch t {
873
+ case (#empty) { false };
874
+ case (#leaf(l)) {
875
+ List.some(
876
+ l.keyvals, func ((k : Key<K>, v:V)) : Bool = f(k.key, v)
877
+ )
878
+ };
879
+ case (#branch(b)) { rec(b.left) or rec(b.right) };
880
+ };
881
+ };
882
+ rec(t)
883
+ };
884
+
885
+ /// Test whether all key-value pairs have a given property.
886
+ public func all<K, V>(t : Trie<K, V>, f : (K, V) -> Bool) : Bool {
887
+ func rec(t : Trie<K, V>) : Bool {
888
+ switch t {
889
+ case (#empty) { true };
890
+ case (#leaf(l)) {
891
+ List.all(
892
+ l.keyvals, func ((k : Key<K>, v : V)) : Bool=f(k.key, v)
893
+ )
894
+ };
895
+ case (#branch(b)) { rec(b.left) and rec(b.right) };
896
+ };
897
+ };
898
+ rec(t)
899
+ };
900
+
901
+ /// Project the nth key-value pair from the trie.
902
+ ///
903
+ /// Note: This position is not meaningful; it's only here so that we
904
+ /// can inject tries into arrays using functions like `Array.tabulate`.
905
+ public func nth<K, V>(t : Trie<K, V>, i : Nat) : ?(Key<K>, V) {
906
+ func rec(t : Trie<K, V>, i : Nat) : ?(Key<K>, V) {
907
+ switch t {
908
+ case (#empty) { P.unreachable() };
909
+ case (#leaf(l)) { List.get(l.keyvals, i) };
910
+ case (#branch(b)) {
911
+ let size_left = size(b.left);
912
+ if (i < size_left) { rec(b.left, i) }
913
+ else { rec(b.right, i - size_left) }
914
+ }
915
+ }
916
+ };
917
+ if (i >= size(t)) {
918
+ return null
919
+ };
920
+ rec(t, i)
921
+ };
922
+
923
+
924
+ /// Gather the collection of key-value pairs into an array of a (possibly-distinct) type.
925
+ public func toArray<K, V, W>(t : Trie<K, V>, f : (K, V) -> W) : [W] {
926
+ let a = A.tabulate<W> (
927
+ size(t),
928
+ func (i : Nat) : W {
929
+ let (k, v) = switch (nth(t, i)) {
930
+ case null { P.unreachable() };
931
+ case (?x) { x };
932
+ };
933
+ f(k.key, v)
934
+ }
935
+ );
936
+ a
937
+ };
938
+
939
+ /// Test for "deep emptiness": subtrees that have branching structure,
940
+ /// but no leaves. These can result from naive filtering operations;
941
+ /// filter uses this function to avoid creating such subtrees.
942
+ public func isEmpty<K, V>(t : Trie<K, V>) : Bool {
943
+ size(t) == 0
944
+ };
945
+
946
+ /// Filter the key-value pairs by a given predicate.
947
+ public func filter<K, V>(t : Trie<K, V>, f : (K, V) -> Bool) : Trie<K, V> {
948
+ func rec(t : Trie<K, V>, bitpos : Nat) : Trie<K, V> {
949
+ switch t {
950
+ case (#empty) { #empty };
951
+ case (#leaf(l)) {
952
+ leaf(
953
+ List.filter(
954
+ l.keyvals,
955
+ func ((k : Key<K>, v : V)) : Bool = f(k.key, v)
956
+ ),
957
+ bitpos
958
+ )
959
+ };
960
+ case (#branch(b)) {
961
+ let fl = rec(b.left, bitpos + 1);
962
+ let fr = rec(b.right, bitpos + 1);
963
+ if (isEmpty(fl) and isEmpty(fr)) {
964
+ #empty
965
+ } else {
966
+ branch(fl, fr)
967
+ }
968
+ }
969
+ }
970
+ };
971
+ rec(t, 0)
972
+ };
973
+
974
+ /// Map and filter the key-value pairs by a given predicate.
975
+ public func mapFilter<K, V, W>(t : Trie<K, V>, f : (K, V) -> ?W) : Trie<K, W> {
976
+ func rec(t : Trie<K, V>, bitpos : Nat) : Trie<K, W> {
977
+ switch t {
978
+ case (#empty) { #empty };
979
+ case (#leaf(l)) {
980
+ leaf(
981
+ List.mapFilter(
982
+ l.keyvals,
983
+ // retain key and hash, but update key's value using f:
984
+ func ((k : Key<K>, v : V)) : ?(Key<K>, W) {
985
+ switch (f(k.key, v)) {
986
+ case null { null };
987
+ case (?w) { ?({key = k.key; hash = k.hash}, w) };
988
+ }
989
+ }
990
+ ),
991
+ bitpos
992
+ )
993
+ };
994
+ case (#branch(b)) {
995
+ let fl = rec(b.left, bitpos + 1);
996
+ let fr = rec(b.right, bitpos + 1);
997
+ if (isEmpty(fl) and isEmpty(fr)) {
998
+ #empty
999
+ } else {
1000
+ branch(fl, fr)
1001
+ }
1002
+ }
1003
+ }
1004
+ };
1005
+
1006
+ rec(t, 0)
1007
+ };
1008
+
1009
+ /// Test for equality, but naively, based on structure.
1010
+ /// Does not attempt to remove "junk" in the tree;
1011
+ /// For instance, a "smarter" approach would equate
1012
+ /// `#bin {left = #empty; right = #empty}`
1013
+ /// with
1014
+ /// `#empty`.
1015
+ /// We do not observe that equality here.
1016
+ public func equalStructure<K, V>(
1017
+ tl : Trie<K, V>,
1018
+ tr : Trie<K, V>,
1019
+ keq : (K, K) -> Bool,
1020
+ veq : (V, V) -> Bool
1021
+ ) : Bool {
1022
+ func rec(tl : Trie<K, V>, tr : Trie<K, V>) : Bool {
1023
+ switch (tl, tr) {
1024
+ case (#empty, #empty) { true };
1025
+ case (#leaf(l1), #leaf(l2)) {
1026
+ List.equal(l1.keyvals, l2.keyvals,
1027
+ func ((k1 : Key<K>, v1 : V), (k2 : Key<K>, v2 : V)) : Bool =
1028
+ keq(k1.key, k2.key) and veq(v1, v2)
1029
+ )
1030
+ };
1031
+ case (#branch(b1), #branch(b2)) {
1032
+ rec(b1.left, b2.left) and rec(b2.right, b2.right)
1033
+ };
1034
+ case _ { false };
1035
+ }
1036
+ };
1037
+ rec(tl, tr)
1038
+ };
1039
+
1040
+ /// Replace the given key's value in the trie,
1041
+ /// and only if successful, do the success continuation,
1042
+ /// otherwise, return the failure value
1043
+ public func replaceThen<K, V, X>(
1044
+ t : Trie<K, V>, k : Key<K>, k_eq : (K, K) -> Bool, v2 : V,
1045
+ success: (Trie<K, V>, V) -> X,
1046
+ fail: () -> X
1047
+ ) : X {
1048
+ let (t2, ov) = replace(t, k, k_eq, ?v2);
1049
+ switch ov {
1050
+ case null { /* no prior value; failure to remove */ fail() };
1051
+ case (?v1) { success(t2, v1) };
1052
+ }
1053
+ };
1054
+
1055
+ /// Put the given key's value in the trie; return the new trie; assert that no prior value is associated with the key
1056
+ public func putFresh<K, V>(t : Trie<K, V>, k : Key<K>, k_eq : (K, K) -> Bool, v : V) : Trie<K, V> {
1057
+ let (t2, none) = replace(t, k, k_eq, ?v);
1058
+ switch none {
1059
+ case null {};
1060
+ case (?_) assert false;
1061
+ };
1062
+ t2
1063
+ };
1064
+
1065
+ /// Put the given key's value in the 2D trie; return the new 2D trie.
1066
+ public func put2D<K1, K2, V>(
1067
+ t : Trie2D<K1, K2, V>,
1068
+ k1 : Key<K1>,
1069
+ k1_eq : (K1, K1) -> Bool,
1070
+ k2 : Key<K2>,
1071
+ k2_eq : (K2, K2) -> Bool,
1072
+ v:V
1073
+ ) : Trie2D<K1, K2, V> {
1074
+ let inner = find(t, k1, k1_eq);
1075
+ let (updated_inner, _) = switch inner {
1076
+ case null { put(#empty, k2, k2_eq, v) };
1077
+ case (?inner) { put(inner, k2, k2_eq, v) };
1078
+ };
1079
+ let (updated_outer, _) = put(t, k1, k1_eq, updated_inner);
1080
+ updated_outer;
1081
+ };
1082
+
1083
+ /// Put the given key's value in the trie; return the new trie;
1084
+ public func put3D<K1, K2, K3, V> (
1085
+ t : Trie3D<K1, K2, K3, V>,
1086
+ k1 : Key<K1>,
1087
+ k1_eq : (K1, K1) -> Bool,
1088
+ k2 : Key<K2>,
1089
+ k2_eq : (K2, K2) -> Bool,
1090
+ k3 : Key<K3>,
1091
+ k3_eq : (K3, K3) -> Bool,
1092
+ v : V
1093
+ ) : Trie3D<K1, K2, K3, V> {
1094
+ let inner1 = find(t, k1, k1_eq);
1095
+ let (updated_inner1, _) = switch inner1 {
1096
+ case null {
1097
+ put(
1098
+ #empty, k2, k2_eq,
1099
+ (put(#empty, k3, k3_eq, v)).0
1100
+ )
1101
+ };
1102
+ case (?inner1) {
1103
+ let inner2 = find(inner1, k2, k2_eq);
1104
+ let (updated_inner2, _) = switch inner2 {
1105
+ case null { put(#empty, k3, k3_eq, v) };
1106
+ case (?inner2) { put(inner2, k3, k3_eq, v) };
1107
+ };
1108
+ put(inner1, k2, k2_eq, updated_inner2 )
1109
+ };
1110
+ };
1111
+ let (updated_outer, _) = put(t, k1, k1_eq, updated_inner1);
1112
+ updated_outer;
1113
+ };
1114
+
1115
+ /// Remove the given key's value in the trie; return the new trie
1116
+ public func remove<K, V>(t : Trie<K, V>, k : Key<K>, k_eq : (K, K) -> Bool) : (Trie<K, V>, ?V) {
1117
+ replace(t, k, k_eq, null)
1118
+ };
1119
+
1120
+ /// Remove the given key's value in the trie,
1121
+ /// and only if successful, do the success continuation,
1122
+ /// otherwise, return the failure value
1123
+ public func removeThen<K, V, X>(
1124
+ t : Trie<K, V>,
1125
+ k : Key<K>,
1126
+ k_eq : (K, K) -> Bool,
1127
+ success : (Trie<K, V>, V) -> X,
1128
+ fail : () -> X
1129
+ ) : X {
1130
+ let (t2, ov) = replace(t, k, k_eq, null);
1131
+ switch ov {
1132
+ case null { /* no prior value; failure to remove */ fail() };
1133
+ case (?v) { success(t2, v) };
1134
+ }
1135
+ };
1136
+
1137
+
1138
+ /// remove the given key-key pair's value in the 2D trie; return the
1139
+ /// new trie, and the prior value, if any.
1140
+ public func remove2D<K1, K2, V>(
1141
+ t : Trie2D<K1, K2, V>,
1142
+ k1 : Key<K1>,
1143
+ k1_eq : (K1, K1) -> Bool,
1144
+ k2 : Key<K2>,
1145
+ k2_eq : (K2, K2) -> Bool
1146
+ ) : (Trie2D<K1, K2, V>, ?V) {
1147
+ switch (find(t, k1, k1_eq)) {
1148
+ case null {
1149
+ (t, null)
1150
+ };
1151
+ case (?inner) {
1152
+ let (updated_inner, ov) = remove(inner, k2, k2_eq);
1153
+ let (updated_outer, _) = put(t, k1, k1_eq, updated_inner);
1154
+ (updated_outer, ov)
1155
+ };
1156
+ }
1157
+ };
1158
+
1159
+ /// Remove the given key-key pair's value in the 3D trie; return the
1160
+ /// new trie, and the prior value, if any.
1161
+ public func remove3D<K1, K2, K3, V>(
1162
+ t : Trie3D<K1, K2, K3, V>,
1163
+ k1 : Key<K1>,
1164
+ k1_eq : (K1, K1) -> Bool,
1165
+ k2 : Key<K2>,
1166
+ k2_eq : (K2, K2) -> Bool,
1167
+ k3 : Key<K3>,
1168
+ k3_eq : (K3, K3) -> Bool,
1169
+ ) : (Trie3D<K1, K2, K3, V>, ?V) {
1170
+ switch (find(t, k1, k1_eq)) {
1171
+ case null {
1172
+ (t, null)
1173
+ };
1174
+ case (?inner) {
1175
+ let (updated_inner, ov) = remove2D(inner, k2, k2_eq, k3, k3_eq);
1176
+ let (updated_outer, _) = put(t, k1, k1_eq, updated_inner);
1177
+ (updated_outer, ov)
1178
+ };
1179
+ }
1180
+ };
1181
+
1182
+ /// Like [`mergeDisjoint`](#mergedisjoint), except instead of merging a
1183
+ /// pair, it merges the collection of dimension-2 sub-trees of a 2D
1184
+ /// trie.
1185
+ public func mergeDisjoint2D<K1, K2, V>(
1186
+ t : Trie2D<K1, K2, V>,
1187
+ k1_eq : (K1, K1) -> Bool,
1188
+ k2_eq : (K2, K2) -> Bool
1189
+ ) : Trie<K2, V> {
1190
+ foldUp(
1191
+ t,
1192
+ func (t1 : Trie<K2, V>, t2 : Trie<K2, V>) : Trie<K2, V> {
1193
+ mergeDisjoint(t1, t2, k2_eq)
1194
+ },
1195
+ func (_ : K1, t : Trie<K2, V>) : Trie<K2, V> { t },
1196
+ #empty
1197
+ )
1198
+ };
1199
+
1200
+ }