atomic-fns 0.3.1 → 0.3.3
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/.esdoc.json +2 -2
- package/README.md +48 -13
- package/core/collections/BTree.d.ts +229 -0
- package/core/collections/BTree.js +1165 -0
- package/core/collections/BTree.ts +1340 -0
- package/core/collections/Heap.d.ts +69 -0
- package/core/collections/Heap.js +181 -0
- package/core/collections/Heap.ts +189 -0
- package/core/collections/LRUCache.d.ts +131 -0
- package/core/collections/LRUCache.js +388 -0
- package/core/collections/LRUCache.ts +465 -0
- package/core/collections/SortedMap.d.ts +92 -0
- package/core/collections/SortedMap.js +130 -0
- package/core/collections/SortedMap.ts +146 -0
- package/core/collections/SortedSet.d.ts +63 -0
- package/core/collections/SortedSet.js +93 -0
- package/core/collections/SortedSet.ts +104 -0
- package/core/collections/SortedTree.d.ts +103 -0
- package/core/collections/SortedTree.js +141 -0
- package/core/collections/SortedTree.ts +160 -0
- package/core/collections/SplayTree.d.ts +105 -0
- package/core/collections/SplayTree.js +338 -0
- package/core/collections/SplayTree.ts +354 -0
- package/core/collections/abc.d.ts +152 -0
- package/core/collections/abc.js +39 -0
- package/core/collections/abc.ts +176 -0
- package/core/collections/deque.d.ts +110 -0
- package/core/collections/deque.js +285 -0
- package/core/collections/deque.ts +329 -0
- package/core/collections/frozenset.d.ts +19 -0
- package/core/collections/frozenset.js +33 -0
- package/core/collections/frozenset.ts +37 -0
- package/{src → core}/collections/index.d.ts +41 -162
- package/{src → core}/collections/index.js +57 -165
- package/{src → core}/collections/index.ts +61 -225
- package/core/collections/test.html +10 -0
- package/core/collections/typedArrays.d.ts +31 -0
- package/core/collections/typedArrays.js +143 -0
- package/core/collections/typedArrays.ts +163 -0
- package/core/datetime/index.d.ts +45 -0
- package/core/datetime/index.js +71 -0
- package/core/datetime/index.ts +128 -0
- package/{src → core}/functools/index.d.ts +2 -2
- package/{src → core}/functools/index.js +2 -2
- package/{src → core}/functools/index.ts +2 -2
- package/{src → core}/globals/index.d.ts +2 -7
- package/{src → core}/globals/index.js +5 -2
- package/{src → core}/globals/index.ts +6 -9
- package/{src → core}/index.d.ts +0 -1
- package/{src → core}/index.js +0 -1
- package/{src → core}/index.ts +0 -1
- package/{src → core}/itertools/index.d.ts +9 -7
- package/{src → core}/itertools/index.js +25 -9
- package/{src → core}/itertools/index.ts +39 -16
- package/{src → core}/math/index.d.ts +0 -0
- package/{src → core}/math/index.js +0 -0
- package/{src → core}/math/index.ts +0 -0
- package/core/operators/index.d.ts +148 -0
- package/core/operators/index.js +271 -0
- package/core/operators/index.ts +282 -0
- package/{src → core}/random/index.d.ts +0 -0
- package/{src → core}/random/index.js +0 -0
- package/{src → core}/random/index.ts +0 -0
- package/package.json +6 -6
- package/tests/BTree.spec.ts +143 -0
- package/tests/Cache.spec.ts +161 -0
- package/tests/Heap.spec.ts +66 -0
- package/tests/SortedMap.spec.ts +144 -0
- package/tests/SortedSet.spec.ts +84 -0
- package/tests/SortedTree.spec.ts +164 -0
- package/tests/SplayTree.spec.ts +121 -0
- package/tests/collections.spec.ts +1 -1
- package/tests/deque.spec.ts +107 -0
- package/tests/globals.spec.ts +2 -2
- package/tests/itertools.spec.ts +2 -2
- package/tests/math.spec.ts +1 -1
- package/tests/operators.spec.ts +5 -5
- package/tests/random.spec.ts +10 -10
- package/tsconfig.json +10 -10
- package/tsconfig.node.json +2 -2
- package/tsconfig.xo.json +2 -2
- package/src/datetime/index.d.ts +0 -6
- package/src/datetime/index.js +0 -6
- package/src/datetime/index.ts +0 -7
- package/src/operators/index.d.ts +0 -31
- package/src/operators/index.js +0 -171
- package/src/operators/index.ts +0 -175
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic operations for both values and objects.
|
|
3
|
+
*
|
|
4
|
+
* @module Operators
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { call, isObject } from '../globals/index.js'
|
|
8
|
+
|
|
9
|
+
/** This is the identity function. It always returns the same value that was passed in */
|
|
10
|
+
export const id = (x) => x
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Called to implement truth value testing.
|
|
14
|
+
* @param x
|
|
15
|
+
* @returns {boolean} `x.bool()` if exists or `!!x`.
|
|
16
|
+
*/
|
|
17
|
+
export const bool = (x) => {
|
|
18
|
+
const r = call(x, 'bool')
|
|
19
|
+
if (r != null) return r
|
|
20
|
+
return !!x
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Returns `true` if `x` is a falsy value.
|
|
25
|
+
* @see {@link bool}
|
|
26
|
+
*/
|
|
27
|
+
export const not = (x) => !bool(x)
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Returns `true` if `x` is an instance of class `y`
|
|
31
|
+
* @param x The instance object
|
|
32
|
+
* @param y The parent class
|
|
33
|
+
* @returns {boolean}
|
|
34
|
+
*/
|
|
35
|
+
export const isinstance = (x, y) => x instanceof y
|
|
36
|
+
|
|
37
|
+
/** Describes a function used to compare two values.
|
|
38
|
+
*
|
|
39
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#parameters Array.sort()}
|
|
40
|
+
*/
|
|
41
|
+
export type Comparer<T = any> = (x: T, y: T) => number
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Checks whether `x` is a comparable type and returns the result of `x.compare(y)`.
|
|
45
|
+
* Otherwise the return value of the compare function is equivalent to `obj === other ? 0 : obj < other ? -1 : 1`
|
|
46
|
+
* @param {*} x An initial value
|
|
47
|
+
* @param {*} y Other value to compare
|
|
48
|
+
* @returns {number} The comparison result
|
|
49
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#parameters Array.sort()}
|
|
50
|
+
* @see {@link eq}
|
|
51
|
+
* @see {@link lt}
|
|
52
|
+
*/
|
|
53
|
+
export function compare(x, y): number {
|
|
54
|
+
if (x === y || eq(x, y)) return 0
|
|
55
|
+
const op = call(x, 'compare', y)
|
|
56
|
+
if (typeof op === 'number') return op
|
|
57
|
+
if (x < y || lt(x, y)) return -1
|
|
58
|
+
return 1
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Compares two values to check if they are the same. If `x` is an object with an `eq` method, it returns `x.eq(y)`.
|
|
63
|
+
* Otherwise it returns the result of `x === y`.
|
|
64
|
+
* @param {*} x
|
|
65
|
+
* @param {*} y
|
|
66
|
+
* @returns {boolean} `true` if `x` and `y` are equal or `false` otherwise.
|
|
67
|
+
*/
|
|
68
|
+
export function eq(x, y) {
|
|
69
|
+
return x === y || call(x, 'eq', y) != null
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Compares two values to check if `x` is strictly less than `y`. If `x` is an object with a `lt` method, it returns `x.lt(y)`.
|
|
74
|
+
* Otherwise it returns the result of `x < y`.
|
|
75
|
+
* @param {*} x
|
|
76
|
+
* @param {*} y
|
|
77
|
+
* @returns {boolean} `true` if `x` is strictly less than `y` or `false` otherwise.
|
|
78
|
+
*/
|
|
79
|
+
export function lt(x, y) {
|
|
80
|
+
return x < y || call(x, 'lt', y) != null
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Compares two values to check if `x <= y`. If `x` is an object with a `lte` method, it returns `x.lte(y)`, also checks `x.eq(y)` and `x.lt(y)`.
|
|
85
|
+
* Otherwise it returns the result of `x <= y`.
|
|
86
|
+
* @param {*} x
|
|
87
|
+
* @param {*} y
|
|
88
|
+
* @returns {boolean} `true` if `x` is less than or equal to `y` or `false` otherwise.
|
|
89
|
+
* @see {@link eq}
|
|
90
|
+
* @see {@link lt}
|
|
91
|
+
*/
|
|
92
|
+
export function lte(x, y) {
|
|
93
|
+
if (x <= y || call(x, 'lte', y)) return true
|
|
94
|
+
if (x === y || eq(x, y)) return true
|
|
95
|
+
if (x < y || lt(x, y)) return true
|
|
96
|
+
return false
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Compares two values to check if `x` is strictly greater than `y`. If `x` is an object with a `gt` method, it returns `x.gt(y)`.
|
|
101
|
+
* Otherwise it returns the result of `x > y`.
|
|
102
|
+
* @param {*} x
|
|
103
|
+
* @param {*} y
|
|
104
|
+
* @returns {boolean} `true` if `x` is strictly greater than `y` or `false` otherwise.
|
|
105
|
+
*/
|
|
106
|
+
export function gt(x, y) {
|
|
107
|
+
return x > y || call(x, 'gt', y)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Compares two values to check if `x >= y`. If `x` is an object with a `gte` method, it returns `x.gte(y)`, also checks `x.eq(y)` and `x.gt(y)`.
|
|
112
|
+
* Otherwise it returns the result of `x >= y`.
|
|
113
|
+
* @param {*} x
|
|
114
|
+
* @param {*} y
|
|
115
|
+
* @returns {boolean} `true` if `x` is less greater than or equal to `y` or `false` otherwise.
|
|
116
|
+
* @see {@link eq}
|
|
117
|
+
* @see {@link gt}
|
|
118
|
+
*/
|
|
119
|
+
export function gte(x, y) {
|
|
120
|
+
if (x <= y || call(x, 'gte', y)) return true
|
|
121
|
+
if (x === y || eq(x, y)) return true
|
|
122
|
+
if (x > y || gt(x, y)) return true
|
|
123
|
+
return false
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The addition (+) operator. If `x` is an object with an `add` method, it returns `x.add(y)`.
|
|
128
|
+
* Otherwise it returns the result of `x + y`.
|
|
129
|
+
* @param {*} x
|
|
130
|
+
* @param {*} y
|
|
131
|
+
* @returns {*} x + y
|
|
132
|
+
*/
|
|
133
|
+
export function add(x, y) {
|
|
134
|
+
const op = call(x, 'add', y)
|
|
135
|
+
if (op != null) return op
|
|
136
|
+
return x + y
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The subtraction (-) operator. If `x` is an object with a `sub` method, it returns `x.sub(y)`.
|
|
141
|
+
* Otherwise it returns the result of `x - y`.
|
|
142
|
+
* @param {*} x
|
|
143
|
+
* @param {*} y
|
|
144
|
+
* @returns {*} x - y
|
|
145
|
+
*/
|
|
146
|
+
export function sub(x, y) {
|
|
147
|
+
const op = call(x, 'sub', y)
|
|
148
|
+
if (op != null) return op
|
|
149
|
+
return x - y
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The multiplication (*) operator. If `x` is an object with an `mul` method, it returns `x.mul(y)`.
|
|
154
|
+
* Otherwise it returns the result of `x * y`.
|
|
155
|
+
* @param {*} x
|
|
156
|
+
* @param {*} y
|
|
157
|
+
* @returns {*} x * y
|
|
158
|
+
*/
|
|
159
|
+
export function mul(x, y) {
|
|
160
|
+
const op = call(x, 'mul', y)
|
|
161
|
+
if (op != null) return op
|
|
162
|
+
return x * y
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* The division (/) operator. If `x` is an object with a `div` method, it returns `x.div(y)`.
|
|
166
|
+
* Otherwise it returns the result of `x / y`.
|
|
167
|
+
* @param {*} x
|
|
168
|
+
* @param {*} y
|
|
169
|
+
* @returns {*} x / y
|
|
170
|
+
*/
|
|
171
|
+
export function div(x, y) {
|
|
172
|
+
const op = call(x, 'div', y)
|
|
173
|
+
if (op != null) return op
|
|
174
|
+
return x / y
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* The modulo (%) operator. If `x` is an object with a `mod` method, it returns `x.mod(y)`.
|
|
178
|
+
* Otherwise it returns the result of `x % y`.
|
|
179
|
+
* @param {*} x
|
|
180
|
+
* @param {*} y
|
|
181
|
+
* @returns {*} x % y
|
|
182
|
+
*/
|
|
183
|
+
export function mod(x, y) {
|
|
184
|
+
const op = call(x, 'mod', y)
|
|
185
|
+
if (op != null) return op
|
|
186
|
+
return x % y
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* The power (**) operator. If `x` is an object with a `pow` method, it returns `x.pow(y)`.
|
|
191
|
+
* Otherwise it returns the result of `x ** y`.
|
|
192
|
+
* @param {*} x
|
|
193
|
+
* @param {*} y
|
|
194
|
+
* @returns {*} x ** y
|
|
195
|
+
*/
|
|
196
|
+
export function pow(x, y) {
|
|
197
|
+
const op = call(x, 'pow', y)
|
|
198
|
+
if (op != null) return op
|
|
199
|
+
return x ** y
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Performs a shallow comparison of two objects or arrays to check if they have the same keys, length and values.
|
|
204
|
+
* @param obj
|
|
205
|
+
* @param other
|
|
206
|
+
* @returns `true` if the objects are considered equal.
|
|
207
|
+
* @see {@link deepEqual}
|
|
208
|
+
*/
|
|
209
|
+
export function shallowEqual(obj, other) {
|
|
210
|
+
if (obj === other) return true
|
|
211
|
+
|
|
212
|
+
if (!obj || !other) return false
|
|
213
|
+
|
|
214
|
+
if (Array.isArray(obj)) {
|
|
215
|
+
// compare the arrays
|
|
216
|
+
if (obj.length !== other.length) return false
|
|
217
|
+
|
|
218
|
+
for (let i = 0; i < obj.length; i++) {
|
|
219
|
+
if (obj[i] !== other[i]) {
|
|
220
|
+
return false
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return true
|
|
224
|
+
} else if (isObject(obj)) {
|
|
225
|
+
// compare the object keys
|
|
226
|
+
const objKeys = Object.keys(obj)
|
|
227
|
+
const otherKeys = Object.keys(other)
|
|
228
|
+
|
|
229
|
+
if (objKeys.length !== otherKeys.length) return false
|
|
230
|
+
|
|
231
|
+
for (const key of objKeys) {
|
|
232
|
+
if (!Object.prototype.hasOwnProperty.call(other, key) || obj[key] !== other[key]) {
|
|
233
|
+
return false
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return true
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return false
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Performs a recursive comparison of two objects or arrays to check if they have the same keys, length and values. Unlike {@link shallowEqual} this method compares nested values also.
|
|
244
|
+
* @param obj
|
|
245
|
+
* @param other
|
|
246
|
+
* @returns `true` if the objects are considered equal.
|
|
247
|
+
*/
|
|
248
|
+
export function deepEqual(obj, other, checker = eq) {
|
|
249
|
+
if (checker(obj, other)) return true
|
|
250
|
+
|
|
251
|
+
if (!obj || !other) return false
|
|
252
|
+
|
|
253
|
+
if (Array.isArray(obj)) {
|
|
254
|
+
// compare the arrays
|
|
255
|
+
if (obj.length !== other.length) return false
|
|
256
|
+
|
|
257
|
+
for (let i = 0; i < obj.length; i++) {
|
|
258
|
+
if (!deepEqual(obj[i], other[i], checker)) {
|
|
259
|
+
return false
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return true
|
|
263
|
+
} else if (isObject(obj)) {
|
|
264
|
+
// compare the object keys
|
|
265
|
+
const objKeys = Object.keys(obj)
|
|
266
|
+
const otherKeys = Object.keys(other)
|
|
267
|
+
|
|
268
|
+
if (objKeys.length !== otherKeys.length) return false
|
|
269
|
+
|
|
270
|
+
for (const key of objKeys) {
|
|
271
|
+
if (
|
|
272
|
+
!Object.prototype.hasOwnProperty.call(other, key) ||
|
|
273
|
+
!deepEqual(obj[key], other[key], checker)
|
|
274
|
+
) {
|
|
275
|
+
return false
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return true
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return false
|
|
282
|
+
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "atomic-fns",
|
|
3
3
|
"description": "Like Lodash, but for ESNext and with types. Stop shipping code built for browsers from 2015.",
|
|
4
|
-
"version": "0.3.
|
|
5
|
-
"main": "
|
|
4
|
+
"version": "0.3.3",
|
|
5
|
+
"main": "core/index.js",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"types": "
|
|
8
|
-
"module": "
|
|
7
|
+
"types": "core/index.d.ts",
|
|
8
|
+
"module": "core/index.js",
|
|
9
9
|
"sideEffects": false,
|
|
10
10
|
"devDependencies": {
|
|
11
11
|
"@types/jasmine": "^4.3.0",
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
"test": "NODE_OPTIONS=--loader=ts-node/esm jasmine --config=jasmine.json",
|
|
33
33
|
"tsc": "tsc -p ./tsconfig.xo.json --declaration",
|
|
34
34
|
"tsc:w": "tsc -p ./tsconfig.xo.json --declaration --watch",
|
|
35
|
-
"lint": "eslint
|
|
36
|
-
"lint:fix": "eslint
|
|
35
|
+
"lint": "eslint core/**/*.ts",
|
|
36
|
+
"lint:fix": "eslint core/**/*.ts --fix",
|
|
37
37
|
"build": "npm run lint:fix && npm run tsc && npm run docs",
|
|
38
38
|
"docs": "typedoc"
|
|
39
39
|
},
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import assert from 'assert'
|
|
2
|
+
import { BTree } from '../core/collections/BTree.js'
|
|
3
|
+
import { times } from '../core/itertools/index.js'
|
|
4
|
+
|
|
5
|
+
describe('BTree', () => {
|
|
6
|
+
it('get', () => {
|
|
7
|
+
let tree = new BTree()
|
|
8
|
+
|
|
9
|
+
for (let i = 100; i > 0; i--) {
|
|
10
|
+
tree.set(i, i * 2)
|
|
11
|
+
assert(tree.get(i) == i * 2)
|
|
12
|
+
}
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('add', () => {
|
|
16
|
+
let tree = new BTree()
|
|
17
|
+
|
|
18
|
+
for (let i = 100; i > 0; i--) {
|
|
19
|
+
tree.add(i)
|
|
20
|
+
assert(tree.contains(i))
|
|
21
|
+
assert(tree.get(i) === null)
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('set', () => {
|
|
26
|
+
let tree = new BTree()
|
|
27
|
+
|
|
28
|
+
for (let i = 100; i > 0; i--) {
|
|
29
|
+
tree.set(i, i)
|
|
30
|
+
assert(tree.contains(i))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
assert(tree.size === 100)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('remove', () => {
|
|
37
|
+
let tree = new BTree()
|
|
38
|
+
for (let i = 100; i > 0; i--) {
|
|
39
|
+
tree.set(i, i)
|
|
40
|
+
}
|
|
41
|
+
assert(tree.contains(100))
|
|
42
|
+
tree.remove(100)
|
|
43
|
+
assert(tree.contains(100) === false)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('clear', () => {
|
|
47
|
+
let tree = new BTree()
|
|
48
|
+
for (let i = 100; i > 0; i--) {
|
|
49
|
+
tree.set(i, i)
|
|
50
|
+
}
|
|
51
|
+
tree.clear()
|
|
52
|
+
assert(tree.size === 0)
|
|
53
|
+
assert(tree.contains(100) === false)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('iterates in order', () => {
|
|
57
|
+
let tree = new BTree()
|
|
58
|
+
let entries: any[] = []
|
|
59
|
+
for (let i = 9; i >= 0; i--) {
|
|
60
|
+
tree.set(i, i * 2)
|
|
61
|
+
entries.push([i, i * 2])
|
|
62
|
+
}
|
|
63
|
+
entries.sort((x, y) => x[0] - y[0])
|
|
64
|
+
assert.deepEqual([...tree], entries)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('minKey', () => {
|
|
68
|
+
let tree = new BTree()
|
|
69
|
+
let keys: number[] = []
|
|
70
|
+
for (let i = 10; i >= 0; i--) {
|
|
71
|
+
keys.push(i)
|
|
72
|
+
tree.set(i, i * 2)
|
|
73
|
+
}
|
|
74
|
+
keys.sort(tree._compare)
|
|
75
|
+
assert(tree.minKey() === keys[0])
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('maxKey', () => {
|
|
79
|
+
let tree = new BTree()
|
|
80
|
+
let keys: number[] = []
|
|
81
|
+
for (let i = 10; i >= 0; i--) {
|
|
82
|
+
keys.push(i)
|
|
83
|
+
tree.set(i, i * 2)
|
|
84
|
+
}
|
|
85
|
+
keys.sort(tree._compare)
|
|
86
|
+
assert(tree.maxKey() === keys[keys.length - 1])
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('clone', () => {
|
|
90
|
+
let tree = new BTree()
|
|
91
|
+
let entries: any[] = []
|
|
92
|
+
for (let i = 100; i > 0; i--) {
|
|
93
|
+
tree.set(i, i)
|
|
94
|
+
entries.push([i, i])
|
|
95
|
+
}
|
|
96
|
+
let t2 = tree.clone()
|
|
97
|
+
assert.deepEqual([...t2.reversed()], entries)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
it('freeze', () => {
|
|
101
|
+
let tree = new BTree()
|
|
102
|
+
for (let i = 10; i > 0; i--) {
|
|
103
|
+
tree.set(i, i)
|
|
104
|
+
}
|
|
105
|
+
tree.freeze()
|
|
106
|
+
assert.throws(tree.clear)
|
|
107
|
+
assert.throws(() => tree.add(11))
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('reversed', () => {
|
|
111
|
+
let tree = new BTree()
|
|
112
|
+
let entries: any[] = []
|
|
113
|
+
for (let i = 100; i > 0; i--) {
|
|
114
|
+
tree.set(i, i)
|
|
115
|
+
entries.push([i, i])
|
|
116
|
+
}
|
|
117
|
+
assert.deepEqual([...tree.reversed()], entries)
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
it('keys', () => {
|
|
121
|
+
let tree = new BTree()
|
|
122
|
+
for (let i = 9; i >= 0; i--) {
|
|
123
|
+
tree.set(i, i)
|
|
124
|
+
}
|
|
125
|
+
assert.deepEqual([...tree.keys()], [...times(10, (i) => i)])
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
it('values', () => {
|
|
129
|
+
let tree = new BTree()
|
|
130
|
+
for (let i = 9; i >= 0; i--) {
|
|
131
|
+
tree.set(i, i * 2)
|
|
132
|
+
}
|
|
133
|
+
assert.deepEqual([...tree.values()], [...times(10, (i) => i * 2)])
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
it('entries', () => {
|
|
137
|
+
let tree = new BTree()
|
|
138
|
+
for (let i = 9; i >= 0; i--) {
|
|
139
|
+
tree.set(i, i * 2)
|
|
140
|
+
}
|
|
141
|
+
assert.deepEqual([...tree.entries()], [...times(10, (i) => [i, i * 2])])
|
|
142
|
+
})
|
|
143
|
+
})
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import assert from 'assert'
|
|
2
|
+
import { Cache, LRUCache } from '../core/collections/LRUCache.js'
|
|
3
|
+
|
|
4
|
+
describe('Cache', () => {
|
|
5
|
+
it('empty', () => {
|
|
6
|
+
let cache = new Cache()
|
|
7
|
+
assert(cache.size === 0)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('contains', () => {
|
|
11
|
+
let cache = new Cache(2)
|
|
12
|
+
assert(cache.capacity === 2)
|
|
13
|
+
assert(!cache.contains(1))
|
|
14
|
+
cache.set(1, 'foo')
|
|
15
|
+
cache.set(2, 'bar')
|
|
16
|
+
assert(cache.contains(1))
|
|
17
|
+
assert(cache.contains(2))
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('evicts LRU keys', () => {
|
|
21
|
+
let cache = new Cache(2)
|
|
22
|
+
assert(cache.capacity === 2)
|
|
23
|
+
cache.set(1, 'foo')
|
|
24
|
+
cache.set(2, 'bar')
|
|
25
|
+
assert(cache.get(1) === 'foo')
|
|
26
|
+
cache.set(3, 'other')
|
|
27
|
+
assert(cache.get(2) === undefined)
|
|
28
|
+
assert.deepEqual([...cache.keys()], [3, 1])
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('keys', () => {
|
|
32
|
+
let cache = new Cache(5)
|
|
33
|
+
for (const value of [1, 2, 3, 4, 5]) {
|
|
34
|
+
cache.set(value, value)
|
|
35
|
+
}
|
|
36
|
+
assert.deepEqual([...cache.keys()], [5, 4, 3, 2, 1])
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('values', () => {
|
|
40
|
+
let cache = new Cache(5)
|
|
41
|
+
for (const value of [1, 2, 3, 4, 5]) {
|
|
42
|
+
cache.set(value, value)
|
|
43
|
+
}
|
|
44
|
+
assert.deepEqual([...cache.keys()], [5, 4, 3, 2, 1])
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('entries', () => {
|
|
48
|
+
let cache = new Cache(3)
|
|
49
|
+
for (const value of [1, 2, 3]) {
|
|
50
|
+
cache.set(value, 0)
|
|
51
|
+
}
|
|
52
|
+
assert.deepEqual(
|
|
53
|
+
[...cache.entries()],
|
|
54
|
+
[
|
|
55
|
+
[3, 0],
|
|
56
|
+
[2, 0],
|
|
57
|
+
[1, 0]
|
|
58
|
+
]
|
|
59
|
+
)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('iterator', () => {
|
|
63
|
+
let cache = new Cache(3)
|
|
64
|
+
for (const value of [1, 2, 3]) {
|
|
65
|
+
cache.set(value, 0)
|
|
66
|
+
}
|
|
67
|
+
let values = [...cache]
|
|
68
|
+
assert.deepEqual(values, [...cache.entries()])
|
|
69
|
+
})
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
describe('LRUCache', () => {
|
|
73
|
+
it('empty', () => {
|
|
74
|
+
let cache = new LRUCache()
|
|
75
|
+
assert(cache.size === 0)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('contains', () => {
|
|
79
|
+
let cache = new LRUCache(2)
|
|
80
|
+
assert(cache.capacity === 2)
|
|
81
|
+
assert(!cache.contains(1))
|
|
82
|
+
cache.set(1, 'foo')
|
|
83
|
+
cache.set(2, 'bar')
|
|
84
|
+
assert(cache.contains(1))
|
|
85
|
+
assert(cache.contains(2))
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it('evicts LRU keys', () => {
|
|
89
|
+
let cache = new LRUCache(2)
|
|
90
|
+
assert(cache.capacity === 2)
|
|
91
|
+
cache.set(1, 'foo')
|
|
92
|
+
cache.set(2, 'bar')
|
|
93
|
+
assert(cache.get(1) === 'foo')
|
|
94
|
+
cache.set(3, 'other')
|
|
95
|
+
assert(cache.get(2) === undefined)
|
|
96
|
+
assert.deepEqual([...cache.keys()], [3, 1])
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('keys', () => {
|
|
100
|
+
let cache = new LRUCache(5)
|
|
101
|
+
for (const value of [1, 2, 3, 4, 5]) {
|
|
102
|
+
cache.set(value, value)
|
|
103
|
+
}
|
|
104
|
+
assert.deepEqual([...cache.keys()], [5, 4, 3, 2, 1])
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('values', () => {
|
|
108
|
+
let cache = new LRUCache(5)
|
|
109
|
+
for (const value of [1, 2, 3, 4, 5]) {
|
|
110
|
+
cache.set(value, value)
|
|
111
|
+
}
|
|
112
|
+
assert.deepEqual([...cache.keys()], [5, 4, 3, 2, 1])
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
it('entries', () => {
|
|
116
|
+
let cache = new LRUCache(3)
|
|
117
|
+
for (const value of [1, 2, 3]) {
|
|
118
|
+
cache.set(value, 0)
|
|
119
|
+
}
|
|
120
|
+
assert.deepEqual(
|
|
121
|
+
[...cache.entries()],
|
|
122
|
+
[
|
|
123
|
+
[3, 0],
|
|
124
|
+
[2, 0],
|
|
125
|
+
[1, 0]
|
|
126
|
+
]
|
|
127
|
+
)
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
it('iterator', () => {
|
|
131
|
+
let cache = new LRUCache(3)
|
|
132
|
+
for (const value of [1, 2, 3]) {
|
|
133
|
+
cache.set(value, 0)
|
|
134
|
+
}
|
|
135
|
+
let values = [...cache]
|
|
136
|
+
assert.deepEqual(values, [...cache.entries()])
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it('delete', () => {
|
|
140
|
+
let cache = new LRUCache()
|
|
141
|
+
for (const value of [1, 2, 3]) {
|
|
142
|
+
cache.set(value, 0)
|
|
143
|
+
}
|
|
144
|
+
assert(cache.contains(1))
|
|
145
|
+
assert(cache.delete(1) === true)
|
|
146
|
+
assert(!cache.contains(1))
|
|
147
|
+
// try deleting again
|
|
148
|
+
assert(cache.delete(1) === false)
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
it('remove', () => {
|
|
152
|
+
let cache = new LRUCache()
|
|
153
|
+
for (const value of [1, 2, 3]) {
|
|
154
|
+
cache.set(value, 0)
|
|
155
|
+
}
|
|
156
|
+
assert(cache.contains(1))
|
|
157
|
+
assert(cache.remove(1) === 0)
|
|
158
|
+
assert(!cache.contains(1))
|
|
159
|
+
assert(cache.delete(2))
|
|
160
|
+
})
|
|
161
|
+
})
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import assert from 'assert'
|
|
2
|
+
import { Heap } from '../core/collections/Heap.js'
|
|
3
|
+
import { sorted } from '../core/itertools/index.js'
|
|
4
|
+
|
|
5
|
+
describe('Heap', () => {
|
|
6
|
+
it('empty', () => {
|
|
7
|
+
let h = new Heap()
|
|
8
|
+
assert(h.empty())
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('heapifies array', () => {
|
|
12
|
+
let arr = [7, 9, 3, 5, 8, 1, 2, 6, 4]
|
|
13
|
+
let h = new Heap(arr)
|
|
14
|
+
assert.deepEqual([...h.values()], arr)
|
|
15
|
+
for (const x of sorted(arr)) {
|
|
16
|
+
assert(h.contains(x))
|
|
17
|
+
assert(h.pop() === x)
|
|
18
|
+
}
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('max heapifies', () => {
|
|
22
|
+
let arr = [7, 9, 3, 5, 8, 1, 2, 6, 4]
|
|
23
|
+
let h = new Heap(arr, (x, y) => {
|
|
24
|
+
if (x > y) return -1
|
|
25
|
+
if (x < y) return 1
|
|
26
|
+
return 0
|
|
27
|
+
})
|
|
28
|
+
assert.deepEqual([...h.values()], arr)
|
|
29
|
+
for (const x of sorted(arr, true)) {
|
|
30
|
+
assert(h.contains(x))
|
|
31
|
+
assert(h.pop() === x)
|
|
32
|
+
}
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('add', () => {
|
|
36
|
+
let h = new Heap()
|
|
37
|
+
let arr = [7, 9, 3, 5, 8, 1, 2, 6, 4]
|
|
38
|
+
for (const value of arr) {
|
|
39
|
+
h.add(value)
|
|
40
|
+
assert(h.contains(value))
|
|
41
|
+
}
|
|
42
|
+
assert(h.size === arr.length)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('pop', () => {
|
|
46
|
+
let h = new Heap()
|
|
47
|
+
let arr = [7, 9, 3, 5, 8, 1, 2, 6, 4]
|
|
48
|
+
for (const value of arr) {
|
|
49
|
+
h.add(value)
|
|
50
|
+
}
|
|
51
|
+
assert(h.pop() === 1)
|
|
52
|
+
assert(h.size === arr.length - 1)
|
|
53
|
+
assert(h.pop() === 2)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it('remove', () => {
|
|
57
|
+
let h = new Heap()
|
|
58
|
+
let arr = [7, 9, 3, 5, 8, 1, 2, 6, 4]
|
|
59
|
+
for (const value of arr) {
|
|
60
|
+
h.add(value)
|
|
61
|
+
}
|
|
62
|
+
assert(h.pop() === 1)
|
|
63
|
+
assert(h.size === arr.length - 1)
|
|
64
|
+
assert(h.pop() === 2)
|
|
65
|
+
})
|
|
66
|
+
})
|