@grain/stdlib 0.5.13 → 0.6.0
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/CHANGELOG.md +193 -0
- package/LICENSE +1 -1
- package/README.md +25 -2
- package/array.gr +1512 -199
- package/array.md +2032 -94
- package/bigint.gr +239 -140
- package/bigint.md +450 -106
- package/buffer.gr +595 -102
- package/buffer.md +903 -145
- package/bytes.gr +401 -110
- package/bytes.md +551 -63
- package/char.gr +228 -49
- package/char.md +373 -7
- package/exception.gr +26 -12
- package/exception.md +29 -5
- package/float32.gr +130 -109
- package/float32.md +185 -57
- package/float64.gr +112 -99
- package/float64.md +185 -57
- package/hash.gr +47 -37
- package/hash.md +21 -3
- package/int16.gr +430 -0
- package/int16.md +618 -0
- package/int32.gr +200 -269
- package/int32.md +254 -289
- package/int64.gr +142 -225
- package/int64.md +254 -289
- package/int8.gr +511 -0
- package/int8.md +786 -0
- package/json.gr +2084 -0
- package/json.md +608 -0
- package/list.gr +120 -68
- package/list.md +125 -80
- package/map.gr +560 -57
- package/map.md +672 -56
- package/marshal.gr +239 -227
- package/marshal.md +36 -4
- package/number.gr +626 -676
- package/number.md +738 -153
- package/option.gr +33 -35
- package/option.md +58 -42
- package/package.json +2 -2
- package/path.gr +148 -187
- package/path.md +47 -96
- package/pervasives.gr +75 -416
- package/pervasives.md +85 -180
- package/priorityqueue.gr +433 -74
- package/priorityqueue.md +422 -54
- package/queue.gr +362 -80
- package/queue.md +433 -38
- package/random.gr +67 -75
- package/random.md +68 -40
- package/range.gr +135 -63
- package/range.md +198 -43
- package/rational.gr +284 -0
- package/rational.md +545 -0
- package/regex.gr +933 -1066
- package/regex.md +59 -60
- package/result.gr +23 -25
- package/result.md +54 -39
- package/runtime/atof/common.gr +78 -82
- package/runtime/atof/common.md +22 -10
- package/runtime/atof/decimal.gr +102 -127
- package/runtime/atof/decimal.md +28 -7
- package/runtime/atof/lemire.gr +56 -71
- package/runtime/atof/lemire.md +9 -1
- package/runtime/atof/parse.gr +83 -110
- package/runtime/atof/parse.md +12 -2
- package/runtime/atof/slow.gr +28 -35
- package/runtime/atof/slow.md +9 -1
- package/runtime/atof/table.gr +19 -18
- package/runtime/atof/table.md +10 -2
- package/runtime/atoi/parse.gr +153 -136
- package/runtime/atoi/parse.md +50 -1
- package/runtime/bigint.gr +410 -517
- package/runtime/bigint.md +71 -57
- package/runtime/compare.gr +176 -85
- package/runtime/compare.md +31 -1
- package/runtime/dataStructures.gr +144 -32
- package/runtime/dataStructures.md +267 -31
- package/runtime/debugPrint.gr +34 -15
- package/runtime/debugPrint.md +37 -5
- package/runtime/equal.gr +53 -52
- package/runtime/equal.md +30 -1
- package/runtime/exception.gr +38 -47
- package/runtime/exception.md +10 -8
- package/runtime/gc.gr +23 -152
- package/runtime/gc.md +13 -17
- package/runtime/malloc.gr +31 -31
- package/runtime/malloc.md +11 -3
- package/runtime/numberUtils.gr +191 -172
- package/runtime/numberUtils.md +17 -9
- package/runtime/numbers.gr +1695 -1021
- package/runtime/numbers.md +1098 -134
- package/runtime/string.gr +540 -242
- package/runtime/string.md +76 -6
- package/runtime/unsafe/constants.gr +30 -13
- package/runtime/unsafe/constants.md +80 -0
- package/runtime/unsafe/conv.gr +55 -28
- package/runtime/unsafe/conv.md +41 -9
- package/runtime/unsafe/memory.gr +10 -30
- package/runtime/unsafe/memory.md +15 -19
- package/runtime/unsafe/tags.gr +37 -21
- package/runtime/unsafe/tags.md +88 -8
- package/runtime/unsafe/wasmf32.gr +30 -36
- package/runtime/unsafe/wasmf32.md +64 -56
- package/runtime/unsafe/wasmf64.gr +30 -36
- package/runtime/unsafe/wasmf64.md +64 -56
- package/runtime/unsafe/wasmi32.gr +49 -66
- package/runtime/unsafe/wasmi32.md +102 -94
- package/runtime/unsafe/wasmi64.gr +52 -79
- package/runtime/unsafe/wasmi64.md +108 -100
- package/runtime/utils/printing.gr +13 -15
- package/runtime/utils/printing.md +11 -3
- package/runtime/wasi.gr +294 -295
- package/runtime/wasi.md +62 -42
- package/set.gr +574 -64
- package/set.md +634 -54
- package/stack.gr +181 -64
- package/stack.md +271 -42
- package/string.gr +453 -533
- package/string.md +241 -151
- package/uint16.gr +369 -0
- package/uint16.md +585 -0
- package/uint32.gr +470 -0
- package/uint32.md +737 -0
- package/uint64.gr +471 -0
- package/uint64.md +737 -0
- package/uint8.gr +369 -0
- package/uint8.md +585 -0
- package/uri.gr +1093 -0
- package/uri.md +477 -0
- package/{sys → wasi}/file.gr +914 -500
- package/{sys → wasi}/file.md +454 -50
- package/wasi/process.gr +292 -0
- package/{sys → wasi}/process.md +164 -6
- package/wasi/random.gr +77 -0
- package/wasi/random.md +80 -0
- package/{sys → wasi}/time.gr +15 -22
- package/{sys → wasi}/time.md +5 -5
- package/immutablearray.gr +0 -929
- package/immutablearray.md +0 -1038
- package/immutablemap.gr +0 -493
- package/immutablemap.md +0 -479
- package/immutablepriorityqueue.gr +0 -360
- package/immutablepriorityqueue.md +0 -291
- package/immutableset.gr +0 -498
- package/immutableset.md +0 -449
- package/runtime/debug.gr +0 -2
- package/runtime/debug.md +0 -6
- package/runtime/unsafe/errors.gr +0 -36
- package/runtime/unsafe/errors.md +0 -204
- package/sys/process.gr +0 -254
- package/sys/random.gr +0 -79
- package/sys/random.md +0 -66
|
@@ -1,360 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module ImmutablePriorityQueue: An immutable priority queue. A priority queue is a data structure that maintains elements in a priority order. Elements with higher priority are served before elements with lower priority when extracting from the priority queue.
|
|
3
|
-
*
|
|
4
|
-
* @example import ImmutablePriorityQueue from "immutablepriorityqueue"
|
|
5
|
-
*
|
|
6
|
-
* @since v0.5.3
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import List from "list"
|
|
10
|
-
import Array from "array"
|
|
11
|
-
|
|
12
|
-
// implementation based on immutable skew binomial queue with global root optimization
|
|
13
|
-
// as described in the paper "Optimal Purely Functional Priority Queues" by Chris Okasaki.
|
|
14
|
-
|
|
15
|
-
// rank is a stand-in for height of this skew binomial tree
|
|
16
|
-
record Node<a> {
|
|
17
|
-
val: a,
|
|
18
|
-
rank: Number,
|
|
19
|
-
children: List<Node<a>>,
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// an optimization over binomial queue that allows keeping track of the root value
|
|
23
|
-
|
|
24
|
-
// a skew binomial queue is defined as a forest of heap-ordered skew binomial trees
|
|
25
|
-
record PQRoot<a> {
|
|
26
|
-
rootVal: a,
|
|
27
|
-
pq: List<Node<a>>,
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* @section Types: Type declarations included in the ImmutablePriorityQueue module.
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Immutable data structure which maintains a priority order for its elements.
|
|
36
|
-
*/
|
|
37
|
-
record ImmutablePriorityQueue<a> {
|
|
38
|
-
comp: (a, a) -> Number,
|
|
39
|
-
size: Number,
|
|
40
|
-
root: Option<PQRoot<a>>,
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* @section Values: Functions and constants for working with ImmutablePriorityQueues.
|
|
45
|
-
*/
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* An empty priority queue with the default `compare` comparator.
|
|
49
|
-
*
|
|
50
|
-
* @since v0.5.4
|
|
51
|
-
*/
|
|
52
|
-
export let empty = {
|
|
53
|
-
let empty = { comp: compare, size: 0, root: None }
|
|
54
|
-
empty
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Creates a new priority queue with a comparator function, which is used to
|
|
59
|
-
* determine priority of elements. The comparator function takes two elements
|
|
60
|
-
* and must return 0 if both share priority, a positive number if the first
|
|
61
|
-
* has greater priority, and a negative number if the first has less priority.
|
|
62
|
-
*
|
|
63
|
-
* @param comp: The comparator function used to indicate priority order
|
|
64
|
-
* @returns An empty priority queue
|
|
65
|
-
*
|
|
66
|
-
* @example ImmutablePriorityQueue.make(compare) // creates a min priority queue of numbers using the compare pervasive
|
|
67
|
-
* @example ImmutablePriorityQueue.make((a, b) => String.length(b) - String.length(a)) // creates a priority queue by string length (longest to shortest)
|
|
68
|
-
*
|
|
69
|
-
* @since v0.5.3
|
|
70
|
-
*/
|
|
71
|
-
export let make = comp => {
|
|
72
|
-
{ comp, size: 0, root: None }
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Gets the number of elements in a priority queue.
|
|
77
|
-
*
|
|
78
|
-
* @param pq: The priority queue to inspect
|
|
79
|
-
* @returns The number of elements in the priority queue
|
|
80
|
-
*
|
|
81
|
-
* @since v0.5.3
|
|
82
|
-
*/
|
|
83
|
-
export let size = pq => {
|
|
84
|
-
pq.size
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Determines if the priority queue contains no elements.
|
|
89
|
-
*
|
|
90
|
-
* @param pq: The priority queue to check
|
|
91
|
-
* @returns `true` if the priority queue is empty and `false` otherwise
|
|
92
|
-
*
|
|
93
|
-
* @since v0.5.3
|
|
94
|
-
*/
|
|
95
|
-
export let isEmpty = pq => {
|
|
96
|
-
pq.size == 0
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
let skewLinkNodes = (comp, newNode, node1, node2) => {
|
|
100
|
-
// make the two nodes with larger values children of the node with the smallest value
|
|
101
|
-
if (comp(node1.val, newNode.val) <= 0 && comp(node1.val, node2.val) <= 0) {
|
|
102
|
-
{
|
|
103
|
-
val: node1.val,
|
|
104
|
-
rank: node1.rank + 1,
|
|
105
|
-
children: [newNode, node2, ...node1.children],
|
|
106
|
-
}
|
|
107
|
-
} else if (
|
|
108
|
-
comp(node2.val, newNode.val) <= 0 && comp(node2.val, node1.val) <= 0
|
|
109
|
-
) {
|
|
110
|
-
{
|
|
111
|
-
val: node2.val,
|
|
112
|
-
rank: node2.rank + 1,
|
|
113
|
-
children: [newNode, node1, ...node2.children],
|
|
114
|
-
}
|
|
115
|
-
} else {
|
|
116
|
-
{ val: newNode.val, rank: node1.rank + 1, children: [node1, node2] }
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
let skewInsert = (comp, val, pq) => {
|
|
121
|
-
let newNode = { val, rank: 0, children: [] }
|
|
122
|
-
match (pq) {
|
|
123
|
-
// the only time two trees will have the same rank is if they are the
|
|
124
|
-
// smallest-ranked trees in the queue, in which case we should link
|
|
125
|
-
// them with the new node
|
|
126
|
-
[node1, node2, ...rest] when node1.rank == node2.rank =>
|
|
127
|
-
[skewLinkNodes(comp, newNode, node1, node2), ...rest],
|
|
128
|
-
_ => [newNode, ...pq],
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Produces a new priority queue by inserting the given element into the given priority queue.
|
|
134
|
-
*
|
|
135
|
-
* @param val: The value to add into the priority queue
|
|
136
|
-
* @param pq: The priority queue
|
|
137
|
-
* @returns A new priority queue with the given element inserted
|
|
138
|
-
*/
|
|
139
|
-
export let push = (val, pq) => {
|
|
140
|
-
let { comp, size, root } = pq
|
|
141
|
-
match (root) {
|
|
142
|
-
None => { comp, size: 1, root: Some({ rootVal: val, pq: [] }) },
|
|
143
|
-
Some({ rootVal, pq }) => {
|
|
144
|
-
// make the new value the root if it has higher priority than the highest priority value
|
|
145
|
-
let (morePriorityVal, lessPriorityVal) =
|
|
146
|
-
if (comp(val, rootVal) <= 0) (val, rootVal) else (rootVal, val)
|
|
147
|
-
|
|
148
|
-
let newRoot = Some(
|
|
149
|
-
{ rootVal: morePriorityVal, pq: skewInsert(comp, lessPriorityVal, pq) }
|
|
150
|
-
)
|
|
151
|
-
{ comp, size: size + 1, root: newRoot }
|
|
152
|
-
},
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Retrieves the highest priority element in the priority queue. It is not
|
|
158
|
-
* removed from the queue.
|
|
159
|
-
*
|
|
160
|
-
* @param pq: The priority queue to inspect
|
|
161
|
-
* @returns `Some(value)` containing the highest priority element or `None` if the priority queue is empty
|
|
162
|
-
*
|
|
163
|
-
* @since v0.5.3
|
|
164
|
-
*/
|
|
165
|
-
export let peek = pq => {
|
|
166
|
-
match (pq.root) {
|
|
167
|
-
None => None,
|
|
168
|
-
Some({ rootVal, _ }) => Some(rootVal),
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
let linkNodes = (comp, node1, node2) => {
|
|
173
|
-
// make the node with higher priority the parent of the node with smaller
|
|
174
|
-
// priority to presere heap-ordering
|
|
175
|
-
let (morePriority, lessPriority) =
|
|
176
|
-
if (comp(node1.val, node2.val) <= 0) (node1, node2) else (node2, node1)
|
|
177
|
-
{
|
|
178
|
-
val: morePriority.val,
|
|
179
|
-
rank: morePriority.rank + 1,
|
|
180
|
-
children: [lessPriority, ...morePriority.children],
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// step through the trees in the priority queue in increasing rank order until we
|
|
185
|
-
// find a missing rank, linking trees of equal rank as we go
|
|
186
|
-
let rec ins = (comp, node, pq) => {
|
|
187
|
-
match (pq) {
|
|
188
|
-
[] => [node],
|
|
189
|
-
[firstNode, ...rest] => {
|
|
190
|
-
if (node.rank < firstNode.rank) {
|
|
191
|
-
[node, firstNode, ...rest]
|
|
192
|
-
} else {
|
|
193
|
-
ins(comp, linkNodes(comp, node, firstNode), rest)
|
|
194
|
-
}
|
|
195
|
-
},
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
let uniquify = (comp, pq) => {
|
|
200
|
-
match (pq) {
|
|
201
|
-
[] => [],
|
|
202
|
-
[node, ...rest] => ins(comp, node, rest),
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
// step through the trees of two priority queues in increasing rank order,
|
|
207
|
-
// performing a simple link whenever we find two trees of equal rank
|
|
208
|
-
let rec mergeUniq = (comp, pq1, pq2) => {
|
|
209
|
-
match ((pq1, pq2)) {
|
|
210
|
-
([], ts) | (ts, []) => ts,
|
|
211
|
-
([node1, ...rest1], [node2, ...rest2]) => {
|
|
212
|
-
if (node1.rank < node2.rank) {
|
|
213
|
-
[node1, ...mergeUniq(comp, rest1, pq2)]
|
|
214
|
-
} else if (node2.rank < node1.rank) {
|
|
215
|
-
[node2, ...mergeUniq(comp, pq1, rest2)]
|
|
216
|
-
} else {
|
|
217
|
-
ins(comp, linkNodes(comp, node1, node2), mergeUniq(comp, rest1, rest2))
|
|
218
|
-
}
|
|
219
|
-
},
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
let merge = (comp, pq1, pq2) =>
|
|
224
|
-
mergeUniq(comp, uniquify(comp, pq1), uniquify(comp, pq2))
|
|
225
|
-
|
|
226
|
-
// splits the node with the minimum value out from the rest of the nodes
|
|
227
|
-
let rec separateHighestPriority = (comp, pq) => {
|
|
228
|
-
match (pq) {
|
|
229
|
-
// empty list case should never happen; this is here just to satisfy the compiler
|
|
230
|
-
[] => fail "Impossible: getHighestPriority called with empty PQ",
|
|
231
|
-
[node] => (node, []),
|
|
232
|
-
[node, ...rest] => {
|
|
233
|
-
let (currMinNode, currNonMinNodes) = separateHighestPriority(comp, rest)
|
|
234
|
-
if (comp(node.val, currMinNode.val) <= 0) {
|
|
235
|
-
(node, rest)
|
|
236
|
-
} else {
|
|
237
|
-
(currMinNode, [node, ...currNonMinNodes])
|
|
238
|
-
}
|
|
239
|
-
},
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
// splits the nodes of rank 0 out from the other nodes
|
|
244
|
-
let rec splitRankZero = (rankZeroVals, nonRankZeroNodes, pq) => {
|
|
245
|
-
match (pq) {
|
|
246
|
-
[] => (rankZeroVals, nonRankZeroNodes),
|
|
247
|
-
[node, ...rest] => {
|
|
248
|
-
if (node.rank == 0) {
|
|
249
|
-
splitRankZero([node.val, ...rankZeroVals], nonRankZeroNodes, rest)
|
|
250
|
-
} else {
|
|
251
|
-
splitRankZero(rankZeroVals, [node, ...nonRankZeroNodes], rest)
|
|
252
|
-
}
|
|
253
|
-
},
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
let withoutHighestPriority = (comp, pq) => {
|
|
258
|
-
// split out the node with the highest priority
|
|
259
|
-
let (hpNode, nonHpNodes) = separateHighestPriority(comp, pq)
|
|
260
|
-
// split out the values with nodes of rank 0, which will all be singleton nodes
|
|
261
|
-
let (rankZeroVals, nonRankZeroNodes) = splitRankZero([], [], hpNode.children)
|
|
262
|
-
|
|
263
|
-
let mergedPq = merge(comp, nonHpNodes, nonRankZeroNodes)
|
|
264
|
-
List.reduce((pq, val) => skewInsert(comp, val, pq), mergedPq, rankZeroVals)
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
let rec findHighestPriority = (comp, pq) => {
|
|
268
|
-
match (pq) {
|
|
269
|
-
// empty list case should never happen; this is here just to satisfy the compiler
|
|
270
|
-
[] => fail "Impossible: findHighestPriority with empty PQ",
|
|
271
|
-
[node] => node.val,
|
|
272
|
-
[node, ...rest] => {
|
|
273
|
-
let currMin = findHighestPriority(comp, rest)
|
|
274
|
-
if (comp(node.val, currMin) <= 0) node.val else currMin
|
|
275
|
-
},
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* Produces a new priority queue without the highest priority element in the
|
|
281
|
-
* given priority queue. If the input priority queue is empty, this function will
|
|
282
|
-
* return it.
|
|
283
|
-
*
|
|
284
|
-
* @param pq: The priority queue
|
|
285
|
-
* @returns A new priority queue without the highest priority element
|
|
286
|
-
*
|
|
287
|
-
* @since v0.5.3
|
|
288
|
-
*/
|
|
289
|
-
export let pop = pq => {
|
|
290
|
-
let pqWithRoot = pq
|
|
291
|
-
let { comp, size, root } = pq
|
|
292
|
-
match (root) {
|
|
293
|
-
None => pq,
|
|
294
|
-
Some({ pq, rootVal }) => {
|
|
295
|
-
let newRoot = if (pq == []) {
|
|
296
|
-
None
|
|
297
|
-
} else {
|
|
298
|
-
Some(
|
|
299
|
-
{
|
|
300
|
-
rootVal: findHighestPriority(comp, pq),
|
|
301
|
-
pq: withoutHighestPriority(comp, pq),
|
|
302
|
-
}
|
|
303
|
-
)
|
|
304
|
-
}
|
|
305
|
-
{ comp, size: size - 1, root: newRoot }
|
|
306
|
-
},
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
/**
|
|
311
|
-
* Produces a list of all elements in the priority queue in priority order.
|
|
312
|
-
*
|
|
313
|
-
* @param pq: The priority queue to drain
|
|
314
|
-
* @returns A list of all elements in the priority in priority order
|
|
315
|
-
*
|
|
316
|
-
* @since v0.5.3
|
|
317
|
-
*/
|
|
318
|
-
export let drain = pq => {
|
|
319
|
-
let rec drainRec = (acc, pq) => {
|
|
320
|
-
match (pq.root) {
|
|
321
|
-
None => acc,
|
|
322
|
-
Some(root) => drainRec([root.rootVal, ...acc], pop(pq)),
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
List.reverse(drainRec([], pq))
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* Constructs a new priority queue initialized with the elements in the list
|
|
330
|
-
* using a custom comparator function, which is used to determine priority of
|
|
331
|
-
* elements. The comparator function takes two elements and must return 0 if
|
|
332
|
-
* both share priority, a positive number if the first has greater priority,
|
|
333
|
-
* and a negative number if the first has less priority.
|
|
334
|
-
*
|
|
335
|
-
* @param list: A list of values used to initialize the priority queue
|
|
336
|
-
* @param comp: A comparator function used to assign priority to elements
|
|
337
|
-
* @returns A priority queue containing the elements from the list
|
|
338
|
-
*
|
|
339
|
-
* @since v0.5.3
|
|
340
|
-
*/
|
|
341
|
-
export let fromList = (list, comp) => {
|
|
342
|
-
List.reduce((pq, val) => push(val, pq), make(comp), list)
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* Constructs a new priority queue initialized with the elements in the array
|
|
347
|
-
* using a custom comparator function, which is used to determine priority of
|
|
348
|
-
* elements. The comparator function takes two elements and must return 0 if
|
|
349
|
-
* both share priority, a positive number if the first has greater priority,
|
|
350
|
-
* and a negative number if the first has less priority.
|
|
351
|
-
*
|
|
352
|
-
* @param array: An array of values used to initialize the priority queue
|
|
353
|
-
* @param comp: A comparator function used to assign priority to elements
|
|
354
|
-
* @returns A priority queue containing the elements from the array
|
|
355
|
-
*
|
|
356
|
-
* @since v0.5.4
|
|
357
|
-
*/
|
|
358
|
-
export let fromArray = (array, comp) => {
|
|
359
|
-
Array.reduce((pq, val) => push(val, pq), make(comp), array)
|
|
360
|
-
}
|
|
@@ -1,291 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: ImmutablePriorityQueue
|
|
3
|
-
---
|
|
4
|
-
|
|
5
|
-
An immutable priority queue. A priority queue is a data structure that maintains elements in a priority order. Elements with higher priority are served before elements with lower priority when extracting from the priority queue.
|
|
6
|
-
|
|
7
|
-
<details disabled>
|
|
8
|
-
<summary tabindex="-1">Added in <code>0.5.3</code></summary>
|
|
9
|
-
No other changes yet.
|
|
10
|
-
</details>
|
|
11
|
-
|
|
12
|
-
```grain
|
|
13
|
-
import ImmutablePriorityQueue from "immutablepriorityqueue"
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
## Types
|
|
17
|
-
|
|
18
|
-
Type declarations included in the ImmutablePriorityQueue module.
|
|
19
|
-
|
|
20
|
-
### ImmutablePriorityQueue.**ImmutablePriorityQueue**
|
|
21
|
-
|
|
22
|
-
```grain
|
|
23
|
-
type ImmutablePriorityQueue<a>
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
Immutable data structure which maintains a priority order for its elements.
|
|
27
|
-
|
|
28
|
-
## Values
|
|
29
|
-
|
|
30
|
-
Functions and constants for working with ImmutablePriorityQueues.
|
|
31
|
-
|
|
32
|
-
### ImmutablePriorityQueue.**empty**
|
|
33
|
-
|
|
34
|
-
<details disabled>
|
|
35
|
-
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
36
|
-
No other changes yet.
|
|
37
|
-
</details>
|
|
38
|
-
|
|
39
|
-
```grain
|
|
40
|
-
empty : ImmutablePriorityQueue<a>
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
An empty priority queue with the default `compare` comparator.
|
|
44
|
-
|
|
45
|
-
### ImmutablePriorityQueue.**make**
|
|
46
|
-
|
|
47
|
-
<details disabled>
|
|
48
|
-
<summary tabindex="-1">Added in <code>0.5.3</code></summary>
|
|
49
|
-
No other changes yet.
|
|
50
|
-
</details>
|
|
51
|
-
|
|
52
|
-
```grain
|
|
53
|
-
make : ((a, a) -> Number) -> ImmutablePriorityQueue<a>
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
Creates a new priority queue with a comparator function, which is used to
|
|
57
|
-
determine priority of elements. The comparator function takes two elements
|
|
58
|
-
and must return 0 if both share priority, a positive number if the first
|
|
59
|
-
has greater priority, and a negative number if the first has less priority.
|
|
60
|
-
|
|
61
|
-
Parameters:
|
|
62
|
-
|
|
63
|
-
|param|type|description|
|
|
64
|
-
|-----|----|-----------|
|
|
65
|
-
|`comp`|`(a, a) -> Number`|The comparator function used to indicate priority order|
|
|
66
|
-
|
|
67
|
-
Returns:
|
|
68
|
-
|
|
69
|
-
|type|description|
|
|
70
|
-
|----|-----------|
|
|
71
|
-
|`ImmutablePriorityQueue<a>`|An empty priority queue|
|
|
72
|
-
|
|
73
|
-
Examples:
|
|
74
|
-
|
|
75
|
-
```grain
|
|
76
|
-
ImmutablePriorityQueue.make(compare) // creates a min priority queue of numbers using the compare pervasive
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
```grain
|
|
80
|
-
ImmutablePriorityQueue.make((a, b) => String.length(b) - String.length(a)) // creates a priority queue by string length (longest to shortest)
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
### ImmutablePriorityQueue.**size**
|
|
84
|
-
|
|
85
|
-
<details disabled>
|
|
86
|
-
<summary tabindex="-1">Added in <code>0.5.3</code></summary>
|
|
87
|
-
No other changes yet.
|
|
88
|
-
</details>
|
|
89
|
-
|
|
90
|
-
```grain
|
|
91
|
-
size : ImmutablePriorityQueue<a> -> Number
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
Gets the number of elements in a priority queue.
|
|
95
|
-
|
|
96
|
-
Parameters:
|
|
97
|
-
|
|
98
|
-
|param|type|description|
|
|
99
|
-
|-----|----|-----------|
|
|
100
|
-
|`pq`|`ImmutablePriorityQueue<a>`|The priority queue to inspect|
|
|
101
|
-
|
|
102
|
-
Returns:
|
|
103
|
-
|
|
104
|
-
|type|description|
|
|
105
|
-
|----|-----------|
|
|
106
|
-
|`Number`|The number of elements in the priority queue|
|
|
107
|
-
|
|
108
|
-
### ImmutablePriorityQueue.**isEmpty**
|
|
109
|
-
|
|
110
|
-
<details disabled>
|
|
111
|
-
<summary tabindex="-1">Added in <code>0.5.3</code></summary>
|
|
112
|
-
No other changes yet.
|
|
113
|
-
</details>
|
|
114
|
-
|
|
115
|
-
```grain
|
|
116
|
-
isEmpty : ImmutablePriorityQueue<a> -> Bool
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
Determines if the priority queue contains no elements.
|
|
120
|
-
|
|
121
|
-
Parameters:
|
|
122
|
-
|
|
123
|
-
|param|type|description|
|
|
124
|
-
|-----|----|-----------|
|
|
125
|
-
|`pq`|`ImmutablePriorityQueue<a>`|The priority queue to check|
|
|
126
|
-
|
|
127
|
-
Returns:
|
|
128
|
-
|
|
129
|
-
|type|description|
|
|
130
|
-
|----|-----------|
|
|
131
|
-
|`Bool`|`true` if the priority queue is empty and `false` otherwise|
|
|
132
|
-
|
|
133
|
-
### ImmutablePriorityQueue.**push**
|
|
134
|
-
|
|
135
|
-
```grain
|
|
136
|
-
push : (a, ImmutablePriorityQueue<a>) -> ImmutablePriorityQueue<a>
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
Produces a new priority queue by inserting the given element into the given priority queue.
|
|
140
|
-
|
|
141
|
-
Parameters:
|
|
142
|
-
|
|
143
|
-
|param|type|description|
|
|
144
|
-
|-----|----|-----------|
|
|
145
|
-
|`val`|`a`|The value to add into the priority queue|
|
|
146
|
-
|`pq`|`ImmutablePriorityQueue<a>`|The priority queue|
|
|
147
|
-
|
|
148
|
-
Returns:
|
|
149
|
-
|
|
150
|
-
|type|description|
|
|
151
|
-
|----|-----------|
|
|
152
|
-
|`ImmutablePriorityQueue<a>`|A new priority queue with the given element inserted|
|
|
153
|
-
|
|
154
|
-
### ImmutablePriorityQueue.**peek**
|
|
155
|
-
|
|
156
|
-
<details disabled>
|
|
157
|
-
<summary tabindex="-1">Added in <code>0.5.3</code></summary>
|
|
158
|
-
No other changes yet.
|
|
159
|
-
</details>
|
|
160
|
-
|
|
161
|
-
```grain
|
|
162
|
-
peek : ImmutablePriorityQueue<a> -> Option<a>
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
Retrieves the highest priority element in the priority queue. It is not
|
|
166
|
-
removed from the queue.
|
|
167
|
-
|
|
168
|
-
Parameters:
|
|
169
|
-
|
|
170
|
-
|param|type|description|
|
|
171
|
-
|-----|----|-----------|
|
|
172
|
-
|`pq`|`ImmutablePriorityQueue<a>`|The priority queue to inspect|
|
|
173
|
-
|
|
174
|
-
Returns:
|
|
175
|
-
|
|
176
|
-
|type|description|
|
|
177
|
-
|----|-----------|
|
|
178
|
-
|`Option<a>`|`Some(value)` containing the highest priority element or `None` if the priority queue is empty|
|
|
179
|
-
|
|
180
|
-
### ImmutablePriorityQueue.**pop**
|
|
181
|
-
|
|
182
|
-
<details disabled>
|
|
183
|
-
<summary tabindex="-1">Added in <code>0.5.3</code></summary>
|
|
184
|
-
No other changes yet.
|
|
185
|
-
</details>
|
|
186
|
-
|
|
187
|
-
```grain
|
|
188
|
-
pop : ImmutablePriorityQueue<a> -> ImmutablePriorityQueue<a>
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
Produces a new priority queue without the highest priority element in the
|
|
192
|
-
given priority queue. If the input priority queue is empty, this function will
|
|
193
|
-
return it.
|
|
194
|
-
|
|
195
|
-
Parameters:
|
|
196
|
-
|
|
197
|
-
|param|type|description|
|
|
198
|
-
|-----|----|-----------|
|
|
199
|
-
|`pq`|`ImmutablePriorityQueue<a>`|The priority queue|
|
|
200
|
-
|
|
201
|
-
Returns:
|
|
202
|
-
|
|
203
|
-
|type|description|
|
|
204
|
-
|----|-----------|
|
|
205
|
-
|`ImmutablePriorityQueue<a>`|A new priority queue without the highest priority element|
|
|
206
|
-
|
|
207
|
-
### ImmutablePriorityQueue.**drain**
|
|
208
|
-
|
|
209
|
-
<details disabled>
|
|
210
|
-
<summary tabindex="-1">Added in <code>0.5.3</code></summary>
|
|
211
|
-
No other changes yet.
|
|
212
|
-
</details>
|
|
213
|
-
|
|
214
|
-
```grain
|
|
215
|
-
drain : ImmutablePriorityQueue<a> -> List<a>
|
|
216
|
-
```
|
|
217
|
-
|
|
218
|
-
Produces a list of all elements in the priority queue in priority order.
|
|
219
|
-
|
|
220
|
-
Parameters:
|
|
221
|
-
|
|
222
|
-
|param|type|description|
|
|
223
|
-
|-----|----|-----------|
|
|
224
|
-
|`pq`|`ImmutablePriorityQueue<a>`|The priority queue to drain|
|
|
225
|
-
|
|
226
|
-
Returns:
|
|
227
|
-
|
|
228
|
-
|type|description|
|
|
229
|
-
|----|-----------|
|
|
230
|
-
|`List<a>`|A list of all elements in the priority in priority order|
|
|
231
|
-
|
|
232
|
-
### ImmutablePriorityQueue.**fromList**
|
|
233
|
-
|
|
234
|
-
<details disabled>
|
|
235
|
-
<summary tabindex="-1">Added in <code>0.5.3</code></summary>
|
|
236
|
-
No other changes yet.
|
|
237
|
-
</details>
|
|
238
|
-
|
|
239
|
-
```grain
|
|
240
|
-
fromList : (List<a>, ((a, a) -> Number)) -> ImmutablePriorityQueue<a>
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
Constructs a new priority queue initialized with the elements in the list
|
|
244
|
-
using a custom comparator function, which is used to determine priority of
|
|
245
|
-
elements. The comparator function takes two elements and must return 0 if
|
|
246
|
-
both share priority, a positive number if the first has greater priority,
|
|
247
|
-
and a negative number if the first has less priority.
|
|
248
|
-
|
|
249
|
-
Parameters:
|
|
250
|
-
|
|
251
|
-
|param|type|description|
|
|
252
|
-
|-----|----|-----------|
|
|
253
|
-
|`list`|`List<a>`|A list of values used to initialize the priority queue|
|
|
254
|
-
|`comp`|`(a, a) -> Number`|A comparator function used to assign priority to elements|
|
|
255
|
-
|
|
256
|
-
Returns:
|
|
257
|
-
|
|
258
|
-
|type|description|
|
|
259
|
-
|----|-----------|
|
|
260
|
-
|`ImmutablePriorityQueue<a>`|A priority queue containing the elements from the list|
|
|
261
|
-
|
|
262
|
-
### ImmutablePriorityQueue.**fromArray**
|
|
263
|
-
|
|
264
|
-
<details disabled>
|
|
265
|
-
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
266
|
-
No other changes yet.
|
|
267
|
-
</details>
|
|
268
|
-
|
|
269
|
-
```grain
|
|
270
|
-
fromArray : (Array<a>, ((a, a) -> Number)) -> ImmutablePriorityQueue<a>
|
|
271
|
-
```
|
|
272
|
-
|
|
273
|
-
Constructs a new priority queue initialized with the elements in the array
|
|
274
|
-
using a custom comparator function, which is used to determine priority of
|
|
275
|
-
elements. The comparator function takes two elements and must return 0 if
|
|
276
|
-
both share priority, a positive number if the first has greater priority,
|
|
277
|
-
and a negative number if the first has less priority.
|
|
278
|
-
|
|
279
|
-
Parameters:
|
|
280
|
-
|
|
281
|
-
|param|type|description|
|
|
282
|
-
|-----|----|-----------|
|
|
283
|
-
|`array`|`Array<a>`|An array of values used to initialize the priority queue|
|
|
284
|
-
|`comp`|`(a, a) -> Number`|A comparator function used to assign priority to elements|
|
|
285
|
-
|
|
286
|
-
Returns:
|
|
287
|
-
|
|
288
|
-
|type|description|
|
|
289
|
-
|----|-----------|
|
|
290
|
-
|`ImmutablePriorityQueue<a>`|A priority queue containing the elements from the array|
|
|
291
|
-
|