Skip to content

feat: add boolean dtype support to strided/base/binary-addon-dispatch #2526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2021 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.
Expand All @@ -27,13 +27,15 @@
var resolve = require( '@stdlib/strided/base/dtype-resolve-enum' );
var reinterpretComplex64 = require( '@stdlib/strided/base/reinterpret-complex64' );
var reinterpretComplex128 = require( '@stdlib/strided/base/reinterpret-complex128' );
var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' );
var format = require( '@stdlib/string/format' );


// VARIABLES //

var COMPLEX64 = resolve( 'complex64' );
var COMPLEX128 = resolve( 'complex128' );
var BOOLEAN = resolve( 'bool' );


// MAIN //
Expand Down Expand Up @@ -154,7 +156,7 @@
var viewY;
var viewZ;

// WARNING: we assume that, if we're provided something resembling a typed array, we're provided a typed array; however, this can lead to potential unintended errors as the native add-on may not work with non-typed array objects (e.g., generic arrays)...

Check warning on line 159 in lib/node_modules/@stdlib/strided/base/binary-addon-dispatch/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'warning' comment: 'WARNING: we assume that, if we're...'
if (
!isTypedArrayLike( x ) ||
!isTypedArrayLike( y ) ||
Expand All @@ -173,20 +175,26 @@
viewX = reinterpretComplex64( x, 0 );
} else if ( dtypeX === COMPLEX128 ) {
viewX = reinterpretComplex128( x, 0 );
} else if ( dtypeX === BOOLEAN ) {
viewX = reinterpretBoolean( x, 0 );
} else {
viewX = x;
}
if ( dtypeY === COMPLEX64 ) {
viewY = reinterpretComplex64( y, 0 );
} else if ( dtypeY === COMPLEX128 ) {
viewY = reinterpretComplex128( y, 0 );
} else if ( dtypeY === BOOLEAN ) {
viewY = reinterpretBoolean( y, 0 );
} else {
viewY = y;
}
if ( dtypeZ === COMPLEX64 ) {
viewZ = reinterpretComplex64( z, 0 );
} else if ( dtypeZ === COMPLEX128 ) {
viewZ = reinterpretComplex128( z, 0 );
} else if ( dtypeZ === BOOLEAN ) {
viewZ = reinterpretBoolean( z, 0 );
} else {
viewZ = z;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2021 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.
Expand All @@ -28,6 +28,7 @@
var resolve = require( '@stdlib/strided/base/dtype-resolve-enum' );
var reinterpretComplex64 = require( '@stdlib/strided/base/reinterpret-complex64' );
var reinterpretComplex128 = require( '@stdlib/strided/base/reinterpret-complex128' );
var reinterpretBoolean = require( '@stdlib/strided/base/reinterpret-boolean' );
var offsetView = require( '@stdlib/strided/base/offset-view' );
var minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' );
var format = require( '@stdlib/string/format' );
Expand All @@ -37,6 +38,7 @@

var COMPLEX64 = resolve( 'complex64' );
var COMPLEX128 = resolve( 'complex128' );
var BOOLEAN = resolve( 'bool' );


// MAIN //
Expand Down Expand Up @@ -169,7 +171,7 @@
var viewY;
var viewZ;

// WARNING: we assume that, if we're provided something resembling a typed array, we're provided a typed array; however, this can lead to potential unintended errors as the native add-on may not work with non-typed array objects (e.g., generic arrays)...

Check warning on line 174 in lib/node_modules/@stdlib/strided/base/binary-addon-dispatch/lib/ndarray.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected 'warning' comment: 'WARNING: we assume that, if we're...'
if (
!isTypedArrayLike( x ) ||
!isTypedArrayLike( y ) ||
Expand Down Expand Up @@ -200,20 +202,26 @@
viewX = reinterpretComplex64( x, offsetX );
} else if ( dtypeX === COMPLEX128 ) {
viewX = reinterpretComplex128( x, offsetX );
} else if ( dtypeX === BOOLEAN ) {
viewX = reinterpretBoolean( x, offsetX );
} else {
viewX = offsetView( x, offsetX );
}
if ( dtypeY === COMPLEX64 ) {
viewY = reinterpretComplex64( y, offsetY );
} else if ( dtypeY === COMPLEX128 ) {
viewY = reinterpretComplex128( y, offsetY );
} else if ( dtypeY === BOOLEAN ) {
viewY = reinterpretBoolean( y, offsetY );
} else {
viewY = offsetView( y, offsetY );
}
if ( dtypeZ === COMPLEX64 ) {
viewZ = reinterpretComplex64( z, offsetZ );
} else if ( dtypeZ === COMPLEX128 ) {
viewZ = reinterpretComplex128( z, offsetZ );
} else if ( dtypeZ === BOOLEAN ) {
viewZ = reinterpretBoolean( z, offsetZ );
} else {
viewZ = offsetView( z, offsetZ );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2021 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.
Expand All @@ -25,8 +25,10 @@ var noop = require( '@stdlib/utils/noop' );
var Float64Array = require( '@stdlib/array/float64' );
var Complex64Array = require( '@stdlib/array/complex64' );
var Complex128Array = require( '@stdlib/array/complex128' );
var BooleanArray = require( '@stdlib/array/bool' );
var isFloat32Array = require( '@stdlib/assert/is-float32array' );
var isFloat64Array = require( '@stdlib/assert/is-float64array' );
var isUint8Array = require( '@stdlib/assert/is-uint8array' );
var resolve = require( '@stdlib/strided/base/dtype-resolve-enum' );
var dispatch = require( './../lib' );

Expand Down Expand Up @@ -134,6 +136,44 @@ tape( 'the function returns a function which dispatches to an addon function whe
}
});

