Skip to content

Commit 8a55ea2

Browse files
Jaysukh-409kgryte
andauthored
feat: add boolean dtype support to array/filled
PR-URL: #2471 Ref: #2304 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent cfb39e4 commit 8a55ea2

File tree

7 files changed

+319
-55
lines changed

7 files changed

+319
-55
lines changed

lib/node_modules/@stdlib/array/filled/README.md

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
@license Apache-2.0
44
5-
Copyright (c) 2020 The Stdlib Authors.
5+
Copyright (c) 2024 The Stdlib Authors.
66
77
Licensed under the Apache License, Version 2.0 (the "License");
88
you may not use this file except in compliance with the License.
@@ -18,7 +18,7 @@ limitations under the License.
1818
1919
-->
2020

21-
# Filled Array
21+
# filledarray
2222

2323
> Create a filled array.
2424
@@ -42,29 +42,14 @@ var filledarray = require( '@stdlib/array/filled' );
4242

4343
#### filledarray( \[dtype] )
4444

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

4747
```javascript
4848
var arr = filledarray();
4949
// returns <Float64Array>
5050
```
5151

52-
The function recognizes the following data types:
53-
54-
- `float64`: double-precision floating-point numbers (IEEE 754)
55-
- `float32`: single-precision floating-point numbers (IEEE 754)
56-
- `complex128`: double-precision complex floating-point numbers
57-
- `complex64`: single-precision complex floating-point numbers
58-
- `int32`: 32-bit two's complement signed integers
59-
- `uint32`: 32-bit unsigned integers
60-
- `int16`: 16-bit two's complement signed integers
61-
- `uint16`: 16-bit unsigned integers
62-
- `int8`: 8-bit two's complement signed integers
63-
- `uint8`: 8-bit unsigned integers
64-
- `uint8c`: 8-bit unsigned integers clamped to `0-255`
65-
- `generic`: generic JavaScript values
66-
67-
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.
52+
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.
6853

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

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

