atomic-fns 0.3.4 → 0.4.2

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 (106) hide show
  1. package/README.md +3 -3
  2. package/{core → dist}/collections/BTree.d.ts +6 -6
  3. package/{core → dist}/collections/BTree.js +17 -17
  4. package/{core → dist}/collections/Heap.d.ts +3 -3
  5. package/{core → dist}/collections/Heap.js +0 -0
  6. package/dist/collections/LRUCache.d.ts +80 -0
  7. package/dist/collections/LRUCache.js +126 -0
  8. package/{core → dist}/collections/SortedMap.d.ts +2 -2
  9. package/{core → dist}/collections/SortedMap.js +0 -0
  10. package/{core → dist}/collections/SortedSet.d.ts +3 -3
  11. package/{core → dist}/collections/SortedSet.js +1 -1
  12. package/{core → dist}/collections/SortedTree.d.ts +3 -3
  13. package/{core → dist}/collections/SortedTree.js +1 -1
  14. package/{core → dist}/collections/SplayTree.d.ts +5 -5
  15. package/{core → dist}/collections/SplayTree.js +0 -0
  16. package/{core → dist}/collections/abc.d.ts +1 -1
  17. package/{core → dist}/collections/abc.js +0 -0
  18. package/dist/collections/deque.d.ts +117 -0
  19. package/{core → dist}/collections/deque.js +61 -53
  20. package/{core → dist}/collections/frozenset.d.ts +0 -0
  21. package/{core → dist}/collections/frozenset.js +3 -6
  22. package/{core → dist}/collections/index.d.ts +4 -4
  23. package/{core → dist}/collections/index.js +0 -0
  24. package/{core → dist}/functools/index.d.ts +28 -13
  25. package/{core → dist}/functools/index.js +34 -12
  26. package/{core → dist}/globals/index.d.ts +8 -11
  27. package/{core → dist}/globals/index.js +8 -15
  28. package/dist/i18n/date/index.d.ts +204 -0
  29. package/dist/i18n/date/index.js +530 -0
  30. package/dist/i18n/date/utils.d.ts +65 -0
  31. package/dist/i18n/date/utils.js +312 -0
  32. package/dist/i18n/duration.d.ts +112 -0
  33. package/dist/i18n/duration.js +392 -0
  34. package/dist/i18n/format.d.ts +26 -0
  35. package/dist/i18n/format.js +495 -0
  36. package/{core → dist}/i18n/index.d.ts +1 -1
  37. package/{core → dist}/i18n/index.js +1 -1
  38. package/{core → dist}/index.d.ts +0 -0
  39. package/{core → dist}/index.js +0 -0
  40. package/{core → dist}/itertools/index.d.ts +27 -27
  41. package/{core → dist}/itertools/index.js +24 -24
  42. package/{core → dist}/math/index.d.ts +3 -3
  43. package/{core → dist}/math/index.js +0 -0
  44. package/{core → dist}/operators/index.d.ts +2 -2
  45. package/{core → dist}/operators/index.js +2 -2
  46. package/{core → dist}/random/index.d.ts +1 -1
  47. package/{core → dist}/random/index.js +3 -3
  48. package/package.json +28 -42
  49. package/.editorconfig +0 -12
  50. package/.esdoc.json +0 -27
  51. package/.eslintignore +0 -1
  52. package/.eslintrc.json +0 -35
  53. package/.firebaserc +0 -5
  54. package/.prettierrc +0 -19
  55. package/core/collections/BTree.ts +0 -1340
  56. package/core/collections/Heap.ts +0 -189
  57. package/core/collections/LRUCache.d.ts +0 -152
  58. package/core/collections/LRUCache.js +0 -409
  59. package/core/collections/LRUCache.ts +0 -486
  60. package/core/collections/SortedMap.ts +0 -146
  61. package/core/collections/SortedSet.ts +0 -110
  62. package/core/collections/SortedTree.ts +0 -160
  63. package/core/collections/SplayTree.ts +0 -354
  64. package/core/collections/abc.ts +0 -176
  65. package/core/collections/deque.d.ts +0 -110
  66. package/core/collections/deque.ts +0 -329
  67. package/core/collections/frozenset.ts +0 -37
  68. package/core/collections/index.ts +0 -1024
  69. package/core/collections/test.html +0 -10
  70. package/core/collections/typedArrays.d.ts +0 -31
  71. package/core/collections/typedArrays.js +0 -143
  72. package/core/collections/typedArrays.ts +0 -163
  73. package/core/functools/index.ts +0 -258
  74. package/core/globals/index.ts +0 -481
  75. package/core/i18n/date/format.d.ts +0 -16
  76. package/core/i18n/date/format.js +0 -307
  77. package/core/i18n/date/format.ts +0 -343
  78. package/core/i18n/date/index.d.ts +0 -223
  79. package/core/i18n/date/index.js +0 -387
  80. package/core/i18n/date/index.ts +0 -471
  81. package/core/i18n/date/test.html +0 -67
  82. package/core/i18n/index.ts +0 -8
  83. package/core/index.ts +0 -8
  84. package/core/itertools/index.ts +0 -494
  85. package/core/math/index.ts +0 -152
  86. package/core/operators/index.ts +0 -310
  87. package/core/random/index.ts +0 -66
  88. package/firebase.json +0 -10
  89. package/jasmine.json +0 -14
  90. package/tests/BTree.spec.ts +0 -143
  91. package/tests/Cache.spec.ts +0 -161
  92. package/tests/Heap.spec.ts +0 -66
  93. package/tests/SortedMap.spec.ts +0 -144
  94. package/tests/SortedSet.spec.ts +0 -84
  95. package/tests/SortedTree.spec.ts +0 -164
  96. package/tests/SplayTree.spec.ts +0 -121
  97. package/tests/collections.spec.ts +0 -288
  98. package/tests/deque.spec.ts +0 -107
  99. package/tests/globals.spec.ts +0 -270
  100. package/tests/itertools.spec.ts +0 -266
  101. package/tests/math.spec.ts +0 -25
  102. package/tests/operators.spec.ts +0 -40
  103. package/tests/random.spec.ts +0 -46
  104. package/tsconfig.json +0 -126
  105. package/tsconfig.node.json +0 -18
  106. package/tsconfig.xo.json +0 -9
