From d272d024abf5a116ad44215cbdde55d4585d73af Mon Sep 17 00:00:00 2001 From: stdlib-bot Date: Tue, 25 Jun 2024 19:58:04 +0000 Subject: [PATCH] Auto-generated commit --- CHANGELOG.md | 6 ++- base/mskfilter/lib/assign.js | 53 +++++++++++++++++++++- base/mskfilter/test/test.assign.js | 60 +++++++++++++++++++++++++ base/mskfilter/test/test.main.js | 13 ++++++ base/take/lib/assign.js | 63 +++++++++++++++++++++++++- base/take/test/test.assign.js | 71 +++++++++++++++++++++++++++++- base/take/test/test.main.js | 15 ++++++- 7 files changed, 276 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8b3ea6b..7e8bb568 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@
-## Unreleased (2024-06-24) +## Unreleased (2024-06-25)
@@ -939,6 +939,7 @@ This release closes the following issue: ##### Features +- [`c04e29b`](https://github.com/stdlib-js/stdlib/commit/c04e29bf919dcc5f1368fecd977db696041ff8db) - add boolean dtype support to `array/base/mskfilter` [(#2450)](https://github.com/stdlib-js/stdlib/pull/2450) - [`d34ae78`](https://github.com/stdlib-js/stdlib/commit/d34ae789c63908f3edd1e7a746abd4cc60619b27) - add `assign` method
@@ -1164,6 +1165,7 @@ This release closes the following issue: ##### Features +- [`760979b`](https://github.com/stdlib-js/stdlib/commit/760979b6efafb5c235daa718c17d7eb5cef82237) - add boolean dtype support to `array/base/take` [(#2453)](https://github.com/stdlib-js/stdlib/pull/2453) - [`3a57fb4`](https://github.com/stdlib-js/stdlib/commit/3a57fb47211679fa520b5e1613b41aebd30eccf6) - add `assign` method and add support for an index accessor array
@@ -2258,6 +2260,8 @@ A total of 13 people contributed to this release. Thank you to the following con
+- [`c04e29b`](https://github.com/stdlib-js/stdlib/commit/c04e29bf919dcc5f1368fecd977db696041ff8db) - **feat:** add boolean dtype support to `array/base/mskfilter` [(#2450)](https://github.com/stdlib-js/stdlib/pull/2450) _(by Jaysukh Makvana, Athan Reines)_ +- [`760979b`](https://github.com/stdlib-js/stdlib/commit/760979b6efafb5c235daa718c17d7eb5cef82237) - **feat:** add boolean dtype support to `array/base/take` [(#2453)](https://github.com/stdlib-js/stdlib/pull/2453) _(by Jaysukh Makvana, Athan Reines)_ - [`c41435f`](https://github.com/stdlib-js/stdlib/commit/c41435fa51e75a6329d78b8e801590476ece06af) - **feat:** add boolean dtype support to `array/base/cuany` [(#2446)](https://github.com/stdlib-js/stdlib/pull/2446) _(by Jaysukh Makvana)_ - [`3c451db`](https://github.com/stdlib-js/stdlib/commit/3c451db98b94e5d89a2aaffca29c4ca93835605f) - **feat:** add boolean dtype support to `array/base/cuevery` [(#2447)](https://github.com/stdlib-js/stdlib/pull/2447) _(by Jaysukh Makvana)_ - [`fbc42b4`](https://github.com/stdlib-js/stdlib/commit/fbc42b4c66cf695c6c114f64bf3eff65186026f0) - **feat:** add `includes` method to `array/bool` [(#2441)](https://github.com/stdlib-js/stdlib/pull/2441) _(by Jaysukh Makvana)_ diff --git a/base/mskfilter/lib/assign.js b/base/mskfilter/lib/assign.js index e1952413..e76d884b 100644 --- a/base/mskfilter/lib/assign.js +++ b/base/mskfilter/lib/assign.js @@ -21,8 +21,10 @@ // MODULES // var isComplexDataType = require( './../../../base/assert/is-complex-floating-point-data-type' ); +var isBooleanDataType = require( './../../../base/assert/is-boolean-data-type' ); var arraylike2object = require( './../../../base/arraylike2object' ); var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); +var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' ); // FUNCTIONS // @@ -163,6 +165,48 @@ function complex( x, mask, out, stride, offset ) { return out; } +/** +* Applies a mask to a boolean array and assigns unmasked values to elements in a boolean output array. +* +* @private +* @param {Collection} x - boolean input array view +* @param {Object} mask - mask array object +* @param {Collection} out - boolean output array view +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @returns {Collection} output array view +* +* @example +* var Uint8Array = require( '@stdlib/array/uint8' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = new Uint8Array( [ 1, 0, 0, 1 ] ); +* var mask = [ 0, 1, 0, 1 ]; +* +* var out = new Uint8Array( 4 ); +* +* var arr = boolean( x, arraylike2object( mask ), out, 1, 0 ); +* // returns [ 0, 1, 0, 0 ] +*/ +function boolean( x, mask, out, stride, offset ) { + var mdata; + var mget; + var io; + var i; + + mdata = mask.data; + mget = mask.accessors[ 0 ]; + + io = offset; + for ( i = 0; i < mdata.length; i++ ) { + if ( mget( mdata, i ) ) { + out[ io ] = x[ i ]; + io += stride; + } + } + return out; +} + // MAIN // @@ -200,7 +244,7 @@ function assign( x, mask, out, stride, offset ) { mo.accessorProtocol || oo.accessorProtocol ) { - // Note: we only explicitly support complex-to-complex, as this function should not be concerned with casting rules, etc. That is left to userland... + // Note: we only explicitly support a limited set of dtype-to-dtype pairs, as this function should not be concerned with casting rules, etc. That is left to userland... if ( isComplexDataType( xo.dtype ) && isComplexDataType( oo.dtype ) @@ -208,6 +252,13 @@ function assign( x, mask, out, stride, offset ) { complex( reinterpret( x, 0 ), mo, reinterpret( out, 0 ), stride, offset ); // eslint-disable-line max-len return out; } + if ( + isBooleanDataType( xo.dtype ) && + isBooleanDataType( oo.dtype ) + ) { + boolean( reinterpretBoolean( x, 0 ), mo, reinterpretBoolean( out, 0 ), stride, offset ); // eslint-disable-line max-len + return out; + } accessors( xo, mo, oo, stride, offset ); return out; } diff --git a/base/mskfilter/test/test.assign.js b/base/mskfilter/test/test.assign.js index e6f28b1b..86b11dd9 100644 --- a/base/mskfilter/test/test.assign.js +++ b/base/mskfilter/test/test.assign.js @@ -22,9 +22,11 @@ var tape = require( 'tape' ); var Complex64Array = require( './../../../complex64' ); +var BooleanArray = require( './../../../bool' ); var toAccessorArray = require( './../../../base/to-accessor-array' ); var Int32Array = require( './../../../int32' ); var isSameComplex64Array = require( '@stdlib/assert/is-same-complex64array' ); +var isSameBooleanArray = require( '@stdlib/assert/is-same-booleanarray' ); var zeros = require( './../../../zeros' ); var mskfilter = require( './../lib/assign.js' ); @@ -193,6 +195,58 @@ tape( 'the function filters array elements (complex typed array)', function test t.end(); }); +tape( 'the function filters array elements (boolean array)', function test( t ) { + var expected; + var actual; + var mask; + var out; + var x; + + x = new BooleanArray( [ true, false, false, true ] ); + + mask = [ 0, 1, 0, 1 ]; + out = new BooleanArray( 2 ); + actual = mskfilter( x, mask, out, 1, 0 ); + expected = new BooleanArray( [ false, true ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' ); + + mask = [ 0, 0, 0, 0 ]; + out = new BooleanArray( 0 ); + actual = mskfilter( x, mask, out, 1, 0 ); + expected = new BooleanArray( [] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' ); + + mask = [ 0, 0, 0, 1 ]; + out = new BooleanArray( 1 ); + actual = mskfilter( x, mask, out, 1, 0 ); + expected = new BooleanArray( [ true ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' ); + + mask = [ 1, 1, 1, 1 ]; + out = new BooleanArray( 4 ); + actual = mskfilter( x, mask, out, 1, 0 ); + expected = new BooleanArray( [ true, false, false, true ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' ); + + mask = [ 0, 1, 0, 1 ]; + out = new BooleanArray( 4 ); + actual = mskfilter( x, mask, out, -2, out.length-1 ); + expected = new BooleanArray( [ false, true, false, false ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'the function filters array elements (accessors)', function test( t ) { var expected; var actual; @@ -285,5 +339,11 @@ tape( 'the function returns leaves an output array unchanged if provided a secon actual = mskfilter( x, mask, out, 1, 0 ); t.strictEqual( isSameComplex64Array( actual, expected ), true, 'returns expected value' ); + x = new BooleanArray( [ true, false, false, true ] ); + out = new BooleanArray( [ false, false, false, false ] ); + expected = new BooleanArray( [ false, false, false, false ] ); + actual = mskfilter( x, mask, out, 1, 0 ); + t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' ); + t.end(); }); diff --git a/base/mskfilter/test/test.main.js b/base/mskfilter/test/test.main.js index ed8aa704..ec7598e5 100644 --- a/base/mskfilter/test/test.main.js +++ b/base/mskfilter/test/test.main.js @@ -22,6 +22,7 @@ var tape = require( 'tape' ); var Complex64Array = require( './../../../complex64' ); +var BooleanArray = require( './../../../bool' ); var isSameComplex64 = require( '@stdlib/assert/is-same-complex64' ); var isArray = require( '@stdlib/assert/is-array' ); var mskfilter = require( './../lib' ); @@ -91,6 +92,18 @@ tape( 'the function filters array elements (accessors)', function test( t ) { for ( i = 0; i < expected.length; i++ ) { t.strictEqual( isSameComplex64( actual[ i ], expected[ i ] ), true, 'returns expected value' ); } + + x = new BooleanArray( [ true, false, false, true ] ); + mask = [ 0, 1, 0, 1 ]; + actual = mskfilter( x, mask ); + expected = [ x.get( 1 ), x.get( 3 ) ]; + + t.strictEqual( isArray( actual ), true, 'returns expected value' ); + t.notEqual( actual, x, 'returns different reference' ); + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( actual[ i ], expected[ i ], 'returns expected value' ); + } + t.end(); }); diff --git a/base/take/lib/assign.js b/base/take/lib/assign.js index ad81c592..8dce6431 100644 --- a/base/take/lib/assign.js +++ b/base/take/lib/assign.js @@ -21,8 +21,10 @@ // MODULES // var isComplexDataType = require( './../../../base/assert/is-complex-floating-point-data-type' ); +var isBooleanDataType = require( './../../../base/assert/is-boolean-data-type' ); var arraylike2object = require( './../../../base/arraylike2object' ); var reinterpret = require( '@stdlib/strided/base/reinterpret-complex' ); +var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' ); var ind = require( '@stdlib/ndarray/base/ind' ).factory; @@ -191,6 +193,58 @@ function complex( x, indices, mode, out, stride, offset ) { return out; } +/** +* Takes elements from a boolean array and assigns the values to elements in a boolean output array. +* +* @private +* @param {Collection} x - boolean value input array view +* @param {Object} indices - index array object +* @param {string} mode - index mode +* @param {Collection} out - boolean value output array view +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array offset +* @returns {Collection} output array view +* +* @example +* var Uint8Array = require( '@stdlib/array/uint8' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = new Uint8Array( [ 1, 0, 0, 1 ] ); +* var indices = [ 0, 0, 1, 1 ]; +* +* var out = new Uint8Array( 4 ); +* +* var arr = boolean( x, arraylike2object( indices ), 'throw', out, 1, 0 ); +* // returns [ 1, 1, 0, 0 ] +*/ +function boolean( x, indices, mode, out, stride, offset ) { + var getIndex; + var idata; + var iget; + var max; + var io; + var i; + var j; + + idata = indices.data; + iget = indices.accessors[ 0 ]; + + // Resolve a function for returning an index according to the specified index mode: + getIndex = ind( mode ); + + // Resolve the maximum index: + max = x.length - 1; + + // Extract each desired element from the provided array... + io = offset; + for ( i = 0; i < idata.length; i++ ) { + j = getIndex( iget( idata, i ), max ); + out[ io ] = x[ j ]; + io += stride; + } + return out; +} + // MAIN // @@ -229,7 +283,7 @@ function assign( x, indices, mode, out, stride, offset ) { io.accessorProtocol || oo.accessorProtocol ) { - // Note: we only explicitly support complex-to-complex, as this function should not be concerned with casting rules, etc. That is left to userland... + // Note: we only explicitly support a limited set of dtype-to-dtype pairs, as this function should not be concerned with casting rules, etc. That is left to userland... if ( isComplexDataType( xo.dtype ) && isComplexDataType( oo.dtype ) @@ -237,6 +291,13 @@ function assign( x, indices, mode, out, stride, offset ) { complex( reinterpret( x, 0 ), io, mode, reinterpret( out, 0 ), stride, offset ); // eslint-disable-line max-len return out; } + if ( + isBooleanDataType( xo.dtype ) && + isBooleanDataType( oo.dtype ) + ) { + boolean( reinterpretBoolean( x, 0 ), io, mode, reinterpretBoolean( out, 0 ), stride, offset ); // eslint-disable-line max-len + return out; + } accessors( xo, io, mode, oo, stride, offset ); return out; } diff --git a/base/take/test/test.assign.js b/base/take/test/test.assign.js index dca85fbb..6e103738 100644 --- a/base/take/test/test.assign.js +++ b/base/take/test/test.assign.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2022 The Stdlib Authors. +* Copyright (c) 2024 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,10 +23,12 @@ var tape = require( 'tape' ); var Complex128Array = require( './../../../complex128' ); var Complex64Array = require( './../../../complex64' ); +var BooleanArray = require( './../../../bool' ); var Float64Array = require( './../../../float64' ); var toAccessorArray = require( './../../../base/to-accessor-array' ); var isSameComplex128Array = require( '@stdlib/assert/is-same-complex128array' ); var isSameComplex64Array = require( '@stdlib/assert/is-same-complex64array' ); +var isSameBooleanArray = require( '@stdlib/assert/is-same-booleanarray' ); var zeros = require( './../../../zeros' ); var take = require( './../lib/assign.js' ); @@ -173,6 +175,50 @@ tape( 'the function takes elements from an array (complex typed array)', functio t.end(); }); +tape( 'the function takes elements from an array (boolean array)', function test( t ) { + var expected; + var indices; + var actual; + var out; + var x; + + x = new BooleanArray( [ true, false, false, true ] ); + + indices = [ 1, 3 ]; + out = new BooleanArray( indices.length ); + actual = take( x, indices, 'throw', out, 1, 0 ); + expected = new BooleanArray( [ false, true ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' ); + + indices = [ 1, 1, 3, 3 ]; + out = new BooleanArray( indices.length*2 ); + actual = take( x, indices, 'throw', out, 2, 0 ); + expected = new BooleanArray( [ false, false, false, false, true, false, true, false ] ); // eslint-disable-line max-len + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' ); + + indices = [ 3, 2, 1, 0 ]; + out = new BooleanArray( indices.length ); + actual = take( x, indices, 'throw', out, -1, out.length-1 ); + expected = new BooleanArray( [ true, false, false, true ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' ); + + indices = [ 1, 1, 1, 1 ]; + out = new BooleanArray( indices.length+1 ); + actual = take( x, indices, 'throw', out, 1, 1 ); + expected = new BooleanArray( [ false, false, false, false, false ] ); + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' ); + + t.end(); +}); + tape( 'the function takes elements from an array (accessors)', function test( t ) { var expected; var indices; @@ -254,6 +300,12 @@ tape( 'the function returns leaves an output array unchanged if provided a secon actual = take( x, [], 'throw', out, 1, 0 ); t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' ); + x = new BooleanArray( [ true, false, false, true ] ); + out = new BooleanArray( [ false, false, false, false ] ); + expected = new BooleanArray( [ false, false, false, false ] ); + actual = take( x, [], 'throw', out, 1, 0 ); + t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' ); + t.end(); }); @@ -325,6 +377,23 @@ tape( 'when the "mode" is "throw", the function throws an error if provided an o } }); +tape( 'when the "mode" is "throw", the function throws an error if provided an out-of-bounds index (boolean)', function test( t ) { + var indices; + var out; + var x; + + x = new BooleanArray( [ true, false ] ); + indices = [ 4, 5, 1, 2 ]; + out = new BooleanArray( x.length ); + + t.throws( badValue, RangeError, 'throws an error' ); + t.end(); + + function badValue() { + take( x, indices, 'throw', out, 1, 0 ); + } +}); + tape( 'when the "mode" is "normalize", the function normalizes negative indices (generic)', function test( t ) { var expected; var indices; diff --git a/base/take/test/test.main.js b/base/take/test/test.main.js index 47d782ac..2a3bc929 100644 --- a/base/take/test/test.main.js +++ b/base/take/test/test.main.js @@ -1,7 +1,7 @@ /** * @license Apache-2.0 * -* Copyright (c) 2022 The Stdlib Authors. +* Copyright (c) 2024 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,7 @@ var tape = require( 'tape' ); var Complex64Array = require( './../../../complex64' ); +var BooleanArray = require( './../../../bool' ); var toAccessorArray = require( './../../../base/to-accessor-array' ); var realf = require( '@stdlib/complex/realf' ); var imagf = require( '@stdlib/complex/imagf' ); @@ -88,6 +89,18 @@ tape( 'the function takes elements from an array (accessors)', function test( t t.strictEqual( realf( v ), realf( expected ), 'returns expected value' ); t.strictEqual( imagf( v ), imagf( expected ), 'returns expected value' ); } + + x = new BooleanArray( [ true, false, false, true ] ); + indices = toAccessorArray( [ 1, 1, 3, 3 ] ); + actual = take( x, indices, 'throw' ); + + t.notEqual( actual, x, 'returns different reference' ); + for ( i = 0; i < indices.length; i++ ) { + v = actual[ i ]; + expected = x.get( indices.get( i ) ); + t.strictEqual( v, expected, 'returns expected value' ); + } + t.end(); });