Skip to content

Commit 79d01c7

Browse files
committed
refactor: special case boolean arrays
1 parent 4fdb218 commit 79d01c7

File tree

2 files changed

+163
-7
lines changed

2 files changed

+163
-7
lines changed

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

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
// MODULES //
2222

2323
var isComplexDataType = require( '@stdlib/array/base/assert/is-complex-floating-point-data-type' );
24+
var isBooleanDataType = require( '@stdlib/array/base/assert/is-boolean-data-type' );
2425
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
25-
var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' );
26+
var reinterpretComplex = require( '@stdlib/strided/base/reinterpret-complex' );
27+
var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' );
2628
var ind = require( '@stdlib/ndarray/base/ind' ).factory;
2729
var format = require( '@stdlib/string/format' );
2830

@@ -179,6 +181,52 @@ function complex( x, indices, values, stride, getIndex, maxIndex ) {
179181
return x;
180182
}
181183

184+
/**
185+
* Replaces elements in a boolean array with provided values.
186+
*
187+
* @private
188+
* @param {Uint8Array} x - input array
189+
* @param {Object} indices - index array object
190+
* @param {Uint8Array} values - values to set
191+
* @param {NonNegativeInteger} stride - index stride for accessing elements in `values`
192+
* @param {Function} getIndex - function for resolving an array index
193+
* @param {NonNegativeInteger} maxIndex - maximum array index (inclusive)
194+
* @returns {Uint8Array} input array
195+
*
196+
* @example
197+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
198+
* var Uint8Array = require( '@stdlib/array/uint8' );
199+
* var ind = require( '@stdlib/ndarray/base/ind' ).factory;
200+
*
201+
* var x = new Uint8Array( [ 1, 0, 0, 1 ] );
202+
*
203+
* var indices = [ 3, 1, 2, 0 ];
204+
* var values = new Uint8Array( [ 0, 1, 1, 0 ] );
205+
*
206+
* var getIndex = ind( 'throw' );
207+
*
208+
* var out = boolean( x, arraylike2object( indices ), values, 1, getIndex, x.length-1 );
209+
* // returns <Uint8Array>[ 0, 1, 1, 0 ]
210+
*/
211+
function boolean( x, indices, values, stride, getIndex, maxIndex ) {
212+
var idata;
213+
var iget;
214+
var iv;
215+
var i;
216+
var j;
217+
218+
idata = indices.data;
219+
iget = indices.accessors[ 0 ];
220+
221+
iv = 0;
222+
for ( i = 0; i < idata.length; i++ ) {
223+
j = getIndex( iget( idata, i ), maxIndex );
224+
x[ j ] = values[ iv ];
225+
iv += stride;
226+
}
227+
return x;
228+
}
229+
182230

183231
// MAIN //
184232

