atomic-fns 0.3.5 → 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.
- package/README.md +2 -2
- package/{core → dist}/collections/BTree.d.ts +6 -6
- package/{core → dist}/collections/BTree.js +17 -17
- package/{core → dist}/collections/Heap.d.ts +3 -3
- package/{core → dist}/collections/Heap.js +0 -0
- package/dist/collections/LRUCache.d.ts +80 -0
- package/dist/collections/LRUCache.js +126 -0
- package/{core → dist}/collections/SortedMap.d.ts +2 -2
- package/{core → dist}/collections/SortedMap.js +0 -0
- package/{core → dist}/collections/SortedSet.d.ts +3 -3
- package/{core → dist}/collections/SortedSet.js +1 -1
- package/{core → dist}/collections/SortedTree.d.ts +3 -3
- package/{core → dist}/collections/SortedTree.js +1 -1
- package/{core → dist}/collections/SplayTree.d.ts +5 -5
- package/{core → dist}/collections/SplayTree.js +0 -0
- package/{core → dist}/collections/abc.d.ts +0 -0
- package/{core → dist}/collections/abc.js +0 -0
- package/{core → dist}/collections/deque.d.ts +6 -6
- package/{core → dist}/collections/deque.js +0 -0
- package/{core → dist}/collections/frozenset.d.ts +0 -0
- package/{core → dist}/collections/frozenset.js +3 -6
- package/{core → dist}/collections/index.d.ts +4 -4
- package/{core → dist}/collections/index.js +0 -0
- package/{core → dist}/functools/index.d.ts +27 -13
- package/{core → dist}/functools/index.js +34 -12
- package/{core → dist}/globals/index.d.ts +7 -10
- package/{core → dist}/globals/index.js +7 -14
- package/dist/i18n/date/index.d.ts +204 -0
- package/dist/i18n/date/index.js +530 -0
- package/dist/i18n/date/utils.d.ts +65 -0
- package/dist/i18n/date/utils.js +312 -0
- package/dist/i18n/duration.d.ts +112 -0
- package/dist/i18n/duration.js +392 -0
- package/dist/i18n/format.d.ts +26 -0
- package/dist/i18n/format.js +495 -0
- package/{core → dist}/i18n/index.d.ts +1 -1
- package/{core → dist}/i18n/index.js +1 -1
- package/{core → dist}/index.d.ts +0 -0
- package/{core → dist}/index.js +0 -0
- package/{core → dist}/itertools/index.d.ts +27 -27
- package/{core → dist}/itertools/index.js +24 -24
- package/{core → dist}/math/index.d.ts +3 -3
- package/{core → dist}/math/index.js +0 -0
- package/{core → dist}/operators/index.d.ts +2 -2
- package/{core → dist}/operators/index.js +2 -2
- package/{core → dist}/random/index.d.ts +1 -1
- package/{core → dist}/random/index.js +3 -3
- package/package.json +28 -42
- package/.editorconfig +0 -12
- package/.esdoc.json +0 -27
- package/.eslintignore +0 -1
- package/.eslintrc.json +0 -35
- package/.firebaserc +0 -5
- package/.prettierrc +0 -19
- package/core/collections/BTree.ts +0 -1340
- package/core/collections/Heap.ts +0 -189
- package/core/collections/LRUCache.d.ts +0 -152
- package/core/collections/LRUCache.js +0 -409
- package/core/collections/LRUCache.ts +0 -486
- package/core/collections/SortedMap.ts +0 -146
- package/core/collections/SortedSet.ts +0 -110
- package/core/collections/SortedTree.ts +0 -160
- package/core/collections/SplayTree.ts +0 -354
- package/core/collections/abc.ts +0 -176
- package/core/collections/deque.ts +0 -338
- package/core/collections/frozenset.ts +0 -37
- package/core/collections/index.ts +0 -1024
- package/core/collections/test.html +0 -10
- package/core/collections/typedArrays.d.ts +0 -31
- package/core/collections/typedArrays.js +0 -143
- package/core/collections/typedArrays.ts +0 -163
- package/core/functools/index.ts +0 -260
- package/core/globals/index.ts +0 -481
- package/core/i18n/date/format.d.ts +0 -16
- package/core/i18n/date/format.js +0 -307
- package/core/i18n/date/format.ts +0 -343
- package/core/i18n/date/index.d.ts +0 -224
- package/core/i18n/date/index.js +0 -387
- package/core/i18n/date/index.ts +0 -471
- package/core/i18n/date/test.html +0 -67
- package/core/i18n/index.ts +0 -8
- package/core/index.ts +0 -8
- package/core/itertools/index.ts +0 -494
- package/core/math/index.ts +0 -152
- package/core/operators/index.ts +0 -310
- package/core/random/index.ts +0 -66
- package/firebase.json +0 -10
- package/jasmine.json +0 -14
- package/tests/BTree.spec.ts +0 -143
- package/tests/Cache.spec.ts +0 -161
- package/tests/Heap.spec.ts +0 -66
- package/tests/SortedMap.spec.ts +0 -144
- package/tests/SortedSet.spec.ts +0 -84
- package/tests/SortedTree.spec.ts +0 -164
- package/tests/SplayTree.spec.ts +0 -121
- package/tests/collections.spec.ts +0 -288
- package/tests/deque.spec.ts +0 -107
- package/tests/globals.spec.ts +0 -270
- package/tests/itertools.spec.ts +0 -266
- package/tests/math.spec.ts +0 -25
- package/tests/operators.spec.ts +0 -40
- package/tests/random.spec.ts +0 -46
- package/tsconfig.json +0 -126
- package/tsconfig.node.json +0 -18
- package/tsconfig.xo.json +0 -9
|
@@ -1,486 +0,0 @@
|
|
|
1
|
-
import { Iteratee, NotImplementedError, ValueError } from '../globals/index.js'
|
|
2
|
-
import { IterableIterator } from '../itertools/index.js'
|
|
3
|
-
import { Mapping } from './abc.js'
|
|
4
|
-
import { getPointerArray } from './typedArrays.js'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Base append-only Cache collection that supports adding and accessing keys in O(1).
|
|
8
|
-
* The collection has a fixed given size (default 1024) and when full, it evicts keys using LRU approach. This operation is also O(1).
|
|
9
|
-
* @template K, V
|
|
10
|
-
*/
|
|
11
|
-
export class Cache<K, V = any> extends Mapping<K, V> {
|
|
12
|
-
capacity: number
|
|
13
|
-
head: number = 0
|
|
14
|
-
tail: number = 0
|
|
15
|
-
count: number = 0
|
|
16
|
-
protected items = new Map<K, number>()
|
|
17
|
-
protected forward: Uint8Array | Uint16Array | Uint32Array
|
|
18
|
-
protected backward: Uint8Array | Uint16Array | Uint32Array
|
|
19
|
-
protected K: K[]
|
|
20
|
-
protected V: V[]
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Create a new cache object of fixed size.
|
|
24
|
-
* @param {number} [capacity=1024] The maximum number of keys to keep in cache.
|
|
25
|
-
* @param {Function} [Keys=Array] The constructor to use for the Keys array. When using numeric values, a TypedArray constructor can be used.
|
|
26
|
-
* @param {Function} [Values=Array] The constructor to use for the Values array. When using numeric values, a TypedArray constructor can be used.
|
|
27
|
-
*/
|
|
28
|
-
constructor(capacity = 1024, Keys = Array, Values = Array) {
|
|
29
|
-
super()
|
|
30
|
-
this.capacity = capacity
|
|
31
|
-
|
|
32
|
-
if (typeof this.capacity !== 'number' || this.capacity <= 0)
|
|
33
|
-
throw new ValueError('LRUCache capacity should be a positive integer.')
|
|
34
|
-
else if (!isFinite(this.capacity) || Math.floor(this.capacity) !== this.capacity)
|
|
35
|
-
throw new ValueError('LRUCache capacity should be a finite positive integer.')
|
|
36
|
-
|
|
37
|
-
const PointerArray = getPointerArray(capacity)
|
|
38
|
-
this.forward = new PointerArray(capacity)
|
|
39
|
-
this.backward = new PointerArray(capacity)
|
|
40
|
-
this.K = new Keys(capacity)
|
|
41
|
-
this.V = new Values(capacity)
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* This is just an alias of {@link Cache.set}.
|
|
46
|
-
* @param {K} key The key to add (note value will be `undefined`).
|
|
47
|
-
*/
|
|
48
|
-
add(key: K) {
|
|
49
|
-
return this.set(key, undefined)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Method used to check whether the key exists in the cache.
|
|
54
|
-
*
|
|
55
|
-
* @param {any} key - Key.
|
|
56
|
-
* @return {boolean}
|
|
57
|
-
*/
|
|
58
|
-
contains(key) {
|
|
59
|
-
return this.items.has(key)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Method used to clear the structure.
|
|
64
|
-
*/
|
|
65
|
-
clear() {
|
|
66
|
-
this.count = 0
|
|
67
|
-
this.head = 0
|
|
68
|
-
this.tail = 0
|
|
69
|
-
this.items = new Map()
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Returns the number of keys in the cache.
|
|
74
|
-
*/
|
|
75
|
-
get size() {
|
|
76
|
-
return this.count
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Method used to splay a value on top.
|
|
81
|
-
*
|
|
82
|
-
* @param {number} pointer - Pointer of the value to splay on top.
|
|
83
|
-
* @return {Cache}
|
|
84
|
-
*/
|
|
85
|
-
protected splayOnTop(pointer) {
|
|
86
|
-
const oldHead = this.head
|
|
87
|
-
|
|
88
|
-
if (this.head === pointer) return this
|
|
89
|
-
|
|
90
|
-
const previous = this.backward[pointer]
|
|
91
|
-
const next = this.forward[pointer]
|
|
92
|
-
|
|
93
|
-
if (this.tail === pointer) {
|
|
94
|
-
this.tail = previous
|
|
95
|
-
} else {
|
|
96
|
-
this.backward[next] = previous
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
this.forward[previous] = next
|
|
100
|
-
|
|
101
|
-
this.backward[oldHead] = pointer
|
|
102
|
-
this.head = pointer
|
|
103
|
-
this.forward[pointer] = oldHead
|
|
104
|
-
|
|
105
|
-
return this
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Method used to get the value attached to the given key. Will move the
|
|
110
|
-
* related key to the front of the underlying linked list.
|
|
111
|
-
*
|
|
112
|
-
* @param {any} key - Key.
|
|
113
|
-
* @return {any}
|
|
114
|
-
*/
|
|
115
|
-
get(key) {
|
|
116
|
-
const pointer = this.items.get(key)
|
|
117
|
-
|
|
118
|
-
if (typeof pointer === 'undefined') return
|
|
119
|
-
|
|
120
|
-
this.splayOnTop(pointer)
|
|
121
|
-
|
|
122
|
-
return this.V[pointer]
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Method used to set the value for the given key in the cache.
|
|
127
|
-
*
|
|
128
|
-
* @param {any} key - Key.
|
|
129
|
-
* @param {any} value - Value.
|
|
130
|
-
* @return {undefined}
|
|
131
|
-
*/
|
|
132
|
-
set(key, value) {
|
|
133
|
-
let pointer = this.items.get(key)
|
|
134
|
-
|
|
135
|
-
// The key already exists, we just need to update the value and splay on top
|
|
136
|
-
if (typeof pointer !== 'undefined') {
|
|
137
|
-
this.splayOnTop(pointer)
|
|
138
|
-
this.V[pointer] = value
|
|
139
|
-
|
|
140
|
-
return
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// The cache is not yet full
|
|
144
|
-
if (this.count < this.capacity) {
|
|
145
|
-
pointer = this.count++
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// Cache is full, we need to drop the last value
|
|
149
|
-
else {
|
|
150
|
-
pointer = this.tail
|
|
151
|
-
this.tail = this.backward[pointer]
|
|
152
|
-
this.items.delete(this.K[pointer])
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// Storing key & value
|
|
156
|
-
this.items.set(key, pointer)
|
|
157
|
-
this.K[pointer] = key
|
|
158
|
-
this.V[pointer] = value
|
|
159
|
-
|
|
160
|
-
// Moving the item at the front of the list
|
|
161
|
-
this.forward[pointer] = this.head
|
|
162
|
-
this.backward[this.head] = pointer
|
|
163
|
-
this.head = pointer
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Method used to get the value attached to the given key. Does not modify
|
|
168
|
-
* the ordering of the underlying linked list.
|
|
169
|
-
*
|
|
170
|
-
* @param {any} key - Key.
|
|
171
|
-
* @return {any}
|
|
172
|
-
*/
|
|
173
|
-
peek(key) {
|
|
174
|
-
const pointer = this.items.get(key)
|
|
175
|
-
|
|
176
|
-
if (typeof pointer === 'undefined') return
|
|
177
|
-
|
|
178
|
-
return this.V[pointer]
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Method used to iterate over the cache's entries using a callback.
|
|
183
|
-
*
|
|
184
|
-
* @param {Iteratee<V>} iteratee - Function to call for each item.
|
|
185
|
-
*/
|
|
186
|
-
forEach(iteratee: Iteratee<V>) {
|
|
187
|
-
let i = 0
|
|
188
|
-
const l = this.count
|
|
189
|
-
|
|
190
|
-
let pointer = this.head
|
|
191
|
-
const keys = this.K
|
|
192
|
-
const values = this.V
|
|
193
|
-
const forward = this.forward
|
|
194
|
-
|
|
195
|
-
while (i < l) {
|
|
196
|
-
iteratee(values[pointer], keys[pointer], this)
|
|
197
|
-
pointer = forward[pointer]
|
|
198
|
-
i++
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Method used to create an iterator over the cache's keys from most
|
|
204
|
-
* recently used to least recently used.
|
|
205
|
-
*
|
|
206
|
-
* @return {Iterator<K>}
|
|
207
|
-
*/
|
|
208
|
-
keys() {
|
|
209
|
-
let i = 0
|
|
210
|
-
const l = this.count
|
|
211
|
-
|
|
212
|
-
let pointer = this.head
|
|
213
|
-
const keys = this.K
|
|
214
|
-
const forward = this.forward
|
|
215
|
-
|
|
216
|
-
return IterableIterator(() => {
|
|
217
|
-
if (i >= l) return { done: true }
|
|
218
|
-
|
|
219
|
-
const key = keys[pointer]
|
|
220
|
-
|
|
221
|
-
i++
|
|
222
|
-
|
|
223
|
-
if (i < l) pointer = forward[pointer]
|
|
224
|
-
|
|
225
|
-
return {
|
|
226
|
-
done: false,
|
|
227
|
-
value: key
|
|
228
|
-
}
|
|
229
|
-
})
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* Method used to create an iterator over the cache's values from most
|
|
234
|
-
* recently used to least recently used.
|
|
235
|
-
*
|
|
236
|
-
* @return {Iterator<V>}
|
|
237
|
-
*/
|
|
238
|
-
values() {
|
|
239
|
-
let i = 0
|
|
240
|
-
const l = this.count
|
|
241
|
-
|
|
242
|
-
let pointer = this.head
|
|
243
|
-
const values = this.V
|
|
244
|
-
const forward = this.forward
|
|
245
|
-
|
|
246
|
-
return IterableIterator(() => {
|
|
247
|
-
if (i >= l) return { done: true }
|
|
248
|
-
|
|
249
|
-
const value = values[pointer]
|
|
250
|
-
|
|
251
|
-
i++
|
|
252
|
-
|
|
253
|
-
if (i < l) pointer = forward[pointer]
|
|
254
|
-
|
|
255
|
-
return {
|
|
256
|
-
done: false,
|
|
257
|
-
value
|
|
258
|
-
}
|
|
259
|
-
})
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* Method used to create an iterator over the cache's entries from most
|
|
264
|
-
* recently used to least recently used.
|
|
265
|
-
*
|
|
266
|
-
* @return {Iterator<[K, V]>}
|
|
267
|
-
*/
|
|
268
|
-
entries() {
|
|
269
|
-
let i = 0
|
|
270
|
-
const l = this.count
|
|
271
|
-
|
|
272
|
-
let pointer = this.head
|
|
273
|
-
const keys = this.K
|
|
274
|
-
const values = this.V
|
|
275
|
-
const forward = this.forward
|
|
276
|
-
|
|
277
|
-
return IterableIterator<[K, V]>(() => {
|
|
278
|
-
if (i >= l) return { done: true }
|
|
279
|
-
|
|
280
|
-
const key = keys[pointer]
|
|
281
|
-
const value = values[pointer]
|
|
282
|
-
|
|
283
|
-
i++
|
|
284
|
-
|
|
285
|
-
if (i < l) pointer = forward[pointer]
|
|
286
|
-
|
|
287
|
-
return { value: [key, value] }
|
|
288
|
-
})
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
/**
|
|
292
|
-
* NotImplemented -- throws an exception.
|
|
293
|
-
* @param key
|
|
294
|
-
* @returns
|
|
295
|
-
*/
|
|
296
|
-
remove(key: any) {
|
|
297
|
-
throw new NotImplementedError(`Base Cache class does not implement remove().`)
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
[Symbol.iterator]() {
|
|
301
|
-
return this.entries()
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
/**
|
|
305
|
-
* Convenience known methods.
|
|
306
|
-
*/
|
|
307
|
-
inspect() {
|
|
308
|
-
const proxy = new Map()
|
|
309
|
-
|
|
310
|
-
const iterator = this.entries()
|
|
311
|
-
let step
|
|
312
|
-
|
|
313
|
-
while (((step = iterator.next()), !step.done)) proxy.set(step.value[0], step.value[1])
|
|
314
|
-
|
|
315
|
-
// Trick so that node displays the name of the constructor
|
|
316
|
-
Object.defineProperty(proxy, 'constructor', {
|
|
317
|
-
value: Cache,
|
|
318
|
-
enumerable: false
|
|
319
|
-
})
|
|
320
|
-
|
|
321
|
-
return proxy
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
325
|
-
return this.inspect()
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* Implements a Least Recently Used fixed-capacity cache which supports updating, removing, and accessing keys in O(1).
|
|
331
|
-
*/
|
|
332
|
-
export class LRUCache<K, V> extends Cache<K, V> {
|
|
333
|
-
deletedSize = 0
|
|
334
|
-
deleted: Uint8Array | Uint16Array | Uint32Array
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* Create a new LRU cache object of fixed size.
|
|
338
|
-
* @param {number} [capacity=1024] The maximum number of keys to keep in cache.
|
|
339
|
-
* @param {Function} [Keys=Array] The constructor to use for the Keys array. When using numeric values, a TypedArray constructor can be used.
|
|
340
|
-
* @param {Function} [Values=Array] The constructor to use for the Values array. When using numeric values, a TypedArray constructor can be used.
|
|
341
|
-
*/
|
|
342
|
-
constructor(capacity = 1024, Keys = Array, Values = Array) {
|
|
343
|
-
super(capacity, Keys, Values)
|
|
344
|
-
const PointerArray = getPointerArray(capacity)
|
|
345
|
-
this.deleted = new PointerArray(capacity)
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
/** Remove all elements in the cache. */
|
|
349
|
-
clear() {
|
|
350
|
-
super.clear()
|
|
351
|
-
this.deletedSize = 0
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
/**
|
|
355
|
-
* Method used to set the value for the given key in the cache.
|
|
356
|
-
*
|
|
357
|
-
* @param {any} key - Key.
|
|
358
|
-
* @param {any} value - Value.
|
|
359
|
-
* @return {undefined}
|
|
360
|
-
*/
|
|
361
|
-
set(key, value) {
|
|
362
|
-
let pointer = this.items.get(key)
|
|
363
|
-
|
|
364
|
-
// The key already exists, we just need to update the value and splay on top
|
|
365
|
-
if (typeof pointer !== 'undefined') {
|
|
366
|
-
this.splayOnTop(pointer)
|
|
367
|
-
this.V[pointer] = value
|
|
368
|
-
|
|
369
|
-
return
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
// The cache is not yet full
|
|
373
|
-
if (this.count < this.capacity) {
|
|
374
|
-
if (this.deletedSize > 0) {
|
|
375
|
-
// If there is a "hole" in the pointer list, reuse it
|
|
376
|
-
pointer = this.deleted[--this.deletedSize]
|
|
377
|
-
} else {
|
|
378
|
-
// otherwise append to the pointer list
|
|
379
|
-
pointer = this.count
|
|
380
|
-
}
|
|
381
|
-
this.count++
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
// Cache is full, we need to drop the last value
|
|
385
|
-
else {
|
|
386
|
-
pointer = this.tail
|
|
387
|
-
this.tail = this.backward[pointer]
|
|
388
|
-
this.items.delete(this.K[pointer])
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
// Storing key & value
|
|
392
|
-
this.items.set(key, pointer)
|
|
393
|
-
this.K[pointer] = key
|
|
394
|
-
this.V[pointer] = value
|
|
395
|
-
|
|
396
|
-
// Moving the item at the front of the list
|
|
397
|
-
this.forward[pointer] = this.head
|
|
398
|
-
this.backward[this.head] = pointer
|
|
399
|
-
this.head = pointer
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
/**
|
|
403
|
-
* Method used to delete the entry for the given key in the cache.
|
|
404
|
-
*
|
|
405
|
-
* @param {any} key - Key.
|
|
406
|
-
* @return {boolean} - true if the item was present
|
|
407
|
-
*/
|
|
408
|
-
delete(key) {
|
|
409
|
-
const pointer = this.items.get(key)
|
|
410
|
-
|
|
411
|
-
if (typeof pointer === 'undefined') {
|
|
412
|
-
return false
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
this.items.delete(key)
|
|
416
|
-
|
|
417
|
-
if (this.count === 1) {
|
|
418
|
-
this.count = 0
|
|
419
|
-
this.head = 0
|
|
420
|
-
this.tail = 0
|
|
421
|
-
this.deletedSize = 0
|
|
422
|
-
return true
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
const previous = this.backward[pointer]
|
|
426
|
-
const next = this.forward[pointer]
|
|
427
|
-
|
|
428
|
-
if (this.head === pointer) {
|
|
429
|
-
this.head = next
|
|
430
|
-
}
|
|
431
|
-
if (this.tail === pointer) {
|
|
432
|
-
this.tail = previous
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
this.forward[previous] = next
|
|
436
|
-
this.backward[next] = previous
|
|
437
|
-
|
|
438
|
-
this.count--
|
|
439
|
-
this.deleted[this.deletedSize++] = pointer
|
|
440
|
-
|
|
441
|
-
return true
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
/**
|
|
445
|
-
* Method used to remove and return the value for the given key in the cache.
|
|
446
|
-
*
|
|
447
|
-
* @param {any} key - Key.
|
|
448
|
-
* @return {any} The value, if present; the missing indicator if absent
|
|
449
|
-
*/
|
|
450
|
-
remove(key) {
|
|
451
|
-
const pointer = this.items.get(key)
|
|
452
|
-
|
|
453
|
-
if (typeof pointer === 'undefined') {
|
|
454
|
-
return
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
const dead = this.V[pointer]
|
|
458
|
-
this.items.delete(key)
|
|
459
|
-
|
|
460
|
-
if (this.count === 1) {
|
|
461
|
-
this.count = 0
|
|
462
|
-
this.head = 0
|
|
463
|
-
this.tail = 0
|
|
464
|
-
this.deletedSize = 0
|
|
465
|
-
return dead
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
const previous = this.backward[pointer]
|
|
469
|
-
const next = this.forward[pointer]
|
|
470
|
-
|
|
471
|
-
if (this.head === pointer) {
|
|
472
|
-
this.head = next
|
|
473
|
-
}
|
|
474
|
-
if (this.tail === pointer) {
|
|
475
|
-
this.tail = previous
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
this.forward[previous] = next
|
|
479
|
-
this.backward[next] = previous
|
|
480
|
-
|
|
481
|
-
this.count--
|
|
482
|
-
this.deleted[this.deletedSize++] = pointer
|
|
483
|
-
|
|
484
|
-
return dead
|
|
485
|
-
}
|
|
486
|
-
}
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import { Mapping } from './abc.js'
|
|
2
|
-
import { SplayTree } from './SplayTree.js'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* A `SortedMap` 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.
|
|
6
|
-
* @see {@link Operators.compare}
|
|
7
|
-
* @see {@link Operators.lt}
|
|
8
|
-
* @template K, V
|
|
9
|
-
*/
|
|
10
|
-
export class SortedMap<K, V> extends Mapping<K, V> {
|
|
11
|
-
private readonly store: SplayTree<K, V>
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Creates a new `SortedMap` object.
|
|
15
|
-
* @param {Iterable<[K, V]>} [iterable] Initialize the map with entries.
|
|
16
|
-
* @param {Comparer} [compareFn] The `compare` function to sort keys.
|
|
17
|
-
*/
|
|
18
|
-
constructor(iterable?: Iterable<[K, V]>, compareFn?) {
|
|
19
|
-
super()
|
|
20
|
-
this.store = new SplayTree(compareFn)
|
|
21
|
-
if (iterable) {
|
|
22
|
-
for (const pair of iterable) {
|
|
23
|
-
this.store.set(pair[0], pair[1] ?? (null as V))
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Adds a new `key` with a `null` value, if the `key` doesn't exist.
|
|
30
|
-
* @param {K} key
|
|
31
|
-
* @returns {this} Returns the Map object.
|
|
32
|
-
*/
|
|
33
|
-
add(key: K) {
|
|
34
|
-
this.store.set(key, null as V)
|
|
35
|
-
return this
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Returns the value associated with the `key` or `undefined`.
|
|
40
|
-
* @param {K} key
|
|
41
|
-
* @returns {?V} The key value or `undefined`.
|
|
42
|
-
*/
|
|
43
|
-
get(key: K) {
|
|
44
|
-
return this.store.get(key)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Sets the value for the given `key`.
|
|
49
|
-
* @param {K} key
|
|
50
|
-
* @param {V} value
|
|
51
|
-
* @returns {this} Returns the Map object.
|
|
52
|
-
*/
|
|
53
|
-
set(key: K, value: V) {
|
|
54
|
-
return this.store.set(key, value)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Returns the smallest key in the tree.
|
|
59
|
-
* @returns {?K}
|
|
60
|
-
*/
|
|
61
|
-
min() {
|
|
62
|
-
return this.store.min()
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Returns the largest key in the tree.
|
|
67
|
-
* @returns {?K}
|
|
68
|
-
*/
|
|
69
|
-
max() {
|
|
70
|
-
return this.store.max()
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Returns `true` if the `key` existed in the map and was removed.
|
|
75
|
-
* @param {K} key
|
|
76
|
-
* @returns {boolean} `true` if key was removed, otherwise `false`.
|
|
77
|
-
*/
|
|
78
|
-
delete(item: K) {
|
|
79
|
-
return this.store.remove(item) !== undefined
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Removes all key/value pairs in the tree.
|
|
84
|
-
*/
|
|
85
|
-
clear() {
|
|
86
|
-
return this.store.clear()
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Returns the total key/value pairs in the tree.
|
|
91
|
-
* @returns {number}
|
|
92
|
-
*/
|
|
93
|
-
get size() {
|
|
94
|
-
return this.store.size
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Check if there are no key/value pairs in the tree.
|
|
99
|
-
* @returns {boolean} `true` if is empty.
|
|
100
|
-
*/
|
|
101
|
-
empty() {
|
|
102
|
-
return this.store.empty()
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Returns `true` if the `key` exists in the map.
|
|
107
|
-
* @param {K} key
|
|
108
|
-
* @returns {boolean} `true` if key is found, otherwise `false`.
|
|
109
|
-
*/
|
|
110
|
-
contains(key: K) {
|
|
111
|
-
return this.store.contains(key)
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Returns `true` if the `key` exists in the map.
|
|
116
|
-
* @param {K} key
|
|
117
|
-
* @returns {boolean} `true` if key is found, otherwise `false`.
|
|
118
|
-
*/
|
|
119
|
-
has(key: K) {
|
|
120
|
-
return this.store.contains(key)
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Returns a new iterator of all **ordered** keys in the map.
|
|
125
|
-
* @returns {Iterable<K>}
|
|
126
|
-
*/
|
|
127
|
-
keys() {
|
|
128
|
-
return this.store.keys()
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Returns a new iterator of all values **ordered** by their keys.
|
|
133
|
-
* @returns {Iterable<V>}
|
|
134
|
-
*/
|
|
135
|
-
values() {
|
|
136
|
-
return this.store.values()
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Returns a new iterator of all key value pairs **ordered** by their keys.
|
|
141
|
-
* @returns {Iterable<[K, V]>}
|
|
142
|
-
*/
|
|
143
|
-
entries() {
|
|
144
|
-
return this.store.entries()
|
|
145
|
-
}
|
|
146
|
-
}
|