@stdlib/array-index 0.1.0

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.
@@ -0,0 +1,539 @@
1
+ /*
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ // TypeScript Version: 4.1
20
+
21
+ /// <reference types="@stdlib/types"/>
22
+
23
+ import { Collection, AccessorArrayLike, DataType } from '@stdlib/types/array';
24
+
25
+ /**
26
+ * Boolean index array.
27
+ */
28
+ type BooleanIndexArray = Collection<boolean> | AccessorArrayLike<boolean>;
29
+
30
+ /**
31
+ * Integer index array.
32
+ */
33
+ type IntegerIndexArray = Collection<number> | AccessorArrayLike<number>;
34
+
35
+ /**
36
+ * Index array.
37
+ */
38
+ type IndexArray = BooleanIndexArray | IntegerIndexArray;
39
+
40
+ /**
41
+ * Interface describing function options.
42
+ */
43
+ interface Options {
44
+ /**
45
+ * Boolean indicating whether to continue persisting an index object after first usage (default: `false`).
46
+ */
47
+ persist?: boolean;
48
+ }
49
+
50
+ /**
51
+ * Interface describing an object containing array index data.
52
+ */
53
+ interface BaseArrayObject {
54
+ /**
55
+ * The underlying array associated with an array index.
56
+ */
57
+ data: IndexArray;
58
+
59
+ /**
60
+ * The type of array index.
61
+ */
62
+ type: 'int' | 'bool' | 'mask';
63
+
64
+ /**
65
+ * The data type of the underlying array.
66
+ */
67
+ dtype: DataType;
68
+ }
69
+
70
+ /**
71
+ * Interface describing an object containing mask array data.
72
+ */
73
+ interface MaskArrayObject extends BaseArrayObject {
74
+ /**
75
+ * The underlying array associated with an array index.
76
+ */
77
+ data: Uint8Array;
78
+
79
+ /**
80
+ * The type of array index.
81
+ */
82
+ type: 'mask';
83
+
84
+ /**
85
+ * The data type of the underlying array.
86
+ */
87
+ dtype: 'uint8';
88
+ }
89
+
90
+ /**
91
+ * Interface describing an object containing integer array data.
92
+ */
93
+ interface Int32ArrayObject extends BaseArrayObject {
94
+ /**
95
+ * The underlying array associated with an array index.
96
+ */
97
+ data: Int32Array;
98
+
99
+ /**
100
+ * The type of array index.
101
+ */
102
+ type: 'int';
103
+
104
+ /**
105
+ * The data type of the underlying array.
106
+ */
107
+ dtype: 'int32';
108
+ }
109
+
110
+ /**
111
+ * Interface describing an object containing integer array data.
112
+ */
113
+ interface IntegerArrayObject extends BaseArrayObject {
114
+ /**
115
+ * The underlying array associated with an array index.
116
+ */
117
+ data: IntegerIndexArray;
118
+
119
+ /**
120
+ * The type of array index.
121
+ */
122
+ type: 'int';
123
+
124
+ /**
125
+ * The data type of the underlying array.
126
+ */
127
+ dtype: 'generic';
128
+ }
129
+
130
+ /**
131
+ * Interface describing an object containing boolean array data.
132
+ */
133
+ interface BooleanArrayObject extends BaseArrayObject {
134
+ /**
135
+ * The underlying array associated with an array index.
136
+ */
137
+ data: BooleanIndexArray;
138
+
139
+ /**
140
+ * The type of array index.
141
+ */
142
+ type: 'bool';
143
+
144
+ /**
145
+ * The data type of the underlying array.
146
+ */
147
+ dtype: 'generic';
148
+ }
149
+
150
+ /**
151
+ * Array object data.
152
+ */
153
+ type ArrayObject = MaskArrayObject | Int32ArrayObject | BooleanArrayObject | IntegerArrayObject | null;
154
+
155
+ /**
156
+ * Interface describing an array index object.
157
+ */
158
+ interface BaseArrayIndex {
159
+ /**
160
+ * Read-only property returning the data associated with an `ArrayIndex` instance.
161
+ */
162
+ data: IndexArray;
163
+
164
+ /**
165
+ * Read-only property returning the underlying array index data type.
166
+ */
167
+ dtype: DataType;
168
+
169
+ /**
170
+ * Read-only property returning the unique identifier associated with an `ArrayIndex` instance.
171
+ */
172
+ id: string;
173
+
174
+ /**
175
+ * Boolean indicating if an `ArrayIndex` instance is actively cached.
176
+ */
177
+ isCached: boolean;
178
+
179
+ /**
180
+ * Read-only property returning the array index type.
181
+ */
182
+ type: 'int' | 'bool' | 'mask';
183
+
184
+ /**
185
+ * Serializes an `ArrayIndex` to a string.
186
+ *
187
+ * @returns serialized string
188
+ *
189
+ * @example
190
+ * var Uint8Array = require( '@stdlib/array-uint8' );
191
+ *
192
+ * var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) );
193
+ * // returns <ArrayIndex>
194
+ *
195
+ * var str = idx.toString();
196
+ * // e.g., 'ArrayIndex<0>'
197
+ */
198
+ toString(): string;
199
+ }
200
+
201
+ /**
202
+ * Interface describing a mask array index object.
203
+ */
204
+ interface MaskArrayIndex extends BaseArrayIndex {
205
+ /**
206
+ * Read-only property returning the array index type.
207
+ */
208
+ type: 'mask';
209
+
210
+ /**
211
+ * Read-only property returning the underlying array index data type.
212
+ */
213
+ dtype: 'uint8';
214
+
215
+ /**
216
+ * Read-only property returning the underlying array data.
217
+ */
218
+ data: Uint8Array;
219
+ }
220
+
221
+ /**
222
+ * Interface describing an integer array index object.
223
+ */
224
+ interface Int32ArrayIndex extends BaseArrayIndex {
225
+ /**
226
+ * Read-only property returning the array index type.
227
+ */
228
+ type: 'int';
229
+
230
+ /**
231
+ * Read-only property returning the underlying array index data type.
232
+ */
233
+ dtype: 'int32';
234
+
235
+ /**
236
+ * Read-only property returning the underlying array data.
237
+ */
238
+ data: Int32Array;
239
+ }
240
+
241
+ /**
242
+ * Interface describing a boolean array index object.
243
+ */
244
+ interface BooleanArrayIndex extends BaseArrayIndex {
245
+ /**
246
+ * Read-only property returning the array index type.
247
+ */
248
+ type: 'bool';
249
+
250
+ /**
251
+ * Read-only property returning the underlying array index data type.
252
+ */
253
+ dtype: 'generic';
254
+
255
+ /**
256
+ * Read-only property returning the underlying array data.
257
+ */
258
+ data: BooleanIndexArray;
259
+ }
260
+
261
+ /**
262
+ * Interface describing an integer array index object.
263
+ */
264
+ interface IntegerArrayIndex extends BaseArrayIndex {
265
+ /**
266
+ * Read-only property returning the array index type.
267
+ */
268
+ type: 'int';
269
+
270
+ /**
271
+ * Read-only property returning the underlying array index data type.
272
+ */
273
+ dtype: 'generic';
274
+
275
+ /**
276
+ * Read-only property returning the underlying array data.
277
+ */
278
+ data: IntegerIndexArray;
279
+ }
280
+
281
+ /**
282
+ * Array index object.
283
+ */
284
+ type ArrayIndex = MaskArrayIndex | Int32ArrayIndex | BooleanArrayIndex | IntegerArrayIndex;
285
+
286
+ /**
287
+ * Interface defining an `ArrayIndex` constructor which is both "newable" and "callable".
288
+ */
289
+ interface Constructor {
290
+ /**
291
+ * Array index constructor.
292
+ *
293
+ * @param x - input array
294
+ * @param options - function options
295
+ * @param options.persist - boolean indicating whether to continue persisting an index object after first usage
296
+ * @returns ArrayIndex instance
297
+ *
298
+ * @example
299
+ * var Uint8Array = require( '@stdlib/array-uint8' );
300
+ *
301
+ * var x = new Uint8Array( [ 1, 0, 1, 0 ] );
302
+ *
303
+ * var idx = new ArrayIndex( x );
304
+ * // returns <ArrayIndex>
305
+ */
306
+ new( x: Uint8Array, options?: Options ): MaskArrayIndex;
307
+
308
+ /**
309
+ * Array index constructor.
310
+ *
311
+ * @param x - input array
312
+ * @param options - function options
313
+ * @param options.persist - boolean indicating whether to continue persisting an index object after first usage
314
+ * @returns ArrayIndex instance
315
+ *
316
+ * @example
317
+ * var Int32Array = require( '@stdlib/array-int32' );
318
+ *
319
+ * var x = new Int32Array( [ 1, 0, 1, 0 ] );
320
+ *
321
+ * var idx = new ArrayIndex( x );
322
+ * // returns <ArrayIndex>
323
+ */
324
+ new( x: Int32Array, options?: Options ): Int32ArrayIndex;
325
+
326
+ /**
327
+ * Array index constructor.
328
+ *
329
+ * @param x - input array
330
+ * @param options - function options
331
+ * @param options.persist - boolean indicating whether to continue persisting an index object after first usage
332
+ * @returns ArrayIndex instance
333
+ *
334
+ * @example
335
+ * var x = [ 1, 2, 3, 4 ];
336
+ *
337
+ * var idx = new ArrayIndex( x );
338
+ * // returns <ArrayIndex>
339
+ */
340
+ new( x: IntegerIndexArray, options?: Options ): IntegerArrayIndex;
341
+
342
+ /**
343
+ * Array index constructor.
344
+ *
345
+ * @param x - input array
346
+ * @param options - function options
347
+ * @param options.persist - boolean indicating whether to continue persisting an index object after first usage
348
+ * @returns ArrayIndex instance
349
+ *
350
+ * @example
351
+ * var x = [ true, false, true, false ];
352
+ *
353
+ * var idx = new ArrayIndex( x );
354
+ * // returns <ArrayIndex>
355
+ */
356
+ new( x: BooleanIndexArray, options?: Options ): BooleanArrayIndex;
357
+
358
+ /**
359
+ * Array index constructor.
360
+ *
361
+ * @param x - input array
362
+ * @param options - function options
363
+ * @param options.persist - boolean indicating whether to continue persisting an index object after first usage
364
+ * @returns ArrayIndex instance
365
+ *
366
+ * @example
367
+ * var Uint8Array = require( '@stdlib/array-uint8' );
368
+ *
369
+ * var x = new Uint8Array( [ 1, 0, 1, 0 ] );
370
+ *
371
+ * var idx = new ArrayIndex( x );
372
+ * // returns <ArrayIndex>
373
+ */
374
+ new( x: IndexArray, options?: Options ): ArrayIndex;
375
+ /**
376
+ * Array index constructor.
377
+ *
378
+ * @param x - input array
379
+ * @param options - function options
380
+ * @param options.persist - boolean indicating whether to continue persisting an index object after first usage
381
+ * @returns ArrayIndex instance
382
+ *
383
+ * @example
384
+ * var Uint8Array = require( '@stdlib/array-uint8' );
385
+ *
386
+ * var x = new Uint8Array( [ 1, 0, 1, 0 ] );
387
+ *
388
+ * var idx = ArrayIndex( x );
389
+ * // returns <ArrayIndex>
390
+ */
391
+ ( x: Uint8Array, options?: Options ): MaskArrayIndex;
392
+
393
+ /**
394
+ * Array index constructor.
395
+ *
396
+ * @param x - input array
397
+ * @param options - function options
398
+ * @param options.persist - boolean indicating whether to continue persisting an index object after first usage
399
+ * @returns ArrayIndex instance
400
+ *
401
+ * @example
402
+ * var Int32Array = require( '@stdlib/array-int32' );
403
+ *
404
+ * var x = new Int32Array( [ 1, 0, 1, 0 ] );
405
+ *
406
+ * var idx = ArrayIndex( x );
407
+ * // returns <ArrayIndex>
408
+ */
409
+ ( x: Int32Array, options?: Options ): Int32ArrayIndex;
410
+
411
+ /**
412
+ * Array index constructor.
413
+ *
414
+ * @param x - input array
415
+ * @param options - function options
416
+ * @param options.persist - boolean indicating whether to continue persisting an index object after first usage
417
+ * @returns ArrayIndex instance
418
+ *
419
+ * @example
420
+ * var x = [ 1, 2, 3, 4 ];
421
+ *
422
+ * var idx = ArrayIndex( x );
423
+ * // returns <ArrayIndex>
424
+ */
425
+ ( x: IntegerIndexArray, options?: Options ): IntegerArrayIndex;
426
+
427
+ /**
428
+ * Array index constructor.
429
+ *
430
+ * @param x - input array
431
+ * @param options - function options
432
+ * @param options.persist - boolean indicating whether to continue persisting an index object after first usage
433
+ * @returns ArrayIndex instance
434
+ *
435
+ * @example
436
+ * var x = [ true, false, true, false ];
437
+ *
438
+ * var idx = ArrayIndex( x );
439
+ * // returns <ArrayIndex>
440
+ */
441
+ ( x: BooleanIndexArray, options?: Options ): BooleanArrayIndex;
442
+
443
+ /**
444
+ * Array index constructor.
445
+ *
446
+ * @param x - input array
447
+ * @param options - function options
448
+ * @param options.persist - boolean indicating whether to continue persisting an index object after first usage
449
+ * @returns ArrayIndex instance
450
+ *
451
+ * @example
452
+ * var Uint8Array = require( '@stdlib/array-uint8' );
453
+ *
454
+ * var x = new Uint8Array( [ 1, 0, 1, 0 ] );
455
+ *
456
+ * var idx = ArrayIndex( x );
457
+ * // returns <ArrayIndex>
458
+ */
459
+ ( x: IndexArray, options?: Options ): ArrayIndex;
460
+
461
+ /**
462
+ * String value of the constructor name.
463
+ */
464
+ name: 'ArrayIndex';
465
+
466
+ /**
467
+ * Frees the `ArrayIndex` associated with a provided identifier.
468
+ *
469
+ * @param id - identifier
470
+ * @returns boolean indicating whether an `ArrayIndex` was successfully freed
471
+ *
472
+ * @example
473
+ * var Uint8Array = require( '@stdlib/array-uint8' );
474
+ *
475
+ * var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ), {
476
+ * 'persist': true
477
+ * });
478
+ * // returns <ArrayIndex>
479
+ *
480
+ * // ...
481
+ *
482
+ * var out = ArrayIndex.free( idx.id );
483
+ * // returns true
484
+ */
485
+ free( id: string ): boolean;
486
+
487
+ /**
488
+ * Returns the array associated with a provided identifier.
489
+ *
490
+ * @param id - identifier
491
+ * @returns object containing array index data
492
+ *
493
+ * @example
494
+ * var Uint8Array = require( '@stdlib/array-uint8' );
495
+ *
496
+ * var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ), {
497
+ * 'persist': true
498
+ * });
499
+ * // returns <ArrayIndex>
500
+ *
501
+ * // ...
502
+ *
503
+ * var o = ArrayIndex.get( idx.id );
504
+ * // returns {...}
505
+ *
506
+ * var d = o.data;
507
+ * // returns <Uint8Array>[ 1, 0, 1, 0 ]
508
+ *
509
+ * var t = o.type;
510
+ * // returns 'mask'
511
+ *
512
+ * var dt = o.dtype;
513
+ * // returns 'uint8'
514
+ */
515
+ get<T extends BaseArrayObject = ArrayObject>( id: string ): T;
516
+ }
517
+
518
+ /**
519
+ * Array index constructor.
520
+ *
521
+ * @param x - input array
522
+ * @param options - function options
523
+ * @param options.persist - boolean indicating whether to continue persisting an index object after first usage
524
+ * @returns ArrayIndex instance
525
+ *
526
+ * @example
527
+ * var Uint8Array = require( '@stdlib/array-uint8' );
528
+ *
529
+ * var x = new Uint8Array( [ 1, 0, 1, 0 ] );
530
+ *
531
+ * var idx = new ArrayIndex( x );
532
+ * // returns <ArrayIndex>
533
+ */
534
+ declare var ctor: Constructor;
535
+
536
+
537
+ // EXPORTS //
538
+
539
+ export = ctor;
package/lib/cache.js ADDED
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var LinkedList = require( '@stdlib/utils-linked-list' );
24
+
25
+
26
+ // MAIN //
27
+
28
+ /**
29
+ * Cache for storing index arrays.
30
+ *
31
+ * @private
32
+ * @name cache
33
+ * @type {LinkedList}
34
+ */
35
+ var cache = new LinkedList(); // note: created as a linked list to allow for more efficient removal of expired index arrays
36
+
37
+
38
+ // EXPORTS //
39
+
40
+ module.exports = cache;
@@ -0,0 +1,59 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var cache = require( './cache.js' );
24
+
25
+
26
+ // MAIN //
27
+
28
+ /**
29
+ * Performs garbage collection on the index cache.
30
+ *
31
+ * @private
32
+ * @returns {Object} garbage collection results
33
+ */
34
+ function gc() {
35
+ var node;
36
+ var N;
37
+ var M;
38
+ var v;
39
+
40
+ node = cache.first();
41
+ N = cache.length;
42
+ while ( node ) {
43
+ v = node.value;
44
+ if ( !v.persist ) {
45
+ cache.remove( node );
46
+ }
47
+ node = node.next;
48
+ }
49
+ M = cache.length;
50
+ return {
51
+ 'size': M,
52
+ 'removed': N - M
53
+ };
54
+ }
55
+
56
+
57
+ // EXPORTS //
58
+
59
+ module.exports = gc;
@@ -0,0 +1,42 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ 'use strict';
20
+
21
+ // MAIN //
22
+
23
+ /**
24
+ * Returns default options.
25
+ *
26
+ * @private
27
+ * @returns {Object} defaults
28
+ *
29
+ * @example
30
+ * var o = defaults();
31
+ * // returns {...}
32
+ */
33
+ function defaults() {
34
+ return {
35
+ 'persist': false
36
+ };
37
+ }
38
+
39
+
40
+ // EXPORTS //
41
+
42
+ module.exports = defaults;