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.
- package/README.md +3 -3
- 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 +1 -1
- package/{core → dist}/collections/abc.js +0 -0
- package/dist/collections/deque.d.ts +117 -0
- package/{core → dist}/collections/deque.js +61 -53
- 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 +28 -13
- package/{core → dist}/functools/index.js +34 -12
- package/{core → dist}/globals/index.d.ts +8 -11
- package/{core → dist}/globals/index.js +8 -15
- 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.d.ts +0 -110
- package/core/collections/deque.ts +0 -329
- 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 -258
- 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 -223
- 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
package/core/collections/Heap.ts
DELETED
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
import type { Comparer } from '../operators/index.js'
|
|
2
|
-
import { compare } from '../operators/index.js'
|
|
3
|
-
import { Collection } from './abc.js'
|
|
4
|
-
|
|
5
|
-
export class Heap<T> extends Collection {
|
|
6
|
-
private readonly heap: T[]
|
|
7
|
-
private readonly compare: Comparer
|
|
8
|
-
private count: number = 0
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Initializes a new Heap instance.
|
|
12
|
-
*
|
|
13
|
-
* **Note:** When constructing the heap from an array, it will operate directly on this array. For other iterables, it will create a new array.
|
|
14
|
-
*
|
|
15
|
-
* @param {Iterable<T>} [container=[]] The initial values.
|
|
16
|
-
* @param {Comparer} [cmp=compare] Compare function. Defaults to smaller values first.
|
|
17
|
-
*/
|
|
18
|
-
constructor(container: Iterable<T> = [], cmp: Comparer = compare) {
|
|
19
|
-
super()
|
|
20
|
-
this.compare = cmp
|
|
21
|
-
if (Array.isArray(container)) {
|
|
22
|
-
// use the provided array to avoid copying.
|
|
23
|
-
this.heap = container
|
|
24
|
-
} else {
|
|
25
|
-
this.heap = Array.from(container)
|
|
26
|
-
}
|
|
27
|
-
this.count = this.heap.length
|
|
28
|
-
const halfLength = this.count >> 1
|
|
29
|
-
// Heapify the items
|
|
30
|
-
for (let parent = (this.count - 1) >> 1; parent >= 0; --parent) {
|
|
31
|
-
heapifyDown(this.heap, parent, halfLength, this.compare)
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
at(n: number) {
|
|
36
|
-
return this.heap.at(n)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
get size() {
|
|
40
|
-
return this.count
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
clear() {
|
|
44
|
-
this.count = 0
|
|
45
|
-
this.heap.length = 0
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Push element into a container in order.
|
|
50
|
-
* @param item The element to push.
|
|
51
|
-
*/
|
|
52
|
-
add(item: T) {
|
|
53
|
-
this.heap.push(item)
|
|
54
|
-
heapifyUp(this.heap, this.count, this.compare)
|
|
55
|
-
this.count += 1
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Removes the top element.
|
|
60
|
-
*/
|
|
61
|
-
pop() {
|
|
62
|
-
if (!this.count) return
|
|
63
|
-
const value = this.heap[0]
|
|
64
|
-
const last = this.heap.pop() as T
|
|
65
|
-
this.count -= 1
|
|
66
|
-
if (this.count) {
|
|
67
|
-
this.heap[0] = last
|
|
68
|
-
heapifyDown(this.heap, 0, this.count >> 1, this.compare)
|
|
69
|
-
}
|
|
70
|
-
return value
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Accesses the top element.
|
|
75
|
-
*/
|
|
76
|
-
top() {
|
|
77
|
-
if (!this.count) return
|
|
78
|
-
return this.heap[0] as T | undefined
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Check if element is in heap.
|
|
83
|
-
* @param item The item want to find.
|
|
84
|
-
* @return `true` if element exists.
|
|
85
|
-
*/
|
|
86
|
-
contains(item: T) {
|
|
87
|
-
if (!this.count) return false
|
|
88
|
-
return this.heap.includes(item)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Remove specified item from heap.
|
|
93
|
-
* @param item The item want to remove.
|
|
94
|
-
* @return `true` if the item was removed.
|
|
95
|
-
*/
|
|
96
|
-
remove(item: T) {
|
|
97
|
-
const index = this.heap.indexOf(item)
|
|
98
|
-
if (index < 0) return false
|
|
99
|
-
if (index === 0) {
|
|
100
|
-
this.pop()
|
|
101
|
-
} else if (index === this.count - 1) {
|
|
102
|
-
this.heap.pop()
|
|
103
|
-
this.count -= 1
|
|
104
|
-
} else {
|
|
105
|
-
this.heap.splice(index, 1, this.heap.pop() as T)
|
|
106
|
-
this.count -= 1
|
|
107
|
-
heapifyUp(this.heap, index, this.compare)
|
|
108
|
-
heapifyDown(this.heap, index, this.count >> 1, this.compare)
|
|
109
|
-
}
|
|
110
|
-
return true
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Returns an iterable with all the values in the heap.
|
|
115
|
-
* @returns {Iterable<T>} The values in the heap.
|
|
116
|
-
*/
|
|
117
|
-
values() {
|
|
118
|
-
return this.heap[Symbol.iterator]()
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function heapifyDown(heap, i: number, halfLength: number, compareFn = compare) {
|
|
123
|
-
const item = heap[i]
|
|
124
|
-
while (i < halfLength) {
|
|
125
|
-
let left = (i << 1) | 1
|
|
126
|
-
const right = left + 1
|
|
127
|
-
let minItem = heap[left]
|
|
128
|
-
if (right < heap.length && compareFn(minItem, heap[right]) > 0) {
|
|
129
|
-
left = right
|
|
130
|
-
minItem = heap[right]
|
|
131
|
-
}
|
|
132
|
-
if (compareFn(minItem, item) >= 0) break
|
|
133
|
-
heap[i] = minItem
|
|
134
|
-
i = left
|
|
135
|
-
}
|
|
136
|
-
heap[i] = item
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function heapifyUp(heap, i: number, compareFn) {
|
|
140
|
-
const item = heap[i]
|
|
141
|
-
while (i > 0) {
|
|
142
|
-
const parent = (i - 1) >> 1
|
|
143
|
-
const parentItem = heap[parent]
|
|
144
|
-
if (compareFn(parentItem, item) <= 0) break
|
|
145
|
-
heap[i] = parentItem
|
|
146
|
-
i = parent
|
|
147
|
-
}
|
|
148
|
-
heap[i] = item
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Transform any array into a heap, in-place, in linear time.
|
|
153
|
-
* @param {Array} heap
|
|
154
|
-
* @param {Comparer} [compareFn=compare] Custom compare function
|
|
155
|
-
*/
|
|
156
|
-
export function heapify(heap, compareFn = compare) {
|
|
157
|
-
const size = heap.length
|
|
158
|
-
const halfLength = size >> 1
|
|
159
|
-
for (let parent = (size - 1) >> 1; parent >= 0; --parent) {
|
|
160
|
-
heapifyDown(heap, parent, halfLength, compareFn)
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Push the value `item` onto the `heap`, maintaining the heap invariant.
|
|
166
|
-
* @param {Array} heap
|
|
167
|
-
* @param {*} item
|
|
168
|
-
* @param {Comparer} [compareFn=compare] Custom compare function
|
|
169
|
-
*/
|
|
170
|
-
export function heappush<T>(heap: T[], item: T, compareFn = compare) {
|
|
171
|
-
heap.push(item)
|
|
172
|
-
heapifyUp(heap, heap.length - 1, compareFn)
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Pop and return the smallest item from the `heap`, maintaining the heap invariant.
|
|
177
|
-
* @param {Array} heap
|
|
178
|
-
* @param {Comparer} [compareFn=compare] Custom compare function
|
|
179
|
-
*/
|
|
180
|
-
export function heappop<T>(heap: T[], compareFn = compare) {
|
|
181
|
-
const last = heap.pop() as T
|
|
182
|
-
if (heap.length) {
|
|
183
|
-
const item = heap[0]
|
|
184
|
-
heap[0] = last
|
|
185
|
-
heapifyDown(heap, 0, heap.length >> 1, compareFn)
|
|
186
|
-
return item
|
|
187
|
-
}
|
|
188
|
-
return last
|
|
189
|
-
}
|
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import { Iteratee } from '../globals/index.js';
|
|
2
|
-
import { Mapping } from './abc.js';
|
|
3
|
-
/**
|
|
4
|
-
* Base append-only Cache collection that supports adding and accessing keys in O(1).
|
|
5
|
-
* The collection has a fixed given size (default 1024) and when full, it evicts keys using LRU approach. This operation is also O(1).
|
|
6
|
-
* @template K, V
|
|
7
|
-
*/
|
|
8
|
-
export declare class Cache<K, V = any> extends Mapping<K, V> {
|
|
9
|
-
capacity: number;
|
|
10
|
-
head: number;
|
|
11
|
-
tail: number;
|
|
12
|
-
count: number;
|
|
13
|
-
protected items: Map<K, number>;
|
|
14
|
-
protected forward: Uint8Array | Uint16Array | Uint32Array;
|
|
15
|
-
protected backward: Uint8Array | Uint16Array | Uint32Array;
|
|
16
|
-
protected K: K[];
|
|
17
|
-
protected V: V[];
|
|
18
|
-
/**
|
|
19
|
-
* Create a new cache object of fixed size.
|
|
20
|
-
* @param {number} [capacity=1024] The maximum number of keys to keep in cache.
|
|
21
|
-
* @param {Function} [Keys=Array] The constructor to use for the Keys array. When using numeric values, a TypedArray constructor can be used.
|
|
22
|
-
* @param {Function} [Values=Array] The constructor to use for the Values array. When using numeric values, a TypedArray constructor can be used.
|
|
23
|
-
*/
|
|
24
|
-
constructor(capacity?: number, Keys?: ArrayConstructor, Values?: ArrayConstructor);
|
|
25
|
-
/**
|
|
26
|
-
* This is just an alias of {@link Cache.set}.
|
|
27
|
-
* @param {K} key The key to add (note value will be `undefined`).
|
|
28
|
-
*/
|
|
29
|
-
add(key: K): void;
|
|
30
|
-
/**
|
|
31
|
-
* Method used to check whether the key exists in the cache.
|
|
32
|
-
*
|
|
33
|
-
* @param {any} key - Key.
|
|
34
|
-
* @return {boolean}
|
|
35
|
-
*/
|
|
36
|
-
contains(key: any): boolean;
|
|
37
|
-
/**
|
|
38
|
-
* Method used to clear the structure.
|
|
39
|
-
*/
|
|
40
|
-
clear(): void;
|
|
41
|
-
/**
|
|
42
|
-
* Returns the number of keys in the cache.
|
|
43
|
-
*/
|
|
44
|
-
get size(): number;
|
|
45
|
-
/**
|
|
46
|
-
* Method used to splay a value on top.
|
|
47
|
-
*
|
|
48
|
-
* @param {number} pointer - Pointer of the value to splay on top.
|
|
49
|
-
* @return {Cache}
|
|
50
|
-
*/
|
|
51
|
-
protected splayOnTop(pointer: any): this;
|
|
52
|
-
/**
|
|
53
|
-
* Method used to get the value attached to the given key. Will move the
|
|
54
|
-
* related key to the front of the underlying linked list.
|
|
55
|
-
*
|
|
56
|
-
* @param {any} key - Key.
|
|
57
|
-
* @return {any}
|
|
58
|
-
*/
|
|
59
|
-
get(key: any): V | undefined;
|
|
60
|
-
/**
|
|
61
|
-
* Method used to set the value for the given key in the cache.
|
|
62
|
-
*
|
|
63
|
-
* @param {any} key - Key.
|
|
64
|
-
* @param {any} value - Value.
|
|
65
|
-
* @return {undefined}
|
|
66
|
-
*/
|
|
67
|
-
set(key: any, value: any): void;
|
|
68
|
-
/**
|
|
69
|
-
* Method used to get the value attached to the given key. Does not modify
|
|
70
|
-
* the ordering of the underlying linked list.
|
|
71
|
-
*
|
|
72
|
-
* @param {any} key - Key.
|
|
73
|
-
* @return {any}
|
|
74
|
-
*/
|
|
75
|
-
peek(key: any): V | undefined;
|
|
76
|
-
/**
|
|
77
|
-
* Method used to iterate over the cache's entries using a callback.
|
|
78
|
-
*
|
|
79
|
-
* @param {Iteratee<V>} iteratee - Function to call for each item.
|
|
80
|
-
*/
|
|
81
|
-
forEach(iteratee: Iteratee<V>): void;
|
|
82
|
-
/**
|
|
83
|
-
* Method used to create an iterator over the cache's keys from most
|
|
84
|
-
* recently used to least recently used.
|
|
85
|
-
*
|
|
86
|
-
* @return {Iterator<K>}
|
|
87
|
-
*/
|
|
88
|
-
keys(): Iterator<any, any, undefined> & Iterable<any>;
|
|
89
|
-
/**
|
|
90
|
-
* Method used to create an iterator over the cache's values from most
|
|
91
|
-
* recently used to least recently used.
|
|
92
|
-
*
|
|
93
|
-
* @return {Iterator<V>}
|
|
94
|
-
*/
|
|
95
|
-
values(): Iterator<any, any, undefined> & Iterable<any>;
|
|
96
|
-
/**
|
|
97
|
-
* Method used to create an iterator over the cache's entries from most
|
|
98
|
-
* recently used to least recently used.
|
|
99
|
-
*
|
|
100
|
-
* @return {Iterator<[K, V]>}
|
|
101
|
-
*/
|
|
102
|
-
entries(): Iterator<[K, V], any, undefined> & Iterable<[K, V]>;
|
|
103
|
-
/**
|
|
104
|
-
* NotImplemented -- throws an exception.
|
|
105
|
-
* @param key
|
|
106
|
-
* @returns
|
|
107
|
-
*/
|
|
108
|
-
remove(key: any): void;
|
|
109
|
-
[Symbol.iterator](): Iterator<[K, V], any, undefined> & Iterable<[K, V]>;
|
|
110
|
-
/**
|
|
111
|
-
* Convenience known methods.
|
|
112
|
-
*/
|
|
113
|
-
inspect(): Map<any, any>;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Implements a Least Recently Used fixed-capacity cache which supports updating, removing, and accessing keys in O(1).
|
|
117
|
-
*/
|
|
118
|
-
export declare class LRUCache<K, V> extends Cache<K, V> {
|
|
119
|
-
deletedSize: number;
|
|
120
|
-
deleted: Uint8Array | Uint16Array | Uint32Array;
|
|
121
|
-
/**
|
|
122
|
-
* Create a new LRU cache object of fixed size.
|
|
123
|
-
* @param {number} [capacity=1024] The maximum number of keys to keep in cache.
|
|
124
|
-
* @param {Function} [Keys=Array] The constructor to use for the Keys array. When using numeric values, a TypedArray constructor can be used.
|
|
125
|
-
* @param {Function} [Values=Array] The constructor to use for the Values array. When using numeric values, a TypedArray constructor can be used.
|
|
126
|
-
*/
|
|
127
|
-
constructor(capacity?: number, Keys?: ArrayConstructor, Values?: ArrayConstructor);
|
|
128
|
-
/** Remove all elements in the cache. */
|
|
129
|
-
clear(): void;
|
|
130
|
-
/**
|
|
131
|
-
* Method used to set the value for the given key in the cache.
|
|
132
|
-
*
|
|
133
|
-
* @param {any} key - Key.
|
|
134
|
-
* @param {any} value - Value.
|
|
135
|
-
* @return {undefined}
|
|
136
|
-
*/
|
|
137
|
-
set(key: any, value: any): void;
|
|
138
|
-
/**
|
|
139
|
-
* Method used to delete the entry for the given key in the cache.
|
|
140
|
-
*
|
|
141
|
-
* @param {any} key - Key.
|
|
142
|
-
* @return {boolean} - true if the item was present
|
|
143
|
-
*/
|
|
144
|
-
delete(key: any): boolean;
|
|
145
|
-
/**
|
|
146
|
-
* Method used to remove and return the value for the given key in the cache.
|
|
147
|
-
*
|
|
148
|
-
* @param {any} key - Key.
|
|
149
|
-
* @return {any} The value, if present; the missing indicator if absent
|
|
150
|
-
*/
|
|
151
|
-
remove(key: any): V | undefined;
|
|
152
|
-
}
|