Skip to content

Commit b079d65

Browse files
committed
feat: add support for integer array indexing assignment
1 parent b834046 commit b079d65

File tree

13 files changed

+2256
-118
lines changed

13 files changed

+2256
-118
lines changed

lib/node_modules/@stdlib/array/to-fancy/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ im = imag( v );
456456
```javascript
457457
var Uint8Array = require( '@stdlib/array/uint8' );
458458
var Int32Array = require( '@stdlib/array/int32' );
459+
var BooleanArray = require( '@stdlib/array/bool' );
459460
var array2fancy = require( '@stdlib/array/to-fancy' );
460461

461462
var x = [ 1, 2, 3, 4, 5, 6 ];
@@ -492,6 +493,10 @@ i = idx( [ true, false, false, true, true, true ] ); // boolean array
492493
z = y[ i ];
493494
// returns [ 1, -9, -8, 6 ]
494495

496+
i = idx( new BooleanArray( [ true, false, false, true, true, true ] ) ); // boolean array
497+
z = y[ i ];
498+
// returns [ 1, -9, -8, 6 ]
499+
495500
i = idx( new Uint8Array( [ 0, 0, 1, 0, 0, 1 ] ) ); // mask array
496501
z = y[ i ];
497502
// returns [ 1, 2, -9, -8 ]

lib/node_modules/@stdlib/array/to-fancy/benchmark/benchmark.set.js

+38
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,41 @@ bench( pkg+'::set,subsequence:len=1', opts, function benchmark( b ) {
155155
b.pass( 'benchmark finished' );
156156
b.end();
157157
});
158+
159+
bench( pkg+'::set,integer_array:len=1', opts, function benchmark( b ) {
160+
var values;
161+
var base;
162+
var opts;
163+
var idx;
164+
var x;
165+
var v;
166+
var i;
167+
168+
base = zeroTo( 100, 'generic' );
169+
x = array2fancy( base );
170+
171+
opts = {
172+
'persist': true
173+
};
174+
values = [
175+
array2fancy.idx( [ 0 ], opts ),
176+
array2fancy.idx( [ 1 ], opts ),
177+
array2fancy.idx( [ 2 ], opts )
178+
];
179+
180+
b.tic();
181+
for ( i = 0; i < b.iterations; i++ ) {
182+
idx = values[ i%values.length ];
183+
x[ idx ] = i * 2;
184+
v = base[ i%3 ];
185+
if ( v !== v ) {
186+
b.fail( 'should not return NaN' );
187+
}
188+
}
189+
b.toc();
190+
if ( isnan( v ) ) {
191+
b.fail( 'should not return NaN' );
192+
}
193+
b.pass( 'benchmark finished' );
194+
b.end();
195+
});

lib/node_modules/@stdlib/array/to-fancy/docs/types/index.d.ts

+2-112
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
/// <reference types="@stdlib/types"/>
2222

23-
import { Collection, ArrayLike, AccessorArrayLike, Complex128Array, Complex64Array, ArrayIndexObject } from '@stdlib/types/array';
23+
import { Collection, ArrayLike, AccessorArrayLike, ComplexTypedArray, TypedArray, BooleanTypedArray, ArrayIndexObject } from '@stdlib/types/array';
2424
import ArrayIndex = require( '@stdlib/array/index' );
2525

2626
/**
@@ -74,17 +74,6 @@ interface Array2Fancy {
7474
*
7575
* var v = y[ ':' ];
7676
* // returns <Float64Array>[ 1.0, 2.0, 3.0, 4.0 ]
77-
*/
78-
( x: Float64Array, options?: Options ): Float64Array;
79-
80-
/**
81-
* Converts an array to an object supporting fancy indexing.
82-
*
83-
* @param x - input array
84-
* @param options - function options
85-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
86-
* @param options.cache - cache for resolving array index objects
87-
* @returns fancy array
8877
*
8978
* @example
9079
* var Float32Array = require( '@stdlib/array/float32' );
@@ -96,17 +85,6 @@ interface Array2Fancy {
9685
*
9786
* var v = y[ ':' ];
9887
* // returns <Float32Array>[ 1.0, 2.0, 3.0, 4.0 ]
99-
*/
100-
( x: Float32Array, options?: Options ): Float32Array;
101-
102-
/**
103-
* Converts an array to an object supporting fancy indexing.
104-
*
105-
* @param x - input array
106-
* @param options - function options
107-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
108-
* @param options.cache - cache for resolving array index objects
109-
* @returns fancy array
11088
*
11189
* @example
11290
* var Complex128Array = require( '@stdlib/array/complex128' );
@@ -118,17 +96,6 @@ interface Array2Fancy {
11896
*
11997
* var v = y[ ':' ];
12098
* // returns <Complex128Array>[ 1.0, 2.0, 3.0, 4.0 ]
121-
*/
122-
( x: Complex128Array, options?: Options ): Complex128Array;
123-
124-
/**
125-
* Converts an array to an object supporting fancy indexing.
126-
*
127-
* @param x - input array
128-
* @param options - function options
129-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
130-
* @param options.cache - cache for resolving array index objects
131-
* @returns fancy array
13299
*
133100
* @example
134101
* var Complex64Array = require( '@stdlib/array/complex64' );
@@ -140,17 +107,6 @@ interface Array2Fancy {
140107
*
141108
* var v = y[ ':' ];
142109
* // returns <Complex64Array>[ 1.0, 2.0, 3.0, 4.0 ]
143-
*/
144-
( x: Complex64Array, options?: Options ): Complex64Array;
145-
146-
/**
147-
* Converts an array to an object supporting fancy indexing.
148-
*
149-
* @param x - input array
150-
* @param options - function options
151-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
152-
* @param options.cache - cache for resolving array index objects
153-
* @returns fancy array
154110
*
155111
* @example
156112
* var Int32Array = require( '@stdlib/array/int32' );
@@ -162,17 +118,6 @@ interface Array2Fancy {
162118
*
163119
* var v = y[ ':' ];
164120
* // returns <Int32Array>[ 1, 2, 3, 4 ]
165-
*/
166-
( x: Int32Array, options?: Options ): Int32Array;
167-
168-
/**
169-
* Converts an array to an object supporting fancy indexing.
170-
*
171-
* @param x - input array
172-
* @param options - function options
173-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
174-
* @param options.cache - cache for resolving array index objects
175-
* @returns fancy array
176121
*
177122
* @example
178123
* var Int16Array = require( '@stdlib/array/int16' );
@@ -184,17 +129,6 @@ interface Array2Fancy {
184129
*
185130
* var v = y[ ':' ];
186131
* // returns <Int16Array>[ 1, 2, 3, 4 ]
187-
*/
188-
( x: Int16Array, options?: Options ): Int16Array;
189-
190-
/**
191-
* Converts an array to an object supporting fancy indexing.
192-
*
193-
* @param x - input array
194-
* @param options - function options
195-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
196-
* @param options.cache - cache for resolving array index objects
197-
* @returns fancy array
198132
*
199133
* @example
200134
* var Int8Array = require( '@stdlib/array/int8' );
@@ -206,17 +140,6 @@ interface Array2Fancy {
206140
*
207141
* var v = y[ ':' ];
208142
* // returns <Int8Array>[ 1, 2, 3, 4 ]
209-
*/
210-
( x: Int8Array, options?: Options ): Int8Array;
211-
212-
/**
213-
* Converts an array to an object supporting fancy indexing.
214-
*
215-
* @param x - input array
216-
* @param options - function options
217-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
218-
* @param options.cache - cache for resolving array index objects
219-
* @returns fancy array
220143
*
221144
* @example
222145
* var Uint32Array = require( '@stdlib/array/uint32' );
@@ -228,17 +151,6 @@ interface Array2Fancy {
228151
*
229152
* var v = y[ ':' ];
230153
* // returns <Uint32Array>[ 1, 2, 3, 4 ]
231-
*/
232-
( x: Uint32Array, options?: Options ): Uint32Array;
233-
234-
/**
235-
* Converts an array to an object supporting fancy indexing.
236-
*
237-
* @param x - input array
238-
* @param options - function options
239-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
240-
* @param options.cache - cache for resolving array index objects
241-
* @returns fancy array
242154
*
243155
* @example
244156
* var Uint16Array = require( '@stdlib/array/uint16' );
@@ -250,17 +162,6 @@ interface Array2Fancy {
250162
*
251163
* var v = y[ ':' ];
252164
* // returns <Uint16Array>[ 1, 2, 3, 4 ]
253-
*/
254-
( x: Uint16Array, options?: Options ): Uint16Array;
255-
256-
/**
257-
* Converts an array to an object supporting fancy indexing.
258-
*
259-
* @param x - input array
260-
* @param options - function options
261-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
262-
* @param options.cache - cache for resolving array index objects
263-
* @returns fancy array
264165
*
265166
* @example
266167
* var Uint8Array = require( '@stdlib/array/uint8' );
@@ -272,17 +173,6 @@ interface Array2Fancy {
272173
*
273174
* var v = y[ ':' ];
274175
* // returns <Uint8Array>[ 1, 2, 3, 4 ]
275-
*/
276-
( x: Uint8Array, options?: Options ): Uint8Array;
277-
278-
/**
279-
* Converts an array to an object supporting fancy indexing.
280-
*
281-
* @param x - input array
282-
* @param options - function options
283-
* @param options.strict - boolean indicating whether to enforce strict bounds checking
284-
* @param options.cache - cache for resolving array index objects
285-
* @returns fancy array
286176
*
287177
* @example
288178
* var Uint8ClampedArray = require( '@stdlib/array/uint8c' );
@@ -295,7 +185,7 @@ interface Array2Fancy {
295185
* var v = y[ ':' ];
296186
* // returns <Uint8ClampedArray>[ 1, 2, 3, 4 ]
297187
*/
298-
( x: Uint8ClampedArray, options?: Options ): Uint8ClampedArray;
188+
<T extends TypedArray | ComplexTypedArray | BooleanTypedArray>( x: T, options?: Options ): T;
299189

300190
/**
301191
* Converts an array to an object supporting fancy indexing.

lib/node_modules/@stdlib/array/to-fancy/docs/types/test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import array2fancy = require( './index' );
3838
array2fancy( new Uint8ClampedArray( [ 1, 2, 3 ] ) ); // $ExpectType Uint8ClampedArray
3939
array2fancy( new Complex128Array( [ 1, 2, 3, 4, 5, 6 ] ) ); // $ExpectType Complex128Array
4040
array2fancy( new Complex64Array( [ 1, 2, 3, 4, 5, 6 ] ) ); // $ExpectType Complex64Array
41+
array2fancy( new BooleanArray( [ true, false, true ] ) ); // $ExpectType BooleanArray
4142

4243
const opts = {
4344
'strict': true
@@ -54,6 +55,7 @@ import array2fancy = require( './index' );
5455
array2fancy( new Uint8ClampedArray( [ 1, 2, 3 ] ), opts ); // $ExpectType Uint8ClampedArray
5556
array2fancy( new Complex128Array( [ 1, 2, 3, 4, 5, 6 ] ), opts ); // $ExpectType Complex128Array
5657
array2fancy( new Complex64Array( [ 1, 2, 3, 4, 5, 6 ] ), opts ); // $ExpectType Complex64Array
58+
array2fancy( new BooleanArray( [ true, false, true ] ), opts ); // $ExpectType BooleanArray
5759
}
5860

5961
// The compiler throws an error if the function is provided a first argument which is not an array-like value...

lib/node_modules/@stdlib/array/to-fancy/examples/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
var Uint8Array = require( '@stdlib/array/uint8' );
2222
var Int32Array = require( '@stdlib/array/int32' );
23+
var BooleanArray = require( '@stdlib/array/bool' );
2324
var array2fancy = require( './../lib' );
2425

2526
var x = [ 1, 2, 3, 4, 5, 6 ];
@@ -63,6 +64,11 @@ z = y[ i ];
6364
console.log( z );
6465
// => [ 1, -9, -8, 6 ]
6566

67+
i = idx( new BooleanArray( [ true, false, false, true, true, true ] ) ); // boolean array
68+
z = y[ i ];
69+
console.log( z );
70+
// => [ 1, -9, -8, 6 ]
71+
6672
i = idx( new Uint8Array( [ 0, 0, 1, 0, 0, 1 ] ) ); // mask array
6773
z = y[ i ];
6874
console.log( z );

lib/node_modules/@stdlib/array/to-fancy/lib/get_elements.js

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ var prop2array = require( './prop2array.js' );
3636
* @param {Object} target - target object
3737
* @param {string} property - index string
3838
* @param {Object} ctx - context object
39-
* @param {boolean} ctx.strict - boolean indicating whether to enforce strict bounds checking
4039
* @param {Object} ctx.cache - cache for resolving array index objects
4140
* @param {Function} ctx.postGetArray - function to process a retrieved array
4241
* @throws {Error} invalid array index

lib/node_modules/@stdlib/array/to-fancy/lib/set.js

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
2424
var hasProperty = require( '@stdlib/assert/has-property' );
2525
var isIntegerString = require( './is_integer_string.js' );
26+
var isArrayIndexString = require( './is_array_index_string.js' );
27+
var setElements = require( './set_elements.js' );
2628
var setElement = require( './set_element.js' );
2729
var setValue = require( './set_value.js' );
2830
var setSlice = require( './set_slice.js' );
@@ -71,6 +73,9 @@ function factory( ctx ) {
7173
if ( hasProperty( property ) || !isString( property ) ) {
7274
return setValue( target, property, value, ctx );
7375
}
76+
if ( isArrayIndexString( property ) ) {
77+
return setElements( target, property, value, ctx );
78+
}
7479
out = setSlice( target, property, value, receiver, ctx );
7580
if ( out ) {
7681
return out;

0 commit comments

Comments
 (0)