ic-mops 0.8.5 → 0.8.7
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/package.json +3 -1
- package/.mops/base@0.7.4/LICENSE +0 -208
- package/.mops/base@0.7.4/README.md +0 -64
- package/.mops/base@0.7.4/mops.toml +0 -5
- package/.mops/base@0.7.4/src/Array.mo +0 -686
- package/.mops/base@0.7.4/src/AssocList.mo +0 -203
- package/.mops/base@0.7.4/src/Blob.mo +0 -55
- package/.mops/base@0.7.4/src/Bool.mo +0 -44
- package/.mops/base@0.7.4/src/Buffer.mo +0 -1937
- package/.mops/base@0.7.4/src/CertifiedData.mo +0 -29
- package/.mops/base@0.7.4/src/Char.mo +0 -67
- package/.mops/base@0.7.4/src/Debug.mo +0 -15
- package/.mops/base@0.7.4/src/Deque.mo +0 -75
- package/.mops/base@0.7.4/src/Error.mo +0 -41
- package/.mops/base@0.7.4/src/ExperimentalCycles.mo +0 -51
- package/.mops/base@0.7.4/src/ExperimentalInternetComputer.mo +0 -36
- package/.mops/base@0.7.4/src/ExperimentalStableMemory.mo +0 -121
- package/.mops/base@0.7.4/src/Float.mo +0 -150
- package/.mops/base@0.7.4/src/Func.mo +0 -38
- package/.mops/base@0.7.4/src/Hash.mo +0 -83
- package/.mops/base@0.7.4/src/HashMap.mo +0 -229
- package/.mops/base@0.7.4/src/Heap.mo +0 -113
- package/.mops/base@0.7.4/src/Int.mo +0 -150
- package/.mops/base@0.7.4/src/Int16.mo +0 -159
- package/.mops/base@0.7.4/src/Int32.mo +0 -160
- package/.mops/base@0.7.4/src/Int64.mo +0 -161
- package/.mops/base@0.7.4/src/Int8.mo +0 -160
- package/.mops/base@0.7.4/src/Iter.mo +0 -220
- package/.mops/base@0.7.4/src/IterType.mo +0 -7
- package/.mops/base@0.7.4/src/List.mo +0 -433
- package/.mops/base@0.7.4/src/Nat.mo +0 -75
- package/.mops/base@0.7.4/src/Nat16.mo +0 -146
- package/.mops/base@0.7.4/src/Nat32.mo +0 -146
- package/.mops/base@0.7.4/src/Nat64.mo +0 -146
- package/.mops/base@0.7.4/src/Nat8.mo +0 -146
- package/.mops/base@0.7.4/src/None.mo +0 -19
- package/.mops/base@0.7.4/src/Option.mo +0 -160
- package/.mops/base@0.7.4/src/Order.mo +0 -46
- package/.mops/base@0.7.4/src/Prelude.mo +0 -33
- package/.mops/base@0.7.4/src/Principal.mo +0 -58
- package/.mops/base@0.7.4/src/RBTree.mo +0 -218
- package/.mops/base@0.7.4/src/Random.mo +0 -188
- package/.mops/base@0.7.4/src/Result.mo +0 -210
- package/.mops/base@0.7.4/src/Stack.mo +0 -40
- package/.mops/base@0.7.4/src/Text.mo +0 -615
- package/.mops/base@0.7.4/src/Time.mo +0 -37
- package/.mops/base@0.7.4/src/Trie.mo +0 -1200
- package/.mops/base@0.7.4/src/TrieMap.mo +0 -180
- package/.mops/base@0.7.4/src/TrieSet.mo +0 -97
- package/.mops/base@0.8.3/LICENSE +0 -208
- package/.mops/base@0.8.3/README.md +0 -64
- package/.mops/base@0.8.3/mops.toml +0 -6
- package/.mops/base@0.8.3/src/Array.mo +0 -717
- package/.mops/base@0.8.3/src/AssocList.mo +0 -404
- package/.mops/base@0.8.3/src/Blob.mo +0 -212
- package/.mops/base@0.8.3/src/Bool.mo +0 -44
- package/.mops/base@0.8.3/src/Buffer.mo +0 -2660
- package/.mops/base@0.8.3/src/CertifiedData.mo +0 -53
- package/.mops/base@0.8.3/src/Char.mo +0 -65
- package/.mops/base@0.8.3/src/Debug.mo +0 -56
- package/.mops/base@0.8.3/src/Deque.mo +0 -243
- package/.mops/base@0.8.3/src/Error.mo +0 -68
- package/.mops/base@0.8.3/src/ExperimentalCycles.mo +0 -151
- package/.mops/base@0.8.3/src/ExperimentalInternetComputer.mo +0 -60
- package/.mops/base@0.8.3/src/ExperimentalStableMemory.mo +0 -348
- package/.mops/base@0.8.3/src/Float.mo +0 -843
- package/.mops/base@0.8.3/src/Func.mo +0 -46
- package/.mops/base@0.8.3/src/Hash.mo +0 -82
- package/.mops/base@0.8.3/src/HashMap.mo +0 -457
- package/.mops/base@0.8.3/src/Heap.mo +0 -233
- package/.mops/base@0.8.3/src/Int.mo +0 -365
- package/.mops/base@0.8.3/src/Int16.mo +0 -521
- package/.mops/base@0.8.3/src/Int32.mo +0 -522
- package/.mops/base@0.8.3/src/Int64.mo +0 -522
- package/.mops/base@0.8.3/src/Int8.mo +0 -522
- package/.mops/base@0.8.3/src/Iter.mo +0 -227
- package/.mops/base@0.8.3/src/IterType.mo +0 -7
- package/.mops/base@0.8.3/src/List.mo +0 -930
- package/.mops/base@0.8.3/src/Nat.mo +0 -305
- package/.mops/base@0.8.3/src/Nat16.mo +0 -144
- package/.mops/base@0.8.3/src/Nat32.mo +0 -144
- package/.mops/base@0.8.3/src/Nat64.mo +0 -144
- package/.mops/base@0.8.3/src/Nat8.mo +0 -144
- package/.mops/base@0.8.3/src/None.mo +0 -19
- package/.mops/base@0.8.3/src/Option.mo +0 -154
- package/.mops/base@0.8.3/src/Order.mo +0 -46
- package/.mops/base@0.8.3/src/Prelude.mo +0 -33
- package/.mops/base@0.8.3/src/Principal.mo +0 -249
- package/.mops/base@0.8.3/src/RBTree.mo +0 -681
- package/.mops/base@0.8.3/src/Random.mo +0 -270
- package/.mops/base@0.8.3/src/Result.mo +0 -209
- package/.mops/base@0.8.3/src/Stack.mo +0 -93
- package/.mops/base@0.8.3/src/Text.mo +0 -761
- package/.mops/base@0.8.3/src/Time.mo +0 -36
- package/.mops/base@0.8.3/src/Timer.mo +0 -62
- package/.mops/base@0.8.3/src/Trie.mo +0 -1603
- package/.mops/base@0.8.3/src/TrieMap.mo +0 -392
- package/.mops/base@0.8.3/src/TrieSet.mo +0 -148
- package/network.txt +0 -1
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
/// Hash values
|
|
2
|
-
|
|
3
|
-
import Prim "mo:⛔";
|
|
4
|
-
import Iter "Iter";
|
|
5
|
-
|
|
6
|
-
module {
|
|
7
|
-
|
|
8
|
-
/// Hash values represent a string of _hash bits_, packed into a `Nat32`.
|
|
9
|
-
public type Hash = Nat32;
|
|
10
|
-
|
|
11
|
-
/// The hash length, always 31.
|
|
12
|
-
public let length : Nat = 31; // Why not 32?
|
|
13
|
-
|
|
14
|
-
/// Project a given bit from the bit vector.
|
|
15
|
-
public func bit(h : Hash, pos : Nat) : Bool {
|
|
16
|
-
assert (pos <= length);
|
|
17
|
-
(h & (Prim.natToNat32(1) << Prim.natToNat32(pos))) != Prim.natToNat32(0)
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
/// Test if two hashes are equal
|
|
21
|
-
public func equal(ha : Hash, hb : Hash) : Bool {
|
|
22
|
-
ha == hb
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/// Computes a hash from the least significant 32-bits of `n`, ignoring other bits.
|
|
26
|
-
/// @deprecated For large `Nat` values consider using a bespoke hash function that considers all of the argument's bits.
|
|
27
|
-
public func hash(n : Nat) : Hash {
|
|
28
|
-
let j = Prim.intToNat32Wrap(n);
|
|
29
|
-
hashNat8(
|
|
30
|
-
[j & (255 << 0),
|
|
31
|
-
j & (255 << 8),
|
|
32
|
-
j & (255 << 16),
|
|
33
|
-
j & (255 << 24)
|
|
34
|
-
]);
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
/// @deprecated This function will be removed in future.
|
|
38
|
-
public func debugPrintBits(bits : Hash) {
|
|
39
|
-
for (j in Iter.range(0, length - 1)) {
|
|
40
|
-
if (bit(bits, j)) {
|
|
41
|
-
Prim.debugPrint("1")
|
|
42
|
-
} else {
|
|
43
|
-
Prim.debugPrint("0")
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/// @deprecated This function will be removed in future.
|
|
49
|
-
public func debugPrintBitsRev(bits : Hash) {
|
|
50
|
-
for (j in Iter.revRange(length - 1, 0)) {
|
|
51
|
-
if (bit(bits, Prim.abs(j))) {
|
|
52
|
-
Prim.debugPrint("1")
|
|
53
|
-
} else {
|
|
54
|
-
Prim.debugPrint("0")
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
/// Jenkin's one at a time:
|
|
61
|
-
///
|
|
62
|
-
/// https://en.wikipedia.org/wiki/Jenkins_hash_function#one_at_a_time
|
|
63
|
-
///
|
|
64
|
-
/// The input type should actually be `[Nat8]`.
|
|
65
|
-
/// Note: Be sure to explode each `Nat8` of a `Nat32` into its own `Nat32`, and to shift into lower 8 bits.
|
|
66
|
-
|
|
67
|
-
// should this really be public?
|
|
68
|
-
// NB: Int.mo contains a local copy of hashNat8 (redefined to suppress the deprecation warning).
|
|
69
|
-
/// @deprecated This function may be removed or changed in future.
|
|
70
|
-
public func hashNat8(key : [Hash]) : Hash {
|
|
71
|
-
var hash : Nat32 = 0;
|
|
72
|
-
for (natOfKey in key.vals()) {
|
|
73
|
-
hash := hash +% natOfKey;
|
|
74
|
-
hash := hash +% hash << 10;
|
|
75
|
-
hash := hash ^ (hash >> 6);
|
|
76
|
-
};
|
|
77
|
-
hash := hash +% hash << 3;
|
|
78
|
-
hash := hash ^ (hash >> 11);
|
|
79
|
-
hash := hash +% hash << 15;
|
|
80
|
-
return hash;
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
}
|
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
/// Mutable hash map (aka Hashtable)
|
|
2
|
-
///
|
|
3
|
-
/// This module defines an imperative hash map (hash table), with a general key and value type.
|
|
4
|
-
///
|
|
5
|
-
/// It has a minimal object-oriented interface: `get`, `set`, `delete`, `count` and `entries`.
|
|
6
|
-
///
|
|
7
|
-
/// The class is parameterized by the key's equality and hash functions,
|
|
8
|
-
/// and an initial capacity. However, as with the `Buffer` class, no array allocation
|
|
9
|
-
/// happens until the first `set`.
|
|
10
|
-
///
|
|
11
|
-
/// Internally, table growth policy is very simple, for now:
|
|
12
|
-
/// Double the current capacity when the expected bucket list size grows beyond a certain constant.
|
|
13
|
-
|
|
14
|
-
import Prim "mo:⛔";
|
|
15
|
-
import P "Prelude";
|
|
16
|
-
import A "Array";
|
|
17
|
-
import Hash "Hash";
|
|
18
|
-
import Iter "Iter";
|
|
19
|
-
import AssocList "AssocList";
|
|
20
|
-
import Nat32 "Nat32";
|
|
21
|
-
|
|
22
|
-
module {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
// hash field avoids re-hashing the key when the array grows.
|
|
26
|
-
type Key<K> = (Hash.Hash, K);
|
|
27
|
-
|
|
28
|
-
// key-val list type
|
|
29
|
-
type KVs<K, V> = AssocList.AssocList<Key<K>, V>;
|
|
30
|
-
|
|
31
|
-
/// An imperative HashMap with a minimal object-oriented interface.
|
|
32
|
-
/// Maps keys of type `K` to values of type `V`.
|
|
33
|
-
public class HashMap<K, V>(
|
|
34
|
-
initCapacity : Nat,
|
|
35
|
-
keyEq : (K, K) -> Bool,
|
|
36
|
-
keyHash : K -> Hash.Hash) {
|
|
37
|
-
|
|
38
|
-
var table : [var KVs<K, V>] = [var];
|
|
39
|
-
var _count : Nat = 0;
|
|
40
|
-
|
|
41
|
-
/// Returns the number of entries in this HashMap.
|
|
42
|
-
public func size() : Nat = _count;
|
|
43
|
-
|
|
44
|
-
/// Deletes the entry with the key `k`. Doesn't do anything if the key doesn't
|
|
45
|
-
/// exist.
|
|
46
|
-
public func delete(k : K) = ignore remove(k);
|
|
47
|
-
|
|
48
|
-
func keyHash_(k : K) : Key<K> = (keyHash(k), k);
|
|
49
|
-
|
|
50
|
-
func keyHashEq(k1 : Key<K>, k2 : Key<K>) : Bool { k1.0 == k2.0 and keyEq(k1.1, k2.1) };
|
|
51
|
-
|
|
52
|
-
/// Removes the entry with the key `k` and returns the associated value if it
|
|
53
|
-
/// existed or `null` otherwise.
|
|
54
|
-
public func remove(k : K) : ?V {
|
|
55
|
-
let m = table.size();
|
|
56
|
-
if (m > 0) {
|
|
57
|
-
let h = Prim.nat32ToNat(keyHash(k));
|
|
58
|
-
let pos = h % m;
|
|
59
|
-
let (kvs2, ov) = AssocList.replace<Key<K>, V>(table[pos], keyHash_(k), keyHashEq, null);
|
|
60
|
-
table[pos] := kvs2;
|
|
61
|
-
switch(ov){
|
|
62
|
-
case null { };
|
|
63
|
-
case _ { _count -= 1; }
|
|
64
|
-
};
|
|
65
|
-
ov
|
|
66
|
-
} else {
|
|
67
|
-
null
|
|
68
|
-
};
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
/// Gets the entry with the key `k` and returns its associated value if it
|
|
72
|
-
/// existed or `null` otherwise.
|
|
73
|
-
public func get(k : K) : ?V {
|
|
74
|
-
let h = Prim.nat32ToNat(keyHash(k));
|
|
75
|
-
let m = table.size();
|
|
76
|
-
let v = if (m > 0) {
|
|
77
|
-
AssocList.find<Key<K>, V>(table[h % m], keyHash_(k), keyHashEq)
|
|
78
|
-
} else {
|
|
79
|
-
null
|
|
80
|
-
};
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
/// Insert the value `v` at key `k`. Overwrites an existing entry with key `k`
|
|
84
|
-
public func put(k : K, v : V) = ignore replace(k, v);
|
|
85
|
-
|
|
86
|
-
/// Insert the value `v` at key `k` and returns the previous value stored at
|
|
87
|
-
/// `k` or `null` if it didn't exist.
|
|
88
|
-
public func replace(k : K, v : V) : ?V {
|
|
89
|
-
if (_count >= table.size()) {
|
|
90
|
-
let size =
|
|
91
|
-
if (_count == 0) {
|
|
92
|
-
if (initCapacity > 0) {
|
|
93
|
-
initCapacity
|
|
94
|
-
} else {
|
|
95
|
-
1
|
|
96
|
-
}
|
|
97
|
-
} else {
|
|
98
|
-
table.size() * 2;
|
|
99
|
-
};
|
|
100
|
-
let table2 = A.init<KVs<K, V>>(size, null);
|
|
101
|
-
for (i in table.keys()) {
|
|
102
|
-
var kvs = table[i];
|
|
103
|
-
label moveKeyVals : ()
|
|
104
|
-
loop {
|
|
105
|
-
switch kvs {
|
|
106
|
-
case null { break moveKeyVals };
|
|
107
|
-
case (?((k, v), kvsTail)) {
|
|
108
|
-
let pos2 = Nat32.toNat(k.0) % table2.size(); // critical: uses saved hash. no re-hash.
|
|
109
|
-
table2[pos2] := ?((k,v), table2[pos2]);
|
|
110
|
-
kvs := kvsTail;
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
};
|
|
115
|
-
table := table2;
|
|
116
|
-
};
|
|
117
|
-
let h = Prim.nat32ToNat(keyHash(k));
|
|
118
|
-
let pos = h % table.size();
|
|
119
|
-
let (kvs2, ov) = AssocList.replace<Key<K>, V>(table[pos], keyHash_(k), keyHashEq, ?v);
|
|
120
|
-
table[pos] := kvs2;
|
|
121
|
-
switch(ov){
|
|
122
|
-
case null { _count += 1 };
|
|
123
|
-
case _ {}
|
|
124
|
-
};
|
|
125
|
-
ov
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
/// An `Iter` over the keys.
|
|
129
|
-
public func keys() : Iter.Iter<K>
|
|
130
|
-
{ Iter.map(entries(), func (kv : (K, V)) : K { kv.0 }) };
|
|
131
|
-
|
|
132
|
-
/// An `Iter` over the values.
|
|
133
|
-
public func vals() : Iter.Iter<V>
|
|
134
|
-
{ Iter.map(entries(), func (kv : (K, V)) : V { kv.1 }) };
|
|
135
|
-
|
|
136
|
-
/// Returns an iterator over the key value pairs in this
|
|
137
|
-
/// `HashMap`. Does _not_ modify the `HashMap`.
|
|
138
|
-
public func entries() : Iter.Iter<(K, V)> {
|
|
139
|
-
if (table.size() == 0) {
|
|
140
|
-
object { public func next() : ?(K, V) { null } }
|
|
141
|
-
}
|
|
142
|
-
else {
|
|
143
|
-
object {
|
|
144
|
-
var kvs = table[0];
|
|
145
|
-
var nextTablePos = 1;
|
|
146
|
-
public func next () : ?(K, V) {
|
|
147
|
-
switch kvs {
|
|
148
|
-
case (?(kv, kvs2)) {
|
|
149
|
-
kvs := kvs2;
|
|
150
|
-
?(kv.0.1, kv.1)
|
|
151
|
-
};
|
|
152
|
-
case null {
|
|
153
|
-
if (nextTablePos < table.size()) {
|
|
154
|
-
kvs := table[nextTablePos];
|
|
155
|
-
nextTablePos += 1;
|
|
156
|
-
next()
|
|
157
|
-
} else {
|
|
158
|
-
null
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
/// clone cannot be an efficient object method,
|
|
170
|
-
/// ...but is still useful in tests, and beyond.
|
|
171
|
-
public func clone<K, V> (
|
|
172
|
-
h : HashMap<K, V>,
|
|
173
|
-
keyEq : (K, K) -> Bool,
|
|
174
|
-
keyHash : K -> Hash.Hash
|
|
175
|
-
) : HashMap<K, V> {
|
|
176
|
-
let h2 = HashMap<K, V>(h.size(), keyEq, keyHash);
|
|
177
|
-
for ((k,v) in h.entries()) {
|
|
178
|
-
h2.put(k,v);
|
|
179
|
-
};
|
|
180
|
-
h2
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
/// Clone from any iterator of key-value pairs
|
|
184
|
-
public func fromIter<K, V>(
|
|
185
|
-
iter : Iter.Iter<(K, V)>,
|
|
186
|
-
initCapacity : Nat,
|
|
187
|
-
keyEq : (K, K) -> Bool,
|
|
188
|
-
keyHash : K -> Hash.Hash
|
|
189
|
-
) : HashMap<K, V> {
|
|
190
|
-
let h = HashMap<K, V>(initCapacity, keyEq, keyHash);
|
|
191
|
-
for ((k, v) in iter) {
|
|
192
|
-
h.put(k, v);
|
|
193
|
-
};
|
|
194
|
-
h
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
public func map<K, V1, V2>(
|
|
198
|
-
h : HashMap<K, V1>,
|
|
199
|
-
keyEq : (K, K) -> Bool,
|
|
200
|
-
keyHash : K -> Hash.Hash,
|
|
201
|
-
mapFn : (K, V1) -> V2,
|
|
202
|
-
) : HashMap<K, V2> {
|
|
203
|
-
let h2 = HashMap<K, V2>(h.size(), keyEq, keyHash);
|
|
204
|
-
for ((k, v1) in h.entries()) {
|
|
205
|
-
let v2 = mapFn(k, v1);
|
|
206
|
-
h2.put(k, v2);
|
|
207
|
-
};
|
|
208
|
-
h2
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
public func mapFilter<K, V1, V2>(
|
|
212
|
-
h : HashMap<K, V1>,
|
|
213
|
-
keyEq : (K, K) -> Bool,
|
|
214
|
-
keyHash : K -> Hash.Hash,
|
|
215
|
-
mapFn : (K, V1) -> ?V2,
|
|
216
|
-
) : HashMap<K, V2> {
|
|
217
|
-
let h2 = HashMap<K, V2>(h.size(), keyEq, keyHash);
|
|
218
|
-
for ((k, v1) in h.entries()) {
|
|
219
|
-
switch (mapFn(k, v1)) {
|
|
220
|
-
case null { };
|
|
221
|
-
case (?v2) {
|
|
222
|
-
h2.put(k, v2);
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
};
|
|
226
|
-
h2
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
/// Priority Queue
|
|
2
|
-
///
|
|
3
|
-
/// This module provides purely-functional priority queue based on leftist heap
|
|
4
|
-
|
|
5
|
-
import O "Order";
|
|
6
|
-
import P "Prelude";
|
|
7
|
-
import L "List";
|
|
8
|
-
import I "Iter";
|
|
9
|
-
|
|
10
|
-
module {
|
|
11
|
-
|
|
12
|
-
public type Tree<T> = ?(Int, T, Tree<T>, Tree<T>);
|
|
13
|
-
|
|
14
|
-
public class Heap<T>(ord : (T, T) -> O.Order) {
|
|
15
|
-
var heap : Tree<T> = null;
|
|
16
|
-
|
|
17
|
-
/// Get purely-functional representation
|
|
18
|
-
public func share() : Tree<T> {
|
|
19
|
-
heap
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
/// Put purely-functional representation into class. Need to make sure the tree is constructed with the same compare function
|
|
23
|
-
public func unsafeUnshare(t : Tree<T>) {
|
|
24
|
-
heap := t;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
/// Insert an element to the heap
|
|
28
|
-
public func put(x : T) {
|
|
29
|
-
heap := merge(heap, ?(1, x, null, null), ord);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
/// Return the minimal element
|
|
33
|
-
public func peekMin() : ?T {
|
|
34
|
-
switch heap {
|
|
35
|
-
case (null) { null };
|
|
36
|
-
case (?(_, x, _, _)) { ?x };
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
/// Delete the minimal element
|
|
41
|
-
public func deleteMin() {
|
|
42
|
-
switch heap {
|
|
43
|
-
case null {};
|
|
44
|
-
case (?(_, _, a, b)) { heap := merge(a, b, ord) };
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/// Remove the minimal element and return its value
|
|
49
|
-
public func removeMin() : ?T {
|
|
50
|
-
switch heap {
|
|
51
|
-
case null { null };
|
|
52
|
-
case (?(_, x, a, b)) {
|
|
53
|
-
heap := merge(a, b, ord);
|
|
54
|
-
?x
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
func rank<T>(heap : Tree<T>) : Int {
|
|
61
|
-
switch heap {
|
|
62
|
-
case null { 0 };
|
|
63
|
-
case (?(r, _, _, _)) { r };
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
func makeT<T>(x : T, a : Tree<T>, b : Tree<T>) : Tree<T> {
|
|
68
|
-
if (rank(a) >= rank(b)) {
|
|
69
|
-
?(rank(b) + 1, x, a, b)
|
|
70
|
-
} else {
|
|
71
|
-
?(rank(a) + 1, x, b, a)
|
|
72
|
-
};
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
func merge<T>(h1 : Tree<T>, h2 : Tree<T>, ord : (T, T) -> O.Order) : Tree<T> {
|
|
76
|
-
switch (h1, h2) {
|
|
77
|
-
case (null, h) { h };
|
|
78
|
-
case (h, null) { h };
|
|
79
|
-
case (?(_, x, a, b), ?(_, y, c, d)) {
|
|
80
|
-
switch (ord(x,y)) {
|
|
81
|
-
case (#less) { makeT(x, a, merge(b, h2, ord)) };
|
|
82
|
-
case _ { makeT(y, c, merge(d, h1, ord)) };
|
|
83
|
-
};
|
|
84
|
-
};
|
|
85
|
-
};
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
/// Convert iterator into a heap in O(N) time.
|
|
89
|
-
public func fromIter<T>(iter : I.Iter<T>, ord : (T, T) -> O.Order) : Heap<T> {
|
|
90
|
-
let heap = Heap<T>(ord);
|
|
91
|
-
func build(xs : L.List<Tree<T>>) : Tree<T> {
|
|
92
|
-
func join(xs : L.List<Tree<T>>) : L.List<Tree<T>> {
|
|
93
|
-
switch(xs) {
|
|
94
|
-
case (null) { null };
|
|
95
|
-
case (?(hd, null)) { ?(hd, null) };
|
|
96
|
-
case (?(h1, ?(h2, tl))) { ?(merge(h1, h2, ord), join(tl)) };
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
switch(xs) {
|
|
100
|
-
case null { P.unreachable() };
|
|
101
|
-
case (?(hd, null)) { hd };
|
|
102
|
-
case _ { build(join(xs)) };
|
|
103
|
-
};
|
|
104
|
-
};
|
|
105
|
-
let list = I.toList(I.map(iter, func (x : T) : Tree<T> { ?(1, x, null, null) } ));
|
|
106
|
-
if (not L.isNil(list)) {
|
|
107
|
-
let t = build(list);
|
|
108
|
-
heap.unsafeUnshare(t);
|
|
109
|
-
};
|
|
110
|
-
heap
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
};
|
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
/// Integer numbers
|
|
2
|
-
///
|
|
3
|
-
/// Most operations on integers (e.g. addition) are available as built-in operators (e.g. `1 + 1`).
|
|
4
|
-
/// This module provides equivalent functions and `Text` conversion.
|
|
5
|
-
|
|
6
|
-
import Prim "mo:⛔";
|
|
7
|
-
import Prelude "Prelude";
|
|
8
|
-
import Hash "Hash";
|
|
9
|
-
|
|
10
|
-
module {
|
|
11
|
-
|
|
12
|
-
/// Infinite precision signed integers.
|
|
13
|
-
public type Int = Prim.Types.Int;
|
|
14
|
-
|
|
15
|
-
/// Returns the absolute value of the number
|
|
16
|
-
public let abs : Int -> Nat = Prim.abs;
|
|
17
|
-
|
|
18
|
-
/// Conversion.
|
|
19
|
-
public let toText : Int -> Text = func(x) {
|
|
20
|
-
if (x == 0) {
|
|
21
|
-
return "0";
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
let isNegative = x < 0;
|
|
25
|
-
var int = if isNegative { -x } else { x };
|
|
26
|
-
|
|
27
|
-
var text = "";
|
|
28
|
-
let base = 10;
|
|
29
|
-
|
|
30
|
-
while (int > 0) {
|
|
31
|
-
let rem = int % base;
|
|
32
|
-
text := (switch (rem) {
|
|
33
|
-
case 0 { "0" };
|
|
34
|
-
case 1 { "1" };
|
|
35
|
-
case 2 { "2" };
|
|
36
|
-
case 3 { "3" };
|
|
37
|
-
case 4 { "4" };
|
|
38
|
-
case 5 { "5" };
|
|
39
|
-
case 6 { "6" };
|
|
40
|
-
case 7 { "7" };
|
|
41
|
-
case 8 { "8" };
|
|
42
|
-
case 9 { "9" };
|
|
43
|
-
case _ { Prelude.unreachable() };
|
|
44
|
-
}) # text;
|
|
45
|
-
int := int / base;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
return if isNegative { "-" # text } else { text };
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
/// Returns the minimum of `x` and `y`.
|
|
52
|
-
public func min(x : Int, y : Int) : Int {
|
|
53
|
-
if (x < y) { x } else { y };
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
/// Returns the maximum of `x` and `y`.
|
|
57
|
-
public func max(x : Int, y : Int) : Int {
|
|
58
|
-
if (x < y) { y } else { x };
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
// this is a local copy of deprecated Hash.hashNat8 (redefined to suppress the warning)
|
|
62
|
-
private func hashNat8(key : [Nat32]) : Hash.Hash {
|
|
63
|
-
var hash : Nat32 = 0;
|
|
64
|
-
for (natOfKey in key.vals()) {
|
|
65
|
-
hash := hash +% natOfKey;
|
|
66
|
-
hash := hash +% hash << 10;
|
|
67
|
-
hash := hash ^ (hash >> 6);
|
|
68
|
-
};
|
|
69
|
-
hash := hash +% hash << 3;
|
|
70
|
-
hash := hash ^ (hash >> 11);
|
|
71
|
-
hash := hash +% hash << 15;
|
|
72
|
-
return hash;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
/// Computes a hash from the least significant 32-bits of `i`, ignoring other bits.
|
|
76
|
-
/// @deprecated For large `Int` values consider using a bespoke hash function that considers all of the argument's bits.
|
|
77
|
-
public func hash(i : Int) : Hash.Hash {
|
|
78
|
-
// CAUTION: This removes the high bits!
|
|
79
|
-
let j = Prim.int32ToNat32(Prim.intToInt32Wrap(i));
|
|
80
|
-
hashNat8(
|
|
81
|
-
[j & (255 << 0),
|
|
82
|
-
j & (255 << 8),
|
|
83
|
-
j & (255 << 16),
|
|
84
|
-
j & (255 << 24)
|
|
85
|
-
]);
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
/// @deprecated This function will be removed in future.
|
|
89
|
-
public func hashAcc(h1 : Hash.Hash, i : Int) : Hash.Hash {
|
|
90
|
-
// CAUTION: This removes the high bits!
|
|
91
|
-
let j = Prim.int32ToNat32(Prim.intToInt32Wrap(i));
|
|
92
|
-
hashNat8(
|
|
93
|
-
[h1,
|
|
94
|
-
j & (255 << 0),
|
|
95
|
-
j & (255 << 8),
|
|
96
|
-
j & (255 << 16),
|
|
97
|
-
j & (255 << 24)
|
|
98
|
-
]);
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
/// Returns `x == y`.
|
|
102
|
-
public func equal(x : Int, y : Int) : Bool { x == y };
|
|
103
|
-
|
|
104
|
-
/// Returns `x != y`.
|
|
105
|
-
public func notEqual(x : Int, y : Int) : Bool { x != y };
|
|
106
|
-
|
|
107
|
-
/// Returns `x < y`.
|
|
108
|
-
public func less(x : Int, y : Int) : Bool { x < y };
|
|
109
|
-
|
|
110
|
-
/// Returns `x <= y`.
|
|
111
|
-
public func lessOrEqual(x : Int, y : Int) : Bool { x <= y };
|
|
112
|
-
|
|
113
|
-
/// Returns `x > y`.
|
|
114
|
-
public func greater(x : Int, y : Int) : Bool { x > y };
|
|
115
|
-
|
|
116
|
-
/// Returns `x >= y`.
|
|
117
|
-
public func greaterOrEqual(x : Int, y : Int) : Bool { x >= y };
|
|
118
|
-
|
|
119
|
-
/// Returns the order of `x` and `y`.
|
|
120
|
-
public func compare(x : Int, y : Int) : { #less; #equal; #greater } {
|
|
121
|
-
if (x < y) { #less }
|
|
122
|
-
else if (x == y) { #equal }
|
|
123
|
-
else { #greater }
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
/// Returns the negation of `x`, `-x` .
|
|
127
|
-
public func neq(x : Int) : Int { -x; };
|
|
128
|
-
|
|
129
|
-
/// Returns the sum of `x` and `y`, `x + y`.
|
|
130
|
-
public func add(x : Int, y : Int) : Int { x + y };
|
|
131
|
-
|
|
132
|
-
/// Returns the difference of `x` and `y`, `x - y`.
|
|
133
|
-
public func sub(x : Int, y : Int) : Int { x - y };
|
|
134
|
-
|
|
135
|
-
/// Returns the product of `x` and `y`, `x * y`.
|
|
136
|
-
public func mul(x : Int, y : Int) : Int { x * y };
|
|
137
|
-
|
|
138
|
-
/// Returns the division of `x` by `y`, `x / y`.
|
|
139
|
-
/// Traps when `y` is zero.
|
|
140
|
-
public func div(x : Int, y : Int) : Int { x / y };
|
|
141
|
-
|
|
142
|
-
/// Returns the remainder of `x` divided by `y`, `x % y`.
|
|
143
|
-
/// Traps when `y` is zero.
|
|
144
|
-
public func rem(x : Int, y : Int) : Int { x % y };
|
|
145
|
-
|
|
146
|
-
/// Returns `x` to the power of `y`, `x ** y`.
|
|
147
|
-
public func pow(x : Int, y : Int) : Int { x ** y };
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
|