Skip to content

Commit a4c7f4f

Browse files
committed
feat: add ndarray/input-casting-policies
--- 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: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - 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 38de753 commit a4c7f4f

File tree

15 files changed

+791
-0
lines changed

15 files changed

+791
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Policies
22+
23+
> List of input ndarray casting policies.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var policies = require( '@stdlib/ndarray/input-casting-policies' );
41+
```
42+
43+
#### policies()
44+
45+
Returns a list of input ndarray casting policies.
46+
47+
```javascript
48+
var out = policies();
49+
// e.g., returns [ 'none', 'promoted', ... ]
50+
```
51+
52+
The output array contains the following policies:
53+
54+
- `none`: do not cast an input ndarray.
55+
- `promoted`: cast an input ndarray to a promoted data type.
56+
- `output`: cast an input ndarray to the data type of the output ndarray.
57+
58+
</section>
59+
60+
<!-- /.usage -->
61+
62+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
63+
64+
<section class="notes">
65+
66+
</section>
67+
68+
<!-- /.notes -->
69+
70+
<!-- Package usage examples. -->
71+
72+
<section class="examples">
73+
74+
## Examples
75+
76+
<!-- eslint no-undef: "error" -->
77+
78+
```javascript
79+
var indexOf = require( '@stdlib/utils/index-of' );
80+
var policies = require( '@stdlib/ndarray/input-casting-policies' );
81+
82+
var POLICIES = policies();
83+
84+
function isPolicy( str ) {
85+
if ( indexOf( POLICIES, str ) === -1 ) {
86+
return false;
87+
}
88+
return true;
89+
}
90+
91+
var bool = isPolicy( 'none' );
92+
// returns true
93+
94+
bool = isPolicy( 'output' );
95+
// returns true
96+
97+
bool = isPolicy( 'promoted' );
98+
// returns true
99+
100+
bool = isPolicy( 'beep' );
101+
// returns false
102+
```
103+
104+
</section>
105+
106+
<!-- /.examples -->
107+
108+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
109+
110+
<section class="references">
111+
112+
</section>
113+
114+
<!-- /.references -->
115+
116+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
117+
118+
<section class="related">
119+
120+
</section>
121+
122+
<!-- /.related -->
123+
124+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
125+
126+
<section class="links">
127+
128+
</section>
129+
130+
<!-- /.links -->
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 isStringArray = require( '@stdlib/assert/is-string-array' ).primitives;
25+
var pkg = require( './../package.json' ).name;
26+
var policies = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var out;
33+
var i;
34+
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
out = policies();
38+
if ( out.length === 0 ) {
39+
b.fail( 'should return a non-empty array' );
40+
}
41+
}
42+
b.toc();
43+
if ( !isStringArray( out ) ) {
44+
b.fail( 'should return an array of strings' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
{{alias}}()
3+
Returns a list of input ndarray casting policies.
4+
5+
The output array contains the following policies:
6+
7+
- none: do not cast an input ndarray.
8+
- promoted: cast an input ndarray to a promoted data type.
9+
- output: cast an input ndarray to the data type of the output ndarray.
10+
11+
Returns
12+
-------
13+
out: Array<string>
14+
List of policies.
15+
16+
Examples
17+
--------
18+
> var out = {{alias}}()
19+
<Array>
20+
21+
See Also
22+
--------
23+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Casting policies.
23+
*/
24+
type Policies = [
25+
'none',
26+
'promoted',
27+
'output'
28+
];
29+
30+
/**
31+
* Returns a list of input ndarray casting policies.
32+
*
33+
* ## Notes
34+
*
35+
* - The output array contains the following policies:
36+
*
37+
* - `none`: do not cast an input ndarray.
38+
* - `promoted`: cast an input ndarray to a promoted data type.
39+
* - `output`: cast an input ndarray to the data type of the output ndarray.
40+
*
41+
* @returns list of policies
42+
*
43+
* @example
44+
* var list = policies();
45+
* // returns [...]
46+
*/
47+
declare function policies(): Policies;
48+
49+
50+
// EXPORTS //
51+
52+
export = policies;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
import policies = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array of strings...
25+
{
26+
policies(); // $ExpectType Policies
27+
}
28+
29+
// The compiler throws an error if the function is provided any arguments...
30+
{
31+
policies( 9 ); // $ExpectError
32+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
var indexOf = require( '@stdlib/utils/index-of' );
22+
var policies = require( './../lib' );
23+
24+
var POLICIES = policies();
25+
26+
function isPolicy( str ) {
27+
if ( indexOf( POLICIES, str ) === -1 ) {
28+
return false;
29+
}
30+
return true;
31+
}
32+
33+
var bool = isPolicy( 'none' );
34+
console.log( bool );
35+
// => true
36+
37+
bool = isPolicy( 'output' );
38+
console.log( bool );
39+
// => true
40+
41+
bool = isPolicy( 'promoted' );
42+
console.log( bool );
43+
// => true
44+
45+
bool = isPolicy( 'beep' );
46+
console.log( bool );
47+
// => false
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
#ifndef STDLIB_NDARRAY_INPUT_CASTING_POLICIES_H
20+
#define STDLIB_NDARRAY_INPUT_CASTING_POLICIES_H
21+
22+
/*
23+
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
24+
*/
25+
#ifdef __cplusplus
26+
extern "C" {
27+
#endif
28+
29+
/**
30+
* Enumeration of input ndarray casting policies.
31+
*/
32+
enum STDLIB_NDARRAY_INPUT_CASTING_POLICY {
33+
// Do not cast an input ndarray:
34+
STDLIB_NDARRAY_INPUT_CASTING_POLICY_NONE = 0,
35+
36+
// Cast an input ndarray to a promoted data type:
37+
STDLIB_NDARRAY_INPUT_CASTING_POLICY_PROMOTED,
38+
39+
// Cast an input ndarray to the data type of the output ndarray:
40+
STDLIB_NDARRAY_INPUT_CASTING_POLICY_OUTPUT,
41+
42+
// "Compute" the number of policies (this works because of how `enum` works: the value is automatically set to the last enumerated type plus 1):
43+
STDLIB_NDARRAY_INPUT_CASTING_NPOLICIES,
44+
45+
// Reserve a signaling value which is guaranteed not to be a valid data type policy enumeration number:
46+
STDLIB_NDARRAY_NO_INPUT_CASTING_POLICY,
47+
48+
// Indicate the start of user defined policy numbers (leaving room for policy growth above):
49+
STDLIB_NDARRAY_USERDEFINED_INPUT_CASTING_POLICY = 256,
50+
};
51+
52+
#ifdef __cplusplus
53+
}
54+
#endif
55+
56+
#endif // !STDLIB_NDARRAY_INPUT_CASTING_POLICIES_H

0 commit comments

Comments
 (0)