@@ -1,110 +0,0 @@
1
- import { Collection } from './abc.js'
2
- import { SplayTree } from './SplayTree.js'
3
-
4
- /**
5
- * A `SortedSet` is a set that holds values in order. Any primitive value (and objects that implement {@link Comparable} or {@link Ordered} interfaces) can be used as values.
6
- * @see {@link Compare}
7
- * @see {@link Ordered}
8
- * @template K, V
9
- */
10
- export class SortedSet<T> extends Collection {
11
- private readonly store: SplayTree<T>
12
- /**
13
- * Creates a new `SortedSet` object.
14
- * @param {Iterable<T>} [iterable] Initialize the set with entries.
15
- * @param {Comparer} [compareFn] The `compare` function to sort keys.
16
- */
17
- constructor(iterable?: Iterable<T>, compareFn?) {
18
- super()
19
- this.store = new SplayTree(compareFn)
20
- if (iterable) {
21
- for (const key of iterable) {
22
- this.store.add(key)
23
- }
24
- }
25
- }
26
-
27
- /**
28
- * Inserts a new value in order in the `Set`, if the value doesn't exist.
29
- * @param {T} value
30
- * @returns {this} Returns the Set object.
31
- */
32
- add(value: T) {
33
- this.store.add(value)
34
- return this
35
- }
36
-
37
- /**
38
- * Returns the smallest value in the set.
39
- * @returns {?T}
40
- */
41
- min() {
42
- return this.store.min()
43
- }
44
-
45
- /**
46
- * Returns the largest value in the set.
47
- * @returns {?T}
48
- */
49
- max() {
50
- return this.store.max()
51
- }
52
-
53
- /**
54
- * Removes the value from the set and returns `true` if the value was found and removed.
55
- * @param {T} value
56
- * @returns {boolean} `true` if the value was found and removed.
57
- */
58
- delete(value: T) {
59
- return this.store.remove(value) !== undefined
60
- }
61
-
62
- /**
63
- * Removes all values in the set.
64
- */
65
- clear() {
66
- return this.store.clear()
67
- }
68
-
69
- /**
70
- * Returns the total number of elements in the set.
71
- * @returns {number}
72
- */
73
- get size() {
74
- return this.store.size
75
- }
76
-
77
- /**
78
- * Check if there are no values in the set.
79
- * @returns {boolean} `true` if is empty.
80
- */
81
- empty() {
82
- return this.store.empty()
83
- }
84
-
85
- /**
86
- * Returns `true` if the `value` exists.
87
- * @param {T} value
88
- * @returns {boolean} `true` if value is found, otherwise `false`.
89
- */
90
- contains(value: T) {
91
- return this.store.contains(value)
92
- }
93
-
94
- /**
95
- * Returns `true` if the `value` exists.
96
- * @param {T} value
97
- * @returns {boolean} `true` if value is found, otherwise `false`.
98
- */
99
- has(value: T) {
100
- return this.store.contains(value)
101
- }
102
-
103
- /**
104
- * Returns a new iterator of all values **in-order**.
105
- * @returns {Iterable<T>}
106
- */
107
- values() {
108
- return this.store.keys()
109
- }
110
- }
@@ -1,160 +0,0 @@
1
- import { Comparer } from '../operators/index.js'
2
- import { Mapping } from './abc.js'
3
- import { BTree } from './BTree.js'
4
-
5
- /**
6
- * A `SortedTree` is a map that holds key value pairs ordered by keys. Any primitive value (and objects that implement {@link Comparable} or {@link Ordered} interfaces) can be used as keys or values.
7
- * @see {@link Compare}
8
- * @see {@link Ordered}
9
- * @template K, V
10
- */
11
- export class SortedTree<K, V> extends Mapping<K, V> {
12
- private readonly store: BTree<K, V>
13
-
14
- /**
15
- * Creates a new `SortedTree` object.
16
- * @param {Iterable<[K, V]>} [iterable] Initialize the tree with entries.
17
- * @param {Comparer} [compareFn] The `compare` function to sort keys.
18
- */
19
- constructor(iterable?: Iterable<[K, V]>, compareFn?: Comparer) {
20
- super()
21
- this.store = new BTree<K, V>(iterable, compareFn)
22
- }
23
-
24
- /**
25
- * Adds a new `key` with a `null` value.
26
- * @param {K} key
27
- * @returns {this} Returns the Tree object.
28
- */
29
- add(key: K) {
30
- this.store.set(key, null as V)
31
- return this
32
- }
33
-
34
- /**
35
- * Returns the value associated with the `key` or `undefined`.
36
- * @param {K} key
37
- * @returns {?V} The key value or `undefined`.
38
- */
39
- get(key: K) {
40
- return this.store.get(key)
41
- }
42
-
43
- /**
44
- * Sets the value for the given `key`.
45
- * @param {K} key
46
- * @param {V} value
47
- * @returns {this} Returns the Tree object.
48
- */
49
- set(key: K, value: V) {
50
- this.store.set(key, value)
51
- return this
52
- }
53
-
54
- /**
55
- * Returns the smallest key in the tree.
56
- * @returns {?K}
57
- */
58
- min() {
59
- return this.store.minKey()
60
- }
61
-
62
- /**
63
- * Returns the largest key in the tree.
64
- * @returns {?K}
65
- */
66
- max() {
67
- return this.store.maxKey()
68
- }
69
-
70
- /**
71
- * Returns `true` if the `key` existed in the tree and was removed.
72
- * @param {K} key
73
- * @returns {boolean} `true` if key was removed, otherwise `false`.
74
- */
75
- delete(key: K) {
76
- return this.store.remove(key)
77
- }
78
-
79
- /**
80
- * Removes all key/value pairs in the tree.
81
- */
82
- clear() {
83
- return this.store.clear()
84
- }
85
-
86
- /**
87
- * Freezes the tree which prevents adding, updating or removing any keys.
88
- * @returns {this}
89
- */
90
- freeze() {
91
- this.store.freeze()
92
- return this
93
- }
94
-
95
- /**
96
- * Returns a new iterator of `[K, V]` pairs in reverse order of keys.
97
- * @returns {Iterable<[K, V]>}
98
- */
99
- reversed() {
100
- return this.store.reversed()
101
- }
102
-
103
- /**
104
- * Returns the total key/value pairs in the tree.
105
- * @returns {number}
106
- */
107
- get size() {
108
- return this.store.size
109
- }
110
-
111
- /**
112
- * Check if there are no key/value pairs in the tree.
113
- * @returns {boolean} `true` if is empty.
114
- */
115
- empty() {
116
- return this.store.empty()
117
- }
118
-
119
- /**
120
- * Returns `true` if the `key` exists in the tree.
121
- * @param {K} key
122
- * @returns {boolean} `true` if key is found, otherwise `false`.
123
- */
124
- contains(key: K) {
125
- return this.store.contains(key)
126
- }
127
-
128
- /**
129
- * Returns `true` if the `key` exists in the tree.
130
- * @param {K} key
131
- * @returns {boolean} `true` if key is found, otherwise `false`.
132
- */
133
- has(key: K) {
134
- return this.store.contains(key)
135
- }
136
-
137
- /**
138
- * Returns a new iterator of all **ordered** keys in the tree.
139
- * @returns {Iterable<K>}
140
- */
141
- keys() {
142
- return this.store.keys()
143
- }
144
-
145
- /**
146
- * Returns a new iterator of all values **ordered** by their keys.
147
- * @returns {Iterable<V>}
148
- */
149
- values() {
150
- return this.store.values()
151
- }
152
-
153
- /**
154
- * Returns a new iterator of all key value pairs **ordered** by their keys.
155
- * @returns {Iterable<[K, V]>}
156
- */
157
- entries() {
158
- return this.store.entries()
159
- }
160
- }
@@ -1,354 +0,0 @@
1
- import { compare, Comparer, eq } from '../operators/index.js'
2
- import { Mapping } from './abc.js'
3
-
4
- /**
5
- * Base SplayTree node class.
6
- * @private
7
- */
8
- class Node<K = any, V = any> {
9
- key: K
10
- value: V
11
- left?: Node<K, V>
12
- right?: Node<K, V>
13
-
14
- constructor(key: K, value: V) {
15
- this.key = key
16
- this.value = value
17
- }
18
-
19
- get height() {
20
- if (this.left && this.right) return 1 + Math.max(this.right.height, this.left.height)
21
- if (this.right) return 1 + this.right.height
22
- if (this.left) return 1 + this.left.height
23
- return 0
24
- }
25
- }
26
-
27
- // In-order traversal of a tree
28
- function* traverse(node?: Node) {
29
- while (node) {
30
- yield* traverse(node.left)
31
- yield node
32
- node = node.right
33
- }
34
- }
35
-
36
- /**
37
- * A splay tree is a *self-balancing binary* search tree with the additional property that recently accessed elements are quick to access again.
38
- * Insertion, look-up and removal in O(log n) *amortized* time.
39
- * For many patterns of non-random operations splay trees can take better than logarithmic time, without requiring advance knowledge of the pattern.
40
- * @see {@link https://en.wikipedia.org/wiki/Splay_tree Splay tree}
41
- * @see {@link https://web.archive.org/web/20220930173835/http://www14.in.tum.de/personen/albers/papers/ipl02.pdf Randomized Splay Trees}
42
- * @template K, V
43
- */
44
- export class SplayTree<K, V = any> extends Mapping<K, V> {
45
- protected root?: Node<K, V>
46
- protected cmp: Comparer
47
- protected randSplay: number
48
- protected count: number
49
-
50
- /**
51
- * Create a new randomized Splay Tree
52
- * @param compareFn
53
- * @param p When an element is accessed, with probability `p` splay the tree. With probability `1 - p`, leave the tree as it is.
54
- */
55
- constructor(compareFn?, p = 0.5) {
56
- super()
57
- this.cmp = compareFn ?? compare
58
- this.count = 0
59
- this.randSplay = p
60
- }
61
-
62
- top() {
63
- return this.root?.key
64
- }
65
-
66
- get size() {
67
- return this.count
68
- }
69
-
70
- empty() {
71
- return !this.root
72
- }
73
-
74
- get height() {
75
- if (!this.root) return 0
76
- return this.root.height
77
- }
78
-
79
- /**
80
- * Returns the value for `key` or `undefined` if the key is not found.
81
- * @param key The key to find.
82
- * @returns {?V} The value for the key or `undefined`.
83
- */
84
- get(key: K) {
85
- if (!this.root) return
86
-
87
- // Note: deterministic splaying (always moves key to front):
88
- // this.splay(key)
89
- // if (this.cmp(key, this.root.key) === 0) return this.root.value
90
-
91
- // Randomized Splay Trees https://doi.org/10.1016%2Fs0020-0190%2801%2900230-7
92
- // The randomized idea is only moving accessed keys with a certain probability `p`.
93
- // Values of `p` closer to 1 tend to perform well.
94
- if (Math.random() <= this.randSplay) {
95
- this.splay(key)
96
- if (this.cmp(key, this.root.key) === 0) return this.root.value
97
- else return undefined
98
- }
99
-
100
- // Lookup the node down the tree
101
- let current = this.root as any
102
- while (current) {
103
- const rank = this.cmp(key, current.key)
104
- if (rank === 0) return current.value
105
- if (rank < 0) current = current.left
106
- else current = current.right
107
- }
108
- }
109
-
110
- /**
111
- * Inserts the `key` and `value` in the tree if the `key` does not exist.
112
- * @param {K} key A key which can be any comparable value
113
- * @param {V} value A value associated with the key
114
- * @returns
115
- */
116
- set(key: K, value: V) {
117
- if (!this.root) {
118
- this.root = new Node(key, value)
119
- this.count += 1
120
- return
121
- }
122
- // Splay on the key to move the last node on the search path for
123
- // the key to the root of the tree.
124
- this.splay(key)
125
- const rank = this.cmp(key, this.root.key)
126
- if (rank === 0) {
127
- // Update the value
128
- this.root.value = value
129
- return
130
- }
131
- const node = new Node(key, value)
132
- if (rank > 0) {
133
- node.left = this.root
134
- node.right = this.root.right
135
- this.root.right = undefined
136
- } else {
137
- node.right = this.root
138
- node.left = this.root.left
139
- this.root.left = undefined
140
- }
141
- this.root = node
142
- this.count += 1
143
- }
144
-
145
- /**
146
- * Removes a specified key if it exists in the tree. The removed value is returned or `undefined` if not found.
147
- * @param {K} key The key to remove.
148
- * @returns {V} The removed value associated with `key`.
149
- */
150
- remove(key: K) {
151
- if (!this.root) {
152
- return
153
- }
154
- this.splay(key)
155
- if (!eq(key, this.root.key)) {
156
- return
157
- }
158
- const removed = this.root
159
- if (!this.root.left) {
160
- this.root = this.root.right
161
- } else {
162
- const right = this.root.right
163
- this.root = this.root.left
164
- //
165
- // Splay to make sure that the new root has an empty right child.
166
- //
167
- this.splay(key)
168
- //
169
- // Insert the original right child as the right child of the new
170
- // root.
171
- //
172
- this.root.right = right
173
- }
174
- this.count -= 1
175
- return removed.value
176
- }
177
-
178
- //
179
- // function find(key)
180
- // @param {number} key Key to find in the tree.
181
- // @return {SplayTree.Node} Node having the specified key.
182
- //
183
- //
184
-
185
- /**
186
- * Returns the minimum key in the tree or subtree.
187
- * @returns The minimum key.
188
- */
189
- min(root = this.root) {
190
- if (!root) return
191
- while (root.left) {
192
- root = root.left
193
- }
194
- return root.key
195
- }
196
-
197
- /**
198
- * Returns the maximum key in the tree or subtree.
199
- * @returns The maximum key.
200
- */
201
- max(root = this.root) {
202
- if (!root) return
203
- while (root.right) {
204
- root = root.right
205
- }
206
- return root.key
207
- }
208
-
209
- /**
210
- * Returns the largest key that is less than a given key.
211
- * @param key The given key
212
- * @returns The largest key found or `undefined`.
213
- */
214
- lowerBound(key) {
215
- if (!this.root) return
216
-
217
- // Splay on the key to move the node with the given key or the last
218
- // node on the search path to the top of the tree.
219
- this.splay(key)
220
-
221
- // Now the result is either the root node or the greatest node in
222
- // the left subtree.
223
- if (this.cmp(this.root.key, key) < 0) {
224
- return this.root
225
- } else if (this.root.left) {
226
- return this.max(this.root.left)
227
- }
228
- }
229
-
230
- /**
231
- * This is the simplified top-down splaying method proposed by Sleator and Tarjan in {@link https://doi.org/10.1145%2F3828.3835 Self-Adjusting Binary Search Trees}.
232
- * @param key
233
- * @see {@link https://doi.org/10.1145%2F3828.3835 Self-Adjusting Binary Search Trees}
234
- */
235
- splay(key) {
236
- if (!this.root) return
237
-
238
- let stub, left, right
239
- // eslint-disable-next-line
240
- stub = left = right = new Node(null, null)
241
-
242
- let root = this.root
243
-
244
- while (true) {
245
- const rank = this.cmp(key, root.key)
246
- if (rank < 0) {
247
- if (!root.left) {
248
- break
249
- }
250
-
251
- if (this.cmp(key, root.left.key) < 0) {
252
- // Rotate right.
253
- const tmp = root.left
254
- root.left = tmp.right
255
- tmp.right = root
256
- root = tmp
257
- if (!root.left) {
258
- break
259
- }
260
- }
261
-
262
- // Link right.
263
- right.left = root
264
- right = root
265
- root = root.left
266
- } else if (rank > 0) {
267
- if (!root.right) {
268
- break
269
- }
270
- if (this.cmp(key, root.right.key) > 0) {
271
- // Rotate left.
272
- const tmp = root.right
273
- root.right = tmp.left
274
- tmp.left = root
275
- root = tmp
276
- if (!root.right) {
277
- break
278
- }
279
- }
280
- // Link left.
281
- left.right = root
282
- left = root
283
- root = root.right
284
- } else {
285
- break
286
- }
287
- }
288
-
289
- // Assemble.
290
- left.right = root.left
291
- right.left = root.right
292
- root.left = stub.right
293
- root.right = stub.left
294
- this.root = root
295
- }
296
-
297
- add(key: K) {
298
- return this.set(key, null as V)
299
- }
300
-
301
- /**
302
- * Returns `true` if the given key is in the tree.
303
- * @param {K} key The key to find.
304
- * @returns `true` if found.
305
- */
306
- contains(key: K): boolean {
307
- return this.get(key) !== undefined
308
- }
309
-
310
- /** Alias of remove but just returns `true` instead of the deleted value. */
311
- delete(key: K): boolean {
312
- // since remove throws, this will throw too
313
- this.remove(key)
314
- return true
315
- }
316
-
317
- clear() {
318
- if (!this.root) return
319
- this.root.left = undefined
320
- this.root.right = undefined
321
- this.root = undefined
322
- this.count = 0
323
- }
324
-
325
- /**
326
- * Returns an ordered iterable of all the keys in the tree.
327
- * @returns {Iterable<K>} An iterable of keys in-order.
328
- */
329
- *keys(): Iterable<K> {
330
- for (const node of traverse(this.root)) {
331
- yield node.key as K
332
- }
333
- }
334
-
335
- /**
336
- * Returns an ordered iterable of all the keys and values in the tree.
337
- * @returns {Iterable<[K,V]>} A iterable of `[key, value]` pairs sorted by key.
338
- */
339
- *entries(): Iterable<[K, V]> {
340
- for (const node of traverse(this.root)) {
341
- yield [node.key as K, node.value as V]
342
- }
343
- }
344
-
345
- /**
346
- * Returns an ordered iterable of all the keys and values in the tree.
347
- * @returns {Iterable<V>} A iterable of `[key, value]` pairs sorted by key.
348
- */
349
- *values(): Iterable<V> {
350
- for (const node of traverse(this.root)) {
351
- yield node.value as V
352
- }
353
- }
354
- }