Skip to content

Commit 4182122

Browse files
committed
fix: ensure support for "generic" dtypes
--- 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: na - task: lint_repl_help status: passed - task: lint_javascript_src status: na - 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 ---
1 parent 01768f7 commit 4182122

8 files changed

Lines changed: 176 additions & 3 deletions

File tree

lib/node_modules/@stdlib/ndarray/base/trues/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var dt = String( getDType( arr ) );
5656

5757
The function accepts the following arguments:
5858

59-
- **dtype**: underlying [data type][@stdlib/ndarray/dtypes]. Must be a "boolean" [data type][@stdlib/ndarray/dtypes].
59+
- **dtype**: underlying [data type][@stdlib/ndarray/dtypes]. Must be a boolean or "generic" [data type][@stdlib/ndarray/dtypes].
6060
- **shape**: array shape.
6161
- **order**: specifies whether an [ndarray][@stdlib/ndarray/base/ctor] is `'row-major'` (C-style) or `'column-major'` (Fortran-style).
6262

@@ -86,6 +86,9 @@ var trues = require( '@stdlib/ndarray/base/trues' );
8686

8787
var arr = trues( 'bool', [ 2, 2 ], 'row-major' );
8888
console.log( ndarray2array( arr ) );
89+
90+
arr = trues( 'generic', [ 2, 2 ], 'row-major' );
91+
console.log( ndarray2array( arr ) );
8992
```
9093

9194
</section>

lib/node_modules/@stdlib/ndarray/base/trues/benchmark/benchmark.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,21 @@ bench( format( '%s:dtype=bool', pkg ), function benchmark( b ) {
4646
b.pass( 'benchmark finished' );
4747
b.end();
4848
});
49+
50+
bench( format( '%s:dtype=generic', pkg ), function benchmark( b ) {
51+
var arr;
52+
var i;
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
arr = trues( 'generic', [ 0 ], 'row-major' );
56+
if ( arr.length !== 0 ) {
57+
b.fail( 'should have length 0' );
58+
}
59+
}
60+
b.toc();
61+
if ( !isndarrayLike( arr ) ) {
62+
b.fail( 'should return an ndarray' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
26+
var format = require( '@stdlib/string/format' );
27+
var pkg = require( './../package.json' ).name;
28+
var trues = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( 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 = trues( 'generic', [ len ], 'row-major' );
56+
if ( arr.length !== len ) {
57+
b.fail( 'unexpected length' );
58+
}
59+
}
60+
b.toc();
61+
if ( !isndarrayLike( arr ) ) {
62+
b.fail( 'should return an ndarray' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
}
67+
}
68+
69+
70+
// MAIN //
71+
72+
/**
73+
* Main execution sequence.
74+
*
75+
* @private
76+
*/
77+
function main() {
78+
var len;
79+
var min;
80+
var max;
81+
var f;
82+
var i;
83+
84+
min = 1; // 10^min
85+
max = 6; // 10^max
86+
87+
for ( i = min; i <= max; i++ ) {
88+
len = pow( 10, i );
89+
f = createBenchmark( len );
90+
bench( format( '%s:dtype=generic,size=%d', pkg, len ), f );
91+
}
92+
}
93+
94+
main();

lib/node_modules/@stdlib/ndarray/base/trues/docs/repl.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Parameters
77
----------
88
dtype: string|DataType
9-
Underlying data type. Must be a "boolean" data type.
9+
Underlying data type. Must be a boolean or "generic" data type.
1010

1111
shape: ArrayLikeObject<integer>
1212
Array shape.

lib/node_modules/@stdlib/ndarray/base/trues/docs/types/index.d.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

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

23-
import { Shape, Order, boolndarray, BooleanDataType } from '@stdlib/types/ndarray';
23+
import { Shape, Order, boolndarray, genericndarray, BooleanDataType, GenericDataType } from '@stdlib/types/ndarray';
2424

2525
/**
2626
* Creates an ndarray filled with `true` values and having a specified shape and data type.
@@ -41,6 +41,25 @@ import { Shape, Order, boolndarray, BooleanDataType } from '@stdlib/types/ndarra
4141
*/
4242
declare function trues( dtype: BooleanDataType, shape: Shape, order: Order ): boolndarray;
4343

44+
/**
45+
* Creates an ndarray filled with `true` values and having a specified shape and data type.
46+
*
47+
* @param dtype - underlying data type
48+
* @param shape - array shape
49+
* @param order - specifies whether an array is row-major (C-style) or column-major (Fortran-style)
50+
* @returns `true`-filled array
51+
*
52+
* @example
53+
* var getDType = require( '@stdlib/ndarray/dtype' );
54+
*
55+
* var arr = trues( 'generic', [ 2, 2 ], 'row-major' );
56+
* // returns <ndarray>[ [ true, true ], [ true, true ] ]
57+
*
58+
* var dt = String( getDType( arr ) );
59+
* // returns 'generic'
60+
*/
61+
declare function trues( dtype: GenericDataType, shape: Shape, order: Order ): genericndarray<boolean>;
62+
4463

4564
// EXPORTS //
4665

lib/node_modules/@stdlib/ndarray/base/trues/docs/types/test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import trues = require( './index' );
2525
{
2626
trues( 'bool', [ 2, 2 ], 'row-major' ); // $ExpectType boolndarray
2727
trues( 'bool', [ 2, 2 ], 'column-major' ); // $ExpectType boolndarray
28+
trues( 'generic', [ 2, 2 ], 'column-major' ); // $ExpectType genericndarray<boolean>
2829
}
2930

3031
// The compiler throws an error if the function is provided a first argument which is an unrecognized/unsupported data type...

lib/node_modules/@stdlib/ndarray/base/trues/examples/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ var trues = require( './../lib' );
2323

2424
var arr = trues( 'bool', [ 2, 2 ], 'row-major' );
2525
console.log( ndarray2array( arr ) );
26+
27+
arr = trues( 'generic', [ 2, 2 ], 'row-major' );
28+
console.log( ndarray2array( arr ) );

lib/node_modules/@stdlib/ndarray/base/trues/test/test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
var tape = require( 'tape' );
2424
var instanceOf = require( '@stdlib/assert/instance-of' );
25+
var isEqualArray = require( '@stdlib/assert/is-equal-array' );
2526
var isEqualBooleanArray = require( '@stdlib/assert/is-equal-booleanarray' );
2627
var BooleanArray = require( '@stdlib/array/bool' );
2728
var ndarray = require( '@stdlib/ndarray/base/ctor' );
@@ -109,6 +110,40 @@ tape( 'the function returns a `true`-filled array (dtype=bool, order=column-majo
109110
t.end();
110111
});
111112

113+
tape( 'the function returns a `true`-filled array (dtype=generic, order=row-major)', function test( t ) {
114+
var expected;
115+
var arr;
116+
117+
expected = [ true, true, true, true ];
118+
119+
arr = trues( 'generic', [ 2, 2 ], 'row-major' );
120+
t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' );
121+
t.strictEqual( String( getDType( arr ) ), 'generic', 'returns expected value' );
122+
t.deepEqual( getShape( arr ), [ 2, 2 ], 'returns expected value' );
123+
t.strictEqual( instanceOf( getData( arr ), Array ), true, 'returns expected value' );
124+
t.strictEqual( isEqualArray( getData( arr ), expected ), true, 'returns expected value' );
125+
t.strictEqual( getOrder( arr ), 'row-major', 'returns expected value' );
126+
127+
t.end();
128+
});
129+
130+
tape( 'the function returns a `true`-filled array (dtype=generic, order=column-major)', function test( t ) {
131+
var expected;
132+
var arr;
133+
134+
expected = [ true, true, true, true ];
135+
136+
arr = trues( 'generic', [ 2, 2 ], 'column-major' );
137+
t.strictEqual( instanceOf( arr, ndarray ), true, 'returns expected value' );
138+
t.strictEqual( String( getDType( arr ) ), 'generic', 'returns expected value' );
139+
t.deepEqual( getShape( arr ), [ 2, 2 ], 'returns expected value' );
140+
t.strictEqual( instanceOf( getData( arr ), Array ), true, 'returns expected value' );
141+
t.strictEqual( isEqualArray( getData( arr ), expected ), true, 'returns expected value' );
142+
t.strictEqual( getOrder( arr ), 'column-major', 'returns expected value' );
143+
144+
t.end();
145+
});
146+
112147
tape( 'the function supports zero-dimensional arrays', function test( t ) {
113148
var expected;
114149
var arr;

0 commit comments

Comments
 (0)