@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.
- package/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +562 -0
- package/SECURITY.md +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +7 -0
- package/docs/types/index.d.ts +539 -0
- package/lib/cache.js +40 -0
- package/lib/cache_gc.js +59 -0
- package/lib/defaults.js +42 -0
- package/lib/find.js +49 -0
- package/lib/id.js +46 -0
- package/lib/index.js +43 -0
- package/lib/main.js +443 -0
- package/lib/validate.js +66 -0
- package/package.json +104 -0
package/README.md
ADDED
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
|
|
3
|
+
@license Apache-2.0
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2024 The Stdlib Authors.
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
you may not use this file except in compliance with the License.
|
|
9
|
+
You may obtain a copy of the License at
|
|
10
|
+
|
|
11
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
|
|
13
|
+
Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
See the License for the specific language governing permissions and
|
|
17
|
+
limitations under the License.
|
|
18
|
+
|
|
19
|
+
-->
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
<details>
|
|
23
|
+
<summary>
|
|
24
|
+
About stdlib...
|
|
25
|
+
</summary>
|
|
26
|
+
<p>We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.</p>
|
|
27
|
+
<p>The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.</p>
|
|
28
|
+
<p>When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.</p>
|
|
29
|
+
<p>To join us in bringing numerical computing to the web, get started by checking us out on <a href="https://github.com/stdlib-js/stdlib">GitHub</a>, and please consider <a href="https://opencollective.com/stdlib">financially supporting stdlib</a>. We greatly appreciate your continued support!</p>
|
|
30
|
+
</details>
|
|
31
|
+
|
|
32
|
+
# ArrayIndex
|
|
33
|
+
|
|
34
|
+
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->
|
|
35
|
+
|
|
36
|
+
> Array index constructor.
|
|
37
|
+
|
|
38
|
+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
|
|
39
|
+
|
|
40
|
+
<section class="intro">
|
|
41
|
+
|
|
42
|
+
In JavaScript, only strings and symbols are valid property names. When providing values for property names which are not string or symbols, the values are serialized to strings **prior to** attempting to access property values. For example, the following
|
|
43
|
+
|
|
44
|
+
```javascript
|
|
45
|
+
// Create an array:
|
|
46
|
+
var x = [ 1, 2, 3, 4 ];
|
|
47
|
+
|
|
48
|
+
// Define a list of indices for elements we want to retrieve from `x`:
|
|
49
|
+
var y = [ 0, 2 ];
|
|
50
|
+
|
|
51
|
+
// Attempt to retrieve the desired elements:
|
|
52
|
+
var v = x[ y ]; // => desired: [ 1, 3 ]
|
|
53
|
+
// returns undefined
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
is equivalent to
|
|
57
|
+
|
|
58
|
+
```javascript
|
|
59
|
+
var x = [ 1, 2, 3, 4 ];
|
|
60
|
+
var y = [ 0, 2 ];
|
|
61
|
+
|
|
62
|
+
var v = x[ y.toString() ];
|
|
63
|
+
// returns undefined
|
|
64
|
+
|
|
65
|
+
// ...which is equivalent to:
|
|
66
|
+
v = x[ '0,2' ];
|
|
67
|
+
// returns undefined
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Accordingly, in order to circumvent built-in property access behavior and support non-traditional access patterns, one can leverage [`Proxy`][@stdlib/proxy/ctor] objects which allow one to intercept property access and to perform transformations before attempting to access elements in a target object.
|
|
71
|
+
|
|
72
|
+
To support the access pattern shown in the example above, one can leverage built-in string serialization behavior to reconstruct the original property value provided prior to serialization. The `ArrayIndex` constructor described below provides one such mechanism.
|
|
73
|
+
|
|
74
|
+
Specifically, instantiated `ArrayIndex` objects are assigned a unique identifier and stored in a local cache. When provided as property values to `ArrayIndex` consumers, instantiated objects serialize to a string containing their unique identifier. `ArrayIndex` consumers can then parse the serialized string to obtain the unique identifier and subsequently recover the original array from the local cache.
|
|
75
|
+
|
|
76
|
+
</section>
|
|
77
|
+
|
|
78
|
+
<!-- /.intro -->
|
|
79
|
+
|
|
80
|
+
<!-- Package usage documentation. -->
|
|
81
|
+
|
|
82
|
+
<section class="installation">
|
|
83
|
+
|
|
84
|
+
## Installation
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
npm install @stdlib/array-index
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
</section>
|
|
91
|
+
|
|
92
|
+
<section class="usage">
|
|
93
|
+
|
|
94
|
+
## Usage
|
|
95
|
+
|
|
96
|
+
```javascript
|
|
97
|
+
var ArrayIndex = require( '@stdlib/array-index' );
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
<a name="main"></a>
|
|
101
|
+
|
|
102
|
+
#### ArrayIndex( x\[, options] )
|
|
103
|
+
|
|
104
|
+
Wraps a provided array as an array index object.
|
|
105
|
+
|
|
106
|
+
```javascript
|
|
107
|
+
var x = [ 1, 2, 3, 4 ];
|
|
108
|
+
|
|
109
|
+
var idx = new ArrayIndex( x );
|
|
110
|
+
// returns <ArrayIndex>
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
The constructor accepts the following arguments:
|
|
114
|
+
|
|
115
|
+
- **x**: input array.
|
|
116
|
+
- **options**: function options.
|
|
117
|
+
|
|
118
|
+
The constructor accepts the following options:
|
|
119
|
+
|
|
120
|
+
- **persist**: boolean indicating whether to continue persisting an index object after first usage. Default: `false`.
|
|
121
|
+
|
|
122
|
+
By default, an `ArrayIndex` is invalidated and removed from an internal cache immediately after a consumer resolves the underlying data associated with an `ArrayIndex` instance using the [`ArrayIndex.get()`](#static-method-get) static method. Immediate invalidation and cache removal ensures that references to the underlying array are not the source of memory leaks.
|
|
123
|
+
|
|
124
|
+
One may, however, want to reuse an `ArrayIndex` instance to avoid additional memory allocation. In order to persist an `ArrayIndex` and prevent automatic cache invalidation, set the `persist` option to `true`.
|
|
125
|
+
|
|
126
|
+
```javascript
|
|
127
|
+
var x = [ 1, 2, 3, 4 ];
|
|
128
|
+
|
|
129
|
+
var idx = new ArrayIndex( x, {
|
|
130
|
+
'persist': true
|
|
131
|
+
});
|
|
132
|
+
// returns <ArrayIndex>
|
|
133
|
+
|
|
134
|
+
// ...
|
|
135
|
+
|
|
136
|
+
var o = ArrayIndex.get( idx.id );
|
|
137
|
+
// returns {...}
|
|
138
|
+
|
|
139
|
+
// ...
|
|
140
|
+
|
|
141
|
+
o = ArrayIndex.get( idx.id );
|
|
142
|
+
// returns {...}
|
|
143
|
+
|
|
144
|
+
// ...
|
|
145
|
+
|
|
146
|
+
// Explicitly free the array index:
|
|
147
|
+
ArrayIndex.free( idx.id );
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
In order to **prevent** memory leaks when working with persisted `ArrayIndex` instances, one **must** remember to manually free persisted instances using the [`ArrayIndex.free()`](#static-method-free) method.
|
|
151
|
+
|
|
152
|
+
* * *
|
|
153
|
+
|
|
154
|
+
### Properties
|
|
155
|
+
|
|
156
|
+
<a name="static-prop-name"></a>
|
|
157
|
+
|
|
158
|
+
#### ArrayIndex.name
|
|
159
|
+
|
|
160
|
+
String value of the `ArrayIndex` constructor name.
|
|
161
|
+
|
|
162
|
+
```javascript
|
|
163
|
+
var str = ArrayIndex.name;
|
|
164
|
+
// returns 'ArrayIndex'
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
<a name="prop-data"></a>
|
|
168
|
+
|
|
169
|
+
#### ArrayIndex.prototype.data
|
|
170
|
+
|
|
171
|
+
**Read-only** property returning the underlying array associated with an `ArrayIndex` instance.
|
|
172
|
+
|
|
173
|
+
```javascript
|
|
174
|
+
var Uint8Array = require( '@stdlib/array-uint8' );
|
|
175
|
+
|
|
176
|
+
var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) );
|
|
177
|
+
// returns <ArrayIndex>
|
|
178
|
+
|
|
179
|
+
var v = idx.data;
|
|
180
|
+
// returns <Uint8Array>[ 1, 0, 1, 0 ]
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
<a name="prop-dtype"></a>
|
|
184
|
+
|
|
185
|
+
#### ArrayIndex.prototype.dtype
|
|
186
|
+
|
|
187
|
+
**Read-only** property returning the data type of the underlying array associated with an `ArrayIndex` instance.
|
|
188
|
+
|
|
189
|
+
```javascript
|
|
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 dt = idx.dtype;
|
|
196
|
+
// returns 'uint8'
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
<a name="prop-id"></a>
|
|
200
|
+
|
|
201
|
+
#### ArrayIndex.prototype.id
|
|
202
|
+
|
|
203
|
+
**Read-only** property returning the unique identifier associated with an `ArrayIndex` instance.
|
|
204
|
+
|
|
205
|
+
```javascript
|
|
206
|
+
var Uint8Array = require( '@stdlib/array-uint8' );
|
|
207
|
+
|
|
208
|
+
var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) );
|
|
209
|
+
// returns <ArrayIndex>
|
|
210
|
+
|
|
211
|
+
var id = idx.id;
|
|
212
|
+
// returns <string>
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
The identifier should be used by `ArrayIndex` consumers to resolve the underlying data associated with an `ArrayIndex` instance.
|
|
216
|
+
|
|
217
|
+
<a name="prop-is-cached"></a>
|
|
218
|
+
|
|
219
|
+
#### ArrayIndex.prototype.isCached
|
|
220
|
+
|
|
221
|
+
**Read-only** property returning a boolean indicating whether an `ArrayIndex` instance is actively cached.
|
|
222
|
+
|
|
223
|
+
```javascript
|
|
224
|
+
var Uint8Array = require( '@stdlib/array-uint8' );
|
|
225
|
+
|
|
226
|
+
var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) );
|
|
227
|
+
// returns <ArrayIndex>
|
|
228
|
+
|
|
229
|
+
var out = idx.isCached;
|
|
230
|
+
// returns true
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
<a name="prop-type"></a>
|
|
234
|
+
|
|
235
|
+
#### ArrayIndex.prototype.type
|
|
236
|
+
|
|
237
|
+
**Read-only** property returning the array index type.
|
|
238
|
+
|
|
239
|
+
```javascript
|
|
240
|
+
var Uint8Array = require( '@stdlib/array-uint8' );
|
|
241
|
+
|
|
242
|
+
var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) );
|
|
243
|
+
// returns <ArrayIndex>
|
|
244
|
+
|
|
245
|
+
var t = idx.type;
|
|
246
|
+
// returns 'mask'
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
The following array index types are supported:
|
|
250
|
+
|
|
251
|
+
- **mask**: mask array, in which a value of zero indicates to include a respective element and a value of one indicates to exclude a respective element. A mask array is the complement of a boolean array.
|
|
252
|
+
- **bool**: boolean array, in which a value of `true` indicates to include a respective element and a value of `false` indicates to exclude a respective element. A boolean array is the complement of a mask array.
|
|
253
|
+
- **int**: integer array, in which each element is an index indicating the position of an element to include. Elements are **not** required to be unique (i.e., more than element may resolve to the same position).
|
|
254
|
+
|
|
255
|
+
* * *
|
|
256
|
+
|
|
257
|
+
### Methods
|
|
258
|
+
|
|
259
|
+
<a name="static-method-free"></a>
|
|
260
|
+
|
|
261
|
+
#### ArrayIndex.free( id )
|
|
262
|
+
|
|
263
|
+
Frees the `ArrayIndex` associated with a provided identifier.
|
|
264
|
+
|
|
265
|
+
```javascript
|
|
266
|
+
var Uint8Array = require( '@stdlib/array-uint8' );
|
|
267
|
+
|
|
268
|
+
var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ), {
|
|
269
|
+
'persist': true
|
|
270
|
+
});
|
|
271
|
+
// returns <ArrayIndex>
|
|
272
|
+
|
|
273
|
+
// ...
|
|
274
|
+
|
|
275
|
+
var out = ArrayIndex.free( idx.id );
|
|
276
|
+
// returns true
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
Once an `ArrayIndex` is freed, the instance is invalid and can no longer be used. Any subsequent `ArrayIndex` operations (i.e., property and method access) will raise an exception.
|
|
280
|
+
|
|
281
|
+
<a name="static-method-get"></a>
|
|
282
|
+
|
|
283
|
+
#### ArrayIndex.get( id )
|
|
284
|
+
|
|
285
|
+
Returns the array associated with the `ArrayIndex` having a provided identifier.
|
|
286
|
+
|
|
287
|
+
```javascript
|
|
288
|
+
var Uint8Array = require( '@stdlib/array-uint8' );
|
|
289
|
+
|
|
290
|
+
var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ), {
|
|
291
|
+
'persist': true
|
|
292
|
+
});
|
|
293
|
+
// returns <ArrayIndex>
|
|
294
|
+
|
|
295
|
+
// ...
|
|
296
|
+
|
|
297
|
+
var o = ArrayIndex.get( idx.id );
|
|
298
|
+
// returns {...}
|
|
299
|
+
|
|
300
|
+
var d = o.data;
|
|
301
|
+
// returns <Uint8Array>[ 1, 0, 1, 0 ]
|
|
302
|
+
|
|
303
|
+
var t = o.type;
|
|
304
|
+
// returns 'mask'
|
|
305
|
+
|
|
306
|
+
var dt = o.dtype;
|
|
307
|
+
// returns 'uint8'
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
The returned object has the following properties:
|
|
311
|
+
|
|
312
|
+
- **data**: the underlying array associated with the `ArrayIndex` identified by the provided `id`.
|
|
313
|
+
- **type**: the type of array index. One of the following: `'int'`, `'bool'`, or `'mask'`.
|
|
314
|
+
- **dtype**: the data type of the underlying array.
|
|
315
|
+
|
|
316
|
+
If the `ArrayIndex` associated with a provided identifier was not explicitly persisted, calling this method will cause the `ArrayIndex` to be invalidated and removed from an internal cache. Any subsequent instance operations (i.e., property and method access) will raise an exception.
|
|
317
|
+
|
|
318
|
+
<a name="method-to-string"></a>
|
|
319
|
+
|
|
320
|
+
#### ArrayIndex.prototype.toString()
|
|
321
|
+
|
|
322
|
+
Serializes an `ArrayIndex` as a string.
|
|
323
|
+
|
|
324
|
+
```javascript
|
|
325
|
+
var Uint8Array = require( '@stdlib/array-uint8' );
|
|
326
|
+
|
|
327
|
+
var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) );
|
|
328
|
+
// returns <ArrayIndex>
|
|
329
|
+
|
|
330
|
+
var str = idx.toString();
|
|
331
|
+
// e.g., 'ArrayIndex<0>'
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
An `ArrayIndex` is intended to be an opaque object used by objects supporting "fancy" indexing (e.g., [fancy arrays][@stdlib/array/to-fancy]). As such, when serialized as a string, a serialized `ArrayIndex` includes only the unique identifier associated with the respective instance.
|
|
335
|
+
|
|
336
|
+
<a name="method-to-json"></a>
|
|
337
|
+
|
|
338
|
+
#### ArrayIndex.prototype.toJSON()
|
|
339
|
+
|
|
340
|
+
Serializes an `ArrayIndex` as a [JSON][json] object.
|
|
341
|
+
|
|
342
|
+
```javascript
|
|
343
|
+
var Uint8Array = require( '@stdlib/array-uint8' );
|
|
344
|
+
|
|
345
|
+
var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) );
|
|
346
|
+
// returns <ArrayIndex>
|
|
347
|
+
|
|
348
|
+
var o = idx.toJSON();
|
|
349
|
+
// returns { 'type': 'ArrayIndex', 'data': { 'type': 'Uint8Array', 'data': [ 1, 0, 1, 0 ] } }
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
`JSON.stringify()` implicitly calls this method when stringifying an `ArrayIndex` instance.
|
|
353
|
+
|
|
354
|
+
</section>
|
|
355
|
+
|
|
356
|
+
<!-- /.usage -->
|
|
357
|
+
|
|
358
|
+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
359
|
+
|
|
360
|
+
* * *
|
|
361
|
+
|
|
362
|
+
<section class="notes">
|
|
363
|
+
|
|
364
|
+
## Notes
|
|
365
|
+
|
|
366
|
+
- `ArrayIndex` instances have no explicit functionality; however, they are used by ["fancy" arrays][@stdlib/array/to-fancy] and other packages for element retrieval and assignment.
|
|
367
|
+
|
|
368
|
+
- Because `ArrayIndex` instances leverage an internal cache implementing the **Singleton pattern**, one **must** be sure to use the same `ArrayIndex` constructor as `ArrayIndex` consumers. If one uses a different `ArrayIndex` constructor, the consumer will **not** be able to resolve the original wrapped array, as the consumer will attempt to resolve an `ArrayIndex` instance in the wrong internal cache.
|
|
369
|
+
|
|
370
|
+
- Because non-persisted `ArrayIndex` instances are freed after first use, in order to avoid holding onto memory and to allow garbage collection, one should avoid scenarios in which an `ArrayIndex` is never used. For example,
|
|
371
|
+
|
|
372
|
+
```javascript
|
|
373
|
+
var Uint8Array = require( '@stdlib/array-uint8' );
|
|
374
|
+
|
|
375
|
+
var data = new Uint8Array( [ 1, 0, 0, 0 ] );
|
|
376
|
+
var idx = new ArrayIndex( data );
|
|
377
|
+
|
|
378
|
+
var o;
|
|
379
|
+
if ( data[ 0 ] === 0 ) {
|
|
380
|
+
// Do something with `idx`...
|
|
381
|
+
o = ArrayIndex.get( idx.id );
|
|
382
|
+
|
|
383
|
+
// ...
|
|
384
|
+
}
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
will leak memory as `idx` is only consumed within an `if` block which never evaluates. In such scenarios, one should either refactor to avoid inadvertently holding onto memory or explicitly free the `ArrayIndex`.
|
|
388
|
+
|
|
389
|
+
```javascript
|
|
390
|
+
var Uint8Array = require( '@stdlib/array-uint8' );
|
|
391
|
+
|
|
392
|
+
var data = new Uint8Array( [ 1, 0, 0, 0 ] );
|
|
393
|
+
var idx = new ArrayIndex( data );
|
|
394
|
+
|
|
395
|
+
var o;
|
|
396
|
+
if ( data[ 0 ] === 0 ) {
|
|
397
|
+
// Do something with `idx`...
|
|
398
|
+
o = ArrayIndex.get( idx.id );
|
|
399
|
+
|
|
400
|
+
// ...
|
|
401
|
+
} else {
|
|
402
|
+
ArrayIndex.free( idx.id );
|
|
403
|
+
}
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
</section>
|
|
407
|
+
|
|
408
|
+
<!-- /.notes -->
|
|
409
|
+
|
|
410
|
+
<!-- Package usage examples. -->
|
|
411
|
+
|
|
412
|
+
* * *
|
|
413
|
+
|
|
414
|
+
<section class="examples">
|
|
415
|
+
|
|
416
|
+
## Examples
|
|
417
|
+
|
|
418
|
+
<!-- eslint no-undef: "error" -->
|
|
419
|
+
|
|
420
|
+
```javascript
|
|
421
|
+
var Uint8Array = require( '@stdlib/array-uint8' );
|
|
422
|
+
var Int32Array = require( '@stdlib/array-int32' );
|
|
423
|
+
var ArrayIndex = require( '@stdlib/array-index' );
|
|
424
|
+
|
|
425
|
+
var x = new Uint8Array( [ 1, 0, 1, 0 ] );
|
|
426
|
+
var i = new ArrayIndex( x );
|
|
427
|
+
// returns <ArrayIndex>
|
|
428
|
+
|
|
429
|
+
var o = ArrayIndex.get( i.id );
|
|
430
|
+
// returns {...}
|
|
431
|
+
|
|
432
|
+
console.log( 'Type: %s. Data type: %s.', o.type, o.dtype );
|
|
433
|
+
|
|
434
|
+
x = [ true, false, true, false ];
|
|
435
|
+
i = new ArrayIndex( x );
|
|
436
|
+
// returns <ArrayIndex>
|
|
437
|
+
|
|
438
|
+
o = ArrayIndex.get( i.id );
|
|
439
|
+
// returns {...}
|
|
440
|
+
|
|
441
|
+
console.log( 'Type: %s. Data type: %s.', o.type, o.dtype );
|
|
442
|
+
|
|
443
|
+
x = new Int32Array( [ 1, 3, 4, 7 ] );
|
|
444
|
+
i = new ArrayIndex( x );
|
|
445
|
+
// returns <ArrayIndex>
|
|
446
|
+
|
|
447
|
+
o = ArrayIndex.get( i.id );
|
|
448
|
+
// returns {...}
|
|
449
|
+
|
|
450
|
+
console.log( 'Type: %s. Data type: %s.', o.type, o.dtype );
|
|
451
|
+
|
|
452
|
+
x = [ 1, 3, 4, 7 ];
|
|
453
|
+
i = new ArrayIndex( x );
|
|
454
|
+
// returns <ArrayIndex>
|
|
455
|
+
|
|
456
|
+
o = ArrayIndex.get( i.id );
|
|
457
|
+
// returns {...}
|
|
458
|
+
|
|
459
|
+
console.log( 'Type: %s. Data type: %s.', o.type, o.dtype );
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
</section>
|
|
463
|
+
|
|
464
|
+
<!-- /.examples -->
|
|
465
|
+
|
|
466
|
+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
467
|
+
|
|
468
|
+
<section class="references">
|
|
469
|
+
|
|
470
|
+
</section>
|
|
471
|
+
|
|
472
|
+
<!-- /.references -->
|
|
473
|
+
|
|
474
|
+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
475
|
+
|
|
476
|
+
<section class="related">
|
|
477
|
+
|
|
478
|
+
</section>
|
|
479
|
+
|
|
480
|
+
<!-- /.related -->
|
|
481
|
+
|
|
482
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
<section class="main-repo" >
|
|
486
|
+
|
|
487
|
+
* * *
|
|
488
|
+
|
|
489
|
+
## Notice
|
|
490
|
+
|
|
491
|
+
This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
|
|
492
|
+
|
|
493
|
+
For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib].
|
|
494
|
+
|
|
495
|
+
#### Community
|
|
496
|
+
|
|
497
|
+
[![Chat][chat-image]][chat-url]
|
|
498
|
+
|
|
499
|
+
---
|
|
500
|
+
|
|
501
|
+
## License
|
|
502
|
+
|
|
503
|
+
See [LICENSE][stdlib-license].
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
## Copyright
|
|
507
|
+
|
|
508
|
+
Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
509
|
+
|
|
510
|
+
</section>
|
|
511
|
+
|
|
512
|
+
<!-- /.stdlib -->
|
|
513
|
+
|
|
514
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
515
|
+
|
|
516
|
+
<section class="links">
|
|
517
|
+
|
|
518
|
+
[npm-image]: http://img.shields.io/npm/v/@stdlib/array-index.svg
|
|
519
|
+
[npm-url]: https://npmjs.org/package/@stdlib/array-index
|
|
520
|
+
|
|
521
|
+
[test-image]: https://github.com/stdlib-js/array-index/actions/workflows/test.yml/badge.svg?branch=v0.1.0
|
|
522
|
+
[test-url]: https://github.com/stdlib-js/array-index/actions/workflows/test.yml?query=branch:v0.1.0
|
|
523
|
+
|
|
524
|
+
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/array-index/main.svg
|
|
525
|
+
[coverage-url]: https://codecov.io/github/stdlib-js/array-index?branch=main
|
|
526
|
+
|
|
527
|
+
<!--
|
|
528
|
+
|
|
529
|
+
[dependencies-image]: https://img.shields.io/david/stdlib-js/array-index.svg
|
|
530
|
+
[dependencies-url]: https://david-dm.org/stdlib-js/array-index/main
|
|
531
|
+
|
|
532
|
+
-->
|
|
533
|
+
|
|
534
|
+
[chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
|
|
535
|
+
[chat-url]: https://app.gitter.im/#/room/#stdlib-js_stdlib:gitter.im
|
|
536
|
+
|
|
537
|
+
[stdlib]: https://github.com/stdlib-js/stdlib
|
|
538
|
+
|
|
539
|
+
[stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors
|
|
540
|
+
|
|
541
|
+
[umd]: https://github.com/umdjs/umd
|
|
542
|
+
[es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
|
|
543
|
+
|
|
544
|
+
[deno-url]: https://github.com/stdlib-js/array-index/tree/deno
|
|
545
|
+
[deno-readme]: https://github.com/stdlib-js/array-index/blob/deno/README.md
|
|
546
|
+
[umd-url]: https://github.com/stdlib-js/array-index/tree/umd
|
|
547
|
+
[umd-readme]: https://github.com/stdlib-js/array-index/blob/umd/README.md
|
|
548
|
+
[esm-url]: https://github.com/stdlib-js/array-index/tree/esm
|
|
549
|
+
[esm-readme]: https://github.com/stdlib-js/array-index/blob/esm/README.md
|
|
550
|
+
[branches-url]: https://github.com/stdlib-js/array-index/blob/main/branches.md
|
|
551
|
+
|
|
552
|
+
[stdlib-license]: https://raw.githubusercontent.com/stdlib-js/array-index/main/LICENSE
|
|
553
|
+
|
|
554
|
+
[json]: http://www.json.org/
|
|
555
|
+
|
|
556
|
+
[@stdlib/array/to-fancy]: https://www.npmjs.com/package/@stdlib/array-to-fancy
|
|
557
|
+
|
|
558
|
+
[@stdlib/proxy/ctor]: https://www.npmjs.com/package/@stdlib/proxy-ctor
|
|
559
|
+
|
|
560
|
+
</section>
|
|
561
|
+
|
|
562
|
+
<!-- /.links -->
|
package/SECURITY.md
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";var o=function(r,e){return function(){return e||r((e={exports:{}}).exports,e),e.exports}};var c=o(function(Z,p){
|
|
2
|
+
function A(){return{persist:!1}}p.exports=A
|
|
3
|
+
});var g=o(function($,h){
|
|
4
|
+
var I=require('@stdlib/assert-is-plain-object/dist'),S=require('@stdlib/assert-has-own-property/dist'),j=require('@stdlib/assert-is-boolean/dist').isPrimitive,y=require('@stdlib/error-tools-fmtprodmsg/dist');function N(r,e){return I(e)?S(e,"persist")&&(r.persist=e.persist,!j(r.persist))?new TypeError(y('null2o',"persist",r.persist)):null:new TypeError(y('null2V',e));}h.exports=N
|
|
5
|
+
});var v=o(function(ee,m){
|
|
6
|
+
var P=require('@stdlib/utils-linked-list/dist'),k=new P;m.exports=k
|
|
7
|
+
});var q=o(function(re,w){
|
|
8
|
+
var C=v();function F(r){for(var e=C.first();e;){if(e.value.id===r)return e;e=e.next}return null}w.exports=F
|
|
9
|
+
});var x=o(function(te,_){
|
|
10
|
+
var b=-1;function R(){return b+=1,b.toString()}_.exports=R
|
|
11
|
+
});var O=o(function(ae,T){
|
|
12
|
+
var d=require('@stdlib/utils-define-nonenumerable-read-only-accessor/dist'),s=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),B=require('@stdlib/utils-define-nonenumerable-property/dist'),J=require('@stdlib/assert-is-collection/dist'),L=require('@stdlib/assert-is-boolean/dist').isPrimitive,V=require('@stdlib/assert-is-integer/dist').isPrimitive,G=require('@stdlib/array-base-assert-is-accessor-array/dist'),U=require('@stdlib/array-to-json/dist'),z=require('@stdlib/array-dtype/dist'),D=require('@stdlib/array-base-copy/dist'),H=require('@stdlib/array-base-resolve-getter/dist'),K=require('@stdlib/error-tools-fmtprodmsg/dist'),M=c(),Q=g(),f=v(),E=q(),W=x();function a(r){var e,t,n,i,u,l;if(!(this instanceof a))return arguments.length>1?new a(r,arguments[1]):new a(r);if(!J(r))throw new TypeError(K('null2O',r));if(i=z(r),i==="generic"||i===null)if(n=H(r),l=n(r,0),L(l))u="bool";else if(V(l))u="int";else throw new TypeError("invalid argument. First argument must be a valid index array.");else if(i==="int32")u="int";else if(i==="uint8")u="mask";else throw new TypeError("invalid argument. First argument must be a valid index array.");if(e=M(),arguments.length>1&&(t=Q(e,arguments[1]),t))throw t;return f.push({id:W(),ref:this,data:r,type:u,dtype:i,persist:e.persist}),s(this,"_node",f.last()),B(this,"_invalidated",!1),this}s(a,"name","ArrayIndex");s(a,"free",function(e){var t,n;return t=E(e),t===null?!1:(n=t.value,s(n.ref,"_invalidated",!0),f.remove(t),n.data=null,!0)});s(a,"get",function(e){var t,n,i;return t=E(e),t===null?null:(i=t.value,n={data:i.data,type:i.type,dtype:i.dtype},i.persist||a.free(e),n)});d(a.prototype,"data",function(){if(this._invalidated)throw new Error("invalid operation. This array index instance has already been freed and can no longer be used.");return this._node.value.data});d(a.prototype,"dtype",function(){if(this._invalidated)throw new Error("invalid operation. This array index instance has already been freed and can no longer be used.");return this._node.value.dtype});d(a.prototype,"id",function(){if(this._invalidated)throw new Error("invalid operation. This array index instance has already been freed and can no longer be used.");return this._node.value.id});d(a.prototype,"isCached",function(){return!this._invalidated});d(a.prototype,"type",function(){if(this._invalidated)throw new Error("invalid operation. This array index instance has already been freed and can no longer be used.");return this._node.value.type});s(a.prototype,"toString",function(){var e;if(this._invalidated)throw new Error("invalid operation. This array index instance has already been freed and can no longer be used.");return e=this._node.value,"ArrayIndex<"+e.id+">"});s(a.prototype,"toJSON",function(){var e,t;if(this._invalidated)throw new Error("invalid operation. This array index instance has already been freed and can no longer be used.");return e=this._node.value,e.dtype==="generic"||e.dtype===null?G(e.data)?t=D(e.data):t=e.data:t=U(e.data),{type:"ArrayIndex",data:t}});T.exports=a
|
|
13
|
+
});var X=O();module.exports=X;
|
|
14
|
+
/** @license Apache-2.0 */
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../lib/defaults.js", "../lib/validate.js", "../lib/cache.js", "../lib/find.js", "../lib/id.js", "../lib/main.js", "../lib/index.js"],
|
|
4
|
+
"sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MAIN //\n\n/**\n* Returns default options.\n*\n* @private\n* @returns {Object} defaults\n*\n* @example\n* var o = defaults();\n* // returns {...}\n*/\nfunction defaults() {\n\treturn {\n\t\t'persist': false\n\t};\n}\n\n\n// EXPORTS //\n\nmodule.exports = defaults;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isObject = require( '@stdlib/assert-is-plain-object' );\nvar hasOwnProp = require( '@stdlib/assert-has-own-property' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Validates function options.\n*\n* @private\n* @param {Object} opts - destination object\n* @param {Options} options - function options\n* @param {boolean} [options.persist] - boolean indicating whether to continue persisting an index object after first usage\n* @returns {(Error|null)} null or an error object\n*\n* @example\n* var opts = {};\n* var options = {\n* 'persist': false\n* };\n* var err = validate( opts, options );\n* if ( err ) {\n* throw err;\n* }\n*/\nfunction validate( opts, options ) {\n\tif ( !isObject( options ) ) {\n\t\treturn new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );\n\t}\n\tif ( hasOwnProp( options, 'persist' ) ) {\n\t\topts.persist = options.persist;\n\t\tif ( !isBoolean( opts.persist ) ) {\n\t\t\treturn new TypeError( format( 'invalid option. `%s` option must be a boolean. Option: `%s`.', 'persist', opts.persist ) );\n\t\t}\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = validate;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar LinkedList = require( '@stdlib/utils-linked-list' );\n\n\n// MAIN //\n\n/**\n* Cache for storing index arrays.\n*\n* @private\n* @name cache\n* @type {LinkedList}\n*/\nvar cache = new LinkedList(); // note: created as a linked list to allow for more efficient removal of expired index arrays\n\n\n// EXPORTS //\n\nmodule.exports = cache;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar cache = require( './cache.js' );\n\n\n// MAIN //\n\n/**\n* Returns an array index object associated with a specified identifier.\n*\n* @private\n* @param {*} id - identifier\n* @returns {(Node|null)} array index object\n*/\nfunction find( id ) { // eslint-disable-line stdlib/no-redeclare\n\tvar node = cache.first();\n\twhile ( node ) {\n\t\tif ( node.value.id === id ) {\n\t\t\treturn node;\n\t\t}\n\t\tnode = node.next;\n\t}\n\treturn null;\n}\n\n\n// EXPORTS //\n\nmodule.exports = find;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// VARIABLES //\n\nvar COUNTER = -1; // TODO: consider another approach for unique identifier generation. For most cases, this should suffice; however, it is possible that two different libraries, both relying on separate copies of this package, may trigger id collisions in the event that instantiated instances were to interact (e.g., a consumer attempting to free an instance instantiated by another copy of the package, etc).\n\n\n// MAIN //\n\n/**\n* Generates a new identifier.\n*\n* @private\n* @returns {string} identifier\n*\n* @example\n* var v = id();\n* // returns <string>\n*/\nfunction id() {\n\tCOUNTER += 1;\n\treturn COUNTER.toString();\n}\n\n\n// EXPORTS //\n\nmodule.exports = id;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n/* eslint-disable no-restricted-syntax, no-invalid-this */\n\n'use strict';\n\n// MODULES //\n\nvar setReadOnlyAccessor = require( '@stdlib/utils-define-nonenumerable-read-only-accessor' );\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar setNonEnumerable = require( '@stdlib/utils-define-nonenumerable-property' );\nvar isCollection = require( '@stdlib/assert-is-collection' );\nvar isBoolean = require( '@stdlib/assert-is-boolean' ).isPrimitive;\nvar isInteger = require( '@stdlib/assert-is-integer' ).isPrimitive;\nvar isAccessorArray = require( '@stdlib/array-base-assert-is-accessor-array' );\nvar array2json = require( '@stdlib/array-to-json' );\nvar dtype = require( '@stdlib/array-dtype' );\nvar copy = require( '@stdlib/array-base-copy' );\nvar resolveGetter = require( '@stdlib/array-base-resolve-getter' );\nvar format = require( '@stdlib/string-format' );\nvar defaults = require( './defaults.js' );\nvar validate = require( './validate.js' );\nvar cache = require( './cache.js' );\nvar findArrayIndex = require( './find.js' );\nvar generateId = require( './id.js' );\n\n\n// MAIN //\n\n/**\n* Array index constructor.\n*\n* @param {Collection} x - input array\n* @param {Options} [options] - function options\n* @param {boolean} [options.persist=false] - boolean indicating whether to continue persisting an index object after first usage\n* @throws {TypeError} first argument must be an array-like object\n* @throws {TypeError} first argument must be a valid index array\n* @throws {TypeError} options argument must be an object\n* @throws {TypeError} must provide valid options\n* @returns {ArrayIndex} ArrayIndex instance\n*\n* @example\n* var Uint8Array = require( '@stdlib/array-uint8' );\n*\n* var x = new Uint8Array( [ 1, 0, 1, 0 ] );\n*\n* var idx = new ArrayIndex( x );\n* // returns <ArrayIndex>\n*/\nfunction ArrayIndex( x ) {\n\tvar opts;\n\tvar err;\n\tvar get;\n\tvar dt;\n\tvar t;\n\tvar v;\n\tif ( !(this instanceof ArrayIndex) ) {\n\t\tif ( arguments.length > 1 ) {\n\t\t\treturn new ArrayIndex( x, arguments[ 1 ] );\n\t\t}\n\t\treturn new ArrayIndex( x );\n\t}\n\tif ( !isCollection( x ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. First argument must be an array-like object. Value: `%s`.', x ) );\n\t}\n\tdt = dtype( x );\n\n\t// When provided a \"generic\" array or an array of an unknown data type, attempt to infer the type of index array...\n\tif ( dt === 'generic' || dt === null ) {\n\t\tget = resolveGetter( x );\n\t\tv = get( x, 0 );\n\n\t\t// Infer the \"type\" of index array from the first element...\n\t\tif ( isBoolean( v ) ) {\n\t\t\tt = 'bool';\n\t\t} else if ( isInteger( v ) ) {\n\t\t\tt = 'int';\n\t\t} else {\n\t\t\tthrow new TypeError( 'invalid argument. First argument must be a valid index array.' );\n\t\t}\n\t} else if ( dt === 'int32' ) {\n\t\tt = 'int';\n\t} else if ( dt === 'uint8' ) {\n\t\tt = 'mask';\n\t} else {\n\t\tthrow new TypeError( 'invalid argument. First argument must be a valid index array.' );\n\t}\n\t// Resolve index options:\n\topts = defaults();\n\tif ( arguments.length > 1 ) {\n\t\terr = validate( opts, arguments[ 1 ] );\n\t\tif ( err ) {\n\t\t\tthrow err;\n\t\t}\n\t}\n\t// Add the array index to the index cache:\n\tcache.push({\n\t\t'id': generateId(),\n\t\t'ref': this,\n\t\t'data': x,\n\t\t'type': t,\n\t\t'dtype': dt,\n\t\t'persist': opts.persist\n\t});\n\n\t// Store a reference to the cache node:\n\tsetReadOnly( this, '_node', cache.last() );\n\n\t// Initialize a boolean flag indicating whether an array index object has been invalidated (i.e., freed):\n\tsetNonEnumerable( this, '_invalidated', false );\n\n\treturn this;\n}\n\n/**\n* Constructor name.\n*\n* @name name\n* @memberof ArrayIndex\n* @readonly\n* @type {string}\n* @default 'ArrayIndex'\n*\n* @example\n* var str = ArrayIndex.name;\n* // returns 'ArrayIndex'\n*/\nsetReadOnly( ArrayIndex, 'name', 'ArrayIndex' );\n\n/**\n* Frees an array index object associated with a provided identifier.\n*\n* @name free\n* @memberof ArrayIndex\n* @type {Function}\n* @param {string} id - identifier\n* @returns {boolean} boolean indicating whether an array index object was successfully freed\n*\n* @example\n* var Uint8Array = require( '@stdlib/array-uint8' );\n*\n* var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ), {\n* 'persist': true\n* });\n* // returns <ArrayIndex>\n*\n* // ...\n*\n* var out = ArrayIndex.free( idx.id );\n* // returns true\n*/\nsetReadOnly( ArrayIndex, 'free', function free( id ) {\n\tvar node;\n\tvar v;\n\n\t// Retrieve the array index object with the specified identifier:\n\tnode = findArrayIndex( id );\n\tif ( node === null ) {\n\t\treturn false;\n\t}\n\tv = node.value;\n\n\t// Invalidate the array instance object:\n\tsetReadOnly( v.ref, '_invalidated', true );\n\n\t// Remove the array instance from the cache:\n\tcache.remove( node );\n\n\t// Remove the reference to the original array:\n\tv.data = null;\n\n\treturn true;\n});\n\n/**\n* Returns the array associated with a provided identifier.\n*\n* @name get\n* @memberof ArrayIndex\n* @type {Function}\n* @param {string} id - identifier\n* @returns {(Object|null)} object containing array index data\n*\n* @example\n* var Uint8Array = require( '@stdlib/array-uint8' );\n*\n* var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ), {\n* 'persist': true\n* });\n* // returns <ArrayIndex>\n*\n* // ...\n*\n* var o = ArrayIndex.get( idx.id );\n* // returns {...}\n*\n* var d = o.data;\n* // returns <Uint8Array>[ 1, 0, 1, 0 ]\n*\n* var t = o.type;\n* // returns 'mask'\n*\n* var dt = o.dtype;\n* // returns 'uint8'\n*/\nsetReadOnly( ArrayIndex, 'get', function get( id ) {\n\tvar node;\n\tvar out;\n\tvar v;\n\n\t// Retrieve the array index object with the specified identifier:\n\tnode = findArrayIndex( id );\n\tif ( node === null ) {\n\t\treturn null;\n\t}\n\tv = node.value;\n\n\t// Assemble the output object:\n\tout = {\n\t\t'data': v.data,\n\t\t'type': v.type,\n\t\t'dtype': v.dtype\n\t};\n\n\t// If the array index object should not be persisted, go ahead and remove the object from the cache...\n\tif ( !v.persist ) {\n\t\tArrayIndex.free( id ); // note: this should come last, after having retrieved all desired array index node data\n\t}\n\treturn out;\n});\n\n/**\n* Returns the underlying array data of array index object.\n*\n* @name data\n* @memberof ArrayIndex.prototype\n* @readonly\n* @type {Collection}\n* @throws {Error} array index is no longer valid\n*\n* @example\n* var Uint8Array = require( '@stdlib/array-uint8' );\n*\n* var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) );\n* // returns <ArrayIndex>\n*\n* var v = idx.data;\n* // returns <Uint8Array>[ 1, 0, 1, 0 ]\n*/\nsetReadOnlyAccessor( ArrayIndex.prototype, 'data', function get() {\n\tif ( this._invalidated ) {\n\t\tthrow new Error( 'invalid operation. This array index instance has already been freed and can no longer be used.' );\n\t}\n\treturn this._node.value.data;\n});\n\n/**\n* Returns the underlying array data type of array index object.\n*\n* @name dtype\n* @memberof ArrayIndex.prototype\n* @readonly\n* @type {string}\n* @throws {Error} array index is no longer valid\n*\n* @example\n* var Uint8Array = require( '@stdlib/array-uint8' );\n*\n* var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) );\n* // returns <ArrayIndex>\n*\n* var t = idx.dtype;\n* // returns 'uint8'\n*/\nsetReadOnlyAccessor( ArrayIndex.prototype, 'dtype', function get() {\n\tif ( this._invalidated ) {\n\t\tthrow new Error( 'invalid operation. This array index instance has already been freed and can no longer be used.' );\n\t}\n\treturn this._node.value.dtype;\n});\n\n/**\n* Returns the identifier associated with an array index object.\n*\n* @name id\n* @memberof ArrayIndex.prototype\n* @readonly\n* @type {string}\n* @throws {Error} array index is no longer valid\n*\n* @example\n* var Uint8Array = require( '@stdlib/array-uint8' );\n*\n* var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) );\n* // returns <ArrayIndex>\n*\n* var id = idx.id;\n* // returns <string>\n*/\nsetReadOnlyAccessor( ArrayIndex.prototype, 'id', function get() {\n\tif ( this._invalidated ) {\n\t\tthrow new Error( 'invalid operation. This array index instance has already been freed and can no longer be used.' );\n\t}\n\treturn this._node.value.id;\n});\n\n/**\n* Returns a boolean indicating if an array index is actively cached.\n*\n* @name isCached\n* @memberof ArrayIndex.prototype\n* @readonly\n* @type {boolean}\n*\n* @example\n* var Uint8Array = require( '@stdlib/array-uint8' );\n*\n* var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) );\n* // returns <ArrayIndex>\n*\n* var out = idx.isCached;\n* // returns true\n*/\nsetReadOnlyAccessor( ArrayIndex.prototype, 'isCached', function get() {\n\treturn !this._invalidated;\n});\n\n/**\n* Returns the type of array index object.\n*\n* @name type\n* @memberof ArrayIndex.prototype\n* @readonly\n* @type {string}\n* @throws {Error} array index is no longer valid\n*\n* @example\n* var Uint8Array = require( '@stdlib/array-uint8' );\n*\n* var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) );\n* // returns <ArrayIndex>\n*\n* var t = idx.type;\n* // returns 'mask'\n*/\nsetReadOnlyAccessor( ArrayIndex.prototype, 'type', function get() {\n\tif ( this._invalidated ) {\n\t\tthrow new Error( 'invalid operation. This array index instance has already been freed and can no longer be used.' );\n\t}\n\treturn this._node.value.type;\n});\n\n/**\n* Serializes an array index object to a string.\n*\n* @name toString\n* @memberof ArrayIndex.prototype\n* @type {Function}\n* @throws {Error} array index is no longer valid\n* @returns {string} serialized array index object\n*\n* @example\n* var Uint8Array = require( '@stdlib/array-uint8' );\n*\n* var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) );\n* // returns <ArrayIndex>\n*\n* var str = idx.toString();\n* // e.g., 'ArrayIndex<0>'\n*/\nsetReadOnly( ArrayIndex.prototype, 'toString', function toString() {\n\tvar v;\n\tif ( this._invalidated ) {\n\t\tthrow new Error( 'invalid operation. This array index instance has already been freed and can no longer be used.' );\n\t}\n\tv = this._node.value;\n\treturn 'ArrayIndex<' + v.id + '>';\n});\n\n/**\n* Serializes an array index object as a JSON object.\n*\n* ## Notes\n*\n* - `JSON.stringify()` implicitly calls this method when stringifying an `ArrayIndex` instance.\n*\n* @name toJSON\n* @memberof ArrayIndex.prototype\n* @type {Function}\n* @throws {Error} array index is no longer valid\n* @returns {Object} serialized array index object\n*\n* @example\n* var Uint8Array = require( '@stdlib/array-uint8' );\n*\n* var idx = new ArrayIndex( new Uint8Array( [ 1, 0, 1, 0 ] ) );\n* // returns <ArrayIndex>\n*\n* var o = idx.toJSON();\n* // returns { 'type': 'ArrayIndex', 'data': { 'type': 'Uint8Array', 'data': [ 1, 0, 1, 0 ] } }\n*/\nsetReadOnly( ArrayIndex.prototype, 'toJSON', function toJSON() {\n\tvar v;\n\tvar o;\n\tif ( this._invalidated ) {\n\t\tthrow new Error( 'invalid operation. This array index instance has already been freed and can no longer be used.' );\n\t}\n\tv = this._node.value;\n\tif ( v.dtype === 'generic' || v.dtype === null ) {\n\t\tif ( isAccessorArray( v.data ) ) {\n\t\t\to = copy( v.data );\n\t\t} else {\n\t\t\to = v.data;\n\t\t}\n\t} else {\n\t\to = array2json( v.data );\n\t}\n\treturn {\n\t\t'type': 'ArrayIndex',\n\t\t'data': o\n\t};\n});\n\n\n// EXPORTS //\n\nmodule.exports = ArrayIndex;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2024 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Array index constructor.\n*\n* @module @stdlib/array-index\n*\n* @example\n* var Uint8Array = require( '@stdlib/array-uint8' );\n* var ArrayIndex = require( '@stdlib/array-index' );\n*\n* var x = new Uint8Array( [ 1, 0, 1, 0 ] );\n*\n* var idx = new ArrayIndex( x );\n* // returns <ArrayIndex>\n*/\n\n// MODULES //\n\nvar main = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"],
|
|
5
|
+
"mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAgCA,SAASC,G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AK,aACT,MAAM,IAAI,MAAO,gGAAiG,EAEnH,OAAAA,EAAI,KAAK,MAAM,MACR,cAAgBA,EAAE,GAAK,GAC/B,CAAC,EAwBDvB,EAAagB,EAAW,UAAW,SAAU,UAAkB,CAC9D,IAAIO,EACAI,EACJ,GAAK,KAAK,aACT,MAAM,IAAI,MAAO,gGAAiG,EAEnH,OAAAJ,EAAI,KAAK,MAAM,MACVA,EAAE,QAAU,WAAaA,EAAE,QAAU,KACpClB,EAAiBkB,EAAE,IAAK,EAC5BI,EAAInB,EAAMe,EAAE,IAAK,EAEjBI,EAAIJ,EAAE,KAGPI,EAAIrB,EAAYiB,EAAE,IAAK,EAEjB,CACN,KAAQ,aACR,KAAQI,CACT,CACD,CAAC,EAKD7B,EAAO,QAAUkB,ICrZjB,IAAIY,EAAO,IAKX,OAAO,QAAUA",
|
|
6
|
+
"names": ["require_defaults", "__commonJSMin", "exports", "module", "defaults", "require_validate", "__commonJSMin", "exports", "module", "isObject", "hasOwnProp", "isBoolean", "format", "validate", "opts", "options", "require_cache", "__commonJSMin", "exports", "module", "LinkedList", "cache", "require_find", "__commonJSMin", "exports", "module", "cache", "find", "id", "node", "require_id", "__commonJSMin", "exports", "module", "COUNTER", "id", "require_main", "__commonJSMin", "exports", "module", "setReadOnlyAccessor", "setReadOnly", "setNonEnumerable", "isCollection", "isBoolean", "isInteger", "isAccessorArray", "array2json", "dtype", "copy", "resolveGetter", "format", "defaults", "validate", "cache", "findArrayIndex", "generateId", "ArrayIndex", "x", "opts", "err", "get", "dt", "t", "v", "id", "node", "out", "o", "main"]
|
|
7
|
+
}
|