Skip to content

Commit dc29f10

Browse files
committed
feat: add "broadcasting" mode to require broadcast compatibility
1 parent 20cd086 commit dc29f10

7 files changed

Lines changed: 82 additions & 31 deletions

File tree

lib/node_modules/@stdlib/array/base/mskput/README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,33 @@ The function supports the following parameters:
5454
The function supports the following modes:
5555

5656
- `'throw'`: specifies that the function must raise an exception when the function is provided insufficient `values` to satisfy the `mask` array.
57+
- `'broadcast'`: specifies that the function must broadcast a single-element `values` array and otherwise raise an exception when the function is provided insufficient `values` to satisfy the `mask` array.
5758
- `'repeat'`: specifies that the function must reuse provided `values` when replacing elements in `x` in order to satisfy the `mask` array.
5859

59-
When `mode` is equal to `'repeat`', the function supports broadcasting a `values` array containing a single element against the number of falsy values in the `mask` array.
60+
When `mode` is equal to `'broadcast`', the function supports broadcasting a `values` array containing a single element against the number of falsy values in the `mask` array.
6061

6162
```javascript
6263
var x = [ 1, 2, 3, 4 ];
6364

64-
var out = mskput( x, [ 1, 0, 1, 0 ], [ 20 ], 'repeat' );
65+
var out = mskput( x, [ 1, 0, 1, 0 ], [ 20 ], 'broadcast' );
6566
// returns [ 1, 20, 3, 20 ]
6667

6768
var bool = ( out === x );
6869
// returns true
6970
```
7071

72+
When `mode` is equal to `repeat`, the function supports recycling elements in a `values` array to satisfy the number of falsy values in the `mask` array.
73+
74+
```javascript
75+
var x = [ 1, 2, 3, 4 ];
76+
77+
var out = mskput( x, [ 0, 0, 1, 0 ], [ 20, 40 ], 'repeat' );
78+
// returns [ 20, 40, 3, 20 ]
79+
80+
var bool = ( out === x );
81+
// returns true
82+
```
83+
7184
</section>
7285

7386
<!-- /.usage -->

