Skip to content

Commit

Permalink
Auto-generated commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stdlib-bot committed Dec 31, 2023
1 parent 08912ee commit c7220b0
Show file tree
Hide file tree
Showing 13 changed files with 721 additions and 5 deletions.
145 changes: 145 additions & 0 deletions base/flag/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<!--
@license Apache-2.0
Copyright (c) 2023 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.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

# flag

> Return a specified flag for provided [ndarray][@stdlib/ndarray/base/ctor].
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var flag = require( '@stdlib/ndarray/base/flag' );
```

#### flag( x, name )

Returns a specified flag for provided [ndarray][@stdlib/ndarray/base/ctor].

```javascript
var zeros = require( '@stdlib/ndarray/zeros' );

var x = zeros( [ 3, 2, 3 ] );
// returns <ndarray>

var o = flag( x, 'READONLY' );
// returns <boolean>
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

<!-- eslint-disable new-cap -->

```javascript
var zeros = require( '@stdlib/ndarray/zeros' );
var slice = require( '@stdlib/ndarray/slice' );
var E = require( '@stdlib/slice/multi' );
var S = require( '@stdlib/slice/ctor' );
var flag = require( '@stdlib/ndarray/base/flag' );

// Create an array:
var x = zeros( [ 10, 10, 10, 10 ] );
// returns <ndarray>

// Define some slices:
var slices = [
// :,:,:,:
E( null, null, null, null ),

// 5:10,4,2:4,::-1
E( S( 5, 10 ), 4, S( 2, 4 ), S( null, null, -1 ) ),

// :,:,2,:
E( null, null, 2, null ),

// 1,2,3,:
E( 1, 2, 3, null ),

// 1,3,::2,4::2
E( 1, 3, S( null, null, 2 ), S( 4, null, 2 ) )
];

// Check whether each slice is row-major contiguous...
var s;
var i;
for ( i = 0; i < slices.length; i++ ) {
s = slice( x, slices[ i ] );
console.log( '%s', flag( s, 'ROW_MAJOR_CONTIGUOUS' ) );
}
```

</section>

<!-- /.examples -->

<!-- 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. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/ndarray/tree/main/base/ctor

</section>

<!-- /.links -->
62 changes: 62 additions & 0 deletions base/flag/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var zeros = require( './../../../zeros' );
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var flag = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var values;
var names;
var out;
var i;

values = [
zeros( [ 10, 10, 10, 1 ] ),
zeros( [ 5, 5, 5, 1, 1 ] ),
zeros( [ 3, 4, 5 ] )
];
names = [
'ROW_MAJOR_CONTIGUOUS',
'COLUMN_MAJOR_CONTIGUOUS',
'READONLY'
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = flag( values[ i%values.length ], names[ i%names.length ] );
if ( typeof out !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
}
b.toc();
if ( !isBoolean( out ) ) {
b.fail( 'should return a boolean' );
}
b.pass( 'benchmark finished' );
b.end();
});
25 changes: 25 additions & 0 deletions base/flag/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

{{alias}}( x, name )
Returns a specified flag for a provided ndarray.

Parameters
----------
x: ndarray
Input ndarray.

name: string|symbol
Flag name.

Returns
-------
out: any
Flag value.

Examples
--------
> var out = {{alias}}( {{alias:@stdlib/ndarray/zeros}}( [ 3, 3, 3 ] ), 'READONLY' )
<boolean>

See Also
--------

48 changes: 48 additions & 0 deletions base/flag/docs/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2023 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

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

import { ndarray } from '@stdlib/types/ndarray';

/**
* Flag name.
*/
type Flag = string | symbol | number;

/**
* Returns a specified flag for a provided ndarray.
*
* @param x - input ndarray
* @param name - flag name
* @returns flag value
*
* @example
* var zeros = require( `@stdlib/ndarray/zeros` );
*
* var o = flag( zeros( [ 3, 3, 3 ] ), 'READONLY' );
* // returns <boolean>
*/
declare function flag<T = unknown>( x: ndarray, name: Flag ): T;


// EXPORTS //

export = flag;
57 changes: 57 additions & 0 deletions base/flag/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2023 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import zeros = require( './../../../../zeros' );
import flag = require( './index' );


// TESTS //

// The function returns an ndarray flag...
{
flag<boolean>( zeros( [ 3, 2, 1 ] ), 'READONLY' ); // $ExpectType boolean
}

// The compiler throws an error if the function is provided a first argument which is not an ndarray...
{
flag( '5', 'READONLY' ); // $ExpectError
flag( 5, 'READONLY' ); // $ExpectError
flag( true, 'READONLY' ); // $ExpectError
flag( false, 'READONLY' ); // $ExpectError
flag( null, 'READONLY' ); // $ExpectError
flag( undefined, 'READONLY' ); // $ExpectError
flag( [ '1', '2' ], 'READONLY' ); // $ExpectError
flag( {}, 'READONLY' ); // $ExpectError
flag( ( x: number ): number => x, 'READONLY' ); // $ExpectError
}

// The compiler throws an error if the function is provided a second argument which is not a valid flag name...
{
flag( zeros( [ 3, 2, 1 ] ), null ); // $ExpectError
flag( zeros( [ 3, 2, 1 ] ), undefined ); // $ExpectError
flag( zeros( [ 3, 2, 1 ] ), [ '1', '2' ] ); // $ExpectError
flag( zeros( [ 3, 2, 1 ] ), {} ); // $ExpectError
flag( zeros( [ 3, 2, 1 ] ), ( x: number ): number => x ); // $ExpectError
}

// The compiler throws an error if the function is provided an unsupported number of arguments...
{
flag(); // $ExpectError
flag( zeros( [ 2, 2 ] ) ); // $ExpectError
flag( zeros( [ 2, 2 ] ), 'READONLY', {} ); // $ExpectError
}
57 changes: 57 additions & 0 deletions base/flag/examples/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2023 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/* eslint-disable new-cap */

'use strict';

var zeros = require( './../../../zeros' );
var slice = require( './../../../slice' );
var E = require( '@stdlib/slice/multi' );
var S = require( '@stdlib/slice/ctor' );
var flag = require( './../lib' );

// Create an array:
var x = zeros( [ 10, 10, 10, 10 ] );
// returns <ndarray>

// Define some slices:
var slices = [
// :,:,:,:
E( null, null, null, null ),

// 5:10,4,2:4,::-1
E( S( 5, 10 ), 4, S( 2, 4 ), S( null, null, -1 ) ),

// :,:,2,:
E( null, null, 2, null ),

// 1,2,3,:
E( 1, 2, 3, null ),

// 1,3,::2,4::2
E( 1, 3, S( null, null, 2 ), S( 4, null, 2 ) )
];

// Check whether each slice is row-major contiguous...
var s;
var i;
for ( i = 0; i < slices.length; i++ ) {
s = slice( x, slices[ i ] );
console.log( '%s', flag( s, 'ROW_MAJOR_CONTIGUOUS' ) );
}
Loading

0 comments on commit c7220b0

Please sign in to comment.