Skip to content

Commit 760979b

Browse files
Jaysukh-409kgryte
andauthored
feat: add boolean dtype support to array/base/take
PR-URL: #2453 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Signed-off-by: Athan Reines <kgryte@gmail.com>
1 parent 581f5a0 commit 760979b

File tree

3 files changed

+146
-3
lines changed

3 files changed

+146
-3
lines changed

lib/node_modules/@stdlib/array/base/take/lib/assign.js

Lines changed: 62 additions & 1 deletion
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' );
2526
var reinterpret = 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

2830

@@ -191,6 +193,58 @@ function complex( x, indices, mode, out, stride, offset ) {
191193
return out;
192194
}
193195

196+
/**
197+
* Takes elements from a boolean array and assigns the values to elements in a boolean output array.
198+
*
199+
* @private
200+
* @param {Collection} x - boolean value input array view
201+
* @param {Object} indices - index array object
202+
* @param {string} mode - index mode
203+
* @param {Collection} out - boolean value output array view
204+
* @param {integer} stride - output array stride
205+
* @param {NonNegativeInteger} offset - output array offset
206+
* @returns {Collection} output array view
207+
*
208+
* @example
209+
* var Uint8Array = require( '@stdlib/array/uint8' );
210+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
211+
*
212+
* var x = new Uint8Array( [ 1, 0, 0, 1 ] );
213+
* var indices = [ 0, 0, 1, 1 ];
214+
*
215+
* var out = new Uint8Array( 4 );
216+
*
217+
* var arr = boolean( x, arraylike2object( indices ), 'throw', out, 1, 0 );
218+
* // returns <Uint8Array>[ 1, 1, 0, 0 ]
219+
*/
220+
function boolean( x, indices, mode, out, stride, offset ) {
221+
var getIndex;
222+
var idata;
223+
var iget;
224+
var max;
225+
var io;
226+
var i;
227+
var j;
228+
229+
idata = indices.data;
230+
iget = indices.accessors[ 0 ];
231+
232+
// Resolve a function for returning an index according to the specified index mode:
233+
getIndex = ind( mode );
234+
235+
// Resolve the maximum index:
236+
max = x.length - 1;
237+
238+
// Extract each desired element from the provided array...
239+
io = offset;
240+
for ( i = 0; i < idata.length; i++ ) {
241+
j = getIndex( iget( idata, i ), max );
242+
out[ io ] = x[ j ];
243+
io += stride;
244+
}
245+
return out;
246+
}
247+
194248

195249
// MAIN //
196250

@@ -229,14 +283,21 @@ function assign( x, indices, mode, out, stride, offset ) {
229283
io.accessorProtocol ||
230284
oo.accessorProtocol
231285
) {
232-
// Note: we only explicitly support complex-to-complex, as this function should not be concerned with casting rules, etc. That is left to userland...
286+
// 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...
233287
if (
234288
isComplexDataType( xo.dtype ) &&
235289
isComplexDataType( oo.dtype )
236290
) {
237291
complex( reinterpret( x, 0 ), io, mode, reinterpret( out, 0 ), stride, offset ); // eslint-disable-line max-len
238292
return out;
239293
}
294+
if (
295+
isBooleanDataType( xo.dtype ) &&
296+
isBooleanDataType( oo.dtype )
297+
) {
298+
boolean( reinterpretBoolean( x, 0 ), io, mode, reinterpretBoolean( out, 0 ), stride, offset ); // eslint-disable-line max-len
299+
return out;
300+
}
240301
accessors( xo, io, mode, oo, stride, offset );
241302
return out;
242303
}

lib/node_modules/@stdlib/array/base/take/test/test.assign.js

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2022 The Stdlib Authors.
4+
* Copyright (c) 2024 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -23,10 +23,12 @@
2323
var tape = require( 'tape' );
2424
var Complex128Array = require( '@stdlib/array/complex128' );
2525
var Complex64Array = require( '@stdlib/array/complex64' );
26+
var BooleanArray = require( '@stdlib/array/bool' );
2627
var Float64Array = require( '@stdlib/array/float64' );
2728
var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
2829
var isSameComplex128Array = require( '@stdlib/assert/is-same-complex128array' );
2930
var isSameComplex64Array = require( '@stdlib/assert/is-same-complex64array' );
31+
var isSameBooleanArray = require( '@stdlib/assert/is-same-booleanarray' );
3032
var zeros = require( '@stdlib/array/zeros' );
3133
var take = require( './../lib/assign.js' );
3234

@@ -173,6 +175,50 @@ tape( 'the function takes elements from an array (complex typed array)', functio
173175
t.end();
174176
});
175177

