Skip to content

Commit ff3b62e

Browse files
committed
Auto-generated commit
1 parent 5e92601 commit ff3b62e

File tree

7 files changed

+139
-66
lines changed

7 files changed

+139
-66
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
55
<section class="release" id="unreleased">
66

7-
## Unreleased (2024-06-18)
7+
## Unreleased (2024-06-19)
88

99
<section class="features">
1010

1111
### Features
1212

13+
- [`550285b`](https://github.com/stdlib-js/stdlib/commit/550285bf4a57dd3dabc8ed3998e7b612515fe082) - add support for boolean and mask array assignment
1314
- [`b079d65`](https://github.com/stdlib-js/stdlib/commit/b079d653226019925581555fdaf9aa927ec69c0e) - add support for integer array indexing assignment
1415
- [`96e896a`](https://github.com/stdlib-js/stdlib/commit/96e896a39be08912b2e06dfb6b671ec13d042412) - add support for boolean array indices
1516

@@ -23,6 +24,7 @@
2324

2425
<details>
2526

27+
- [`550285b`](https://github.com/stdlib-js/stdlib/commit/550285bf4a57dd3dabc8ed3998e7b612515fe082) - **feat:** add support for boolean and mask array assignment _(by Athan Reines)_
2628
- [`074cbef`](https://github.com/stdlib-js/stdlib/commit/074cbef3f9d616c37c2be856a949e257481ff2fc) - **docs:** add note concerning broadcasting of nested array elements _(by Athan Reines)_
2729
- [`b079d65`](https://github.com/stdlib-js/stdlib/commit/b079d653226019925581555fdaf9aa927ec69c0e) - **feat:** add support for integer array indexing assignment _(by Athan Reines)_
2830
- [`96e896a`](https://github.com/stdlib-js/stdlib/commit/96e896a39be08912b2e06dfb6b671ec13d042412) - **feat:** add support for boolean array indices _(by Athan Reines)_

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,35 @@ z = y[ i ];
555555
i = idx( new Int32Array( [ 0, 0, 1, 1, 2, 2 ] ) ); // integer index array
556556
z = y[ i ];
557557
// returns [ 1, 1, 2, 2, -10, -10 ]
558+
559+
// Array index assignment:
560+
x = [ 1, 2, 3, 4, 5, 6 ];
561+
y = array2fancy( x );
562+
563+
i = idx( [ true, false, true, false, true, false ] ); // boolean array
564+
y[ i ] = 5;
565+
z = y[ ':' ];
566+
// returns [ 5, 2, 5, 4, 5, 6 ]
567+
568+
i = idx( new BooleanArray( [ true, false, true, false, true, false ] ) ); // boolean array
569+
y[ i ] = 7;
570+
z = y[ ':' ];
571+
// returns [ 7, 2, 7, 4, 7, 6 ]
572+
573+
i = idx( new Uint8Array( [ 1, 1, 1, 0, 0, 0 ] ) ); // mask array
574+
y[ i ] = 8;
575+
z = y[ ':' ];
576+
// returns [ 7, 2, 7, 8, 8, 8 ]
577+
578+
i = idx( new Int32Array( [ 5, 3, 2 ] ) ); // integer index array
579+
y[ i ] = [ 9, 10, 11 ];
580+
z = y[ ':' ];
581+
// returns [ 7, 2, 11, 10, 8, 9 ]
582+
583+
i = idx( [ 0, 1 ] ); // integer index array
584+
y[ i ] = -1;
585+
z = y[ ':' ];
586+
// returns [ -1, -1, 11, 10, 8, 9 ]
558587
```
559588

560589
</section>

dist/index.js

Lines changed: 57 additions & 57 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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/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
}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,16 @@
4040
"@stdlib/array-base-arraylike2object": "^0.2.1",
4141
"@stdlib/array-base-assert-is-boolean-data-type": "github:stdlib-js/array-base-assert-is-boolean-data-type#main",
4242
"@stdlib/array-base-assert-is-complex-floating-point-data-type": "^0.2.1",
43+
"@stdlib/array-base-assert-is-mostly-safe-data-type-cast": "^0.2.1",
4344
"@stdlib/array-base-assert-is-real-floating-point-data-type": "^0.2.1",
4445
"@stdlib/array-base-assert-is-safe-data-type-cast": "^0.3.1",
4546
"@stdlib/array-base-assert-is-signed-integer-data-type": "^0.2.1",
4647
"@stdlib/array-base-assert-is-unsigned-integer-data-type": "^0.2.1",
4748
"@stdlib/array-base-fancy-slice": "^0.3.1",
4849
"@stdlib/array-base-fancy-slice-assign": "^0.2.1",
4950
"@stdlib/array-base-min-signed-integer-dtype": "^0.2.1",
51+
"@stdlib/array-base-where": "github:stdlib-js/array-base-where#main",
52+
"@stdlib/array-dtype": "^0.2.1",
5053
"@stdlib/array-from-scalar": "^0.2.1",
5154
"@stdlib/array-index": "^0.2.1",
5255
"@stdlib/array-min-dtype": "^0.2.1",

0 commit comments

Comments
 (0)