Skip to content

feat: add boolean dtype support to ndarray/base/buffer #2574

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 3 commits into from
Jul 13, 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
32 changes: 11 additions & 21 deletions lib/node_modules/@stdlib/ndarray/base/buffer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@license Apache-2.0

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

# Contiguous Data Buffer

> Create a zero-filled contiguous linear ndarray data buffer.
> Create a contiguous linear ndarray data buffer.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

Expand All @@ -42,30 +42,14 @@ var buffer = require( '@stdlib/ndarray/base/buffer' );

#### buffer( dtype, size )

Returns a zero-filled contiguous linear ndarray data buffer.
Returns a contiguous linear ndarray data buffer having a specified [data type][@stdlib/ndarray/dtypes].

```javascript
var buf = buffer( 'float64', 3 );
// returns <Float64Array>[ 0.0, 0.0, 0.0 ]
```

The function supports the following data types:

- `binary`: binary.
- `complex64`: single-precision complex floating-point numbers.
- `complex128`: double-precision complex floating-point numbers.
- `float32`: single-precision floating-point numbers.
- `float64`: double-precision floating-point numbers.
- `generic`: values of any type.
- `int16`: signed 16-bit integers.
- `int32`: signed 32-bit integers.
- `int8`: signed 8-bit integers.
- `uint16`: unsigned 16-bit integers.
- `uint32`: unsigned 32-bit integers.
- `uint8`: unsigned 8-bit integers.
- `uint8c`: unsigned clamped 8-bit integers.

If provided an unknown or unsupported data type, the function returns `null`.
If provided an unknown or unsupported [data type][@stdlib/ndarray/dtypes], the function returns `null`.

