-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: add ndarray/base/clip-index
#14432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kgryte
merged 6 commits into
stdlib-js:develop
from
headlessNode:ndarray/base/clip-index
Aug 21, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c77eb5e
feat: add ndarray/base/clip-index
headlessNode 0141d71
fix: copyright year
headlessNode 67f42a9
Merge branch 'develop' into ndarray/base/clip-index
headlessNode 3b2e8a2
fix: apply suggestions from code review
headlessNode bc6cbce
Merge branch 'develop' into ndarray/base/clip-index
headlessNode a0abcac
Apply suggestions from code review
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
216 changes: 216 additions & 0 deletions
216
lib/node_modules/@stdlib/ndarray/base/clip-index/README.md
This file contains hidden or 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,216 @@ | ||
| <!-- | ||
|
|
||
| @license Apache-2.0 | ||
|
|
||
| Copyright (c) 2026 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. | ||
|
|
||
| --> | ||
|
|
||
| # clipIndex | ||
|
|
||
| > Clip an index to the interval `[0,max]`. | ||
|
|
||
| <!-- 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 clipIndex = require( '@stdlib/ndarray/base/clip-index' ); | ||
| ``` | ||
|
|
||
| #### clipIndex( idx, max ) | ||
|
|
||
| Clips an index to the interval `[0,max]`. | ||
|
|
||
| ```javascript | ||
| var idx = clipIndex( 2, 10 ); | ||
| // returns 2 | ||
|
|
||
| idx = clipIndex( -5, 10 ); | ||
| // returns 5 | ||
| ``` | ||
|
|
||
| If provided an out-of-bounds index, the function clips the index to the nearest interval bound. | ||
|
|
||
| ```javascript | ||
| var idx = clipIndex( 15, 10 ); | ||
| // returns 10 | ||
|
|
||
| idx = clipIndex( -15, 10 ); | ||
| // returns 0 | ||
| ``` | ||
|
|
||
| </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"> | ||
|
|
||
| ## Notes | ||
|
|
||
| - Negative indices are normalized according to `max + idx`. | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <!-- Package usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ## Examples | ||
|
|
||
| <!-- eslint no-undef: "error" --> | ||
|
|
||
| ```javascript | ||
| var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); | ||
| var logEachMap = require( '@stdlib/console/log-each-map' ); | ||
| var clipIndex = require( '@stdlib/ndarray/base/clip-index' ); | ||
|
|
||
| var idx = discreteUniform( 100, -20, 20, { | ||
| 'dtype': 'generic' | ||
| }); | ||
|
|
||
| logEachMap( '%d => [0,%d] => %d', idx, 10, clipIndex ); | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| <!-- C interface documentation. --> | ||
|
|
||
| * * * | ||
|
|
||
| <section class="c"> | ||
|
|
||
| ## C APIs | ||
|
|
||
| <!-- 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 --> | ||
|
|
||
| <!-- C usage documentation. --> | ||
|
|
||
| <section class="usage"> | ||
|
|
||
| ### Usage | ||
|
|
||
| ```c | ||
| #include "stdlib/ndarray/base/clip_index.h" | ||
| ``` | ||
|
|
||
| #### stdlib_ndarray_clip_index( idx, max ) | ||
|
|
||
| Clips an index to the interval `[0,max]`. | ||
|
|
||
| ```c | ||
| #include <stdint.h> | ||
|
|
||
| int64_t idx = stdlib_ndarray_clip_index( -2, 8 ); | ||
| // returns 6 | ||
| ``` | ||
|
|
||
| The function accepts the following arguments: | ||
|
|
||
| - **idx**: `[in] int64_t` index. | ||
| - **max**: `[in] int64_t` maximum index. | ||
|
|
||
| ```c | ||
| int64_t stdlib_ndarray_clip_index( const int64_t idx, const int64_t max ); | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.usage --> | ||
|
|
||
| <!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> | ||
|
|
||
| <section class="notes"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.notes --> | ||
|
|
||
| <!-- C API usage examples. --> | ||
|
|
||
| <section class="examples"> | ||
|
|
||
| ### Examples | ||
|
|
||
| ```c | ||
| #include "stdlib/ndarray/base/clip_index.h" | ||
| #include <stdio.h> | ||
| #include <inttypes.h> | ||
|
|
||
| int main( void ) { | ||
| const int64_t idx[] = { -2, 8, 11, 4 }; | ||
|
|
||
| int64_t out; | ||
| int i; | ||
| for ( i = 0; i < 4; i++ ) { | ||
| out = stdlib_ndarray_clip_index( idx[ i ], 10 ); | ||
| printf( "clip_index(%" PRId64 ", 10) => %" PRId64 "\n", idx[ i ], out ); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.examples --> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.c --> | ||
|
|
||
| <!-- 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"> | ||
|
|
||
| </section> | ||
|
|
||
| <!-- /.links --> | ||
54 changes: 54 additions & 0 deletions
54
lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/benchmark.js
This file contains hidden or 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,54 @@ | ||
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2026 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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); | ||
| var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; | ||
| var pkg = require( './../package.json' ).name; | ||
| var clipIndex = require( './../lib' ); | ||
|
|
||
|
|
||
| // MAIN // | ||
|
|
||
| bench( pkg, function benchmark( b ) { | ||
| var values; | ||
| var out; | ||
| var i; | ||
|
|
||
| values = discreteUniform( 100, -20, 20, { | ||
| 'dtype': 'generic' | ||
| }); | ||
|
|
||
| b.tic(); | ||
| for ( i = 0; i < b.iterations; i++ ) { | ||
| out = clipIndex( values[ i%values.length ], 10 ); | ||
| if ( out !== out ) { | ||
| b.fail( 'should not return NaN' ); | ||
| } | ||
| } | ||
| b.toc(); | ||
| if ( !isNonNegativeInteger( out ) ) { | ||
| b.fail( 'should return a nonnegative integer' ); | ||
| } | ||
| b.pass( 'benchmark finished' ); | ||
| b.end(); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.