From 40afb55290b234e0a95d5550230ad133ac5e33b6 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Wed, 22 Apr 2026 03:43:20 +0500 Subject: [PATCH 1/2] feat: add ndarray/base/reinterpret-boolean --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../base/reinterpret-boolean/README.md | 116 ++++++++++++ .../benchmark/benchmark.js | 115 ++++++++++++ .../base/reinterpret-boolean/docs/repl.txt | 31 ++++ .../reinterpret-boolean/docs/types/index.d.ts | 50 +++++ .../reinterpret-boolean/docs/types/test.ts | 58 ++++++ .../reinterpret-boolean/examples/index.js | 33 ++++ .../base/reinterpret-boolean/lib/index.js | 44 +++++ .../base/reinterpret-boolean/lib/main.js | 67 +++++++ .../base/reinterpret-boolean/package.json | 65 +++++++ .../base/reinterpret-boolean/test/test.js | 175 ++++++++++++++++++ 10 files changed, 754 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/README.md b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/README.md new file mode 100644 index 000000000000..ada1461bd248 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/README.md @@ -0,0 +1,116 @@ + + +# reinterpretBoolean + +> Reinterpret a boolean [ndarray][@stdlib/ndarray/base/ctor] as an unsigned 8-bit integer [ndarray][@stdlib/ndarray/base/ctor]. + +
+ +
+ + + +
+ +## Usage + +```javascript +var reinterpretBoolean = require( '@stdlib/ndarray/base/reinterpret-boolean' ); +``` + +#### reinterpretBoolean( x ) + +Reinterprets a boolean [ndarray][@stdlib/ndarray/base/ctor] as an unsigned 8-bit integer [ndarray][@stdlib/ndarray/base/ctor]. + +```javascript +var zeros = require( '@stdlib/ndarray/base/zeros' ); + +var x = zeros( 'bool', [ 2, 2 ], 'row-major' ); +// returns [ [ false, false ], [ false, false ] ] + +var out = reinterpretBoolean( x ); +// returns [ [ 0, 0 ], [ 0, 0 ] ] +``` + +
+ + + +
+ +## Notes + +- The returned [ndarray][@stdlib/ndarray/base/ctor] is a view on the input [ndarray][@stdlib/ndarray/base/ctor] data buffer. +- The returned [ndarray][@stdlib/ndarray/base/ctor] is a "base" [ndarray][@stdlib/ndarray/base/ctor], and, thus, the returned [ndarray][@stdlib/ndarray/base/ctor] does not perform bounds checking or afford any of the guarantees of the non-base [ndarray][@stdlib/ndarray/ctor] constructor. The primary intent of this function is to reinterpret an ndarray-like object within internal implementations and to do so with minimal overhead. + +
+ + + +
+ +## Examples + + + +```javascript +var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var BooleanArray = require( '@stdlib/array/bool' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var reinterpretBoolean = require( '@stdlib/ndarray/base/reinterpret-boolean' ); + +// Create a boolean ndarray: +var buf = new BooleanArray( bernoulli( 4, 0.5 ) ); +var x = ndarray( 'bool', buf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +// Reinterpret as an unsigned 8-bit integer ndarray: +var out = reinterpretBoolean( x ); +console.log( ndarray2array( out ) ); +``` + +
+ + + +
+ +
+ + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/benchmark/benchmark.js new file mode 100644 index 000000000000..4d2ca6bb64e8 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/benchmark/benchmark.js @@ -0,0 +1,115 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var BooleanArray = require( '@stdlib/array/bool' ); +var ndarrayBase = require( '@stdlib/ndarray/base/ctor' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var reinterpretBoolean = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::base_ndarray,2d', pkg ), function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var out; + var i; + + dtype = 'bool'; + buffer = new BooleanArray( 4 ); + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + values = [ + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ), + ndarrayBase( dtype, buffer, shape, strides, offset, order ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = reinterpretBoolean( values[ i%values.length ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::ndarray,2d', pkg ), function benchmark( b ) { + var strides; + var values; + var buffer; + var offset; + var dtype; + var shape; + var order; + var out; + var i; + + dtype = 'bool'; + buffer = new BooleanArray( 4 ); + shape = [ 2, 2 ]; + strides = [ 2, 1 ]; + offset = 0; + order = 'row-major'; + + values = [ + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ), + ndarray( dtype, buffer, shape, strides, offset, order ) + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = reinterpretBoolean( values[ i%values.length ] ); + if ( typeof out !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isndarrayLike( out ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/docs/repl.txt new file mode 100644 index 000000000000..40f0644e93d9 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/docs/repl.txt @@ -0,0 +1,31 @@ + +{{alias}}( x ) + Reinterprets a boolean ndarray as an unsigned 8-bit integer ndarray. + + The returned ndarray is a view on the input ndarray data buffer. + + The returned ndarray is a "base" ndarray, and, thus, the returned ndarray + does not perform bounds checking or afford any of the guarantees of the + non-base ndarray constructor. The primary intent of this function is to + reinterpret an ndarray-like object within internal implementations and to + do so with minimal overhead. + + Parameters + ---------- + x: ndarray + Input ndarray. + + Returns + ------- + out: ndarray + Unsigned 8-bit integer ndarray view. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/base/zeros}}( 'bool', [ 2, 2 ], 'row-major' ); + > var out = {{alias}}( x ) + [ [ 0, 0 ], [ 0, 0 ] ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/docs/types/index.d.ts new file mode 100644 index 000000000000..16c3806fac4b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/docs/types/index.d.ts @@ -0,0 +1,50 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { boolndarray, uint8ndarray } from '@stdlib/types/ndarray'; + +/** +* Reinterprets a boolean ndarray as an unsigned 8-bit integer ndarray. +* +* ## Notes +* +* - The returned ndarray is a view on the input ndarray data buffer. +* - The returned ndarray is a "base" ndarray, and, thus, the returned ndarray does not perform bounds checking or afford any of the guarantees of the non-base ndarray constructor. The primary intent of this function is to reinterpret an ndarray-like object within internal implementations and to do so with minimal overhead. +* +* @param x - input ndarray +* @returns unsigned 8-bit integer ndarray view +* +* @example +* var zeros = require( '@stdlib/ndarray/base/zeros' ); +* +* var x = zeros( 'bool', [ 2, 2 ], 'row-major' ); +* // returns [ [ false, false ], [ false, false ] ] +* +* var out = reinterpretBoolean( x ); +* // returns [ [ 0, 0 ], [ 0, 0 ] ] +*/ +declare function reinterpretBoolean( x: boolndarray ): uint8ndarray; + + +// EXPORTS // + +export = reinterpretBoolean; diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/docs/types/test.ts new file mode 100644 index 000000000000..0664bf30a729 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/docs/types/test.ts @@ -0,0 +1,58 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/base/zeros' ); +import reinterpretBoolean = require( './index' ); + + +// TESTS // + +// The function returns a uint8ndarray... +{ + const x = zeros( 'bool', [ 2, 2 ], 'row-major' ); + + reinterpretBoolean( x ); // $ExpectType uint8ndarray +} + +// The compiler throws an error if the function is not provided a first argument which is a boolndarray... +{ + reinterpretBoolean( '5' ); // $ExpectError + reinterpretBoolean( 5 ); // $ExpectError + reinterpretBoolean( true ); // $ExpectError + reinterpretBoolean( false ); // $ExpectError + reinterpretBoolean( null ); // $ExpectError + reinterpretBoolean( {} ); // $ExpectError + reinterpretBoolean( [ '5' ] ); // $ExpectError + reinterpretBoolean( ( x: number ): number => x ); // $ExpectError + reinterpretBoolean( zeros( 'float64', [ 2, 2 ], 'row-major' ) ); // $ExpectError + reinterpretBoolean( zeros( 'float32', [ 2, 2 ], 'row-major' ) ); // $ExpectError + reinterpretBoolean( zeros( 'int32', [ 2, 2 ], 'row-major' ) ); // $ExpectError + reinterpretBoolean( zeros( 'uint8', [ 2, 2 ], 'row-major' ) ); // $ExpectError + reinterpretBoolean( zeros( 'complex64', [ 2, 2 ], 'row-major' ) ); // $ExpectError + reinterpretBoolean( zeros( 'complex128', [ 2, 2 ], 'row-major' ) ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( 'bool', [ 2, 2 ], 'row-major' ); + + reinterpretBoolean(); // $ExpectError + reinterpretBoolean( x, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/examples/index.js new file mode 100644 index 000000000000..1a64fe2e7ad0 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/examples/index.js @@ -0,0 +1,33 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var bernoulli = require( '@stdlib/random/array/bernoulli' ); +var BooleanArray = require( '@stdlib/array/bool' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var reinterpretBoolean = require( './../lib' ); + +// Create a boolean ndarray: +var buf = new BooleanArray( bernoulli( 4, 0.5 ) ); +var x = ndarray( 'bool', buf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + +// Reinterpret as an unsigned 8-bit integer ndarray: +var out = reinterpretBoolean( x ); +console.log( ndarray2array( out ) ); diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/lib/index.js new file mode 100644 index 000000000000..bc078bf0a966 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/lib/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Reinterpret a boolean ndarray as an unsigned 8-bit integer ndarray. +* +* @module @stdlib/ndarray/base/reinterpret-boolean +* +* @example +* var zeros = require( '@stdlib/ndarray/base/zeros' ); +* var reinterpretBoolean = require( '@stdlib/ndarray/base/reinterpret-boolean' ); +* +* var x = zeros( 'bool', [ 2, 2 ], 'row-major' ); +* // returns [ [ false, false ], [ false, false ] ] +* +* var out = reinterpretBoolean( x ); +* // returns [ [ 0, 0 ], [ 0, 0 ] ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/lib/main.js new file mode 100644 index 000000000000..4040c87651a6 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/lib/main.js @@ -0,0 +1,67 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var reinterpret = require( '@stdlib/strided/base/reinterpret-boolean' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var getShape = require( '@stdlib/ndarray/base/shape' ); +var getStrides = require( '@stdlib/ndarray/base/strides' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getOrder = require( '@stdlib/ndarray/base/order' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); + + +// MAIN // + +/** +* Reinterprets a boolean ndarray as an unsigned 8-bit integer ndarray. +* +* ## Notes +* +* - The returned ndarray is a view on the input ndarray data buffer. +* - The returned ndarray is a "base" ndarray, and, thus, the returned ndarray does not perform bounds checking or afford any of the guarantees of the non-base ndarray constructor. The primary intent of this function is to reinterpret an ndarray-like object within internal implementations and to do so with minimal overhead. +* +* @param {ndarray} x - input ndarray +* @returns {ndarray} unsigned 8-bit integer ndarray view +* +* @example +* var zeros = require( '@stdlib/ndarray/base/zeros' ); +* +* var x = zeros( 'bool', [ 2, 2 ], 'row-major' ); +* // returns [ [ false, false ], [ false, false ] ] +* +* var out = reinterpretBoolean( x ); +* // returns [ [ 0, 0 ], [ 0, 0 ] ] +*/ +function reinterpretBoolean( x ) { + var strides; + var shape; + + shape = getShape( x, true ); + strides = getStrides( x, true ); + + return ndarray( 'uint8', reinterpret( getData( x ), 0 ), shape, strides, getOffset( x ), getOrder( x ) ); +} + + +// EXPORTS // + +module.exports = reinterpretBoolean; diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/package.json b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/package.json new file mode 100644 index 000000000000..fedfaf5ab97a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/ndarray/base/reinterpret-boolean", + "version": "0.0.0", + "description": "Reinterpret a boolean ndarray as an unsigned 8-bit integer ndarray.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "base", + "ndarray", + "reinterpret", + "cast", + "boolean", + "bool", + "uint8", + "view" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/test/test.js b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/test/test.js new file mode 100644 index 000000000000..1df68e814e91 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/test/test.js @@ -0,0 +1,175 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 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. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var BooleanArray = require( '@stdlib/array/bool' ); +var Uint8Array = require( '@stdlib/array/uint8' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var getStrides = require( '@stdlib/ndarray/strides' ); +var getOffset = require( '@stdlib/ndarray/offset' ); +var getOrder = require( '@stdlib/ndarray/order' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var reinterpretBoolean = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof reinterpretBoolean, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns a "base" ndarray instance', function test( t ) { + var buf; + var x; + var y; + + buf = new BooleanArray( [ true, false, true, false ] ); + x = ndarray( 'bool', buf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + y = reinterpretBoolean( x ); + + t.notEqual( y, x, 'returns new instance' ); + t.strictEqual( y instanceof ndarray, true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a reinterpreted ndarray view (row-major)', function test( t ) { + var expected; + var buf; + var x; + var y; + + buf = new BooleanArray( [ true, false, true, false ] ); + x = ndarray( 'bool', buf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + y = reinterpretBoolean( x ); + + expected = [ + [ 1, 0 ], + [ 1, 0 ] + ]; + + t.strictEqual( getData( y ) instanceof Uint8Array, true, 'returns expected value' ); + t.strictEqual( getData( y ).buffer, buf.buffer, 'returns expected value' ); + t.strictEqual( String( getDType( y ) ), 'uint8', 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 2, 1 ], 'returns expected value' ); + t.strictEqual( getOffset( y ), 0, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function returns a reinterpreted ndarray view (column-major)', function test( t ) { + var expected; + var buf; + var x; + var y; + + buf = new BooleanArray( [ true, false, true, false ] ); + x = ndarray( 'bool', buf, [ 2, 2 ], [ 1, 2 ], 0, 'column-major' ); + + y = reinterpretBoolean( x ); + + expected = [ + [ 1, 1 ], + [ 0, 0 ] + ]; + + t.strictEqual( getOrder( y ), 'column-major', 'returns expected value' ); + t.deepEqual( getShape( y ), [ 2, 2 ], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 1, 2 ], 'returns expected value' ); + t.strictEqual( getOffset( y ), 0, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function preserves the input ndarray offset', function test( t ) { + var expected; + var buf; + var x; + var y; + + buf = new BooleanArray( [ true, false, true, false ] ); + x = ndarray( 'bool', buf, [ 2 ], [ 1 ], 1, 'row-major' ); + + y = reinterpretBoolean( x ); + + expected = [ 0, 1 ]; + + t.strictEqual( getOffset( y ), 1, 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 1 ], 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports zero-dimensional input ndarrays', function test( t ) { + var buf; + var x; + var y; + + buf = new BooleanArray( [ true ] ); + x = ndarray( 'bool', buf, [], [ 0 ], 0, 'row-major' ); + + y = reinterpretBoolean( x ); + + t.strictEqual( String( getDType( y ) ), 'uint8', 'returns expected value' ); + t.strictEqual( getOrder( y ), 'row-major', 'returns expected value' ); + t.deepEqual( getShape( y ), [], 'returns expected value' ); + t.deepEqual( getStrides( y ), [ 0 ], 'returns expected value' ); + t.strictEqual( getOffset( y ), 0, 'returns expected value' ); + t.strictEqual( y.get(), 1, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports negative strides', function test( t ) { + var expected; + var buf; + var x; + var y; + + buf = new BooleanArray( [ true, false, true, false ] ); + x = ndarray( 'bool', buf, [ 2, 2 ], [ -2, -1 ], 3, 'row-major' ); + + y = reinterpretBoolean( x ); + + expected = [ + [ 0, 1 ], + [ 0, 1 ] + ]; + + t.deepEqual( getStrides( y ), [ -2, -1 ], 'returns expected value' ); + t.strictEqual( getOffset( y ), 3, 'returns expected value' ); + t.deepEqual( ndarray2array( y ), expected, 'returns expected value' ); + + t.end(); +}); From 8a1c7f3dc97261fe683bc824c55b18d215d2dc0f Mon Sep 17 00:00:00 2001 From: Athan Date: Wed, 22 Apr 2026 02:10:12 -0700 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- .../@stdlib/ndarray/base/reinterpret-boolean/README.md | 4 ++-- .../@stdlib/ndarray/base/reinterpret-boolean/lib/index.js | 4 ++-- .../@stdlib/ndarray/base/reinterpret-boolean/lib/main.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/README.md b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/README.md index ada1461bd248..9a871c841ead 100644 --- a/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/README.md +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/README.md @@ -41,9 +41,9 @@ var reinterpretBoolean = require( '@stdlib/ndarray/base/reinterpret-boolean' ); Reinterprets a boolean [ndarray][@stdlib/ndarray/base/ctor] as an unsigned 8-bit integer [ndarray][@stdlib/ndarray/base/ctor]. ```javascript -var zeros = require( '@stdlib/ndarray/base/zeros' ); +var falses = require( '@stdlib/ndarray/base/falses' ); -var x = zeros( 'bool', [ 2, 2 ], 'row-major' ); +var x = falses( 'bool', [ 2, 2 ], 'row-major' ); // returns [ [ false, false ], [ false, false ] ] var out = reinterpretBoolean( x ); diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/lib/index.js index bc078bf0a966..6ac543c81da3 100644 --- a/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/lib/index.js +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/lib/index.js @@ -24,10 +24,10 @@ * @module @stdlib/ndarray/base/reinterpret-boolean * * @example -* var zeros = require( '@stdlib/ndarray/base/zeros' ); +* var falses = require( '@stdlib/ndarray/base/falses' ); * var reinterpretBoolean = require( '@stdlib/ndarray/base/reinterpret-boolean' ); * -* var x = zeros( 'bool', [ 2, 2 ], 'row-major' ); +* var x = falses( 'bool', [ 2, 2 ], 'row-major' ); * // returns [ [ false, false ], [ false, false ] ] * * var out = reinterpretBoolean( x ); diff --git a/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/lib/main.js index 4040c87651a6..298391590aea 100644 --- a/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/base/reinterpret-boolean/lib/main.js @@ -43,9 +43,9 @@ var getData = require( '@stdlib/ndarray/base/data-buffer' ); * @returns {ndarray} unsigned 8-bit integer ndarray view * * @example -* var zeros = require( '@stdlib/ndarray/base/zeros' ); +* var falses = require( '@stdlib/ndarray/base/falses' ); * -* var x = zeros( 'bool', [ 2, 2 ], 'row-major' ); +* var x = falses( 'bool', [ 2, 2 ], 'row-major' ); * // returns [ [ false, false ], [ false, false ] ] * * var out = reinterpretBoolean( x );