Skip to content

feat: add boolean dtype support to array/filled #2471

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 12 commits into from
Jun 29, 2024
25 changes: 6 additions & 19 deletions lib/node_modules/@stdlib/array/filled/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@license Apache-2.0

Copyright (c) 2020 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 @@ -18,7 +18,7 @@ limitations under the License.

-->

# Filled Array
# filledarray

> Create a filled array.

Expand All @@ -42,29 +42,14 @@ var filledarray = require( '@stdlib/array/filled' );

#### filledarray( \[dtype] )

Creates a filled array having a specified data type `dtype`.
Creates a filled array having a specified [data type][@stdlib/array/dtypes] `dtype`.

```javascript
var arr = filledarray();
// returns <Float64Array>
```

The function recognizes the following data types:

- `float64`: double-precision floating-point numbers (IEEE 754)
- `float32`: single-precision floating-point numbers (IEEE 754)
- `complex128`: double-precision complex floating-point numbers
- `complex64`: single-precision complex floating-point numbers
- `int32`: 32-bit two's complement signed integers
- `uint32`: 32-bit unsigned integers
- `int16`: 16-bit two's complement signed integers
- `uint16`: 16-bit unsigned integers
- `int8`: 8-bit two's complement signed integers
- `uint8`: 8-bit unsigned integers
- `uint8c`: 8-bit unsigned integers clamped to `0-255`
- `generic`: generic JavaScript values

By default, the output array data type is `float64` (i.e., a [typed array][mdn-typed-array]). To specify an alternative data type, provide a `dtype` argument.
By default, the output array [data type][@stdlib/array/dtypes] is `float64` (i.e., a [typed array][mdn-typed-array]). To specify an alternative [data type][@stdlib/array/dtypes], provide a `dtype` argument.

```javascript
var arr = filledarray( 'int32' );
Expand Down Expand Up @@ -235,6 +220,8 @@ for ( i = 0; i < dt.length; i++ ) {

[mdn-arraybuffer]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer

[@stdlib/array/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/dtypes

<!-- <related-links> -->

[@stdlib/array/filled-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/filled-by
Expand Down
18 changes: 18 additions & 0 deletions lib/node_modules/@stdlib/array/filled/benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ bench( pkg+':dtype=float32', function benchmark( b ) {
b.end();
});

bench( pkg+':dtype=bool', function benchmark( b ) {
var arr;
var i;
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
arr = filledarray( true, 0, 'bool' );
if ( arr.length !== 0 ) {
b.fail( 'should have length 0' );
}
}
b.toc();
if ( !isTypedArrayLike( arr ) ) {
b.fail( 'should return a typed array' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+':dtype=complex128', function benchmark( b ) {
var arr;
var v;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* @license Apache-2.0
*
* 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.
* 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 pow = require( '@stdlib/math/base/special/pow' );
var isTypedArrayLike = require( '@stdlib/assert/is-typed-array-like' );
var pkg = require( './../package.json' ).name;
var filledarray = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var arr;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
arr = filledarray( true, len, 'bool' );
if ( arr.length !== len ) {
b.fail( 'unexpected length' );
}
}
b.toc();
if ( !isTypedArrayLike( arr ) ) {
b.fail( 'should return a typed array' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( pkg+':dtype=bool,len='+len, f );
}
}

main();
17 changes: 0 additions & 17 deletions lib/node_modules/@stdlib/array/filled/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,6 @@
{{alias}}( [dtype] )
Creates a filled array.

The function supports the following data types:

- float64: double-precision floating-point numbers (IEEE 754)
- float32: single-precision floating-point numbers (IEEE 754)
- complex128: double-precision complex floating-point numbers
- complex64: single-precision complex floating-point numbers
- int32: 32-bit two's complement signed integers
- uint32: 32-bit unsigned integers
- int16: 16-bit two's complement signed integers
- uint16: 16-bit unsigned integers
- int8: 8-bit two's complement signed integers
- uint8: 8-bit unsigned integers
- uint8c: 8-bit unsigned integers clamped to 0-255
- generic: generic JavaScript values

The default array data type is `float64`.

Parameters
----------
dtype: string (optional)
Expand Down
15 changes: 0 additions & 15 deletions lib/node_modules/@stdlib/array/filled/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,6 @@
/**
* Creates a filled array.
*
* The function recognizes the following data types:
*
* - `float64`: double-precision floating-point numbers (IEEE 754)
* - `float32`: single-precision floating-point numbers (IEEE 754)
* - `complex128`: double-precision complex floating-point numbers
* - `complex64`: single-precision complex floating-point numbers
* - `int32`: 32-bit two's complement signed integers
* - `uint32`: 32-bit unsigned integers
* - `int16`: 16-bit two's complement signed integers
* - `uint16`: 16-bit unsigned integers
* - `int8`: 8-bit two's complement signed integers
* - `uint8`: 8-bit unsigned integers
* - `uint8c`: 8-bit unsigned integers clamped to `0-255`
* - `generic`: generic JavaScript values
*
* @param dtype - data type (default: 'float64')
* @returns filled array
*
Expand All @@ -54,7 +39,7 @@
* var arr = filledarray( 'float32' );
* // returns <Float32Array>
*/
declare function filledarray<T = any, U extends keyof DataTypeMap<T> = 'float64'>( dtype?: U ): DataTypeMap<any>[U];

Check warning on line 42 in lib/node_modules/@stdlib/array/filled/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

Check warning on line 42 in lib/node_modules/@stdlib/array/filled/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Creates a filled array having a specified `length`.
Expand Down
12 changes: 9 additions & 3 deletions lib/node_modules/@stdlib/array/filled/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

// MODULES //

var isComplexDataType = require( '@stdlib/array/base/assert/is-complex-floating-point-data-type' );
var isBooleanDataType = require( '@stdlib/array/base/assert/is-boolean-data-type' );
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
var isCollection = require( '@stdlib/assert/is-collection' );
Expand All @@ -29,6 +31,7 @@ var isFunction = require( '@stdlib/assert/is-function' );
var ctors = require( '@stdlib/array/ctors' );
var gfill = require( '@stdlib/blas/ext/base/gfill' );
var filled = require( '@stdlib/array/base/filled' );
var reinterpretBool = require( '@stdlib/strided/base/reinterpret-boolean' );
var hasIteratorSymbolSupport = require( '@stdlib/assert/has-iterator-symbol-support' );
var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' );
var iterLength = require( '@stdlib/iter/length' );
Expand Down Expand Up @@ -265,10 +268,13 @@ function filledarray() {
arr = new ctor( arguments[1], arguments[2], arguments[3] ); // (ArrayBuffer, byteOffset, length)
}
if ( arr.length > 0 ) {
if ( /^complex/.test( dtype ) ) {
filledAccessors( arr, arguments[ 0 ] );
value = arguments[ 0 ];
if ( isComplexDataType( dtype ) ) {
filledAccessors( arr, value );
} else if ( isBooleanDataType( dtype ) ) {
gfill( arr.length, ( value ) ? 1 : 0, reinterpretBool( arr, 0 ), 1 ); // eslint-disable-line max-len
} else {
gfill( arr.length, arguments[ 0 ], arr, 1 );
gfill( arr.length, value, arr, 1 );
}
}
return arr;
Expand Down
Loading
Loading