Skip to content

Commit 1d52f5a

Browse files
Jaysukh-409kgryte
andauthored
feat: add strided/base/reinterpret-boolean
PR-URL: #2297 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 3edcfe5 commit 1d52f5a

File tree

10 files changed

+651
-0
lines changed

10 files changed

+651
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 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+
# reinterpret
22+
23+
> Reinterpret a [`BooleanArray`][@stdlib/array/bool] as a [`Uint8Array`][@stdlib/array/uint8].
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 reinterpret = require( '@stdlib/strided/base/reinterpret-boolean' );
41+
```
42+
43+
#### reinterpret( x, offset )
44+
45+
Returns a [`Uint8Array`][@stdlib/array/uint8] view of a [`BooleanArray`][@stdlib/array/bool].
46+
47+
```javascript
48+
var BooleanArray = require( '@stdlib/array/bool' );
49+
50+
var x = new BooleanArray( 10 );
51+
52+
var view = reinterpret( x, 0 );
53+
// returns <Uint8Array>
54+
55+
var bool = ( view.buffer === x.buffer );
56+
// returns true
57+
58+
var len = view.length;
59+
// returns 10
60+
```
61+
62+
The `offset` argument specifies the starting index of the returned [`Uint8Array`][@stdlib/array/uint8] view relative to the [`BooleanArray`][@stdlib/array/bool].
63+
64+
```javascript
65+
var BooleanArray = require( '@stdlib/array/bool' );
66+
67+
var x = new BooleanArray( [ true, false, false, true, true, false ] );
68+
69+
var view = reinterpret( x, 2 );
70+
// returns <Uint8Array>
71+
72+
var len = view.length;
73+
// returns 4
74+
75+
var v = view[ 0 ];
76+
// returns 0
77+
78+
v = view[ 1 ];
79+
// returns 1
80+
```
81+
82+
</section>
83+
84+
<!-- /.usage -->
85+
86+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
87+
88+
<section class="notes">
89+
90+
</section>
91+
92+
<!-- /.notes -->
93+
94+
<!-- Package usage examples. -->
95+
96+
<section class="examples">
97+
98+
## Examples
99+
100+
<!-- eslint no-undef: "error" -->
101+
102+
```javascript
103+
var BooleanArray = require( '@stdlib/array/bool' );
104+
var reinterpret = require( '@stdlib/strided/base/reinterpret-boolean' );
105+
106+
// Define a boolean array:
107+
var x = new BooleanArray( [ true, false, false, true, true, false ] );
108+
// returns <BooleanArray>
109+
110+
// Reinterpret as a `uint8` array:
111+
var view = reinterpret( x, 0 );
112+
// returns <Uint8Array>
113+
114+
// Set view elements:
115+
view[ 0 ] = 0;
116+
view[ 1 ] = 1;
117+
118+
// Get the first element of the boolean array:
119+
var v = x.get( 0 );
120+
// returns false
121+
122+
// Get the second element of the boolean array:
123+
v = x.get( 1 );
124+
// returns true
125+
```
126+
127+
</section>
128+
129+
<!-- /.examples -->
130+
131+
<!-- 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. -->
132+
133+
<section class="references">
134+
135+
</section>
136+
137+
<!-- /.references -->
138+
139+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
140+
141+
<section class="related">
142+
143+
</section>
144+
145+
<!-- /.related -->
146+
147+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
148+
149+
<section class="links">
150+
151+
[@stdlib/array/bool]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/bool
152+
153+
[@stdlib/array/uint8]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/uint8
154+
155+
</section>
156+
157+
<!-- /.links -->
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) 2024 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 BooleanArray = require( '@stdlib/array/bool' );
25+
var isUint8Array = require( '@stdlib/assert/is-uint8array' );
26+
var pkg = require( './../package.json' ).name;
27+
var reinterpret = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var values;
34+
var out;
35+
var i;
36+
37+
values = [
38+
new BooleanArray( 10 ),
39+
new BooleanArray( 5 ),
40+
new BooleanArray( 20 )
41+
];
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
out = reinterpret( values[ i%values.length ], 1 );
46+
if ( typeof out !== 'object' ) {
47+
b.fail( 'should return an object' );
48+
}
49+
}
50+
b.toc();
51+
if ( !isUint8Array( out ) ) {
52+
b.fail( 'should return a Uint8Array' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
{{alias}}( x, offset )
3+
Returns a Uint8Array view of a BooleanArray.
4+
5+
Parameters
6+
----------
7+
x: BooleanArray
8+
Input array.
9+
10+
offset: integer
11+
Starting index of the view relative to the BooleanArray.
12+
13+
Returns
14+
-------
15+
out: Uint8Array
16+
Uint8Array view.
17+
18+
Examples
19+
--------
20+
> var x = new {{alias:@stdlib/array/bool}}( 10 );
21+
> var out = {{alias}}( x, 0 )
22+
<Uint8Array>
23+
> var bool = ( out.buffer === x.buffer )
24+
true
25+
26+
See Also
27+
--------
28+
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) 2024 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 { BooleanArray } from '@stdlib/types/array';
24+
25+
/**
26+
* Reinterprets a `BooleanArray` as a `Uint8Array`.
27+
*
28+
* @param x - input array
29+
* @param offset - starting index
30+
* @returns `Uint8Array` view
31+
*
32+
* @example
33+
* var BooleanArray = require( '@stdlib/array/bool' );
34+
*
35+
* var x = new BooleanArray( 10 );
36+
*
37+
* var out = reinterpret( x, 0 );
38+
* // returns <Uint8Array>
39+
*
40+
* var bool = ( out.buffer === x.buffer );
41+
* // returns true
42+
*/
43+
declare function reinterpret( x: BooleanArray, offset: number ): Uint8Array;
44+
45+
46+
// EXPORTS //
47+
48+
export = reinterpret;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 BooleanArray = require( '@stdlib/array/bool' );
20+
import reinterpret = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns a Uint8Array...
26+
{
27+
reinterpret( new BooleanArray( 10 ), 0 ); // $ExpectType Uint8Array
28+
}
29+
30+
// The compiler throws an error if not provided a first argument which is a BooleanArray...
31+
{
32+
reinterpret( '10', 0 ); // $ExpectError
33+
reinterpret( 10, 0 ); // $ExpectError
34+
reinterpret( true, 0 ); // $ExpectError
35+
reinterpret( false, 0 ); // $ExpectError
36+
reinterpret( null, 0 ); // $ExpectError
37+
reinterpret( undefined, 0 ); // $ExpectError
38+
reinterpret( [], 0 ); // $ExpectError
39+
reinterpret( {}, 0 ); // $ExpectError
40+
reinterpret( ( x: number ): number => x, 0 ); // $ExpectError
41+
}
42+
43+
// The compiler throws an error if not provided a second argument which is a number...
44+
{
45+
const x = new BooleanArray( 10 );
46+
47+
reinterpret( x, '10' ); // $ExpectError
48+
reinterpret( x, true ); // $ExpectError
49+
reinterpret( x, false ); // $ExpectError
50+
reinterpret( x, null ); // $ExpectError
51+
reinterpret( x, undefined ); // $ExpectError
52+
reinterpret( x, [] ); // $ExpectError
53+
reinterpret( x, {} ); // $ExpectError
54+
reinterpret( x, ( x: number ): number => x ); // $ExpectError
55+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 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 BooleanArray = require( '@stdlib/array/bool' );
22+
var reinterpret = require( './../lib' );
23+
24+
// Define a boolean array:
25+
var x = new BooleanArray( [ true, false, false, true, true, false ] );
26+
// returns <BooleanArray>
27+
28+
// Reinterpret as a `uint8` array:
29+
var view = reinterpret( x, 0 );
30+
// returns <Uint8Array>
31+
32+
// Set view elements:
33+
view[ 0 ] = 0;
34+
view[ 1 ] = 1;
35+
36+
// Get the first element of the boolean array:
37+
var v = x.get( 0 );
38+
// returns false
39+
40+
// Get the second element of the boolean array:
41+
v = x.get( 1 );
42+
// returns true
43+
44+
console.log( v );

0 commit comments

Comments
 (0)