Skip to content

Commit 494a92b

Browse files
committed
feat: add ndarray/base/to-unique-normalized-indices
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 14073ce commit 494a92b

File tree

10 files changed

+600
-0
lines changed

10 files changed

+600
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# toUniqueNormalizedIndices
22+
23+
> Return a list of unique indices after normalizing to the interval `[0,max]`.
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 toUniqueNormalizedIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' );
41+
```
42+
43+
#### toUniqueNormalizedIndices( indices, max )
44+
45+
Returns a list of unique indices after normalizing to the interval `[0,max]`.
46+
47+
```javascript
48+
var idx = toUniqueNormalizedIndices( [ 2, -5 ], 10 );
49+
// returns [ 2, 6 ]
50+
```
51+
52+
If provided an out-of-bounds index, the function returns `null`.
53+
54+
```javascript
55+
var idx = toUniqueNormalizedIndices( [ 15, -15 ], 10 );
56+
// returns null
57+
```
58+
59+
</section>
60+
61+
<!-- /.usage -->
62+
63+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
64+
65+
<section class="notes">
66+
67+
## Notes
68+
69+
- The function preserves provided index order.
70+
- When provided valid indices, the function always returns a "generic" array.
71+
72+
</section>
73+
74+
<!-- /.notes -->
75+
76+
<!-- Package usage examples. -->
77+
78+
<section class="examples">
79+
80+
## Examples
81+
82+
<!-- eslint no-undef: "error" -->
83+
84+
```javascript
85+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
86+
var toUniqueNormalizedIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' );
87+
88+
// Generate a list of random indices:
89+
var idx = discreteUniform( 100, -15, 15, {
90+
'dtype': 'generic'
91+
});
92+
console.log( idx );
93+
94+
// Generate a list of unique indices normalized to the interval `[0, 15]`:
95+
var out = toUniqueNormalizedIndices( idx, 15 );
96+
console.log( out );
97+
```
98+
99+
</section>
100+
101+
<!-- /.examples -->
102+
103+
<!-- 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. -->
104+
105+
<section class="references">
106+
107+
</section>
108+
109+
<!-- /.references -->
110+
111+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
112+
113+
<section class="related">
114+
115+
</section>
116+
117+
<!-- /.related -->
118+
119+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
120+
121+
<section class="links">
122+
123+
</section>
124+
125+
<!-- /.links -->
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) 2025 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 isNonNegativeIntegerArray = require( '@stdlib/assert/is-nonnegative-integer-array' ).primitives;
25+
var Int32Array = require( '@stdlib/array/int32' );
26+
var pkg = require( './../package.json' ).name;
27+
var toUniqueNormalizedIndices = 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+
[ -4, -5, 4, 5 ],
39+
new Int32Array( [ -4, -5, 4, 5 ] )
40+
];
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
out = toUniqueNormalizedIndices( values[ i%values.length ], 10 );
45+
if ( typeof out !== 'object' ) {
46+
b.fail( 'should return an array' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isNonNegativeIntegerArray( out ) ) {
51+
b.fail( 'should return an array of nonnegative integers' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{{alias}}( indices, max )
3+
Returns a list of unique indices after normalizing to the interval [0,max].
4+
5+
If an index exceeds index bounds, the function returns `null`.
6+
7+
Parameters
8+
----------
9+
indices: Collection<integer>
10+
Indices to normalize.
11+
12+
max: integer
13+
Maximum index value.
14+
15+
Returns
16+
-------
17+
out: Array<integer>|null
18+
Normalized indices.
19+
20+
Examples
21+
--------
22+
> var idx = {{alias}}( [ 2, -5 ], 10 )
23+
[ 2, 6 ]
24+
> idx = {{alias}}( [ 15 ], 10 )
25+
null
26+
27+
See Also
28+
--------
29+
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) 2025 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 { Collection } from '@stdlib/types/array';
24+
25+
/**
26+
* Returns a list of unique indices after normalizing to the interval `[0,max]`.
27+
*
28+
* ## Notes
29+
*
30+
* - If provided an out-of-bounds index, the function returns `null`.
31+
*
32+
* @param indices - indices
33+
* @param max - maximum index
34+
* @returns normalized indices (or null)
35+
*
36+
* @example
37+
* var indices = toUniqueNormalizedIndices( [ -2, 5 ], 10 );
38+
* // returns [ 9, 5 ]
39+
*
40+
* indices = toUniqueNormalizedIndices( [ -2, 15 ], 10 );
41+
* // returns null
42+
*/
43+
declare function toUniqueNormalizedIndices( indices: Collection<number>, max: number ): Array<number> | null;
44+
45+
46+
// EXPORTS //
47+
48+
export = toUniqueNormalizedIndices;
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) 2025 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 toUniqueNormalizedIndices = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a collection of numbers or `null`...
25+
{
26+
toUniqueNormalizedIndices( [ 0, 15 ], 10 ); // $ExpectType number[] | null
27+
}
28+
29+
// The compiler throws an error if the function is provided invalid values...
30+
{
31+
toUniqueNormalizedIndices( true, 3 ); // $ExpectError
32+
toUniqueNormalizedIndices( false, 2 ); // $ExpectError
33+
toUniqueNormalizedIndices( '5', 1 ); // $ExpectError
34+
toUniqueNormalizedIndices( [ '5' ], 1 ); // $ExpectError
35+
toUniqueNormalizedIndices( {}, 2 ); // $ExpectError
36+
toUniqueNormalizedIndices( ( x: number ): number => x, 2 ); // $ExpectError
37+
38+
toUniqueNormalizedIndices( [ 9 ], true ); // $ExpectError
39+
toUniqueNormalizedIndices( [ 9 ], false ); // $ExpectError
40+
toUniqueNormalizedIndices( [ 5 ], '5' ); // $ExpectError
41+
toUniqueNormalizedIndices( [ 8 ], [] ); // $ExpectError
42+
toUniqueNormalizedIndices( [ 9 ], {} ); // $ExpectError
43+
toUniqueNormalizedIndices( [ 8 ], ( x: number ): number => x ); // $ExpectError
44+
45+
toUniqueNormalizedIndices( [], true ); // $ExpectError
46+
toUniqueNormalizedIndices( {}, false ); // $ExpectError
47+
toUniqueNormalizedIndices( false, '5' ); // $ExpectError
48+
toUniqueNormalizedIndices( {}, [] ); // $ExpectError
49+
toUniqueNormalizedIndices( '5', ( x: number ): number => x ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided insufficient arguments...
53+
{
54+
toUniqueNormalizedIndices(); // $ExpectError
55+
toUniqueNormalizedIndices( [ 3 ] ); // $ExpectError
56+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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/array/discrete-uniform' );
22+
var toUniqueNormalizedIndices = require( './../lib' );
23+
24+
// Generate a list of random indices:
25+
var idx = discreteUniform( 100, -15, 15, {
26+
'dtype': 'generic'
27+
});
28+
console.log( idx );
29+
30+
// Generate a list of unique indices normalized to the interval `[0, 15]`:
31+
var out = toUniqueNormalizedIndices( idx, 15 );
32+
console.log( out );
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 a list of unique indices after normalizing to the interval `[0,max]`.
23+
*
24+
* @module @stdlib/ndarray/base/to-unique-normalized-indices
25+
*
26+
* @example
27+
* var toUniqueNormalizedIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' );
28+
*
29+
* var idx = toUniqueNormalizedIndices( [ -2, 5 ], 10 );
30+
* // returns [ 9, 5 ]
31+
*
32+
* idx = toUniqueNormalizedIndices( [ 15 ], 10 );
33+
* // returns null
34+
*/
35+
36+
// MODULES //
37+
38+
var main = require( './main.js' );
39+
40+
41+
// EXPORTS //
42+
43+
module.exports = main;

0 commit comments

Comments
 (0)