@@ -249,12 +297,13 @@ function put( x, indices, values, mode ) {
249297
io.accessorProtocol ||
250298
vo.accessorProtocol
251299
) {
252-
// Note: we only explicitly support complex-to-complex, as this function should not be concerned with casting rules, etc. That is left to userland...
253-
if (
254-
isComplexDataType( xo.dtype ) &&
255-
isComplexDataType( vo.dtype )
256-
) {
257-
complex( reinterpret( x, 0 ), io, reinterpret( values, 0 ), stride, getIndex, max ); // eslint-disable-line max-len
300+
// Note: we only explicitly support select dtype pairs, as this function should not be concerned with casting rules, etc. That is left to userland...
301+
if ( isComplexDataType( xo.dtype ) && isComplexDataType( vo.dtype ) ) {
302+
complex( reinterpretComplex( x, 0 ), io, reinterpretComplex( values, 0 ), stride, getIndex, max ); // eslint-disable-line max-len
303+
return x;
304+
}
305+
if ( isBooleanDataType( xo.dtype ) && isBooleanDataType( vo.dtype ) ) {
306+
boolean( reinterpretBoolean( x, 0 ), io, reinterpretBoolean( values, 0 ), stride, getIndex, max ); // eslint-disable-line max-len
258307
return x;
259308
}
260309
accessors( xo, io, vo, stride, getIndex, max );

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

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
var tape = require( 'tape' );
2424
var Complex64Array = require( '@stdlib/array/complex64' );
2525
var Int32Array = require( '@stdlib/array/int32' );
26+
var BooleanArray = require( '@stdlib/array/bool' );
2627
var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
2728
var Complex64 = require( '@stdlib/complex/float32' );
2829
var realf = require( '@stdlib/complex/realf' );
@@ -323,6 +324,112 @@ tape( 'the function replaces elements in an array (accessors, broadcasting)', fu
323324
t.end();
324325
});
325326

327+
tape( 'the function replaces elements in an array (accessors, complex)', function test( t ) {
328+
var expected;
329+
var indices;
330+
var actual;
331+
var values;
332+
var x;
333+
var v;
334+
var i;
335+
336+
x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
337+
indices = toAccessorArray( [ 1, 1, 3, 3 ] );
338+
values = new Complex64Array( [ 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0 ] );
339+
expected = [
340+
new Complex64( 1.0, 2.0 ),
341+
new Complex64( 30.0, 40.0 ),
342+
new Complex64( 5.0, 6.0 ),
343+
new Complex64( 70.0, 80.0 )
344+
];
345+
actual = put( x, indices, values, 'throw' );
346+
347+
t.strictEqual( actual, x, 'returns expected value' );
348+
for ( i = 0; i < indices.length; i++ ) {
349+
v = actual.get( i );
350+
t.strictEqual( isComplex64( v ), true, 'returns expected value' );
351+
t.strictEqual( realf( v ), realf( expected[ i ] ), 'returns expected value' );
352+
t.strictEqual( imagf( v ), imagf( expected[ i ] ), 'returns expected value' );
353+
}
354+
t.end();
355+
});
356+
357+
tape( 'the function replaces elements in an array (accessors, complex, broadcasting)', function test( t ) {
358+
var expected;
359+
var indices;
360+
var actual;
361+
var values;
362+
var x;
363+
var v;
364+
var i;
365+
366+
x = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 ] );
367+
indices = toAccessorArray( [ 1, 1, 3, 3 ] );
368+
values = new Complex64Array( [ 100.0, 200.0 ] );
369+
expected = [
370+
new Complex64( 1.0, 2.0 ),
371+
new Complex64( 100.0, 200.0 ),
372+
new Complex64( 5.0, 6.0 ),
373+
new Complex64( 100.0, 200.0 )
374+
];
375+
actual = put( x, indices, values, 'throw' );
376+
377+
t.strictEqual( actual, x, 'returns expected value' );
378+
for ( i = 0; i < indices.length; i++ ) {
379+
v = actual.get( i );
380+
t.strictEqual( isComplex64( v ), true, 'returns expected value' );
381+
t.strictEqual( realf( v ), realf( expected[ i ] ), 'returns expected value' );
382+
t.strictEqual( imagf( v ), imagf( expected[ i ] ), 'returns expected value' );
383+
}
384+
t.end();
385+
});
386+
387+
tape( 'the function replaces elements in an array (accessors, boolean)', function test( t ) {
388+
var expected;
389+
var indices;
390+
var actual;
391+
var values;
392+
var x;
393+
var v;
394+
var i;
395+
396+
x = new BooleanArray( [ true, false, false, true ] );
397+
indices = toAccessorArray( [ 1, 1, 3, 3 ] );
398+
values = new BooleanArray( [ false, true, false, true ] );
399+
expected = [ true, true, false, true ];
400+
actual = put( x, indices, values, 'throw' );
401+
402+
t.strictEqual( actual, x, 'returns expected value' );
403+
for ( i = 0; i < indices.length; i++ ) {
404+
v = actual.get( i );
405+
t.strictEqual( v, expected[ i ], 'returns expected value' );
406+
}
407+
t.end();
408+
});
409+
410+
tape( 'the function replaces elements in an array (accessors, boolean, broadcasting)', function test( t ) {
411+
var expected;
412+
var indices;
413+
var actual;
414+
var values;
415+
var x;
416+
var v;
417+
var i;
418+
419+
x = new BooleanArray( [ true, false, false, true ] );
420+
indices = toAccessorArray( [ 1, 1, 3, 3 ] );
421+
values = [ true ];
422+
expected = [ true, true, false, true ];
423+
actual = put( x, indices, values, 'throw' );
424+
425+
t.strictEqual( actual, x, 'returns expected value' );
426+
for ( i = 0; i < indices.length; i++ ) {
427+
v = actual.get( i );
428+
t.strictEqual( v, expected[ i ], 'returns expected value' );
429+
}
430+
t.end();
431+
});
432+
326433
tape( 'the function returns the input array unchanged if provided a second argument which is empty', function test( t ) {
327434
var actual;
328435
var x;

0 commit comments

Comments
 (0)