lib/node_modules/@stdlib/array/base/mskput/benchmark/benchmark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ bench( pkg+'::broadcasting:len=100', function benchmark( b ) {
6565

6666
b.tic();
6767
for ( i = 0; i < b.iterations; i++ ) {
68-
v = mskput( x, mask, [ i ], 'repeat' );
68+
v = mskput( x, mask, [ i ], 'broadcast' );
6969
if ( typeof v !== 'object' ) {
7070
b.fail( 'should return an array' );
7171
}

lib/node_modules/@stdlib/array/base/mskput/docs/repl.txt

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
Replaces elements of an array with provided values according to a provided
44
mask array.
55

6-
When the `mode` is `'repeat'`, the function supports broadcasting a `values`
7-
array containing a single element against the number of falsy values in the
8-
`mask` array.
6+
When the `mode` is `'broadcast'`, the function supports broadcasting a
7+
`values` array containing a single element against the number of falsy
8+
values in the `mask` array.
9+
10+
When the `mode` is `'repeat'`, the function supports recycling elements in a
11+
`values` array to satisfy the number of falsy values in the `mask` array.
912

1013
The function mutates the input array.
1114

@@ -23,12 +26,15 @@
2326
Values to set.
2427

2528
mode: string
26-
String specifying whether to raise an exception when the number of
27-
values to set is less than the number of falsy mask values. The function
28-
supports the following modes:
29+
String specifying behavior when the number of values to set is less than
30+
the number of falsy mask values. The function supports the following
31+
modes:
2932

3033
- 'throw': specifies that the function must raise an exception when the
3134
function is provided insufficient values to satisfy the mask array.
35+
- 'broadcast': specifies that the function must broadcast a single-
36+
element values array and otherwise raise an exception when the function
37+
is provided insufficient values to satisfy the mask array.
3238
- 'repeat': specifies that the function must reuse provided values when
3339
replacing elements in `x` in order to satisfy the mask array.
3440

lib/node_modules/@stdlib/array/base/mskput/docs/types/index.d.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,25 @@ type MaskArray = Collection | AccessorArrayLike<any>;
3434
type ValuesArray<T> = Collection<T> | AccessorArrayLike<T>;
3535

3636
/**
37-
* Mode specifying whether to raise an exception when the number of values to set is less than the number of falsy values in the mask array.
37+
* Mode specifying behavior when the number of values to set is less than the number of falsy values in the mask array.
3838
*
3939
* ## Notes
4040
*
4141
* - The function supports the following modes:
4242
*
4343
* - `'throw'`: specifies that the function must raise an exception when the function is provided insufficient `values` to satisfy the `mask` array.
44+
* - `'broadcast'`: specifies that the function must broadcast a single-element `values` array and otherwise raise an exception when the function is provided insufficient `values` to satisfy the `mask` array.
4445
* - `'repeat'`: specifies that the function must reuse provided `values` when replacing elements in `x` in order to satisfy the `mask` array.
4546
*/
46-
type Mode = 'throw' | 'repeat';
47+
type Mode = 'throw' | 'broadcast' | 'repeat';
4748

4849
/**
4950
* Replaces elements of an array with provided values according to a provided mask array.
5051
*
5152
* @param x - input array
5253
* @param mask - mask array
5354
* @param values - values to set
54-
* @param mode - string specifying whether to raise an exception when the number of values is less than the number of falsy values in the mask array
55+
* @param mode - string specifying behavior when the number of values is less than the number of falsy values in the mask array
5556
* @returns input array
5657
*
5758
* @example
@@ -73,7 +74,7 @@ type Mode = 'throw' | 'repeat';
7374
*
7475
* var x = new Int32Array( [ 1, 2, 3, 4 ] );
7576
*
76-
* var out = mskput( x, [ 1, 0, 0, 1 ], [ 30 ], 'repeat' );
77+
* var out = mskput( x, [ 1, 0, 0, 1 ], [ 30 ], 'broadcast' );
7778
* // returns <Int32Array>[ 1, 30, 30, 4 ]
7879
*
7980
* var bool = ( out === x );
@@ -87,7 +88,7 @@ declare function mskput<T extends TypedArray | BooleanTypedArray, U = unknown>(
8788
* @param x - input array
8889
* @param mask - mask array
8990
* @param values - values to set
90-
* @param mode - string specifying whether to raise an exception when the number of values is less than the number of falsy values in the mask array
91+
* @param mode - string specifying behavior when the number of values is less than the number of falsy values in the mask array
9192
* @returns input array
9293
*
9394
* @example
@@ -112,7 +113,7 @@ declare function mskput<T extends TypedArray | BooleanTypedArray, U = unknown>(
112113
* var mask = [ 1, 0, 0, 1 ];
113114
* var values = new Complex128Array( [ 20.0, 30.0 ] );
114115
*
115-
* var out = mskput( x, mask, values, 'repeat' );
116+
* var out = mskput( x, mask, values, 'broadcast' );
116117
* // returns <Complex128Array>
117118
*
118119
* var bool = ( out === x );
@@ -126,7 +127,7 @@ declare function mskput<T extends ComplexTypedArray>( x: T, mask: MaskArray, val
126127
* @param x - input array
127128
* @param mask - mask array
128129
* @param values - values to set
129-
* @param mode - string specifying whether to raise an exception when the number of values is less than the number of falsy values in the mask array
130+
* @param mode - string specifying behavior when the number of values is less than the number of falsy values in the mask array
130131
* @returns input array
131132
*
132133
* @example
@@ -144,7 +145,7 @@ declare function mskput<T extends ComplexTypedArray>( x: T, mask: MaskArray, val
144145
* @example
145146
* var x = [ 1, 2, 3, 4 ];
146147
*
147-
* var out = mskput( x, [ 1, 0, 0, 1 ], [ 30 ], 'repeat' );
148+
* var out = mskput( x, [ 1, 0, 0, 1 ], [ 30 ], 'broadcast' );
148149
* // returns [ 1, 30, 30, 4 ]
149150
*
150151
* var bool = ( out === x );
@@ -158,7 +159,7 @@ declare function mskput<T = unknown, U = unknown>( x: Array<T>, mask: MaskArray,
158159
* @param x - input array
159160
* @param mask - mask array
160161
* @param values - values to set
161-
* @param mode - string specifying whether to raise an exception when the number of values is less than the number of falsy values in the mask array
162+
* @param mode - string specifying behavior when the number of values is less than the number of falsy values in the mask array
162163
* @returns input array
163164
*
164165
* @example
@@ -179,7 +180,7 @@ declare function mskput<T = unknown, U = unknown>( x: Array<T>, mask: MaskArray,
179180
*
180181
* var x = toAccessorArray( [ 1, 2, 3, 4 ] );
181182
*
182-
* var out = mskput( x, [ 1, 0, 0, 1 ], [ 30 ], 'repeat' );
183+
* var out = mskput( x, [ 1, 0, 0, 1 ], [ 30 ], 'broadcast' );
183184
*
184185
* var bool = ( out === x );
185186
* // returns true
@@ -192,7 +193,7 @@ declare function mskput<T = unknown, U = unknown>( x: AccessorArrayLike<T>, mask
192193
* @param x - input array
193194
* @param mask - mask array
194195
* @param values - values to set
195-
* @param mode - string specifying whether to raise an exception when the number of values is less than the number of falsy values in the mask array
196+
* @param mode - string specifying behavior when the number of values is less than the number of falsy values in the mask array
196197
* @returns input array
197198
*
198199
* @example

lib/node_modules/@stdlib/array/base/mskput/docs/types/test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import mskput = require( './index' );
2828
// The function returns an array...
2929
{
3030
mskput( [ 1, 2, 3, 4 ], [ 1, 0, 0, 1 ], [ 20, 30 ], 'throw' ); // $ExpectType number[]
31+
mskput( [ 1, 2, 3, 4 ], [ 1, 0, 0, 1 ], [ 20, 30 ], 'broadcast' ); // $ExpectType number[]
3132
mskput( [ 1, 2, 3, 4 ], [ 1, 0, 0, 1 ], [ 20, 30 ], 'repeat' ); // $ExpectType number[]
3233

3334
mskput( new Int32Array( [ 1, 2, 3, 4 ] ), [ 1, 0, 0, 1 ], [ 20, 30 ], 'throw' ); // $ExpectType Int32Array

lib/node_modules/@stdlib/array/base/mskput/lib/main.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ function boolean( x, mask, values ) {
214214
* @param {Collection} x - input array
215215
* @param {Collection} mask - mask array
216216
* @param {Collection} values - values to set
217-
* @param {string} mode - string specifying whether to raise an exception when the number of values is less than the number of falsy mask values
217+
* @param {string} mode - string specifying behavior when the number of values is less than the number of falsy mask values
218218
* @throws {Error} insufficient values to satisfy mask array
219219
* @returns {Collection} input array
220220
*
@@ -236,11 +236,23 @@ function boolean( x, mask, values ) {
236236
* var mask = [ 1, 0, 0, 1 ];
237237
* var values = [ 30 ];
238238
*
239-
* var out = mskput( x, mask, values, 'repeat' );
239+
* var out = mskput( x, mask, values, 'broadcast' );
240240
* // returns [ 1, 30, 30, 4 ]
241241
*
242242
* var bool = ( out === x );
243243
* // returns true
244+
*
245+
* @example
246+
* var x = [ 1, 2, 3, 4 ];
247+
*
248+
* var mask = [ 0, 0, 1, 0 ];
249+
* var values = [ 20, 30 ];
250+
*
251+
* var out = mskput( x, mask, values, 'repeat' );
252+
* // returns [ 20, 30, 3, 20 ]
253+
*
254+
* var bool = ( out === x );
255+
* // returns true
244256
*/
245257
function mskput( x, mask, values, mode ) {
246258
var xo;
@@ -250,6 +262,9 @@ function mskput( x, mask, values, mode ) {
250262
if ( mode === 'throw' && countFalsy( mask ) > values.length ) {
251263
throw new Error( 'invalid arguments. Insufficient values to satisfy mask array.' );
252264
}
265+
if ( mode === 'broadcast' && values.length > 1 && countFalsy( mask ) > values.length ) {
266+
throw new Error( 'invalid arguments. Insufficient values to satisfy mask array.' );
267+
}
253268
xo = arraylike2object( x );
254269
mo = arraylike2object( mask );
255270
vo = arraylike2object( values );

lib/node_modules/@stdlib/array/base/mskput/test/test.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,21 @@ tape( 'the function replaces elements in an array (generic, broadcasting)', func
7878

7979
x = [ 1, 2, 3, 4 ];
8080
mask = [ 1, 0, 1, 0 ];
81-
actual = mskput( x, mask, [ 20 ], 'repeat' );
81+
actual = mskput( x, mask, [ 20 ], 'broadcast' );
8282
expected = [ 1, 20, 3, 20 ];
8383
t.strictEqual( actual, x, 'returns expected value' );
8484
t.deepEqual( actual, expected, 'returns expected value' );
8585

8686
x = [ 1, 2, 3, 4 ];
8787
mask = [ 0, 0, 0, 0 ];
88-
actual = mskput( x, mask, [ 20 ], 'repeat' );
88+
actual = mskput( x, mask, [ 20 ], 'broadcast' );
8989
expected = [ 20, 20, 20, 20 ];
9090
t.strictEqual( actual, x, 'returns expected value' );
9191
t.deepEqual( actual, expected, 'returns expected value' );
9292

9393
x = [ 1, 2, 3, 4 ];
9494
mask = [ 1, 1, 1, 1 ];
95-
actual = mskput( x, mask, [ 20 ], 'repeat' );
95+
actual = mskput( x, mask, [ 20 ], 'broadcast' );
9696
expected = [ 1, 2, 3, 4 ];
9797
t.strictEqual( actual, x, 'returns expected value' );
9898
t.deepEqual( actual, expected, 'returns expected value' );
@@ -145,21 +145,21 @@ tape( 'the function replaces elements in an array (typed, broadcasting)', functi
145145

146146
x = new Int32Array( [ 1, 2, 3, 4 ] );
147147
mask = [ 1, 0, 1, 0 ];
148-
actual = mskput( x, mask, [ 20 ], 'repeat' );
148+
actual = mskput( x, mask, [ 20 ], 'broadcast' );
149149
expected = new Int32Array( [ 1, 20, 3, 20 ] );
150150
t.strictEqual( actual, x, 'returns expected value' );
151151
t.deepEqual( actual, expected, 'returns expected value' );
152152

153153
x = new Int32Array( [ 1, 2, 3, 4 ] );
154154
mask = [ 0, 0, 0, 0 ];
155-
actual = mskput( x, mask, [ 20 ], 'repeat' );
155+
actual = mskput( x, mask, [ 20 ], 'broadcast' );
156156
expected = new Int32Array( [ 20, 20, 20, 20 ] );
157157
t.strictEqual( actual, x, 'returns expected value' );
158158
t.deepEqual( actual, expected, 'returns expected value' );
159159

160160
x = new Int32Array( [ 1, 2, 3, 4 ] );
161161
mask = [ 1, 1, 1, 1 ];
162-
actual = mskput( x, mask, [ 20 ], 'repeat' );
162+
actual = mskput( x, mask, [ 20 ], 'broadcast' );
163163
expected = new Int32Array( [ 1, 2, 3, 4 ] );
164164
t.strictEqual( actual, x, 'returns expected value' );
165165
t.deepEqual( actual, expected, 'returns expected value' );
@@ -227,7 +227,7 @@ tape( 'the function replaces elements in an array (accessors, broadcasting)', fu
227227
new Complex64( 5.0, 6.0 ),
228228
new Complex64( 100.0, 200.0 )
229229
];
230-
actual = mskput( x, mask, values, 'repeat' );
230+
actual = mskput( x, mask, values, 'broadcast' );
231231

232232
t.strictEqual( actual, x, 'returns expected value' );
233233
for ( i = 0; i < mask.length; i++ ) {
@@ -287,7 +287,7 @@ tape( 'the function replaces elements in an array (accessors, complex, broadcast
287287
new Complex64( 5.0, 6.0 ),
288288
new Complex64( 100.0, 200.0 )
289289
];
290-
actual = mskput( x, mask, values, 'repeat' );
290+
actual = mskput( x, mask, values, 'broadcast' );
291291

292292
t.strictEqual( actual, x, 'returns expected value' );
293293
for ( i = 0; i < mask.length; i++ ) {
@@ -335,7 +335,7 @@ tape( 'the function replaces elements in an array (accessors, boolean, broadcast
335335
mask = toAccessorArray( [ 1, 0, 0, 1 ] );
336336
values = [ true ];
337337
expected = [ true, true, true, true ];
338-
actual = mskput( x, mask, values, 'repeat' );
338+
actual = mskput( x, mask, values, 'broadcast' );
339339

340340
t.strictEqual( actual, x, 'returns expected value' );
341341
for ( i = 0; i < mask.length; i++ ) {
@@ -359,3 +359,18 @@ tape( 'when the "mode" is "throw", the function throws an error if provided insu
359359
mskput( x, mask, [ 200 ], 'throw' );
360360
}
361361
});
362+
363+
tape( 'when the "mode" is "broadcast", the function throws an error if a provided values array is broadcast incompatible with the number of falsy values in a mask array', function test( t ) {
364+
var mask;
365+
var x;
366+
367+
x = [ 1, 2, 3, 4 ];
368+
mask = [ 0, 0, 0, 0 ];
369+
370+
t.throws( badValue, Error, 'throws an error' );
371+
t.end();
372+
373+
function badValue() {
374+
mskput( x, mask, [ 200, 400 ], 'broadcast' );
375+
}
376+
});

0 commit comments

Comments
 (0)