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.
- package/.mops/base@0.7.4/LICENSE +208 -0
- package/.mops/base@0.7.4/README.md +64 -0
- package/.mops/base@0.7.4/mops.toml +5 -0
- package/.mops/base@0.7.4/src/Array.mo +686 -0
- package/.mops/base@0.7.4/src/AssocList.mo +203 -0
- package/.mops/base@0.7.4/src/Blob.mo +55 -0
- package/.mops/base@0.7.4/src/Bool.mo +44 -0
- package/.mops/base@0.7.4/src/Buffer.mo +1937 -0
- package/.mops/base@0.7.4/src/CertifiedData.mo +29 -0
- package/.mops/base@0.7.4/src/Char.mo +67 -0
- package/.mops/base@0.7.4/src/Debug.mo +15 -0
- package/.mops/base@0.7.4/src/Deque.mo +75 -0
- package/.mops/base@0.7.4/src/Error.mo +41 -0
- package/.mops/base@0.7.4/src/ExperimentalCycles.mo +51 -0
- package/.mops/base@0.7.4/src/ExperimentalInternetComputer.mo +36 -0
- package/.mops/base@0.7.4/src/ExperimentalStableMemory.mo +121 -0
- package/.mops/base@0.7.4/src/Float.mo +150 -0
- package/.mops/base@0.7.4/src/Func.mo +38 -0
- package/.mops/base@0.7.4/src/Hash.mo +83 -0
- package/.mops/base@0.7.4/src/HashMap.mo +229 -0
- package/.mops/base@0.7.4/src/Heap.mo +113 -0
- package/.mops/base@0.7.4/src/Int.mo +150 -0
- package/.mops/base@0.7.4/src/Int16.mo +159 -0
- package/.mops/base@0.7.4/src/Int32.mo +160 -0
- package/.mops/base@0.7.4/src/Int64.mo +161 -0
- package/.mops/base@0.7.4/src/Int8.mo +160 -0
- package/.mops/base@0.7.4/src/Iter.mo +220 -0
- package/.mops/base@0.7.4/src/IterType.mo +7 -0
- package/.mops/base@0.7.4/src/List.mo +433 -0
- package/.mops/base@0.7.4/src/Nat.mo +75 -0
- package/.mops/base@0.7.4/src/Nat16.mo +146 -0
- package/.mops/base@0.7.4/src/Nat32.mo +146 -0
- package/.mops/base@0.7.4/src/Nat64.mo +146 -0
- package/.mops/base@0.7.4/src/Nat8.mo +146 -0
- package/.mops/base@0.7.4/src/None.mo +19 -0
- package/.mops/base@0.7.4/src/Option.mo +160 -0
- package/.mops/base@0.7.4/src/Order.mo +46 -0
- package/.mops/base@0.7.4/src/Prelude.mo +33 -0
- package/.mops/base@0.7.4/src/Principal.mo +58 -0
- package/.mops/base@0.7.4/src/RBTree.mo +218 -0
- package/.mops/base@0.7.4/src/Random.mo +188 -0
- package/.mops/base@0.7.4/src/Result.mo +210 -0
- package/.mops/base@0.7.4/src/Stack.mo +40 -0
- package/.mops/base@0.7.4/src/Text.mo +615 -0
- package/.mops/base@0.7.4/src/Time.mo +37 -0
- package/.mops/base@0.7.4/src/Trie.mo +1200 -0
- package/.mops/base@0.7.4/src/TrieMap.mo +180 -0
- package/.mops/base@0.7.4/src/TrieSet.mo +97 -0
- package/.mops/base@0.8.3/LICENSE +208 -0
- package/.mops/base@0.8.3/README.md +64 -0
- package/.mops/base@0.8.3/mops.toml +6 -0
- package/.mops/base@0.8.3/src/Array.mo +717 -0
- package/.mops/base@0.8.3/src/AssocList.mo +404 -0
- package/.mops/base@0.8.3/src/Blob.mo +212 -0
- package/.mops/base@0.8.3/src/Bool.mo +44 -0
- package/.mops/base@0.8.3/src/Buffer.mo +2660 -0
- package/.mops/base@0.8.3/src/CertifiedData.mo +53 -0
- package/.mops/base@0.8.3/src/Char.mo +65 -0
- package/.mops/base@0.8.3/src/Debug.mo +56 -0
- package/.mops/base@0.8.3/src/Deque.mo +243 -0
- package/.mops/base@0.8.3/src/Error.mo +68 -0
- package/.mops/base@0.8.3/src/ExperimentalCycles.mo +151 -0
- package/.mops/base@0.8.3/src/ExperimentalInternetComputer.mo +60 -0
- package/.mops/base@0.8.3/src/ExperimentalStableMemory.mo +348 -0
- package/.mops/base@0.8.3/src/Float.mo +843 -0
- package/.mops/base@0.8.3/src/Func.mo +46 -0
- package/.mops/base@0.8.3/src/Hash.mo +82 -0
- package/.mops/base@0.8.3/src/HashMap.mo +457 -0
- package/.mops/base@0.8.3/src/Heap.mo +233 -0
- package/.mops/base@0.8.3/src/Int.mo +365 -0
- package/.mops/base@0.8.3/src/Int16.mo +521 -0
- package/.mops/base@0.8.3/src/Int32.mo +522 -0
- package/.mops/base@0.8.3/src/Int64.mo +522 -0
- package/.mops/base@0.8.3/src/Int8.mo +522 -0
- package/.mops/base@0.8.3/src/Iter.mo +227 -0
- package/.mops/base@0.8.3/src/IterType.mo +7 -0
- package/.mops/base@0.8.3/src/List.mo +930 -0
- package/.mops/base@0.8.3/src/Nat.mo +305 -0
- package/.mops/base@0.8.3/src/Nat16.mo +144 -0
- package/.mops/base@0.8.3/src/Nat32.mo +144 -0
- package/.mops/base@0.8.3/src/Nat64.mo +144 -0
- package/.mops/base@0.8.3/src/Nat8.mo +144 -0
- package/.mops/base@0.8.3/src/None.mo +19 -0
- package/.mops/base@0.8.3/src/Option.mo +154 -0
- package/.mops/base@0.8.3/src/Order.mo +46 -0
- package/.mops/base@0.8.3/src/Prelude.mo +33 -0
- package/.mops/base@0.8.3/src/Principal.mo +249 -0
- package/.mops/base@0.8.3/src/RBTree.mo +681 -0
- package/.mops/base@0.8.3/src/Random.mo +270 -0
- package/.mops/base@0.8.3/src/Result.mo +209 -0
- package/.mops/base@0.8.3/src/Stack.mo +93 -0
- package/.mops/base@0.8.3/src/Text.mo +761 -0
- package/.mops/base@0.8.3/src/Time.mo +36 -0
- package/.mops/base@0.8.3/src/Timer.mo +62 -0
- package/.mops/base@0.8.3/src/Trie.mo +1603 -0
- package/.mops/base@0.8.3/src/TrieMap.mo +392 -0
- package/.mops/base@0.8.3/src/TrieSet.mo +148 -0
- package/network.txt +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/// Class `Heap<X>` provides a priority queue of elements of type `X`.
|
|
2
|
+
///
|
|
3
|
+
/// The class wraps a purely-functional implementation based on a leftist heap.
|
|
4
|
+
///
|
|
5
|
+
/// Note on the constructor:
|
|
6
|
+
/// The constructor takes in a comparison function `compare` that defines the
|
|
7
|
+
/// ordering between elements of type `X`. Most primitive types have a default
|
|
8
|
+
/// version of this comparison function defined in their modules (e.g. `Nat.compare`).
|
|
9
|
+
/// The runtime analysis in this documentation assumes that the `compare` function
|
|
10
|
+
/// runs in `O(1)` time and space.
|
|
11
|
+
///
|
|
12
|
+
/// Example:
|
|
13
|
+
/// ```motoko name=initialize
|
|
14
|
+
/// import Heap "mo:base/Heap";
|
|
15
|
+
/// import Text "mo:base/Text";
|
|
16
|
+
///
|
|
17
|
+
/// let heap = Heap.Heap<Text>(Text.compare);
|
|
18
|
+
/// ```
|
|
19
|
+
///
|
|
20
|
+
/// Runtime: `O(1)`
|
|
21
|
+
///
|
|
22
|
+
/// Space: `O(1)`
|
|
23
|
+
|
|
24
|
+
import O "Order";
|
|
25
|
+
import P "Prelude";
|
|
26
|
+
import L "List";
|
|
27
|
+
import I "Iter";
|
|
28
|
+
|
|
29
|
+
module {
|
|
30
|
+
|
|
31
|
+
public type Tree<X> = ?(Int, X, Tree<X>, Tree<X>);
|
|
32
|
+
|
|
33
|
+
public class Heap<X>(compare : (X, X) -> O.Order) {
|
|
34
|
+
var heap : Tree<X> = null;
|
|
35
|
+
|
|
36
|
+
/// Inserts an element into the heap.
|
|
37
|
+
///
|
|
38
|
+
/// Example:
|
|
39
|
+
/// ```motoko include=initialize
|
|
40
|
+
///
|
|
41
|
+
/// heap.put("apple");
|
|
42
|
+
/// heap.peekMin() // => ?"apple"
|
|
43
|
+
/// ```
|
|
44
|
+
///
|
|
45
|
+
/// Runtime: `O(log(n))`
|
|
46
|
+
///
|
|
47
|
+
/// Space: `O(log(n))`
|
|
48
|
+
public func put(x : X) {
|
|
49
|
+
heap := merge(heap, ?(1, x, null, null), compare)
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/// Return the minimal element in the heap, or `null` if the heap is empty.
|
|
53
|
+
///
|
|
54
|
+
/// Example:
|
|
55
|
+
/// ```motoko include=initialize
|
|
56
|
+
///
|
|
57
|
+
/// heap.put("apple");
|
|
58
|
+
/// heap.put("banana");
|
|
59
|
+
/// heap.put("cantaloupe");
|
|
60
|
+
/// heap.peekMin() // => ?"apple"
|
|
61
|
+
/// ```
|
|
62
|
+
///
|
|
63
|
+
/// Runtime: `O(1)`
|
|
64
|
+
///
|
|
65
|
+
/// Space: `O(1)`
|
|
66
|
+
public func peekMin() : ?X {
|
|
67
|
+
switch heap {
|
|
68
|
+
case (null) { null };
|
|
69
|
+
case (?(_, x, _, _)) { ?x }
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/// Delete the minimal element in the heap, if it exists.
|
|
74
|
+
///
|
|
75
|
+
/// Example:
|
|
76
|
+
/// ```motoko include=initialize
|
|
77
|
+
///
|
|
78
|
+
/// heap.put("apple");
|
|
79
|
+
/// heap.put("banana");
|
|
80
|
+
/// heap.put("cantaloupe");
|
|
81
|
+
/// heap.deleteMin();
|
|
82
|
+
/// heap.peekMin(); // => ?"banana"
|
|
83
|
+
/// ```
|
|
84
|
+
///
|
|
85
|
+
/// Runtime: `O(log(n))`
|
|
86
|
+
///
|
|
87
|
+
/// Space: `O(log(n))`
|
|
88
|
+
public func deleteMin() {
|
|
89
|
+
switch heap {
|
|
90
|
+
case null {};
|
|
91
|
+
case (?(_, _, a, b)) { heap := merge(a, b, compare) }
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/// Delete and return the minimal element in the heap, if it exists.
|
|
96
|
+
///
|
|
97
|
+
/// Example:
|
|
98
|
+
/// ```motoko include=initialize
|
|
99
|
+
///
|
|
100
|
+
/// heap.put("apple");
|
|
101
|
+
/// heap.put("banana");
|
|
102
|
+
/// heap.put("cantaloupe");
|
|
103
|
+
/// heap.removeMin(); // => ?"apple"
|
|
104
|
+
/// ```
|
|
105
|
+
///
|
|
106
|
+
/// Runtime: `O(log(n))`
|
|
107
|
+
///
|
|
108
|
+
/// Space: `O(log(n))`
|
|
109
|
+
public func removeMin() : (minElement : ?X) {
|
|
110
|
+
switch heap {
|
|
111
|
+
case null { null };
|
|
112
|
+
case (?(_, x, a, b)) {
|
|
113
|
+
heap := merge(a, b, compare);
|
|
114
|
+
?x
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
/// Return a snapshot of the internal functional tree representation as sharable data.
|
|
120
|
+
/// The returned tree representation is not affected by subsequent changes of the `Heap` instance.
|
|
121
|
+
///
|
|
122
|
+
/// Example:
|
|
123
|
+
/// ```motoko include=initialize
|
|
124
|
+
///
|
|
125
|
+
/// heap.put("banana");
|
|
126
|
+
/// heap.share();
|
|
127
|
+
/// ```
|
|
128
|
+
///
|
|
129
|
+
/// Useful for storing the heap as a stable variable, pretty-printing, and sharing it across async function calls,
|
|
130
|
+
/// i.e. passing it in async arguments or async results.
|
|
131
|
+
///
|
|
132
|
+
/// Runtime: `O(1)`
|
|
133
|
+
///
|
|
134
|
+
/// Space: `O(1)`
|
|
135
|
+
public func share() : Tree<X> {
|
|
136
|
+
heap
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/// Rewraps a snapshot of a heap (obtained by `share()`) in a `Heap` instance.
|
|
140
|
+
/// The wrapping instance must be initialized with the same `compare`
|
|
141
|
+
/// function that created the snapshot.
|
|
142
|
+
///
|
|
143
|
+
/// Example:
|
|
144
|
+
/// ```motoko include=initialize
|
|
145
|
+
///
|
|
146
|
+
/// heap.put("apple");
|
|
147
|
+
/// heap.put("banana");
|
|
148
|
+
/// let snapshot = heap.share();
|
|
149
|
+
/// let heapCopy = Heap.Heap<Text>(Text.compare);
|
|
150
|
+
/// heapCopy.unsafeUnshare(snapshot);
|
|
151
|
+
/// heapCopy.peekMin() // => ?"apple"
|
|
152
|
+
/// ```
|
|
153
|
+
///
|
|
154
|
+
/// Useful for loading a stored heap from a stable variable or accesing a heap
|
|
155
|
+
/// snapshot passed from an async function call.
|
|
156
|
+
///
|
|
157
|
+
/// Runtime: `O(1)`.
|
|
158
|
+
///
|
|
159
|
+
/// Space: `O(1)`.
|
|
160
|
+
public func unsafeUnshare(tree : Tree<X>) {
|
|
161
|
+
heap := tree
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
func rank<X>(heap : Tree<X>) : Int {
|
|
167
|
+
switch heap {
|
|
168
|
+
case null { 0 };
|
|
169
|
+
case (?(r, _, _, _)) { r }
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
func makeT<X>(x : X, a : Tree<X>, b : Tree<X>) : Tree<X> {
|
|
174
|
+
if (rank(a) >= rank(b)) {
|
|
175
|
+
?(rank(b) + 1, x, a, b)
|
|
176
|
+
} else {
|
|
177
|
+
?(rank(a) + 1, x, b, a)
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
func merge<X>(h1 : Tree<X>, h2 : Tree<X>, compare : (X, X) -> O.Order) : Tree<X> {
|
|
182
|
+
switch (h1, h2) {
|
|
183
|
+
case (null, h) { h };
|
|
184
|
+
case (h, null) { h };
|
|
185
|
+
case (?(_, x, a, b), ?(_, y, c, d)) {
|
|
186
|
+
switch (compare(x, y)) {
|
|
187
|
+
case (#less) { makeT(x, a, merge(b, h2, compare)) };
|
|
188
|
+
case _ { makeT(y, c, merge(d, h1, compare)) }
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
/// Returns a new `Heap`, containing all entries given by the iterator `iter`.
|
|
195
|
+
/// The new map is initialized with the provided `compare` function.
|
|
196
|
+
///
|
|
197
|
+
/// Example:
|
|
198
|
+
/// ```motoko include=initialize
|
|
199
|
+
/// let entries = ["banana", "apple", "cantaloupe"];
|
|
200
|
+
/// let iter = entries.vals();
|
|
201
|
+
///
|
|
202
|
+
/// let newHeap = Heap.fromIter<Text>(iter, Text.compare);
|
|
203
|
+
/// newHeap.peekMin() // => ?"apple"
|
|
204
|
+
/// ```
|
|
205
|
+
///
|
|
206
|
+
/// Runtime: `O(size)`
|
|
207
|
+
///
|
|
208
|
+
/// Space: `O(size)`
|
|
209
|
+
public func fromIter<X>(iter : I.Iter<X>, compare : (X, X) -> O.Order) : Heap<X> {
|
|
210
|
+
let heap = Heap<X>(compare);
|
|
211
|
+
func build(xs : L.List<Tree<X>>) : Tree<X> {
|
|
212
|
+
func join(xs : L.List<Tree<X>>) : L.List<Tree<X>> {
|
|
213
|
+
switch (xs) {
|
|
214
|
+
case (null) { null };
|
|
215
|
+
case (?(hd, null)) { ?(hd, null) };
|
|
216
|
+
case (?(h1, ?(h2, tl))) { ?(merge(h1, h2, compare), join(tl)) }
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
switch (xs) {
|
|
220
|
+
case null { P.unreachable() };
|
|
221
|
+
case (?(hd, null)) { hd };
|
|
222
|
+
case _ { build(join(xs)) }
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
let list = I.toList(I.map(iter, func(x : X) : Tree<X> { ?(1, x, null, null) }));
|
|
226
|
+
if (not L.isNil(list)) {
|
|
227
|
+
let t = build(list);
|
|
228
|
+
heap.unsafeUnshare(t)
|
|
229
|
+
};
|
|
230
|
+
heap
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
}
|
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
/// Signed integer numbers with infinite precision (also called big integers).
|
|
2
|
+
///
|
|
3
|
+
/// Most operations on integer numbers (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 from the base library to use this module.
|
|
7
|
+
/// ```motoko name=import
|
|
8
|
+
/// import Int "mo:base/Int";
|
|
9
|
+
/// ```
|
|
10
|
+
|
|
11
|
+
import Prim "mo:⛔";
|
|
12
|
+
import Prelude "Prelude";
|
|
13
|
+
import Hash "Hash";
|
|
14
|
+
|
|
15
|
+
module {
|
|
16
|
+
|
|
17
|
+
/// Infinite precision signed integers.
|
|
18
|
+
public type Int = Prim.Types.Int;
|
|
19
|
+
|
|
20
|
+
/// Returns the absolute value of `x`.
|
|
21
|
+
///
|
|
22
|
+
/// Example:
|
|
23
|
+
/// ```motoko include=import
|
|
24
|
+
/// Int.abs(-12) // => 12
|
|
25
|
+
/// ```
|
|
26
|
+
public func abs(x : Int) : Nat {
|
|
27
|
+
Prim.abs(x)
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/// Converts an integer number to its textual representation. Textual
|
|
31
|
+
/// representation _do not_ contain underscores to represent commas.
|
|
32
|
+
///
|
|
33
|
+
/// Example:
|
|
34
|
+
/// ```motoko include=import
|
|
35
|
+
/// Int.toText(-1234) // => "-1234"
|
|
36
|
+
/// ```
|
|
37
|
+
public func toText(x : Int) : Text {
|
|
38
|
+
if (x == 0) {
|
|
39
|
+
return "0"
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
let isNegative = x < 0;
|
|
43
|
+
var int = if isNegative { -x } else { x };
|
|
44
|
+
|
|
45
|
+
var text = "";
|
|
46
|
+
let base = 10;
|
|
47
|
+
|
|
48
|
+
while (int > 0) {
|
|
49
|
+
let rem = int % base;
|
|
50
|
+
text := (
|
|
51
|
+
switch (rem) {
|
|
52
|
+
case 0 { "0" };
|
|
53
|
+
case 1 { "1" };
|
|
54
|
+
case 2 { "2" };
|
|
55
|
+
case 3 { "3" };
|
|
56
|
+
case 4 { "4" };
|
|
57
|
+
case 5 { "5" };
|
|
58
|
+
case 6 { "6" };
|
|
59
|
+
case 7 { "7" };
|
|
60
|
+
case 8 { "8" };
|
|
61
|
+
case 9 { "9" };
|
|
62
|
+
case _ { Prelude.unreachable() }
|
|
63
|
+
}
|
|
64
|
+
) # text;
|
|
65
|
+
int := int / base
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
return if isNegative { "-" # text } else { text }
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/// Returns the minimum of `x` and `y`.
|
|
72
|
+
///
|
|
73
|
+
/// Example:
|
|
74
|
+
/// ```motoko include=import
|
|
75
|
+
/// Int.min(2, -3) // => -3
|
|
76
|
+
/// ```
|
|
77
|
+
public func min(x : Int, y : Int) : Int {
|
|
78
|
+
if (x < y) { x } else { y }
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/// Returns the maximum of `x` and `y`.
|
|
82
|
+
///
|
|
83
|
+
/// Example:
|
|
84
|
+
/// ```motoko include=import
|
|
85
|
+
/// Int.max(2, -3) // => 2
|
|
86
|
+
/// ```
|
|
87
|
+
public func max(x : Int, y : Int) : Int {
|
|
88
|
+
if (x < y) { y } else { x }
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// this is a local copy of deprecated Hash.hashNat8 (redefined to suppress the warning)
|
|
92
|
+
private func hashNat8(key : [Nat32]) : Hash.Hash {
|
|
93
|
+
var hash : Nat32 = 0;
|
|
94
|
+
for (natOfKey in key.vals()) {
|
|
95
|
+
hash := hash +% natOfKey;
|
|
96
|
+
hash := hash +% hash << 10;
|
|
97
|
+
hash := hash ^ (hash >> 6)
|
|
98
|
+
};
|
|
99
|
+
hash := hash +% hash << 3;
|
|
100
|
+
hash := hash ^ (hash >> 11);
|
|
101
|
+
hash := hash +% hash << 15;
|
|
102
|
+
return hash
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/// Computes a hash from the least significant 32-bits of `i`, ignoring other bits.
|
|
106
|
+
/// @deprecated For large `Int` values consider using a bespoke hash function that considers all of the argument's bits.
|
|
107
|
+
public func hash(i : Int) : Hash.Hash {
|
|
108
|
+
// CAUTION: This removes the high bits!
|
|
109
|
+
let j = Prim.int32ToNat32(Prim.intToInt32Wrap(i));
|
|
110
|
+
hashNat8([
|
|
111
|
+
j & (255 << 0),
|
|
112
|
+
j & (255 << 8),
|
|
113
|
+
j & (255 << 16),
|
|
114
|
+
j & (255 << 24)
|
|
115
|
+
])
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/// Computes an accumulated hash from `h1` and the least significant 32-bits of `i`, ignoring other bits in `i`.
|
|
119
|
+
/// @deprecated For large `Int` values consider using a bespoke hash function that considers all of the argument's bits.
|
|
120
|
+
public func hashAcc(h1 : Hash.Hash, i : Int) : Hash.Hash {
|
|
121
|
+
// CAUTION: This removes the high bits!
|
|
122
|
+
let j = Prim.int32ToNat32(Prim.intToInt32Wrap(i));
|
|
123
|
+
hashNat8([
|
|
124
|
+
h1,
|
|
125
|
+
j & (255 << 0),
|
|
126
|
+
j & (255 << 8),
|
|
127
|
+
j & (255 << 16),
|
|
128
|
+
j & (255 << 24)
|
|
129
|
+
])
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/// Equality function for Int types.
|
|
133
|
+
/// This is equivalent to `x == y`.
|
|
134
|
+
///
|
|
135
|
+
/// Example:
|
|
136
|
+
/// ```motoko include=import
|
|
137
|
+
/// Int.equal(-1, -1); // => true
|
|
138
|
+
/// ```
|
|
139
|
+
///
|
|
140
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
141
|
+
/// to the existing `==` operator) is so that you can use it as a function
|
|
142
|
+
/// value to pass to a higher order function. It is not possible to use `==`
|
|
143
|
+
/// as a function value at the moment.
|
|
144
|
+
///
|
|
145
|
+
/// Example:
|
|
146
|
+
/// ```motoko include=import
|
|
147
|
+
/// import Buffer "mo:base/Buffer";
|
|
148
|
+
///
|
|
149
|
+
/// let buffer1 = Buffer.Buffer<Int>(1);
|
|
150
|
+
/// buffer1.add(-3);
|
|
151
|
+
/// let buffer2 = Buffer.Buffer<Int>(1);
|
|
152
|
+
/// buffer2.add(-3);
|
|
153
|
+
/// Buffer.equal(buffer1, buffer2, Int.equal) // => true
|
|
154
|
+
/// ```
|
|
155
|
+
public func equal(x : Int, y : Int) : Bool { x == y };
|
|
156
|
+
|
|
157
|
+
/// Inequality function for Int types.
|
|
158
|
+
/// This is equivalent to `x != y`.
|
|
159
|
+
///
|
|
160
|
+
/// Example:
|
|
161
|
+
/// ```motoko include=import
|
|
162
|
+
/// Int.notEqual(-1, -2); // => true
|
|
163
|
+
/// ```
|
|
164
|
+
///
|
|
165
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
166
|
+
/// to the existing `!=` operator) is so that you can use it as a function
|
|
167
|
+
/// value to pass to a higher order function. It is not possible to use `!=`
|
|
168
|
+
/// as a function value at the moment.
|
|
169
|
+
public func notEqual(x : Int, y : Int) : Bool { x != y };
|
|
170
|
+
|
|
171
|
+
/// "Less than" function for Int types.
|
|
172
|
+
/// This is equivalent to `x < y`.
|
|
173
|
+
///
|
|
174
|
+
/// Example:
|
|
175
|
+
/// ```motoko include=import
|
|
176
|
+
/// Int.less(-2, 1); // => true
|
|
177
|
+
/// ```
|
|
178
|
+
///
|
|
179
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
180
|
+
/// to the existing `<` operator) is so that you can use it as a function
|
|
181
|
+
/// value to pass to a higher order function. It is not possible to use `<`
|
|
182
|
+
/// as a function value at the moment.
|
|
183
|
+
public func less(x : Int, y : Int) : Bool { x < y };
|
|
184
|
+
|
|
185
|
+
/// "Less than or equal" function for Int types.
|
|
186
|
+
/// This is equivalent to `x <= y`.
|
|
187
|
+
///
|
|
188
|
+
/// Example:
|
|
189
|
+
/// ```motoko include=import
|
|
190
|
+
/// Int.lessOrEqual(-2, 1); // => true
|
|
191
|
+
/// ```
|
|
192
|
+
///
|
|
193
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
194
|
+
/// to the existing `<=` operator) is so that you can use it as a function
|
|
195
|
+
/// value to pass to a higher order function. It is not possible to use `<=`
|
|
196
|
+
/// as a function value at the moment.
|
|
197
|
+
public func lessOrEqual(x : Int, y : Int) : Bool { x <= y };
|
|
198
|
+
|
|
199
|
+
/// "Greater than" function for Int types.
|
|
200
|
+
/// This is equivalent to `x > y`.
|
|
201
|
+
///
|
|
202
|
+
/// Example:
|
|
203
|
+
/// ```motoko include=import
|
|
204
|
+
/// Int.greater(1, -2); // => true
|
|
205
|
+
/// ```
|
|
206
|
+
///
|
|
207
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
208
|
+
/// to the existing `>` operator) is so that you can use it as a function
|
|
209
|
+
/// value to pass to a higher order function. It is not possible to use `>`
|
|
210
|
+
/// as a function value at the moment.
|
|
211
|
+
public func greater(x : Int, y : Int) : Bool { x > y };
|
|
212
|
+
|
|
213
|
+
/// "Greater than or equal" function for Int types.
|
|
214
|
+
/// This is equivalent to `x >= y`.
|
|
215
|
+
///
|
|
216
|
+
/// Example:
|
|
217
|
+
/// ```motoko include=import
|
|
218
|
+
/// Int.greaterOrEqual(1, -2); // => true
|
|
219
|
+
/// ```
|
|
220
|
+
///
|
|
221
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
222
|
+
/// to the existing `>=` operator) is so that you can use it as a function
|
|
223
|
+
/// value to pass to a higher order function. It is not possible to use `>=`
|
|
224
|
+
/// as a function value at the moment.
|
|
225
|
+
public func greaterOrEqual(x : Int, y : Int) : Bool { x >= y };
|
|
226
|
+
|
|
227
|
+
/// General-purpose comparison function for `Int`. Returns the `Order` (
|
|
228
|
+
/// either `#less`, `#equal`, or `#greater`) of comparing `x` with `y`.
|
|
229
|
+
///
|
|
230
|
+
/// Example:
|
|
231
|
+
/// ```motoko include=import
|
|
232
|
+
/// Int.compare(-3, 2) // => #less
|
|
233
|
+
/// ```
|
|
234
|
+
///
|
|
235
|
+
/// This function can be used as value for a high order function, such as a sort function.
|
|
236
|
+
///
|
|
237
|
+
/// Example:
|
|
238
|
+
/// ```motoko include=import
|
|
239
|
+
/// import Array "mo:base/Array";
|
|
240
|
+
/// Array.sort([1, -2, -3], Int.compare) // => [-3, -2, 1]
|
|
241
|
+
/// ```
|
|
242
|
+
public func compare(x : Int, y : Int) : { #less; #equal; #greater } {
|
|
243
|
+
if (x < y) { #less } else if (x == y) { #equal } else { #greater }
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
/// Returns the negation of `x`, `-x` .
|
|
247
|
+
///
|
|
248
|
+
/// Example:
|
|
249
|
+
/// ```motoko include=import
|
|
250
|
+
/// Int.neg(123) // => -123
|
|
251
|
+
/// ```
|
|
252
|
+
public func neg(x : Int) : Int { -x };
|
|
253
|
+
|
|
254
|
+
/// Returns the sum of `x` and `y`, `x + y`.
|
|
255
|
+
///
|
|
256
|
+
/// No overflow since `Int` has infinite precision.
|
|
257
|
+
///
|
|
258
|
+
/// Example:
|
|
259
|
+
/// ```motoko include=import
|
|
260
|
+
/// Int.add(1, -2); // => -1
|
|
261
|
+
/// ```
|
|
262
|
+
///
|
|
263
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
264
|
+
/// to the existing `+` operator) is so that you can use it as a function
|
|
265
|
+
/// value to pass to a higher order function. It is not possible to use `+`
|
|
266
|
+
/// as a function value at the moment.
|
|
267
|
+
///
|
|
268
|
+
/// Example:
|
|
269
|
+
/// ```motoko include=import
|
|
270
|
+
/// import Array "mo:base/Array";
|
|
271
|
+
/// Array.foldLeft([1, -2, -3], 0, Int.add) // => -4
|
|
272
|
+
/// ```
|
|
273
|
+
public func add(x : Int, y : Int) : Int { x + y };
|
|
274
|
+
|
|
275
|
+
/// Returns the difference of `x` and `y`, `x - y`.
|
|
276
|
+
///
|
|
277
|
+
/// No overflow since `Int` has infinite precision.
|
|
278
|
+
///
|
|
279
|
+
/// Example:
|
|
280
|
+
/// ```motoko include=import
|
|
281
|
+
/// Int.sub(1, 2); // => -1
|
|
282
|
+
/// ```
|
|
283
|
+
///
|
|
284
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
285
|
+
/// to the existing `-` operator) is so that you can use it as a function
|
|
286
|
+
/// value to pass to a higher order function. It is not possible to use `-`
|
|
287
|
+
/// as a function value at the moment.
|
|
288
|
+
///
|
|
289
|
+
/// Example:
|
|
290
|
+
/// ```motoko include=import
|
|
291
|
+
/// import Array "mo:base/Array";
|
|
292
|
+
/// Array.foldLeft([1, -2, -3], 0, Int.sub) // => 4
|
|
293
|
+
/// ```
|
|
294
|
+
public func sub(x : Int, y : Int) : Int { x - y };
|
|
295
|
+
|
|
296
|
+
/// Returns the product of `x` and `y`, `x * y`.
|
|
297
|
+
///
|
|
298
|
+
/// No overflow since `Int` has infinite precision.
|
|
299
|
+
///
|
|
300
|
+
/// Example:
|
|
301
|
+
/// ```motoko include=import
|
|
302
|
+
/// Int.mul(-2, 3); // => -6
|
|
303
|
+
/// ```
|
|
304
|
+
///
|
|
305
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
306
|
+
/// to the existing `*` operator) is so that you can use it as a function
|
|
307
|
+
/// value to pass to a higher order function. It is not possible to use `*`
|
|
308
|
+
/// as a function value at the moment.
|
|
309
|
+
///
|
|
310
|
+
/// Example:
|
|
311
|
+
/// ```motoko include=import
|
|
312
|
+
/// import Array "mo:base/Array";
|
|
313
|
+
/// Array.foldLeft([1, -2, -3], 1, Int.mul) // => 6
|
|
314
|
+
/// ```
|
|
315
|
+
public func mul(x : Int, y : Int) : Int { x * y };
|
|
316
|
+
|
|
317
|
+
/// Returns the signed integer division of `x` by `y`, `x / y`.
|
|
318
|
+
/// Rounds the quotient towards zero, which is the same as truncating the decimal places of the quotient.
|
|
319
|
+
///
|
|
320
|
+
/// Traps when `y` is zero.
|
|
321
|
+
///
|
|
322
|
+
/// Example:
|
|
323
|
+
/// ```motoko include=import
|
|
324
|
+
/// Int.div(6, -2); // => -3
|
|
325
|
+
/// ```
|
|
326
|
+
///
|
|
327
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
328
|
+
/// to the existing `/` operator) is so that you can use it as a function
|
|
329
|
+
/// value to pass to a higher order function. It is not possible to use `/`
|
|
330
|
+
/// as a function value at the moment.
|
|
331
|
+
public func div(x : Int, y : Int) : Int { x / y };
|
|
332
|
+
|
|
333
|
+
/// Returns the remainder of the signed integer division of `x` by `y`, `x % y`,
|
|
334
|
+
/// which is defined as `x - x / y * y`.
|
|
335
|
+
///
|
|
336
|
+
/// Traps when `y` is zero.
|
|
337
|
+
///
|
|
338
|
+
/// Example:
|
|
339
|
+
/// ```motoko include=import
|
|
340
|
+
/// Int.rem(6, -4); // => 2
|
|
341
|
+
/// ```
|
|
342
|
+
///
|
|
343
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
344
|
+
/// to the existing `%` operator) is so that you can use it as a function
|
|
345
|
+
/// value to pass to a higher order function. It is not possible to use `%`
|
|
346
|
+
/// as a function value at the moment.
|
|
347
|
+
public func rem(x : Int, y : Int) : Int { x % y };
|
|
348
|
+
|
|
349
|
+
/// Returns `x` to the power of `y`, `x ** y`.
|
|
350
|
+
///
|
|
351
|
+
/// Traps when `y` is negative or `y > 2 ** 32 - 1`.
|
|
352
|
+
/// No overflow since `Int` has infinite precision.
|
|
353
|
+
///
|
|
354
|
+
/// Example:
|
|
355
|
+
/// ```motoko include=import
|
|
356
|
+
/// Int.pow(-2, 3); // => -8
|
|
357
|
+
/// ```
|
|
358
|
+
///
|
|
359
|
+
/// Note: The reason why this function is defined in this library (in addition
|
|
360
|
+
/// to the existing `**` operator) is so that you can use it as a function
|
|
361
|
+
/// value to pass to a higher order function. It is not possible to use `**`
|
|
362
|
+
/// as a function value at the moment.
|
|
363
|
+
public func pow(x : Int, y : Int) : Int { x ** y };
|
|
364
|
+
|
|
365
|
+
}
|