223+
[@stdlib/array/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/dtypes
224+
238225
<!-- <related-links> -->
239226

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

lib/node_modules/@stdlib/array/filled/benchmark/benchmark.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ bench( pkg+':dtype=float32', function benchmark( b ) {
8585
b.end();
8686
});
8787

88+
bench( pkg+':dtype=bool', function benchmark( b ) {
89+
var arr;
90+
var i;
91+
b.tic();
92+
for ( i = 0; i < b.iterations; i++ ) {
93+
arr = filledarray( true, 0, 'bool' );
94+
if ( arr.length !== 0 ) {
95+
b.fail( 'should have length 0' );
96+
}
97+
}
98+
b.toc();
99+
if ( !isTypedArrayLike( arr ) ) {
100+
b.fail( 'should return a typed array' );
101+
}
102+
b.pass( 'benchmark finished' );
103+
b.end();
104+
});
105+
88106
bench( pkg+':dtype=complex128', function benchmark( b ) {
89107
var arr;
90108
var v;
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var isTypedArrayLike = require( '@stdlib/assert/is-typed-array-like' );
26+
var pkg = require( './../package.json' ).name;
27+
var filledarray = require( './../lib' );
28+
29+
30+
// FUNCTIONS //
31+
32+
/**
33+
* Creates a benchmark function.
34+
*
35+
* @private
36+
* @param {PositiveInteger} len - array length
37+
* @returns {Function} benchmark function
38+
*/
39+
function createBenchmark( len ) {
40+
return benchmark;
41+
42+
/**
43+
* Benchmark function.
44+
*
45+
* @private
46+
* @param {Benchmark} b - benchmark instance
47+
*/
48+
function benchmark( b ) {
49+
var arr;
50+
var i;
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
arr = filledarray( true, len, 'bool' );
55+
if ( arr.length !== len ) {
56+
b.fail( 'unexpected length' );
57+
}
58+
}
59+
b.toc();
60+
if ( !isTypedArrayLike( arr ) ) {
61+
b.fail( 'should return a typed array' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
}
66+
}
67+
68+
69+
// MAIN //
70+
71+
/**
72+
* Main execution sequence.
73+
*
74+
* @private
75+
*/
76+
function main() {
77+
var len;
78+
var min;
79+
var max;
80+
var f;
81+
var i;
82+
83+
min = 1; // 10^min
84+
max = 6; // 10^max
85+
86+
for ( i = min; i <= max; i++ ) {
87+
len = pow( 10, i );
88+
f = createBenchmark( len );
89+
bench( pkg+':dtype=bool,len='+len, f );
90+
}
91+
}
92+
93+
main();

lib/node_modules/@stdlib/array/filled/docs/repl.txt

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,6 @@
22
{{alias}}( [dtype] )
33
Creates a filled array.
44

5-
The function supports the following data types:
6-
7-
- float64: double-precision floating-point numbers (IEEE 754)
8-
- float32: single-precision floating-point numbers (IEEE 754)
9-
- complex128: double-precision complex floating-point numbers
10-
- complex64: single-precision complex floating-point numbers
11-
- int32: 32-bit two's complement signed integers
12-
- uint32: 32-bit unsigned integers
13-
- int16: 16-bit two's complement signed integers
14-
- uint16: 16-bit unsigned integers
15-
- int8: 8-bit two's complement signed integers
16-
- uint8: 8-bit unsigned integers
17-
- uint8c: 8-bit unsigned integers clamped to 0-255
18-
- generic: generic JavaScript values
19-
20-
The default array data type is `float64`.
21-
225
Parameters
236
----------
247
dtype: string (optional)

lib/node_modules/@stdlib/array/filled/docs/types/index.d.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,6 @@ import { IterableIterator } from '@stdlib/types/iter';
2828
/**
2929
* Creates a filled array.
3030
*
31-
* The function recognizes the following data types:
32-
*
33-
* - `float64`: double-precision floating-point numbers (IEEE 754)
34-
* - `float32`: single-precision floating-point numbers (IEEE 754)
35-
* - `complex128`: double-precision complex floating-point numbers
36-
* - `complex64`: single-precision complex floating-point numbers
37-
* - `int32`: 32-bit two's complement signed integers
38-
* - `uint32`: 32-bit unsigned integers
39-
* - `int16`: 16-bit two's complement signed integers
40-
* - `uint16`: 16-bit unsigned integers
41-
* - `int8`: 8-bit two's complement signed integers
42-
* - `uint8`: 8-bit unsigned integers
43-
* - `uint8c`: 8-bit unsigned integers clamped to `0-255`
44-
* - `generic`: generic JavaScript values
45-
*
4631
* @param dtype - data type (default: 'float64')
4732
* @returns filled array
4833
*

lib/node_modules/@stdlib/array/filled/lib/main.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
// MODULES //
2222

23+
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' );
2325
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
2426
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
2527
var isCollection = require( '@stdlib/assert/is-collection' );
@@ -29,6 +31,7 @@ var isFunction = require( '@stdlib/assert/is-function' );
2931
var ctors = require( '@stdlib/array/ctors' );
3032
var gfill = require( '@stdlib/blas/ext/base/gfill' );
3133
var filled = require( '@stdlib/array/base/filled' );
34+
var reinterpretBool = require( '@stdlib/strided/base/reinterpret-boolean' );
3235
var hasIteratorSymbolSupport = require( '@stdlib/assert/has-iterator-symbol-support' );
3336
var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' );
3437
var iterLength = require( '@stdlib/iter/length' );
@@ -265,10 +268,13 @@ function filledarray() {
265268
arr = new ctor( arguments[1], arguments[2], arguments[3] ); // (ArrayBuffer, byteOffset, length)
266269
}
267270
if ( arr.length > 0 ) {
268-
if ( /^complex/.test( dtype ) ) {
269-
filledAccessors( arr, arguments[ 0 ] );
271+
value = arguments[ 0 ];
272+
if ( isComplexDataType( dtype ) ) {
273+
filledAccessors( arr, value );
274+
} else if ( isBooleanDataType( dtype ) ) {
275+
gfill( arr.length, ( value ) ? 1 : 0, reinterpretBool( arr, 0 ), 1 ); // eslint-disable-line max-len
270276
} else {
271-
gfill( arr.length, arguments[ 0 ], arr, 1 );
277+
gfill( arr.length, value, arr, 1 );
272278
}
273279
}
274280
return arr;

0 commit comments

Comments
 (0)