```javascript
var buf = buffer( 'float', 3 );
Expand All @@ -80,6 +64,10 @@ var buf = buffer( 'float', 3 );

<section class="notes">

## Notes

- When provided a numeric [data type][@stdlib/ndarray/dtypes], "generic", or "binary", the function returns a zero-filled contiguous linear ndarray data buffer.

</section>

<!-- /.notes -->
Expand All @@ -97,9 +85,9 @@ var dtypes = require( '@stdlib/ndarray/dtypes' );
var buffer = require( '@stdlib/ndarray/base/buffer' );

var DTYPES = dtypes();

var buf;
var i;

for ( i = 0; i < DTYPES.length; i++ ) {
buf = buffer( DTYPES[ i ], 10 );
console.log( buf );
Expand Down Expand Up @@ -130,6 +118,8 @@ for ( i = 0; i < DTYPES.length; i++ ) {

<section class="links">

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

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 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 Down Expand Up @@ -47,6 +47,63 @@ bench( pkg+':dtype=binary', function benchmark( b ) {
b.end();
});

bench( pkg+':dtype=bool', function benchmark( b ) {
var out;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = buffer( 'bool', 10 );
if ( out.length !== 10 ) {
b.fail( 'should have length 10' );
}
}
b.toc();
if ( !isCollection( out ) ) {
b.fail( 'should return an array-like object' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+':dtype=complex64', function benchmark( b ) {
var out;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = buffer( 'complex64', 10 );
if ( out.length !== 10 ) {
b.fail( 'should have length 10' );
}
}
b.toc();
if ( !isCollection( out ) ) {
b.fail( 'should return an array-like object' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+':dtype=complex128', function benchmark( b ) {
var out;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = buffer( 'complex128', 10 );
if ( out.length !== 10 ) {
b.fail( 'should have length 10' );
}
}
b.toc();
if ( !isCollection( out ) ) {
b.fail( 'should return an array-like object' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+':dtype=float64', function benchmark( b ) {
var out;
var i;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 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 Down Expand Up @@ -90,6 +90,15 @@ function main() {
f = createBenchmark( 'binary', len );
bench( pkg+':len='+len+',dtype=binary', f );

f = createBenchmark( 'bool', len );
bench( pkg+':len='+len+',dtype=bool', f );

f = createBenchmark( 'complex64', len );
bench( pkg+':len='+len+',dtype=complex64', f );

f = createBenchmark( 'complex128', len );
bench( pkg+':len='+len+',dtype=complex128', f );

f = createBenchmark( 'float64', len );
bench( pkg+':len='+len+',dtype=float64', f );

Expand Down
18 changes: 1 addition & 17 deletions lib/node_modules/@stdlib/ndarray/base/buffer/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@

{{alias}}( dtype, size )
Returns a zero-filled contiguous linear ndarray data buffer.

The function supports the following data types:

- binary: binary.
- complex64: single-precision complex floating-point numbers.
- complex128: double-precision complex floating-point numbers.
- float32: single-precision floating-point numbers.
- float64: double-precision floating-point numbers.
- generic: values of any type.
- int16: signed 16-bit integers.
- int32: signed 32-bit integers.
- int8: signed 8-bit integers.
- uint16: unsigned 16-bit integers.
- uint32: unsigned 32-bit integers.
- uint8: unsigned 8-bit integers.
- uint8c: unsigned clamped 8-bit integers.
Returns a contiguous linear ndarray data buffer.

Parameters
----------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
/// <reference types="node"/>

import { Buffer } from 'buffer';
import { TypedArray } from '@stdlib/types/array';
import { TypedArray, ComplexTypedArray, BooleanTypedArray } from '@stdlib/types/array';
import { DataType } from '@stdlib/types/ndarray';

/**
* Array or typed array.
*/
type ArrayOrBufferOrTypedArray = Array<any> | TypedArray | Buffer | null;
type ArrayOrBufferOrTypedArray = Array<any> | TypedArray | ComplexTypedArray | BooleanTypedArray | Buffer | null;

Check warning on line 31 in lib/node_modules/@stdlib/ndarray/base/buffer/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type

/**
* Returns a zero-filled contiguous linear ndarray data buffer.
* Returns a contiguous linear ndarray data buffer.
*
* @param dtype - data type
* @param size - buffer size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ var dtypes = require( '@stdlib/ndarray/dtypes' );
var buffer = require( './../lib' );

var DTYPES = dtypes();

var buf;
var i;

for ( i = 0; i < DTYPES.length; i++ ) {
buf = buffer( DTYPES[ i ], 10 );
console.log( buf );
Expand Down
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/ndarray/base/buffer/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
'use strict';

/**
* Create a zero-filled contiguous linear ndarray data buffer.
* Create a contiguous linear ndarray data buffer.
*
* @module @stdlib/ndarray/base/buffer
*
Expand Down
6 changes: 3 additions & 3 deletions lib/node_modules/@stdlib/ndarray/base/buffer/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ function binary( size ) {
}

/**
* Returns a zero-filled typed array.
* Returns a typed array.
*
* @private
* @param {string} dtype - data type
* @param {NonNegativeInteger} size - buffer size
* @returns {(TypedArray|null)} zero-filled typed array
* @returns {(TypedArray|null)} typed array
*/
function typedarray( dtype, size ) {
var ctor = bufferCtors( dtype );
Expand All @@ -76,7 +76,7 @@ function typedarray( dtype, size ) {
// MAIN //

/**
* Returns a zero-filled contiguous linear ndarray data buffer.
* Returns a contiguous linear ndarray data buffer.
*
* @param {string} dtype - data type
* @param {NonNegativeInteger} size - buffer size
Expand Down
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/ndarray/base/buffer/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@stdlib/ndarray/base/buffer",
"version": "0.0.0",
"description": "Create a zero-filled contiguous linear ndarray data buffer.",
"description": "Create a contiguous linear ndarray data buffer.",
"license": "Apache-2.0",
"author": {
"name": "The Stdlib Authors",
Expand Down
10 changes: 8 additions & 2 deletions lib/node_modules/@stdlib/ndarray/base/buffer/test/test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 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 Down Expand Up @@ -33,6 +33,7 @@ var Uint8Array = require( '@stdlib/array/uint8' );
var Uint8ClampedArray = require( '@stdlib/array/uint8c' );
var Complex64Array = require( '@stdlib/array/complex64' );
var Complex128Array = require( '@stdlib/array/complex128' );
var BooleanArray = require( '@stdlib/array/bool' );
var isBuffer = require( '@stdlib/assert/is-buffer' );
var isArray = require( '@stdlib/assert/is-array' );
var isFloat64Array = require( '@stdlib/assert/is-float64array' );
Expand All @@ -46,6 +47,7 @@ var isUint8Array = require( '@stdlib/assert/is-uint8array' );
var isUint8ClampedArray = require( '@stdlib/assert/is-uint8clampedarray' );
var isComplex64Array = require( '@stdlib/assert/is-complex64array' );
var isComplex128Array = require( '@stdlib/assert/is-complex128array' );
var isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
var buffer = require( './../lib' );


Expand All @@ -57,7 +59,7 @@ tape( 'main export is a function', function test( t ) {
t.end();
});

tape( 'the function returns zero-filled contiguous ndarray data buffers', function test( t ) {
tape( 'the function returns contiguous ndarray data buffers', function test( t ) {
var expected;
var dtypes;
var vals;
Expand All @@ -67,6 +69,7 @@ tape( 'the function returns zero-filled contiguous ndarray data buffers', functi

dtypes = [
'binary',
'bool',
'complex64',
'complex128',
'float64',
Expand All @@ -83,6 +86,7 @@ tape( 'the function returns zero-filled contiguous ndarray data buffers', functi
vals = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
expected = [
[ array2buffer( vals ), isBuffer ],
[ new BooleanArray( vals ), isBooleanArray ],
[ new Complex64Array( vals ), isComplex64Array ],
[ new Complex128Array( vals ), isComplex128Array ],
[ new Float64Array( vals ), isFloat64Array ],
Expand Down Expand Up @@ -115,6 +119,7 @@ tape( 'the function returns ndarray data buffers (large allocations)', function

dtypes = [
'binary',
'bool',
'complex64',
'complex128',
'float64',
Expand All @@ -134,6 +139,7 @@ tape( 'the function returns ndarray data buffers (large allocations)', function
}
expected = [
isBuffer,
isBooleanArray,
isComplex64Array,
isComplex128Array,
isFloat64Array,
Expand Down
Loading