Skip to content

Commit c7220b0

Browse files
committed
Auto-generated commit
1 parent 08912ee commit c7220b0

File tree

13 files changed

+721
-5
lines changed

13 files changed

+721
-5
lines changed

base/flag/README.md

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 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+
# flag
22+
23+
> Return a specified flag for provided [ndarray][@stdlib/ndarray/base/ctor].
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 flag = require( '@stdlib/ndarray/base/flag' );
41+
```
42+
43+
#### flag( x, name )
44+
45+
Returns a specified flag for provided [ndarray][@stdlib/ndarray/base/ctor].
46+
47+
```javascript
48+
var zeros = require( '@stdlib/ndarray/zeros' );
49+
50+
var x = zeros( [ 3, 2, 3 ] );
51+
// returns <ndarray>
52+
53+
var o = flag( x, 'READONLY' );
54+
// returns <boolean>
55+
```
56+
57+
</section>
58+
59+
<!-- /.usage -->
60+
61+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
62+
63+
<section class="notes">
64+
65+
</section>
66+
67+
<!-- /.notes -->
68+
69+
<!-- Package usage examples. -->
70+
71+
<section class="examples">
72+
73+
## Examples
74+
75+
<!-- eslint no-undef: "error" -->
76+
77+
<!-- eslint-disable new-cap -->
78+
79+
```javascript
80+
var zeros = require( '@stdlib/ndarray/zeros' );
81+
var slice = require( '@stdlib/ndarray/slice' );
82+
var E = require( '@stdlib/slice/multi' );
83+
var S = require( '@stdlib/slice/ctor' );
84+
var flag = require( '@stdlib/ndarray/base/flag' );
85+
86+
// Create an array:
87+
var x = zeros( [ 10, 10, 10, 10 ] );
88+
// returns <ndarray>
89+
90+
// Define some slices:
91+
var slices = [
92+
// :,:,:,:
93+
E( null, null, null, null ),
94+
95+
// 5:10,4,2:4,::-1
96+
E( S( 5, 10 ), 4, S( 2, 4 ), S( null, null, -1 ) ),
97+
98+
// :,:,2,:
99+
E( null, null, 2, null ),
100+
101+
// 1,2,3,:
102+
E( 1, 2, 3, null ),
103+
104+
// 1,3,::2,4::2
105+
E( 1, 3, S( null, null, 2 ), S( 4, null, 2 ) )
106+
];
107+
108+
// Check whether each slice is row-major contiguous...
109+
var s;
110+
var i;
111+
for ( i = 0; i < slices.length; i++ ) {
112+
s = slice( x, slices[ i ] );
113+
console.log( '%s', flag( s, 'ROW_MAJOR_CONTIGUOUS' ) );
114+
}
115+
```
116+
117+
</section>
118+
119+
<!-- /.examples -->
120+
121+
<!-- 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. -->
122+
123+
<section class="references">
124+
125+
</section>
126+
127+
<!-- /.references -->
128+
129+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
130+
131+
<section class="related">
132+
133+
</section>
134+
135+
<!-- /.related -->
136+
137+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
138+
139+
<section class="links">
140+
141+
[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/ndarray/tree/main/base/ctor
142+
143+
</section>
144+
145+
<!-- /.links -->

base/flag/benchmark/benchmark.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 zeros = require( './../../../zeros' );
25+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
26+
var pkg = require( './../package.json' ).name;
27+
var flag = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var values;
34+
var names;
35+
var out;
36+
var i;
37+
38+
values = [
39+
zeros( [ 10, 10, 10, 1 ] ),
40+
zeros( [ 5, 5, 5, 1, 1 ] ),
41+
zeros( [ 3, 4, 5 ] )
42+
];
43+
names = [
44+
'ROW_MAJOR_CONTIGUOUS',
45+
'COLUMN_MAJOR_CONTIGUOUS',
46+
'READONLY'
47+
];
48+
49+
b.tic();
50+
for ( i = 0; i < b.iterations; i++ ) {
51+
out = flag( values[ i%values.length ], names[ i%names.length ] );
52+
if ( typeof out !== 'boolean' ) {
53+
b.fail( 'should return a boolean' );
54+
}
55+
}
56+
b.toc();
57+
if ( !isBoolean( out ) ) {
58+
b.fail( 'should return a boolean' );
59+
}
60+
b.pass( 'benchmark finished' );
61+
b.end();
62+
});

base/flag/docs/repl.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
{{alias}}( x, name )
3+
Returns a specified flag for a provided ndarray.
4+
5+
Parameters
6+
----------
7+
x: ndarray
8+
Input ndarray.
9+
10+
name: string|symbol
11+
Flag name.
12+
13+
Returns
14+
-------
15+
out: any
16+
Flag value.
17+
18+
Examples
19+
--------
20+
> var out = {{alias}}( {{alias:@stdlib/ndarray/zeros}}( [ 3, 3, 3 ] ), 'READONLY' )
21+
<boolean>
22+
23+
See Also
24+
--------
25+

base/flag/docs/types/index.d.ts

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) 2023 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+
/// <reference types="@stdlib/types"/>
22+
23+
import { ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Flag name.
27+
*/
28+
type Flag = string | symbol | number;
29+
30+
/**
31+
* Returns a specified flag for a provided ndarray.
32+
*
33+
* @param x - input ndarray
34+
* @param name - flag name
35+
* @returns flag value
36+
*
37+
* @example
38+
* var zeros = require( `@stdlib/ndarray/zeros` );
39+
*
40+
* var o = flag( zeros( [ 3, 3, 3 ] ), 'READONLY' );
41+
* // returns <boolean>
42+
*/
43+
declare function flag<T = unknown>( x: ndarray, name: Flag ): T;
44+
45+
46+
// EXPORTS //
47+
48+
export = flag;

base/flag/docs/types/test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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 zeros = require( './../../../../zeros' );
20+
import flag = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns an ndarray flag...
26+
{
27+
flag<boolean>( zeros( [ 3, 2, 1 ] ), 'READONLY' ); // $ExpectType boolean
28+
}
29+
30+
// The compiler throws an error if the function is provided a first argument which is not an ndarray...
31+
{
32+
flag( '5', 'READONLY' ); // $ExpectError
33+
flag( 5, 'READONLY' ); // $ExpectError
34+
flag( true, 'READONLY' ); // $ExpectError
35+
flag( false, 'READONLY' ); // $ExpectError
36+
flag( null, 'READONLY' ); // $ExpectError
37+
flag( undefined, 'READONLY' ); // $ExpectError
38+
flag( [ '1', '2' ], 'READONLY' ); // $ExpectError
39+
flag( {}, 'READONLY' ); // $ExpectError
40+
flag( ( x: number ): number => x, 'READONLY' ); // $ExpectError
41+
}
42+
43+
// The compiler throws an error if the function is provided a second argument which is not a valid flag name...
44+
{
45+
flag( zeros( [ 3, 2, 1 ] ), null ); // $ExpectError
46+
flag( zeros( [ 3, 2, 1 ] ), undefined ); // $ExpectError
47+
flag( zeros( [ 3, 2, 1 ] ), [ '1', '2' ] ); // $ExpectError
48+
flag( zeros( [ 3, 2, 1 ] ), {} ); // $ExpectError
49+
flag( zeros( [ 3, 2, 1 ] ), ( x: number ): number => x ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided an unsupported number of arguments...
53+
{
54+
flag(); // $ExpectError
55+
flag( zeros( [ 2, 2 ] ) ); // $ExpectError
56+
flag( zeros( [ 2, 2 ] ), 'READONLY', {} ); // $ExpectError
57+
}

base/flag/examples/index.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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+
/* eslint-disable new-cap */
20+
21+
'use strict';
22+
23+
var zeros = require( './../../../zeros' );
24+
var slice = require( './../../../slice' );
25+
var E = require( '@stdlib/slice/multi' );
26+
var S = require( '@stdlib/slice/ctor' );
27+
var flag = require( './../lib' );
28+
29+
// Create an array:
30+
var x = zeros( [ 10, 10, 10, 10 ] );
31+
// returns <ndarray>
32+
33+
// Define some slices:
34+
var slices = [
35+
// :,:,:,:
36+
E( null, null, null, null ),
37+
38+
// 5:10,4,2:4,::-1
39+
E( S( 5, 10 ), 4, S( 2, 4 ), S( null, null, -1 ) ),
40+
41+
// :,:,2,:
42+
E( null, null, 2, null ),
43+
44+
// 1,2,3,:
45+
E( 1, 2, 3, null ),
46+
47+
// 1,3,::2,4::2
48+
E( 1, 3, S( null, null, 2 ), S( 4, null, 2 ) )
49+
];
50+
51+
// Check whether each slice is row-major contiguous...
52+
var s;
53+
var i;
54+
for ( i = 0; i < slices.length; i++ ) {
55+
s = slice( x, slices[ i ] );
56+
console.log( '%s', flag( s, 'ROW_MAJOR_CONTIGUOUS' ) );
57+
}

0 commit comments

Comments
 (0)