Skip to content

Commit 88cece6

Browse files
Jaysukh-409kgryte
andauthored
feat: add boolean dtype support to array/pool
PR-URL: #2486 Ref: #2304 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 1d49bc6 commit 88cece6

File tree

11 files changed

+502
-50
lines changed

11 files changed

+502
-50
lines changed

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

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
@license Apache-2.0
44
5-
Copyright (c) 2018 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-
# Typed Array Pool
21+
# typedarraypool
2222

2323
> Allocate typed arrays from a typed array memory pool.
2424
@@ -42,7 +42,7 @@ var typedarraypool = require( '@stdlib/array/pool' );
4242

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

45-
Returns an **uninitialized** [typed array][mdn-typed-array] having a specified data type `dtype`.
45+
Returns an **uninitialized** [typed array][mdn-typed-array] having a specified [data type][@stdlib/array/typed-dtypes] `dtype`.
4646

4747
```javascript
4848
var arr = typedarraypool();
@@ -53,21 +53,7 @@ var arr = typedarraypool();
5353
typedarraypool.free( arr );
5454
```
5555

56-
The function recognizes the following data types:
57-
58-
- `float64`: double-precision floating-point numbers (IEEE 754)
59-
- `float32`: single-precision floating-point numbers (IEEE 754)
60-
- `complex128`: double-precision complex floating-point numbers
61-
- `complex64`: single-precision complex floating-point numbers
62-
- `int32`: 32-bit two's complement signed integers
63-
- `uint32`: 32-bit unsigned integers
64-
- `int16`: 16-bit two's complement signed integers
65-
- `uint16`: 16-bit unsigned integers
66-
- `int8`: 8-bit two's complement signed integers
67-
- `uint8`: 8-bit unsigned integers
68-
- `uint8c`: 8-bit unsigned integers clamped to `0-255`
69-
70-
By default, the output [typed array][mdn-typed-array] is `float64`. To specify an alternative data type, set the `dtype` parameter.
56+
By default, the output [typed array][mdn-typed-array] is `float64`. To specify an alternative [data type][@stdlib/array/typed-dtypes], set the `dtype` parameter.
7157

7258
```javascript
7359
var arr = typedarraypool( 'int32' );
@@ -135,7 +121,7 @@ typedarraypool.free( arr2 );
135121

136122
#### typedarraypool.malloc( \[dtype] )
137123

138-
Returns an **uninitialized** [typed array][mdn-typed-array] having a specified data type `dtype`.
124+
Returns an **uninitialized** [typed array][mdn-typed-array] having a specified [data type][@stdlib/array/typed-dtypes] `dtype`.
139125

140126
```javascript
141127
var arr1 = typedarraypool.malloc();
@@ -207,7 +193,7 @@ typedarraypool.free( arr2 );
207193

208194
#### typedarraypool.calloc( \[dtype] )
209195

210-
Returns a **zero-initialized** [typed array][mdn-typed-array] having a specified data type `dtype`.
196+
Returns a **zero-initialized** [typed array][mdn-typed-array] having a specified [data type][@stdlib/array/typed-dtypes] `dtype`.
211197

