Skip to content

Commit dcb1c90

Browse files
committed
Add utility to return the minimum accessible index based on specified stride parameters
1 parent 28adeac commit dcb1c90

File tree

10 files changed

+558
-0
lines changed

10 files changed

+558
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2021 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+
# minViewBufferIndex
22+
23+
> Return the minimum accessible index based on a set of provided strided array parameters.
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 minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' );
41+
```
42+
43+
#### minViewBufferIndex( N, stride, offset )
44+
45+
Returns the minimum accessible index based on a set of provided strided array parameters.
46+
47+
```javascript
48+
var idx = minViewBufferIndex( 3, -2, 10 );
49+
// returns 6
50+
```
51+
52+
</section>
53+
54+
<!-- /.usage -->
55+
56+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
57+
58+
<section class="notes">
59+
60+
## Notes
61+
62+
- If `N <= 0`, the function returns the specified `offset`; however, do note that, when `N` equals zero, no strided array elements should be accessed.
63+
64+
</section>
65+
66+
<!-- /.notes -->
67+
68+
<!-- Package usage examples. -->
69+
70+
<section class="examples">
71+
72+
## Examples
73+
74+
<!-- eslint no-undef: "error" -->
75+
76+
```javascript
77+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
78+
var minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' );
79+
80+
// Generate a random number of indexed elements:
81+
var N = discreteUniform( 10, 20 );
82+
83+
// Generate a random stride length:
84+
var stride = discreteUniform( -10, 10 );
85+
86+
// Generate a random offset:
87+
var offset = discreteUniform( 0, 100 ) + ( ( stride < 0 ) ? (1-N)*stride : 0 );
88+
89+
// Compute the minimum accessible index:
90+
var idx = minViewBufferIndex( N, stride, offset );
91+
92+
console.log( 'N: %d, stride: %d, offset: %d => %d', N, stride, offset, idx );
93+
```
94+
95+
</section>
96+
97+
<!-- /.examples -->
98+
99+
<!-- 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. -->
100+
101+
<section class="references">
102+
103+
</section>
104+
105+
<!-- /.references -->
106+
107+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
108+
109+
<section class="related">
110+
111+
</section>
112+
113+
<!-- /.related -->
114+
115+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
116+
117+
<section class="links">
118+
119+
</section>
120+
121+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pkg = require( './../package.json' ).name;
26+
var minViewBufferIndex = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var out;
34+
var i;
35+
36+
values = [
37+
1000,
38+
500,
39+
100
40+
];
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
out = minViewBufferIndex( 10, -5, values[ i%values.length ] );
45+
if ( isnan( out ) ) {
46+
b.fail( 'should not return NaN' );
47+
}
48+
}
49+
b.toc();
50+
if ( isnan( out ) ) {
51+
b.fail( 'should not return NaN' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{{alias}}( N, stride, offset )
3+
Returns the minimum accessible index based on a set of provided strided
4+
array parameters.
5+
6+
Parameters
7+
----------
8+
N: integer
9+
Number of indexed elements.
10+
11+
stride: integer
12+
Stride length.
13+
14+
offset: integer
15+
Starting index.
16+
17+
Returns
18+
-------
19+
idx: integer
20+
Minimum accessible index.
21+
22+
Examples
23+
--------
24+
> var idx = {{alias}}( 3, -2, 10 )
25+
6
26+
27+
See Also
28+
--------
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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: 2.0
20+
21+
/**
22+
* Returns the minimum accessible index based on a set of provided strided array parameters.
23+
*
24+
* @param N - number of indexed elements
25+
* @param stride - stride length
26+
* @param offset - starting index
27+
* @returns minimum accessible index
28+
*
29+
* @example
30+
* var idx = minViewBufferIndex( 3, -2, 10 );
31+
* // returns 6
32+
*/
33+
declare function minViewBufferIndex( N: number, stride: number, offset: number ): number; // tslint:disable-line:max-line-length
34+
35+
36+
// EXPORTS //
37+
38+
export = minViewBufferIndex;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 minViewBufferIndex = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a number...
25+
{
26+
minViewBufferIndex( 3, -2, 10 ); // $ExpectType number
27+
}
28+
29+
// The compiler throws an error if not provided a first argument which is a number...
30+
{
31+
minViewBufferIndex( '10', -2, 10 ); // $ExpectError
32+
minViewBufferIndex( true, -2, 10 ); // $ExpectError
33+
minViewBufferIndex( false, -2, 10 ); // $ExpectError
34+
minViewBufferIndex( null, -2, 10 ); // $ExpectError
35+
minViewBufferIndex( undefined, -2, 10 ); // $ExpectError
36+
minViewBufferIndex( [], -2, 10 ); // $ExpectError
37+
minViewBufferIndex( {}, -2, 10 ); // $ExpectError
38+
minViewBufferIndex( ( x: number ): number => x, -2, 10 ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if not provided a second argument which is a number...
42+
{
43+
minViewBufferIndex( 3, '10', 10 ); // $ExpectError
44+
minViewBufferIndex( 3, true, 10 ); // $ExpectError
45+
minViewBufferIndex( 3, false, 10 ); // $ExpectError
46+
minViewBufferIndex( 3, null, 10 ); // $ExpectError
47+
minViewBufferIndex( 3, undefined, 10 ); // $ExpectError
48+
minViewBufferIndex( 3, [], 10 ); // $ExpectError
49+
minViewBufferIndex( 3, {}, 10 ); // $ExpectError
50+
minViewBufferIndex( 3, ( x: number ): number => x, 10 ); // $ExpectError
51+
}
52+
53+
// The compiler throws an error if not provided a third argument which is a number...
54+
{
55+
minViewBufferIndex( 3, -2, '10' ); // $ExpectError
56+
minViewBufferIndex( 3, -2, true ); // $ExpectError
57+
minViewBufferIndex( 3, -2, false ); // $ExpectError
58+
minViewBufferIndex( 3, -2, null ); // $ExpectError
59+
minViewBufferIndex( 3, -2, undefined ); // $ExpectError
60+
minViewBufferIndex( 3, -2, [] ); // $ExpectError
61+
minViewBufferIndex( 3, -2, {} ); // $ExpectError
62+
minViewBufferIndex( 3, -2, ( x: number ): number => x ); // $ExpectError
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
22+
var minViewBufferIndex = require( './../lib' );
23+
24+
// Generate a random number of indexed elements:
25+
var N = discreteUniform( 10, 20 );
26+
27+
// Generate a random stride length:
28+
var stride = discreteUniform( -10, 10 );
29+
30+
// Generate a random offset:
31+
var offset = discreteUniform( 0, 100 ) + ( ( stride < 0 ) ? (1-N)*stride : 0 );
32+
33+
// Compute the minimum accessible index:
34+
var idx = minViewBufferIndex( N, stride, offset );
35+
36+
console.log( 'N: %d, stride: %d, offset: %d => %d', N, stride, offset, idx );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
/**
22+
* Return the minimum accessible index based on a set of provided strided array parameters.
23+
*
24+
* @module @stdlib/strided/base/min-view-buffer-index
25+
*
26+
* @example
27+
* var minViewBufferIndex = require( '@stdlib/strided/base/min-view-buffer-index' );
28+
*
29+
* var idx = minViewBufferIndex( 3, -2, 10 );
30+
* // returns 6
31+
*/
32+
33+
// MODULES //
34+
35+
var main = require( './main.js' );
36+
37+
38+
// EXPORTS //
39+
40+
module.exports = main;

0 commit comments

Comments
 (0)