Skip to content

Commit 348a463

Browse files
committed
feat: add array/base/indices-complement
--- 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 e588900 commit 348a463

File tree

10 files changed

+582
-0
lines changed

10 files changed

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