atomic-fns 0.3.1 → 0.3.3-beta
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 +62 -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 +152 -0
- package/core/collections/LRUCache.js +409 -0
- package/core/collections/LRUCache.ts +486 -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 +69 -0
- package/core/collections/SortedSet.js +99 -0
- package/core/collections/SortedSet.ts +110 -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 +43 -162
- package/{src → core}/collections/index.js +59 -165
- package/{src → core}/collections/index.ts +64 -226
- 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/{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
|
@@ -21,231 +21,24 @@ import {
|
|
|
21
21
|
isSymbol,
|
|
22
22
|
Iteratee,
|
|
23
23
|
keys,
|
|
24
|
-
NotImplementedError,
|
|
25
24
|
set
|
|
26
25
|
} from '../globals/index.js'
|
|
27
26
|
import { enumerate } from '../itertools/index.js'
|
|
28
|
-
import {
|
|
27
|
+
import { compare, eq, id } from '../operators/index.js'
|
|
28
|
+
|
|
29
|
+
export * from './abc.js'
|
|
30
|
+
export * from './BTree.js'
|
|
31
|
+
export * from './deque.js'
|
|
32
|
+
export * from './frozenset.js'
|
|
33
|
+
export * from './Heap.js'
|
|
34
|
+
export * from './LRUCache.js'
|
|
35
|
+
export * from './SortedSet.js'
|
|
36
|
+
export * from './SortedMap.js'
|
|
37
|
+
export * from './SortedTree.js'
|
|
38
|
+
export * from './SplayTree.js'
|
|
29
39
|
|
|
30
40
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* @interface Container
|
|
35
|
-
*/
|
|
36
|
-
export interface Container {
|
|
37
|
-
contains: (x) => boolean
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* These are the "rich comparison" methods, inspired by Python operators.
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* @interface Comparable
|
|
45
|
-
*/
|
|
46
|
-
export interface Comparable {
|
|
47
|
-
/** x < y: calls this.lt(other) reflection. */
|
|
48
|
-
lt?: (other) => boolean
|
|
49
|
-
/** x ≤ y: calls this.le(other) reflection. */
|
|
50
|
-
le?: (other) => boolean
|
|
51
|
-
/** x == y: calls this.eq(other) reflection. */
|
|
52
|
-
eq: (other) => boolean
|
|
53
|
-
/** x != y: calls this.ne(other) reflection. */
|
|
54
|
-
ne: (other) => boolean
|
|
55
|
-
/** x > y: calls this.gt(other) reflection. */
|
|
56
|
-
gt?: (other) => boolean
|
|
57
|
-
/** x ≥ y: calls this.ge(other) reflection. */
|
|
58
|
-
ge?: (other) => boolean
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* A `Collection` is an iterable {@link Container} type.
|
|
63
|
-
* This is an abstract base class for user-defined collection types.
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
* @abstract
|
|
67
|
-
* @class Collection
|
|
68
|
-
* @implements {Container}
|
|
69
|
-
*/
|
|
70
|
-
export abstract class Collection implements Container {
|
|
71
|
-
/**
|
|
72
|
-
* Returns an iterator for the container items
|
|
73
|
-
* @return {Iterator}
|
|
74
|
-
*/
|
|
75
|
-
[Symbol.iterator]() {
|
|
76
|
-
return this
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Implements the iterator protocol and returns the next item in the iterable.
|
|
81
|
-
*/
|
|
82
|
-
next() {}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Adds a new item to the container.
|
|
86
|
-
* @param item - The item to add to the collection.
|
|
87
|
-
*/
|
|
88
|
-
add(item) {}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Checks if item is present in the container.
|
|
92
|
-
* @param item - The item to search for in the collection.
|
|
93
|
-
* @returns `true` when the element is found, else `false`
|
|
94
|
-
*/
|
|
95
|
-
contains(item) {
|
|
96
|
-
return false
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* Returns the total number of elements in the container.
|
|
101
|
-
*/
|
|
102
|
-
size(): number {
|
|
103
|
-
throw new NotImplementedError()
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Remove the first item from the container where `item == x`
|
|
108
|
-
* @returns `true` when the element is found and removed, else `false`
|
|
109
|
-
*/
|
|
110
|
-
remove(x) {
|
|
111
|
-
return false
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Retrieve and remove the item at index `i`.
|
|
116
|
-
* @returns the item or undefined if not found.
|
|
117
|
-
*/
|
|
118
|
-
pop(i) {}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Remove all items from the container
|
|
122
|
-
*/
|
|
123
|
-
clear() {}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Implements an iterable that allows backward iteration.
|
|
128
|
-
* @interface Reversible
|
|
129
|
-
*/
|
|
130
|
-
export interface Reversible<T = unknown> {
|
|
131
|
-
reversed: () => Iterator<T>
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* A sequence is an iterable {@link Collection} type with efficient index-based access.
|
|
136
|
-
* @abstract
|
|
137
|
-
* @class Sequence
|
|
138
|
-
* @extends {Collection}
|
|
139
|
-
* @template T
|
|
140
|
-
*/
|
|
141
|
-
export abstract class Sequence<T> extends Collection implements Reversible<T> {
|
|
142
|
-
/**
|
|
143
|
-
* Return the item at the given key or index
|
|
144
|
-
* @param {*} key
|
|
145
|
-
* @memberof Sequence
|
|
146
|
-
*/
|
|
147
|
-
get(key): T | undefined {
|
|
148
|
-
throw new NotImplementedError()
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Set a new value at the given key or index
|
|
153
|
-
*
|
|
154
|
-
* @param {*} key
|
|
155
|
-
* @param {T} val
|
|
156
|
-
* @memberof Sequence
|
|
157
|
-
*/
|
|
158
|
-
set(key, val: T) {
|
|
159
|
-
throw new NotImplementedError()
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Deletes the given key or index, and its value.
|
|
164
|
-
* Raises ValueError if not key is found.
|
|
165
|
-
*
|
|
166
|
-
* @param {*} key
|
|
167
|
-
* @memberof Sequence
|
|
168
|
-
*/
|
|
169
|
-
delete(key) {
|
|
170
|
-
throw new NotImplementedError()
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Adds a new item to the end of the sequence.
|
|
175
|
-
*
|
|
176
|
-
* @param {T} x
|
|
177
|
-
* @memberof Sequence
|
|
178
|
-
*/
|
|
179
|
-
append(x: T) {}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Append all the items to the sequence.
|
|
183
|
-
*
|
|
184
|
-
* @param {Iterable<T>} iter
|
|
185
|
-
* @memberof Sequence
|
|
186
|
-
*/
|
|
187
|
-
extend(iter: Iterable<T>) {}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Return the index of x in the sequence, or undefined if not found.
|
|
191
|
-
*
|
|
192
|
-
* @param {T} item
|
|
193
|
-
* @return {(number | undefined)}
|
|
194
|
-
* @memberof Sequence
|
|
195
|
-
*/
|
|
196
|
-
indexOf(item: T): number | undefined {
|
|
197
|
-
throw new NotImplementedError()
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
size() {
|
|
201
|
-
return 0
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
reversed(): Iterator<T> {
|
|
205
|
-
throw new NotImplementedError()
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Returns a new immutable Set object with elements from `iterable`. Its contents cannot be altered after it's created.
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
* @class FrozenSet
|
|
214
|
-
* @extends {Set<T>}
|
|
215
|
-
* @template T
|
|
216
|
-
*/
|
|
217
|
-
export class FrozenSet<T = any> extends Set<T> {
|
|
218
|
-
constructor(iterable: Iterable<T>) {
|
|
219
|
-
super(iterable)
|
|
220
|
-
return this.freeze()
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/** @private */
|
|
224
|
-
add() {
|
|
225
|
-
throw TypeError('FrozenSet cannot be modified.')
|
|
226
|
-
return this // eslint-disable-line
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/** @private */
|
|
230
|
-
delete() {
|
|
231
|
-
throw TypeError('FrozenSet cannot be modified.')
|
|
232
|
-
return false // eslint-disable-line
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/** @private */
|
|
236
|
-
clear() {
|
|
237
|
-
throw TypeError('FrozenSet cannot be modified.')
|
|
238
|
-
return this // eslint-disable-line
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/** @private */
|
|
242
|
-
freeze() {
|
|
243
|
-
return Object.freeze(this)
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* Creates a new array with all `falsey` and empty values removed.
|
|
41
|
+
* Creates a new array with all falsy and empty values removed.
|
|
249
42
|
* @param arr The array to compact
|
|
250
43
|
* @returns A new array with the filtered values.
|
|
251
44
|
* @example
|
|
@@ -387,6 +180,22 @@ export function findRight(arr, fn: Iteratee | PropertyKey | Object) {
|
|
|
387
180
|
}
|
|
388
181
|
}
|
|
389
182
|
|
|
183
|
+
/**
|
|
184
|
+
* Performs an efficient array insert operation in the given array. If the index or the array is invalid, it just returns the given array.
|
|
185
|
+
*
|
|
186
|
+
* **Note:** Uses the same behavior as `Array.splice`.
|
|
187
|
+
*
|
|
188
|
+
* @param {Array<*>} arr The given array to insert into
|
|
189
|
+
* @param {number} index The index of the array insert operation.
|
|
190
|
+
* @param {*} value The value to insert in the array at the given `index`.
|
|
191
|
+
* @returns {Array<*>} The given array.
|
|
192
|
+
*/
|
|
193
|
+
export function insert(arr: any[], index: number, value) {
|
|
194
|
+
if (!arr || arr.length <= index || index < 0) return arr
|
|
195
|
+
arr.splice(index, 0, value)
|
|
196
|
+
return arr
|
|
197
|
+
}
|
|
198
|
+
|
|
390
199
|
/**
|
|
391
200
|
* Creates a function that performs a partial deep comparison between a given object and `shape`, returning `true` if the given object has equivalent property values, else `false`.
|
|
392
201
|
* @example
|
|
@@ -942,7 +751,7 @@ uniq([2, 1, 2])
|
|
|
942
751
|
*/
|
|
943
752
|
export function sortedUniq(arr, fn: string | Iteratee = id) {
|
|
944
753
|
const values = uniq(arr, fn)
|
|
945
|
-
values.sort(
|
|
754
|
+
values.sort(compare)
|
|
946
755
|
return values
|
|
947
756
|
}
|
|
948
757
|
|
|
@@ -1140,7 +949,7 @@ export function* union(...args: Array<Iterable<any>>) {
|
|
|
1140
949
|
*
|
|
1141
950
|
* @param arr The collection to iterate over.
|
|
1142
951
|
* @param [func=id] The iteratee to transform keys.
|
|
1143
|
-
* @returns Returns the
|
|
952
|
+
* @returns Returns the aggregated object.
|
|
1144
953
|
*
|
|
1145
954
|
* @example
|
|
1146
955
|
```js
|
|
@@ -1164,6 +973,39 @@ export function groupBy(arr: Iterable<any> | Object, func: Iteratee | PropertyKe
|
|
|
1164
973
|
return results
|
|
1165
974
|
}
|
|
1166
975
|
|
|
976
|
+
/**
|
|
977
|
+
* Similar to {@link groupBy} but it returns a {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map Map} object with the results.
|
|
978
|
+
*
|
|
979
|
+
* @param arr The collection to iterate over.
|
|
980
|
+
* @param {Function | PropertyKey} [func=id] The iteratee to transform keys.
|
|
981
|
+
* @returns {Map} Returns the aggregated map object.
|
|
982
|
+
* @template K, V
|
|
983
|
+
*
|
|
984
|
+
* @example
|
|
985
|
+
```js
|
|
986
|
+
groupByMap([6.1, 4.2, 6.3], Math.floor)
|
|
987
|
+
// => Map { 4: [4.2], 6: [6.1, 6.3] }
|
|
988
|
+
|
|
989
|
+
// The `property` iteratee shorthand.
|
|
990
|
+
groupByMap(['one', 'two', 'three'], 'length')
|
|
991
|
+
// => Map { 3: ['one', 'two'], 5: ['three'] }
|
|
992
|
+
```
|
|
993
|
+
*/
|
|
994
|
+
export function groupByMap<K = any, V = any>(
|
|
995
|
+
arr: Iterable<any> | Object,
|
|
996
|
+
func: Iteratee | PropertyKey = id
|
|
997
|
+
): Map<K, V[]> {
|
|
998
|
+
const results = new Map<K, V[]>()
|
|
999
|
+
const useKey = typeof func === 'function' ? func : partial(get, func)
|
|
1000
|
+
forEach(arr, (value: V) => {
|
|
1001
|
+
const groupKey = useKey(value)
|
|
1002
|
+
const values = results.get(groupKey) || []
|
|
1003
|
+
values.push(value)
|
|
1004
|
+
results.set(groupKey, values)
|
|
1005
|
+
})
|
|
1006
|
+
return results
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1167
1009
|
/**
|
|
1168
1010
|
* Removes all elements from array that `func` returns truthy for and returns an array of the removed elements.
|
|
1169
1011
|
* @param arr The array to remove from.
|
|
@@ -1179,8 +1021,4 @@ export function remove<T>(arr: T[], func: any) {
|
|
|
1179
1021
|
}
|
|
1180
1022
|
|
|
1181
1023
|
// TODO: binary search utils
|
|
1182
|
-
// TODO: heap/heapify
|
|
1183
|
-
// TODO: deque
|
|
1184
1024
|
// TODO: Counter
|
|
1185
|
-
// TODO: Skip list
|
|
1186
|
-
// TODO: BST (BTree)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export declare function getPointerArray(size: any): Uint8ArrayConstructor | Uint16ArrayConstructor | Uint32ArrayConstructor;
|
|
2
|
+
export declare function getSignedPointerArray(size: any): Int8ArrayConstructor | Int16ArrayConstructor | Int32ArrayConstructor | Float64ArrayConstructor;
|
|
3
|
+
/**
|
|
4
|
+
* Function returning the minimal type able to represent the given number.
|
|
5
|
+
*
|
|
6
|
+
* @param {number} value - Value to test.
|
|
7
|
+
* @return {TypedArrayClass}
|
|
8
|
+
*/
|
|
9
|
+
export declare function getNumberType(value: any): Uint8ArrayConstructor | Uint16ArrayConstructor | Uint32ArrayConstructor | Int8ArrayConstructor | Int16ArrayConstructor | Int32ArrayConstructor | Float64ArrayConstructor;
|
|
10
|
+
export declare function getMinimalRepresentation(array: any, getter: any): null;
|
|
11
|
+
/**
|
|
12
|
+
* Function returning whether the given value is a typed array.
|
|
13
|
+
*
|
|
14
|
+
* @param {any} value - Value to test.
|
|
15
|
+
* @return {boolean}
|
|
16
|
+
*/
|
|
17
|
+
export declare function isTypedArray(value: any): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Function used to concat byte arrays.
|
|
20
|
+
*
|
|
21
|
+
* @param {...ByteArray}
|
|
22
|
+
* @return {ByteArray}
|
|
23
|
+
*/
|
|
24
|
+
export declare function concat(): any;
|
|
25
|
+
/**
|
|
26
|
+
* Function used to initialize a byte array of indices.
|
|
27
|
+
*
|
|
28
|
+
* @param {number} length - Length of target.
|
|
29
|
+
* @return {ByteArray}
|
|
30
|
+
*/
|
|
31
|
+
export declare function indices(length: any): any;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When using an unsigned integer array to store pointers, one might want to
|
|
3
|
+
* choose the optimal word size in regards to the actual numbers of pointers
|
|
4
|
+
* to store.
|
|
5
|
+
*
|
|
6
|
+
* This helpers does just that.
|
|
7
|
+
*
|
|
8
|
+
* @param {number} size - Expected size of the array to map.
|
|
9
|
+
* @return {TypedArray}
|
|
10
|
+
*/
|
|
11
|
+
const MAX_8BIT_INTEGER = Math.pow(2, 8) - 1;
|
|
12
|
+
const MAX_16BIT_INTEGER = Math.pow(2, 16) - 1;
|
|
13
|
+
const MAX_32BIT_INTEGER = Math.pow(2, 32) - 1;
|
|
14
|
+
const MAX_SIGNED_8BIT_INTEGER = Math.pow(2, 7) - 1;
|
|
15
|
+
const MAX_SIGNED_16BIT_INTEGER = Math.pow(2, 15) - 1;
|
|
16
|
+
const MAX_SIGNED_32BIT_INTEGER = Math.pow(2, 31) - 1;
|
|
17
|
+
export function getPointerArray(size) {
|
|
18
|
+
const maxIndex = size - 1;
|
|
19
|
+
if (maxIndex <= MAX_8BIT_INTEGER)
|
|
20
|
+
return Uint8Array;
|
|
21
|
+
if (maxIndex <= MAX_16BIT_INTEGER)
|
|
22
|
+
return Uint16Array;
|
|
23
|
+
// if (maxIndex <= MAX_32BIT_INTEGER) return Uint32Array
|
|
24
|
+
return Uint32Array;
|
|
25
|
+
}
|
|
26
|
+
export function getSignedPointerArray(size) {
|
|
27
|
+
const maxIndex = size - 1;
|
|
28
|
+
if (maxIndex <= MAX_SIGNED_8BIT_INTEGER)
|
|
29
|
+
return Int8Array;
|
|
30
|
+
if (maxIndex <= MAX_SIGNED_16BIT_INTEGER)
|
|
31
|
+
return Int16Array;
|
|
32
|
+
if (maxIndex <= MAX_SIGNED_32BIT_INTEGER)
|
|
33
|
+
return Int32Array;
|
|
34
|
+
return Float64Array;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Function returning the minimal type able to represent the given number.
|
|
38
|
+
*
|
|
39
|
+
* @param {number} value - Value to test.
|
|
40
|
+
* @return {TypedArrayClass}
|
|
41
|
+
*/
|
|
42
|
+
export function getNumberType(value) {
|
|
43
|
+
// <= 32 bits itnteger?
|
|
44
|
+
if (value === (value | 0)) {
|
|
45
|
+
// Negative
|
|
46
|
+
if (Math.sign(value) === -1) {
|
|
47
|
+
if (value <= 127 && value >= -128)
|
|
48
|
+
return Int8Array;
|
|
49
|
+
if (value <= 32767 && value >= -32768)
|
|
50
|
+
return Int16Array;
|
|
51
|
+
return Int32Array;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
if (value <= 255)
|
|
55
|
+
return Uint8Array;
|
|
56
|
+
if (value <= 65535)
|
|
57
|
+
return Uint16Array;
|
|
58
|
+
return Uint32Array;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
// 53 bits integer & floats
|
|
62
|
+
// NOTE: it's kinda hard to tell whether we could use 32bits or not...
|
|
63
|
+
return Float64Array;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Function returning the minimal type able to represent the given array
|
|
67
|
+
* of JavaScript numbers.
|
|
68
|
+
*
|
|
69
|
+
* @param {array} array - Array to represent.
|
|
70
|
+
* @param {function} getter - Optional getter.
|
|
71
|
+
* @return {TypedArrayClass}
|
|
72
|
+
*/
|
|
73
|
+
const TYPE_PRIORITY = {
|
|
74
|
+
Uint8Array: 1,
|
|
75
|
+
Int8Array: 2,
|
|
76
|
+
Uint16Array: 3,
|
|
77
|
+
Int16Array: 4,
|
|
78
|
+
Uint32Array: 5,
|
|
79
|
+
Int32Array: 6,
|
|
80
|
+
Float32Array: 7,
|
|
81
|
+
Float64Array: 8
|
|
82
|
+
};
|
|
83
|
+
export function getMinimalRepresentation(array, getter) {
|
|
84
|
+
let maxType = null;
|
|
85
|
+
let maxPriority = 0;
|
|
86
|
+
let p;
|
|
87
|
+
let t;
|
|
88
|
+
let v;
|
|
89
|
+
let i;
|
|
90
|
+
let l;
|
|
91
|
+
for (i = 0, l = array.length; i < l; i++) {
|
|
92
|
+
v = getter ? getter(array[i]) : array[i];
|
|
93
|
+
t = exports.getNumberType(v);
|
|
94
|
+
p = TYPE_PRIORITY[t.name];
|
|
95
|
+
if (p > maxPriority) {
|
|
96
|
+
maxPriority = p;
|
|
97
|
+
maxType = t;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return maxType;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Function returning whether the given value is a typed array.
|
|
104
|
+
*
|
|
105
|
+
* @param {any} value - Value to test.
|
|
106
|
+
* @return {boolean}
|
|
107
|
+
*/
|
|
108
|
+
export function isTypedArray(value) {
|
|
109
|
+
return ArrayBuffer.isView(value);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Function used to concat byte arrays.
|
|
113
|
+
*
|
|
114
|
+
* @param {...ByteArray}
|
|
115
|
+
* @return {ByteArray}
|
|
116
|
+
*/
|
|
117
|
+
export function concat() {
|
|
118
|
+
let length = 0;
|
|
119
|
+
let i;
|
|
120
|
+
let o;
|
|
121
|
+
let l;
|
|
122
|
+
for (i = 0, l = arguments.length; i < l; i++)
|
|
123
|
+
length += arguments[i].length;
|
|
124
|
+
const array = new arguments[0].constructor(length);
|
|
125
|
+
for (i = 0, o = 0; i < l; i++) {
|
|
126
|
+
array.set(arguments[i], o);
|
|
127
|
+
o += arguments[i].length;
|
|
128
|
+
}
|
|
129
|
+
return array;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Function used to initialize a byte array of indices.
|
|
133
|
+
*
|
|
134
|
+
* @param {number} length - Length of target.
|
|
135
|
+
* @return {ByteArray}
|
|
136
|
+
*/
|
|
137
|
+
export function indices(length) {
|
|
138
|
+
const PointerArray = exports.getPointerArray(length);
|
|
139
|
+
const array = new PointerArray(length);
|
|
140
|
+
for (let i = 0; i < length; i++)
|
|
141
|
+
array[i] = i;
|
|
142
|
+
return array;
|
|
143
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When using an unsigned integer array to store pointers, one might want to
|
|
3
|
+
* choose the optimal word size in regards to the actual numbers of pointers
|
|
4
|
+
* to store.
|
|
5
|
+
*
|
|
6
|
+
* This helpers does just that.
|
|
7
|
+
*
|
|
8
|
+
* @param {number} size - Expected size of the array to map.
|
|
9
|
+
* @return {TypedArray}
|
|
10
|
+
*/
|
|
11
|
+
const MAX_8BIT_INTEGER = Math.pow(2, 8) - 1
|
|
12
|
+
const MAX_16BIT_INTEGER = Math.pow(2, 16) - 1
|
|
13
|
+
const MAX_32BIT_INTEGER = Math.pow(2, 32) - 1
|
|
14
|
+
|
|
15
|
+
const MAX_SIGNED_8BIT_INTEGER = Math.pow(2, 7) - 1
|
|
16
|
+
const MAX_SIGNED_16BIT_INTEGER = Math.pow(2, 15) - 1
|
|
17
|
+
const MAX_SIGNED_32BIT_INTEGER = Math.pow(2, 31) - 1
|
|
18
|
+
|
|
19
|
+
export function getPointerArray(size) {
|
|
20
|
+
const maxIndex = size - 1
|
|
21
|
+
|
|
22
|
+
if (maxIndex <= MAX_8BIT_INTEGER) return Uint8Array
|
|
23
|
+
|
|
24
|
+
if (maxIndex <= MAX_16BIT_INTEGER) return Uint16Array
|
|
25
|
+
|
|
26
|
+
// if (maxIndex <= MAX_32BIT_INTEGER) return Uint32Array
|
|
27
|
+
|
|
28
|
+
return Uint32Array
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function getSignedPointerArray(size) {
|
|
32
|
+
const maxIndex = size - 1
|
|
33
|
+
|
|
34
|
+
if (maxIndex <= MAX_SIGNED_8BIT_INTEGER) return Int8Array
|
|
35
|
+
|
|
36
|
+
if (maxIndex <= MAX_SIGNED_16BIT_INTEGER) return Int16Array
|
|
37
|
+
|
|
38
|
+
if (maxIndex <= MAX_SIGNED_32BIT_INTEGER) return Int32Array
|
|
39
|
+
|
|
40
|
+
return Float64Array
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Function returning the minimal type able to represent the given number.
|
|
45
|
+
*
|
|
46
|
+
* @param {number} value - Value to test.
|
|
47
|
+
* @return {TypedArrayClass}
|
|
48
|
+
*/
|
|
49
|
+
export function getNumberType(value) {
|
|
50
|
+
// <= 32 bits itnteger?
|
|
51
|
+
if (value === (value | 0)) {
|
|
52
|
+
// Negative
|
|
53
|
+
if (Math.sign(value) === -1) {
|
|
54
|
+
if (value <= 127 && value >= -128) return Int8Array
|
|
55
|
+
|
|
56
|
+
if (value <= 32767 && value >= -32768) return Int16Array
|
|
57
|
+
|
|
58
|
+
return Int32Array
|
|
59
|
+
} else {
|
|
60
|
+
if (value <= 255) return Uint8Array
|
|
61
|
+
|
|
62
|
+
if (value <= 65535) return Uint16Array
|
|
63
|
+
|
|
64
|
+
return Uint32Array
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// 53 bits integer & floats
|
|
69
|
+
// NOTE: it's kinda hard to tell whether we could use 32bits or not...
|
|
70
|
+
return Float64Array
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Function returning the minimal type able to represent the given array
|
|
75
|
+
* of JavaScript numbers.
|
|
76
|
+
*
|
|
77
|
+
* @param {array} array - Array to represent.
|
|
78
|
+
* @param {function} getter - Optional getter.
|
|
79
|
+
* @return {TypedArrayClass}
|
|
80
|
+
*/
|
|
81
|
+
const TYPE_PRIORITY = {
|
|
82
|
+
Uint8Array: 1,
|
|
83
|
+
Int8Array: 2,
|
|
84
|
+
Uint16Array: 3,
|
|
85
|
+
Int16Array: 4,
|
|
86
|
+
Uint32Array: 5,
|
|
87
|
+
Int32Array: 6,
|
|
88
|
+
Float32Array: 7,
|
|
89
|
+
Float64Array: 8
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function getMinimalRepresentation(array, getter) {
|
|
93
|
+
let maxType = null
|
|
94
|
+
let maxPriority = 0
|
|
95
|
+
let p
|
|
96
|
+
let t
|
|
97
|
+
let v
|
|
98
|
+
let i
|
|
99
|
+
let l
|
|
100
|
+
|
|
101
|
+
for (i = 0, l = array.length; i < l; i++) {
|
|
102
|
+
v = getter ? getter(array[i]) : array[i]
|
|
103
|
+
t = exports.getNumberType(v)
|
|
104
|
+
p = TYPE_PRIORITY[t.name]
|
|
105
|
+
|
|
106
|
+
if (p > maxPriority) {
|
|
107
|
+
maxPriority = p
|
|
108
|
+
maxType = t
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return maxType
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Function returning whether the given value is a typed array.
|
|
117
|
+
*
|
|
118
|
+
* @param {any} value - Value to test.
|
|
119
|
+
* @return {boolean}
|
|
120
|
+
*/
|
|
121
|
+
export function isTypedArray(value) {
|
|
122
|
+
return ArrayBuffer.isView(value)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Function used to concat byte arrays.
|
|
127
|
+
*
|
|
128
|
+
* @param {...ByteArray}
|
|
129
|
+
* @return {ByteArray}
|
|
130
|
+
*/
|
|
131
|
+
export function concat() {
|
|
132
|
+
let length = 0
|
|
133
|
+
let i
|
|
134
|
+
let o
|
|
135
|
+
let l
|
|
136
|
+
|
|
137
|
+
for (i = 0, l = arguments.length; i < l; i++) length += arguments[i].length
|
|
138
|
+
|
|
139
|
+
const array = new arguments[0].constructor(length)
|
|
140
|
+
|
|
141
|
+
for (i = 0, o = 0; i < l; i++) {
|
|
142
|
+
array.set(arguments[i], o)
|
|
143
|
+
o += arguments[i].length
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return array
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Function used to initialize a byte array of indices.
|
|
151
|
+
*
|
|
152
|
+
* @param {number} length - Length of target.
|
|
153
|
+
* @return {ByteArray}
|
|
154
|
+
*/
|
|
155
|
+
export function indices(length) {
|
|
156
|
+
const PointerArray = exports.getPointerArray(length)
|
|
157
|
+
|
|
158
|
+
const array = new PointerArray(length)
|
|
159
|
+
|
|
160
|
+
for (let i = 0; i < length; i++) array[i] = i
|
|
161
|
+
|
|
162
|
+
return array
|
|
163
|
+
}
|
|
@@ -88,7 +88,7 @@ export declare function once(func: Function): (...args: any[]) => any;
|
|
|
88
88
|
export declare type Maybe<T> = T | undefined;
|
|
89
89
|
export declare type Result<T, E = any> = [Maybe<T>, Maybe<E>];
|
|
90
90
|
/**
|
|
91
|
-
* Wraps a function to catch any exceptions inside and return a `go` style error-return type.
|
|
91
|
+
* Wraps a function to catch any exceptions inside and return a `go` style error-return type {@link Result}.
|
|
92
92
|
*
|
|
93
93
|
* @param {Function} fun The function to wrap and catch if it throws.
|
|
94
94
|
* @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
|
|
@@ -101,7 +101,7 @@ export declare function result<T = any, E = unknown>(fun: Function<T>, onFinally
|
|
|
101
101
|
*
|
|
102
102
|
* @param {Function|Promise} awaitable The function or promise to await.
|
|
103
103
|
* @param {?function(err: E, res: T): *} [onFinally] Optional callback that will be called on the `finally` clause if given.
|
|
104
|
-
* @returns {Function} A new function that awaits the `awaitable` param and returns a
|
|
104
|
+
* @returns {Function} A new function that awaits the `awaitable` param and returns a {@link Result} type.
|
|
105
105
|
* @template T,E
|
|
106
106
|
*/
|
|
107
107
|
export declare function resultAsync<T = any, E = unknown>(awaitable: Promise<T> | Function<Promise<T>>, onFinally?: (err: E, res: T) => any): (...args: any[]) => Promise<Result<T, E>>;
|