178+
tape( 'the function takes elements from an array (boolean array)', function test( t ) {
179+
var expected;
180+
var indices;
181+
var actual;
182+
var out;
183+
var x;
184+
185+
x = new BooleanArray( [ true, false, false, true ] );
186+
187+
indices = [ 1, 3 ];
188+
out = new BooleanArray( indices.length );
189+
actual = take( x, indices, 'throw', out, 1, 0 );
190+
expected = new BooleanArray( [ false, true ] );
191+
192+
t.strictEqual( actual, out, 'returns expected value' );
193+
t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' );
194+
195+
indices = [ 1, 1, 3, 3 ];
196+
out = new BooleanArray( indices.length*2 );
197+
actual = take( x, indices, 'throw', out, 2, 0 );
198+
expected = new BooleanArray( [ false, false, false, false, true, false, true, false ] ); // eslint-disable-line max-len
199+
200+
t.strictEqual( actual, out, 'returns expected value' );
201+
t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' );
202+
203+
indices = [ 3, 2, 1, 0 ];
204+
out = new BooleanArray( indices.length );
205+
actual = take( x, indices, 'throw', out, -1, out.length-1 );
206+
expected = new BooleanArray( [ true, false, false, true ] );
207+
208+
t.strictEqual( actual, out, 'returns expected value' );
209+
t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' );
210+
211+
indices = [ 1, 1, 1, 1 ];
212+
out = new BooleanArray( indices.length+1 );
213+
actual = take( x, indices, 'throw', out, 1, 1 );
214+
expected = new BooleanArray( [ false, false, false, false, false ] );
215+
216+
t.strictEqual( actual, out, 'returns expected value' );
217+
t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' );
218+
219+
t.end();
220+
});
221+
176222
tape( 'the function takes elements from an array (accessors)', function test( t ) {
177223
var expected;
178224
var indices;
@@ -254,6 +300,12 @@ tape( 'the function returns leaves an output array unchanged if provided a secon
254300
actual = take( x, [], 'throw', out, 1, 0 );
255301
t.strictEqual( isSameComplex128Array( actual, expected ), true, 'returns expected value' );
256302

303+
x = new BooleanArray( [ true, false, false, true ] );
304+
out = new BooleanArray( [ false, false, false, false ] );
305+
expected = new BooleanArray( [ false, false, false, false ] );
306+
actual = take( x, [], 'throw', out, 1, 0 );
307+
t.strictEqual( isSameBooleanArray( actual, expected ), true, 'returns expected value' );
308+
257309
t.end();
258310
});
259311

@@ -325,6 +377,23 @@ tape( 'when the "mode" is "throw", the function throws an error if provided an o
325377
}
326378
});
327379

380+
tape( 'when the "mode" is "throw", the function throws an error if provided an out-of-bounds index (boolean)', function test( t ) {
381+
var indices;
382+
var out;
383+
var x;
384+
385+
x = new BooleanArray( [ true, false ] );
386+
indices = [ 4, 5, 1, 2 ];
387+
out = new BooleanArray( x.length );
388+
389+
t.throws( badValue, RangeError, 'throws an error' );
390+
t.end();
391+
392+
function badValue() {
393+
take( x, indices, 'throw', out, 1, 0 );
394+
}
395+
});
396+
328397
tape( 'when the "mode" is "normalize", the function normalizes negative indices (generic)', function test( t ) {
329398
var expected;
330399
var indices;

lib/node_modules/@stdlib/array/base/take/test/test.main.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2022 The Stdlib Authors.
4+
* Copyright (c) 2024 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222

2323
var tape = require( 'tape' );
2424
var Complex64Array = require( '@stdlib/array/complex64' );
25+
var BooleanArray = require( '@stdlib/array/bool' );
2526
var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
2627
var realf = require( '@stdlib/complex/realf' );
2728
var imagf = require( '@stdlib/complex/imagf' );
@@ -88,6 +89,18 @@ tape( 'the function takes elements from an array (accessors)', function test( t
8889
t.strictEqual( realf( v ), realf( expected ), 'returns expected value' );
8990
t.strictEqual( imagf( v ), imagf( expected ), 'returns expected value' );
9091
}
92+
93+
x = new BooleanArray( [ true, false, false, true ] );
94+
indices = toAccessorArray( [ 1, 1, 3, 3 ] );
95+
actual = take( x, indices, 'throw' );
96+
97+
t.notEqual( actual, x, 'returns different reference' );
98+
for ( i = 0; i < indices.length; i++ ) {
99+
v = actual[ i ];
100+
expected = x.get( indices.get( i ) );
101+
t.strictEqual( v, expected, 'returns expected value' );
102+
}
103+
91104
t.end();
92105
});
93106

0 commit comments

Comments
 (0)