Skip to content

Commit 550285b

Browse files
committed
feat: add support for boolean and mask array assignment
1 parent ff8652b commit 550285b

3 files changed

Lines changed: 73 additions & 5 deletions

File tree

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,35 @@ z = y[ i ];
522522
i = idx( new Int32Array( [ 0, 0, 1, 1, 2, 2 ] ) ); // integer index array
523523
z = y[ i ];
524524
// returns [ 1, 1, 2, 2, -10, -10 ]
525+
526+
// Array index assignment:
527+
x = [ 1, 2, 3, 4, 5, 6 ];
528+
y = array2fancy( x );
529+
530+
i = idx( [ true, false, true, false, true, false ] ); // boolean array
531+
y[ i ] = 5;
532+
z = y[ ':' ];
533+
// returns [ 5, 2, 5, 4, 5, 6 ]
534+
535+
i = idx( new BooleanArray( [ true, false, true, false, true, false ] ) ); // boolean array
536+
y[ i ] = 7;
537+
z = y[ ':' ];
538+
// returns [ 7, 2, 7, 4, 7, 6 ]
539+
540+
i = idx( new Uint8Array( [ 1, 1, 1, 0, 0, 0 ] ) ); // mask array
541+
y[ i ] = 8;
542+
z = y[ ':' ];
543+
// returns [ 7, 2, 7, 8, 8, 8 ]
544+
545+
i = idx( new Int32Array( [ 5, 3, 2 ] ) ); // integer index array
546+
y[ i ] = [ 9, 10, 11 ];
547+
z = y[ ':' ];
548+
// returns [ 7, 2, 11, 10, 8, 9 ]
549+
550+
i = idx( [ 0, 1 ] ); // integer index array
551+
y[ i ] = -1;
552+
z = y[ ':' ];
553+
// returns [ -1, -1, 11, 10, 8, 9 ]
525554
```
526555

527556
</section>

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,32 @@ i = idx( new Int32Array( [ 0, 0, 1, 1, 2, 2 ] ) ); // integer index array
7878
z = y[ i ];
7979
console.log( z );
8080
// => [ 1, 1, 2, 2, -10, -10 ]
81+
82+
// Array index assignment:
83+
x = [ 1, 2, 3, 4, 5, 6 ];
84+
y = array2fancy( x );
85+
86+
i = idx( [ true, false, true, false, true, false ] ); // boolean array
87+
y[ i ] = 5;
88+
console.log( y );
89+
// => [ 5, 2, 5, 4, 5, 6 ]
90+
91+
i = idx( new BooleanArray( [ true, false, true, false, true, false ] ) ); // boolean array
92+
y[ i ] = 7;
93+
console.log( y );
94+
// => [ 7, 2, 7, 4, 7, 6 ]
95+
96+
i = idx( new Uint8Array( [ 1, 1, 1, 0, 0, 0 ] ) ); // mask array
97+
y[ i ] = 8;
98+
console.log( y );
99+
// => [ 7, 2, 7, 8, 8, 8 ]
100+
101+
i = idx( new Int32Array( [ 5, 3, 2 ] ) ); // integer index array
102+
y[ i ] = [ 9, 10, 11 ];
103+
console.log( y );
104+
// => [ 7, 2, 11, 10, 8, 9 ]
105+
106+
i = idx( [ 0, 1 ] ); // integer index array
107+
y[ i ] = -1;
108+
console.log( y );
109+
// => [ -1, -1, 11, 10, 8, 9 ]

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020

2121
// MODULES //
2222

23+
var isMostlySafeCast = require( '@stdlib/array/base/assert/is-mostly-safe-data-type-cast' );
2324
var isCollection = require( '@stdlib/assert/is-collection' );
2425
var scalar2array = require( '@stdlib/array/from-scalar' );
26+
var dtype = require( '@stdlib/array/dtype' );
2527
var put = require( '@stdlib/array/put' );
28+
var where = require( '@stdlib/array/base/where' ).assign;
2629
var format = require( '@stdlib/string/format' );
2730
var prop2array = require( './prop2array.js' );
2831
var errMessage = require( './error_message.js' );
@@ -52,12 +55,14 @@ var errMessage = require( './error_message.js' );
5255
function setElements( target, property, value, ctx ) {
5356
var idx;
5457
var err;
58+
var dt;
5559
var v;
5660

5761
idx = prop2array( property, ctx.cache );
5862
if ( isCollection( value ) ) {
5963
// When handling collections, we delegate to implementation APIs (see below) to perform argument validation (e.g., ensuring a (mostly) safe cast, broadcast compatibility, etc), so we just reassign the value here:
6064
v = value;
65+
dt = dtype( value ) || 'generic';
6166
} else {
6267
// When provided a "scalar", we need to check whether the value can be safely cast to the target array data type:
6368
err = ctx.validator( value, ctx.dtype );
@@ -71,22 +76,27 @@ function setElements( target, property, value, ctx ) {
7176
}
7277
// As the scalar can be safely cast, convert the scalar to an array having the same data type as the target array to allow for broadcasting during assignment:
7378
v = scalar2array( v, ctx.dtype );
79+
dt = ctx.dtype;
7480
}
7581
if ( idx.type === 'int' ) {
7682
try {
77-
put( target, idx.data, v );
83+
put( target, idx.data, v ); // note: defer to `put` for ensuring a mostly safe cast
7884
} catch ( err ) {
7985
throw new err.constructor( errMessage( err.message ) );
8086
}
8187
return true;
8288
}
89+
// Safe casts are always allowed and allow same kind casts (i.e., downcasts) only when the target array data type is floating-point...
90+
if ( !isMostlySafeCast( dt, ctx.dtype ) ) {
91+
throw new TypeError( format( 'invalid operation. Assigned value cannot be safely cast to the target array data type. Data types: [%s, %s].', dt, ctx.dtype ) );
92+
}
8393
if ( idx.type === 'bool' ) {
84-
// FIXME: where( idx.data, target, value );
85-
return false;
94+
where( idx.data, v, target, target, 1, 0 );
95+
return true;
8696
}
8797
if ( idx.type === 'mask' ) {
88-
// FIXME: where( idx.data, value, target );
89-
return false;
98+
where( idx.data, target, v, target, 1, 0 );
99+
return true;
90100
}
91101
throw new Error( format( 'invalid operation. Unrecognized array index type. Value: `%s`.', idx.type ) );
92102
}

0 commit comments

Comments
 (0)