-
-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
strided/base/reinterpret-boolean
PR-URL: #2297 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
- Loading branch information
1 parent
3edcfe5
commit 1d52f5a
Showing
10 changed files
with
651 additions
and
0 deletions.
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
lib/node_modules/@stdlib/strided/base/reinterpret-boolean/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
<!-- | ||
@license Apache-2.0 | ||
Copyright (c) 2024 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. | ||
--> | ||
|
||
# reinterpret | ||
|
||
> Reinterpret a [`BooleanArray`][@stdlib/array/bool] as a [`Uint8Array`][@stdlib/array/uint8]. | ||
<!-- 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 reinterpret = require( '@stdlib/strided/base/reinterpret-boolean' ); | ||
``` | ||
|
||
#### reinterpret( x, offset ) | ||
|
||
Returns a [`Uint8Array`][@stdlib/array/uint8] view of a [`BooleanArray`][@stdlib/array/bool]. | ||
|
||
```javascript | ||
var BooleanArray = require( '@stdlib/array/bool' ); | ||
|
||
var x = new BooleanArray( 10 ); | ||
|
||
var view = reinterpret( x, 0 ); | ||
// returns <Uint8Array> | ||
|
||
var bool = ( view.buffer === x.buffer ); | ||
// returns true | ||
|
||
var len = view.length; | ||
// returns 10 | ||
``` | ||
|
||
The `offset` argument specifies the starting index of the returned [`Uint8Array`][@stdlib/array/uint8] view relative to the [`BooleanArray`][@stdlib/array/bool]. | ||
|
||
```javascript | ||
var BooleanArray = require( '@stdlib/array/bool' ); | ||
|
||
var x = new BooleanArray( [ true, false, false, true, true, false ] ); | ||
|
||
var view = reinterpret( x, 2 ); | ||
// returns <Uint8Array> | ||
|
||
var len = view.length; | ||
// returns 4 | ||
|
||
var v = view[ 0 ]; | ||
// returns 0 | ||
|
||
v = view[ 1 ]; | ||
// returns 1 | ||
``` | ||
|
||
</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" --> | ||
|
||
```javascript | ||
var BooleanArray = require( '@stdlib/array/bool' ); | ||
var reinterpret = require( '@stdlib/strided/base/reinterpret-boolean' ); | ||
|
||
// Define a boolean array: | ||
var x = new BooleanArray( [ true, false, false, true, true, false ] ); | ||
// returns <BooleanArray> | ||
|
||
// Reinterpret as a `uint8` array: | ||
var view = reinterpret( x, 0 ); | ||
// returns <Uint8Array> | ||
|
||
// Set view elements: | ||
view[ 0 ] = 0; | ||
view[ 1 ] = 1; | ||
|
||
// Get the first element of the boolean array: | ||
var v = x.get( 0 ); | ||
// returns false | ||
|
||
// Get the second element of the boolean array: | ||
v = x.get( 1 ); | ||
// returns true | ||
``` | ||
|
||
</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/array/bool]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/bool | ||
|
||
[@stdlib/array/uint8]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/uint8 | ||
|
||
</section> | ||
|
||
<!-- /.links --> |
56 changes: 56 additions & 0 deletions
56
lib/node_modules/@stdlib/strided/base/reinterpret-boolean/benchmark/benchmark.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 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 BooleanArray = require( '@stdlib/array/bool' ); | ||
var isUint8Array = require( '@stdlib/assert/is-uint8array' ); | ||
var pkg = require( './../package.json' ).name; | ||
var reinterpret = require( './../lib' ); | ||
|
||
|
||
// MAIN // | ||
|
||
bench( pkg, function benchmark( b ) { | ||
var values; | ||
var out; | ||
var i; | ||
|
||
values = [ | ||
new BooleanArray( 10 ), | ||
new BooleanArray( 5 ), | ||
new BooleanArray( 20 ) | ||
]; | ||
|
||
b.tic(); | ||
for ( i = 0; i < b.iterations; i++ ) { | ||
out = reinterpret( values[ i%values.length ], 1 ); | ||
if ( typeof out !== 'object' ) { | ||
b.fail( 'should return an object' ); | ||
} | ||
} | ||
b.toc(); | ||
if ( !isUint8Array( out ) ) { | ||
b.fail( 'should return a Uint8Array' ); | ||
} | ||
b.pass( 'benchmark finished' ); | ||
b.end(); | ||
}); |
28 changes: 28 additions & 0 deletions
28
lib/node_modules/@stdlib/strided/base/reinterpret-boolean/docs/repl.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
{{alias}}( x, offset ) | ||
Returns a Uint8Array view of a BooleanArray. | ||
|
||
Parameters | ||
---------- | ||
x: BooleanArray | ||
Input array. | ||
|
||
offset: integer | ||
Starting index of the view relative to the BooleanArray. | ||
|
||
Returns | ||
------- | ||
out: Uint8Array | ||
Uint8Array view. | ||
|
||
Examples | ||
-------- | ||
> var x = new {{alias:@stdlib/array/bool}}( 10 ); | ||
> var out = {{alias}}( x, 0 ) | ||
<Uint8Array> | ||
> var bool = ( out.buffer === x.buffer ) | ||
true | ||
|
||
See Also | ||
-------- | ||
|
48 changes: 48 additions & 0 deletions
48
lib/node_modules/@stdlib/strided/base/reinterpret-boolean/docs/types/index.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 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 { BooleanArray } from '@stdlib/types/array'; | ||
|
||
/** | ||
* Reinterprets a `BooleanArray` as a `Uint8Array`. | ||
* | ||
* @param x - input array | ||
* @param offset - starting index | ||
* @returns `Uint8Array` view | ||
* | ||
* @example | ||
* var BooleanArray = require( '@stdlib/array/bool' ); | ||
* | ||
* var x = new BooleanArray( 10 ); | ||
* | ||
* var out = reinterpret( x, 0 ); | ||
* // returns <Uint8Array> | ||
* | ||
* var bool = ( out.buffer === x.buffer ); | ||
* // returns true | ||
*/ | ||
declare function reinterpret( x: BooleanArray, offset: number ): Uint8Array; | ||
|
||
|
||
// EXPORTS // | ||
|
||
export = reinterpret; |
55 changes: 55 additions & 0 deletions
55
lib/node_modules/@stdlib/strided/base/reinterpret-boolean/docs/types/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 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 BooleanArray = require( '@stdlib/array/bool' ); | ||
import reinterpret = require( './index' ); | ||
|
||
|
||
// TESTS // | ||
|
||
// The function returns a Uint8Array... | ||
{ | ||
reinterpret( new BooleanArray( 10 ), 0 ); // $ExpectType Uint8Array | ||
} | ||
|
||
// The compiler throws an error if not provided a first argument which is a BooleanArray... | ||
{ | ||
reinterpret( '10', 0 ); // $ExpectError | ||
reinterpret( 10, 0 ); // $ExpectError | ||
reinterpret( true, 0 ); // $ExpectError | ||
reinterpret( false, 0 ); // $ExpectError | ||
reinterpret( null, 0 ); // $ExpectError | ||
reinterpret( undefined, 0 ); // $ExpectError | ||
reinterpret( [], 0 ); // $ExpectError | ||
reinterpret( {}, 0 ); // $ExpectError | ||
reinterpret( ( x: number ): number => x, 0 ); // $ExpectError | ||
} | ||
|
||
// The compiler throws an error if not provided a second argument which is a number... | ||
{ | ||
const x = new BooleanArray( 10 ); | ||
|
||
reinterpret( x, '10' ); // $ExpectError | ||
reinterpret( x, true ); // $ExpectError | ||
reinterpret( x, false ); // $ExpectError | ||
reinterpret( x, null ); // $ExpectError | ||
reinterpret( x, undefined ); // $ExpectError | ||
reinterpret( x, [] ); // $ExpectError | ||
reinterpret( x, {} ); // $ExpectError | ||
reinterpret( x, ( x: number ): number => x ); // $ExpectError | ||
} |
44 changes: 44 additions & 0 deletions
44
lib/node_modules/@stdlib/strided/base/reinterpret-boolean/examples/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* @license Apache-2.0 | ||
* | ||
* Copyright (c) 2024 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'; | ||
|
||
var BooleanArray = require( '@stdlib/array/bool' ); | ||
var reinterpret = require( './../lib' ); | ||
|
||
// Define a boolean array: | ||
var x = new BooleanArray( [ true, false, false, true, true, false ] ); | ||
// returns <BooleanArray> | ||
|
||
// Reinterpret as a `uint8` array: | ||
var view = reinterpret( x, 0 ); | ||
// returns <Uint8Array> | ||
|
||
// Set view elements: | ||
view[ 0 ] = 0; | ||
view[ 1 ] = 1; | ||
|
||
// Get the first element of the boolean array: | ||
var v = x.get( 0 ); | ||
// returns false | ||
|
||
// Get the second element of the boolean array: | ||
v = x.get( 1 ); | ||
// returns true | ||
|
||
console.log( v ); |
Oops, something went wrong.
1d52f5a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage Report
The above coverage report was generated for the changes in this push.