tape( 'the function returns a function which dispatches to an addon function when provided typed arrays (bool)', function test( t ) {
var f;
var x;
var y;
var z;

f = dispatch( addon, fallback );

x = new BooleanArray( 2 );
y = new BooleanArray( x.length );
z = new BooleanArray( x.length );

f( x.length, 'bool', x, 1, 'bool', y, 1, 'bool', z, 1 );

t.end();

function addon( N, dx, ax, sx, dy, ay, sy, dz, az, sz ) {
t.ok( true, 'called addon' );
t.strictEqual( N, x.length, 'returns expected value' );
t.strictEqual( dx, resolve( 'bool' ), 'returns expected value' );
t.strictEqual( isUint8Array( ax ), true, 'returns expected value' );
t.strictEqual( ax.buffer, x.buffer, 'returns expected value' );
t.strictEqual( sx, 1, 'returns expected value' );
t.strictEqual( dy, resolve( 'bool' ), 'returns expected value' );
t.strictEqual( isUint8Array( ay ), true, 'returns expected value' );
t.strictEqual( ay.buffer, y.buffer, 'returns expected value' );
t.strictEqual( sy, 1, 'returns expected value' );
t.strictEqual( dz, resolve( 'bool' ), 'returns expected value' );
t.strictEqual( isUint8Array( az ), true, 'returns expected value' );
t.strictEqual( az.buffer, z.buffer, 'returns expected value' );
t.strictEqual( sz, 1, 'returns expected value' );
}

function fallback() {
t.ok( false, 'called fallback' );
}
});

tape( 'the function returns a function which dispatches to an addon function when provided typed arrays (complex64)', function test( t ) {
var f;
var x;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2021 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.
Expand All @@ -27,8 +27,10 @@ var noop = require( '@stdlib/utils/noop' );
var Float64Array = require( '@stdlib/array/float64' );
var Complex64Array = require( '@stdlib/array/complex64' );
var Complex128Array = require( '@stdlib/array/complex128' );
var BooleanArray = require( '@stdlib/array/bool' );
var isFloat32Array = require( '@stdlib/assert/is-float32array' );
var isFloat64Array = require( '@stdlib/assert/is-float64array' );
var isUint8Array = require( '@stdlib/assert/is-uint8array' );
var resolve = require( '@stdlib/strided/base/dtype-resolve-enum' );
var dispatch = require( './../lib/ndarray.js' );

Expand Down Expand Up @@ -256,6 +258,44 @@ tape( 'the function returns a function which dispatches to an addon function whe
}
});

tape( 'the function returns a function which dispatches to an addon function when provided typed arrays (bool)', function test( t ) {
var f;
var x;
var y;
var z;

f = dispatch( addon, fallback );

x = new BooleanArray( 2 );
y = new BooleanArray( x.length );
z = new BooleanArray( x.length );

f( x.length, 'bool', x, 1, 0, 'bool', y, 1, 0, 'bool', z, 1, 0 );

t.end();

function addon( N, dx, ax, sx, dy, ay, sy, dz, az, sz ) {
t.ok( true, 'called addon' );
t.strictEqual( N, x.length, 'returns expected value' );
t.strictEqual( dx, resolve( 'bool' ), 'returns expected value' );
t.strictEqual( isUint8Array( ax ), true, 'returns expected value' );
t.strictEqual( ax.buffer, x.buffer, 'returns expected value' );
t.strictEqual( sx, 1, 'returns expected value' );
t.strictEqual( dy, resolve( 'bool' ), 'returns expected value' );
t.strictEqual( isUint8Array( ay ), true, 'returns expected value' );
t.strictEqual( ay.buffer, y.buffer, 'returns expected value' );
t.strictEqual( sy, 1, 'returns expected value' );
t.strictEqual( dz, resolve( 'bool' ), 'returns expected value' );
t.strictEqual( isUint8Array( az ), true, 'returns expected value' );
t.strictEqual( az.buffer, z.buffer, 'returns expected value' );
t.strictEqual( sz, 1, 'returns expected value' );
}

function fallback() {
t.ok( false, 'called fallback' );
}
});

tape( 'the function returns a function which dispatches to an addon function when provided typed arrays (complex64)', function test( t ) {
var f;
var x;
Expand Down
Loading