212198
```javascript
213199
var arr1 = typedarraypool.calloc();
@@ -461,6 +447,8 @@ console.log( 'nbytes: %d', typedarray.nbytes );
461447

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

450+
[@stdlib/array/typed-dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/typed-dtypes
451+
464452
<!-- <related-links> -->
465453

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

lib/node_modules/@stdlib/array/pool/benchmark/benchmark.calloc.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2018 The Stdlib Authors.
4+
* Copyright (c) 2024 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
var bench = require( '@stdlib/bench' );
2424
var isTypedArray = require( '@stdlib/assert/is-typed-array' );
2525
var isComplexTypedArray = require( '@stdlib/assert/is-complex-typed-array' );
26+
var isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
2627
var pkg = require( './../package.json' ).name;
2728
var typedarray = require( './../lib' );
2829

@@ -209,6 +210,24 @@ bench( pkg+':calloc:dtype=uint8c', function benchmark( b ) {
209210
b.end();
210211
});
211212

213+
bench( pkg+':calloc:dtype=bool', function benchmark( b ) {
214+
var arr;
215+
var i;
216+
b.tic();
217+
for ( i = 0; i < b.iterations; i++ ) {
218+
arr = typedarray.calloc( 0, 'bool' );
219+
if ( arr.length !== 0 ) {
220+
b.fail( 'should have length 0' );
221+
}
222+
}
223+
b.toc();
224+
if ( !isBooleanArray( arr ) ) {
225+
b.fail( 'should return a boolean array' );
226+
}
227+
b.pass( 'benchmark finished' );
228+
b.end();
229+
});
230+
212231
bench( pkg+':calloc:dtype=complex64', function benchmark( b ) {
213232
var arr;
214233
var i;

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2018 The Stdlib Authors.
4+
* Copyright (c) 2024 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
var bench = require( '@stdlib/bench' );
2424
var isTypedArray = require( '@stdlib/assert/is-typed-array' );
2525
var isComplexTypedArray = require( '@stdlib/assert/is-complex-typed-array' );
26+
var isBooleanArray = require('@stdlib/assert/is-booleanarray');
2627
var pkg = require( './../package.json' ).name;
2728
var typedarray = require( './../lib' );
2829

@@ -209,6 +210,24 @@ bench( pkg+':dtype=uint8c', function benchmark( b ) {
209210
b.end();
210211
});
211212

213+
bench( pkg+':dtype=bool', function benchmark( b ) {
214+
var arr;
215+
var i;
216+
b.tic();
217+
for ( i = 0; i < b.iterations; i++ ) {
218+
arr = typedarray( 0, 'bool' );
219+
if ( arr.length !== 0 ) {
220+
b.fail( 'should have length 0' );
221+
}
222+
}
223+
b.toc();
224+
if ( !isBooleanArray( arr ) ) {
225+
b.fail( 'should return a boolean array' );
226+
}
227+
b.pass( 'benchmark finished' );
228+
b.end();
229+
});
230+
212231
bench( pkg+':dtype=complex64', function benchmark( b ) {
213232
var arr;
214233
var i;
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
26+
var pkg = require( './../package.json' ).name;
27+
var typedarray = require( './../lib' );
28+
29+
30+
// FUNCTIONS //
31+
32+
/**
33+
* Creates a benchmark function.
34+
*
35+
* @private
36+
* @param {Function} fcn - allocation function
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( fcn, len ) {
41+
return benchmark;
42+
43+
/**
44+
* Benchmark function.
45+
*
46+
* @private
47+
* @param {Benchmark} b - benchmark instance
48+
*/
49+
function benchmark( b ) {
50+
var arr;
51+
var i;
52+
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
arr = fcn( len, 'bool' );
56+
if ( arr.length !== len ) {
57+
b.fail( 'unexpected length' );
58+
}
59+
typedarray.free( arr );
60+
}
61+
b.toc();
62+
if ( !isBooleanArray( arr ) ) {
63+
b.fail( 'should return a boolean array' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
}
68+
}
69+
70+
71+
// MAIN //
72+
73+
/**
74+
* Main execution sequence.
75+
*
76+
* @private
77+
*/
78+
function main() {
79+
var len;
80+
var min;
81+
var max;
82+
var f;
83+
var i;
84+
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
len = pow( 10, i );
90+
91+
f = createBenchmark( typedarray, len );
92+
bench( pkg+':dtype=bool,len='+len, f );
93+
94+
f = createBenchmark( typedarray.malloc, len );
95+
bench( pkg+':malloc:dtype=bool,len='+len, f );
96+
97+
f = createBenchmark( typedarray.calloc, len );
98+
bench( pkg+':calloc:dtype=bool,len='+len, f );
99+
}
100+
}
101+
102+
main();

lib/node_modules/@stdlib/array/pool/benchmark/benchmark.malloc.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2018 The Stdlib Authors.
4+
* Copyright (c) 2024 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
var bench = require( '@stdlib/bench' );
2424
var isTypedArray = require( '@stdlib/assert/is-typed-array' );
2525
var isComplexTypedArray = require( '@stdlib/assert/is-complex-typed-array' );
26+
var isBooleanArray = require( '@stdlib/assert/is-booleanarray' );
2627
var pkg = require( './../package.json' ).name;
2728
var typedarray = require( './../lib' );
2829

@@ -209,6 +210,24 @@ bench( pkg+':malloc:dtype=uint8c', function benchmark( b ) {
209210
b.end();
210211
});
211212

213+
bench( pkg+':malloc:dtype=bool', function benchmark( b ) {
214+
var arr;
215+
var i;
216+
b.tic();
217+
for ( i = 0; i < b.iterations; i++ ) {
218+
arr = typedarray.malloc( 0, 'bool' );
219+
if ( arr.length !== 0 ) {
220+
b.fail( 'should have length 0' );
221+
}
222+
}
223+
b.toc();
224+
if ( !isBooleanArray( arr ) ) {
225+
b.fail( 'should return a boolean array' );
226+
}
227+
b.pass( 'benchmark finished' );
228+
b.end();
229+
});
230+
212231
bench( pkg+':malloc:dtype=complex64', function benchmark( b ) {
213232
var arr;
214233
var i;

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

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,6 @@
55
Memory is **uninitialized**, which means that the contents of a returned
66
typed array may contain sensitive contents.
77

8-
The function supports the following data types:
9-
10-
- float64: double-precision floating-point numbers (IEEE 754)
11-
- float32: single-precision floating-point numbers (IEEE 754)
12-
- complex128: double-precision complex floating-point numbers
13-
- complex64: single-precision complex floating-point numbers
14-
- int32: 32-bit two's complement signed integers
15-
- uint32: 32-bit unsigned integers
16-
- int16: 16-bit two's complement signed integers
17-
- uint16: 16-bit unsigned integers
18-
- int8: 8-bit two's complement signed integers
19-
- uint8: 8-bit unsigned integers
20-
- uint8c: 8-bit unsigned integers clamped to 0-255
21-
22-
The default typed array data type is `float64`.
23-
248
Parameters
259
----------
2610
dtype: string (optional)
@@ -272,7 +256,7 @@
272256
--------
273257
> var arr = {{alias}}( 5 )
274258
<Float64Array>
275-
> {{alias}}.free( arr )
259+
> {{alias}}.free( arr );
276260

277261

278262
{{alias}}.clear()
@@ -284,7 +268,7 @@
284268
> var arr = {{alias}}( 5 )
285269
<Float64Array>
286270
> {{alias}}.free( arr );
287-
> {{alias}}.clear()
271+
> {{alias}}.clear();
288272

289273

290274
{{alias}}.highWaterMark
@@ -295,6 +279,7 @@
295279
Examples
296280
--------
297281
> {{alias}}.highWaterMark
282+
<number>
298283

299284

300285
{{alias}}.nbytes
@@ -318,6 +303,7 @@
318303
> var arr = {{alias}}( 5 )
319304
<Float64Array>
320305
> {{alias}}.nbytes
306+
<number>
321307

322308

323309
{{alias}}.factory( [options] )

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2021 The Stdlib Authors.
4+
* Copyright (c) 2024 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -20,13 +20,13 @@
2020

2121
/// <reference types="@stdlib/types"/>
2222

23-
import { ArrayLike, RealOrComplexTypedArray, NumericDataType as DataType } from '@stdlib/types/array';
23+
import { ArrayLike, RealOrComplexTypedArray, BooleanArray, NumericDataType as DataType } from '@stdlib/types/array';
2424
import ArrayBuffer = require( '@stdlib/array/buffer' );
2525

2626
/**
2727
* Typed array or null.
2828
*/
29-
type TypedArrayOrNull = RealOrComplexTypedArray | null;
29+
type TypedArrayOrNull = RealOrComplexTypedArray | BooleanArray | null;
3030

3131
/**
3232
* Interface defining pool options.
@@ -255,7 +255,7 @@ interface Pool {
255255
* // Free the allocated typed array buffer for use in a future allocation:
256256
* typedarraypool.free( arr.buffer );
257257
*/
258-
free( buf: RealOrComplexTypedArray | ArrayBuffer ): void;
258+
free( buf: RealOrComplexTypedArray | BooleanArray | ArrayBuffer ): void;
259259

260260
/**
261261
* Clears the typed array pool allowing garbage collection of previously allocated (and currently free) array buffers.

0 commit comments

Comments
 (0)