From c77eb5e1ee632af5ae4dc904966b3b6a000ad641 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Thu, 20 Aug 2026 10:08:01 +0500 Subject: [PATCH 1/4] feat: add ndarray/base/clip-index --- .../@stdlib/ndarray/base/clip-index/README.md | 130 ++++++++++++++++ .../base/clip-index/benchmark/benchmark.js | 52 +++++++ .../base/clip-index/benchmark/c/Makefile | 146 ++++++++++++++++++ .../base/clip-index/benchmark/c/benchmark.c | 133 ++++++++++++++++ .../ndarray/base/clip-index/docs/repl.txt | 34 ++++ .../base/clip-index/docs/types/index.d.ts | 43 ++++++ .../base/clip-index/docs/types/test.ts | 56 +++++++ .../ndarray/base/clip-index/examples/index.js | 32 ++++ .../include/stdlib/ndarray/base/clip_index.h | 40 +++++ .../ndarray/base/clip-index/lib/index.js | 46 ++++++ .../ndarray/base/clip-index/lib/main.js | 57 +++++++ .../ndarray/base/clip-index/manifest.json | 38 +++++ .../ndarray/base/clip-index/package.json | 72 +++++++++ .../ndarray/base/clip-index/src/main.c | 47 ++++++ .../ndarray/base/clip-index/test/test.js | 53 +++++++ 15 files changed, 979 insertions(+) create mode 100644 lib/node_modules/@stdlib/ndarray/base/clip-index/README.md create mode 100644 lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/c/Makefile create mode 100644 lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/c/benchmark.c create mode 100644 lib/node_modules/@stdlib/ndarray/base/clip-index/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/ndarray/base/clip-index/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/clip-index/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/ndarray/base/clip-index/examples/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/clip-index/include/stdlib/ndarray/base/clip_index.h create mode 100644 lib/node_modules/@stdlib/ndarray/base/clip-index/lib/index.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/clip-index/lib/main.js create mode 100644 lib/node_modules/@stdlib/ndarray/base/clip-index/manifest.json create mode 100644 lib/node_modules/@stdlib/ndarray/base/clip-index/package.json create mode 100644 lib/node_modules/@stdlib/ndarray/base/clip-index/src/main.c create mode 100644 lib/node_modules/@stdlib/ndarray/base/clip-index/test/test.js diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/README.md b/lib/node_modules/@stdlib/ndarray/base/clip-index/README.md new file mode 100644 index 000000000000..74dd51360548 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/README.md @@ -0,0 +1,130 @@ + + +# clipIndex + +> Clip an index to the interval `[0,max]`. + + + +
+ +
+ + + + + +
+ +## 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 +``` + +
+ + + + + +
+ +## Notes + +- Negative indices are normalized according to `max + idx`, and out-of-bounds indices are clipped to the nearest interval bound. + +
+ + + + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var clipIndex = require( '@stdlib/ndarray/base/clip-index' ); + +var idx; +var out; +var i; + +for ( i = 0; i < 100; i++ ) { + idx = discreteUniform( -20, 20 ); + out = clipIndex( idx, 10 ); + console.log( '%d => [%d,%d] => %d', idx, 0, 10, out ); +} +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/benchmark.js new file mode 100644 index 000000000000..52551b70762f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/benchmark.js @@ -0,0 +1,52 @@ +/** +* @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 randu = require( '@stdlib/random/base/randu' ); +var floor = require( '@stdlib/math/base/special/floor' ); +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 out; + var idx; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + idx = floor( randu()*40.0 ) - 20.0; + out = clipIndex( idx, 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(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/c/Makefile b/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/c/Makefile new file mode 100644 index 000000000000..218178c81266 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @license Apache-2.0 +# +# Copyright (c) 2023 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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := benchmark.out + + +# RULES # + +#/ +# Compiles C source files. +# +# @param {string} SOURCE_FILES - list of C source files +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lpthread -lblas`) +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [C_COMPILER] - C compiler +# @param {string} [CFLAGS] - C compiler flags +# @param {(string|void)} [fPIC] - compiler flag indicating whether to generate position independent code +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} SOURCE_FILES - list of C source files +# @param {(string|void)} INCLUDE - list of includes (e.g., `-I /foo/bar -I /beep/boop`) +# @param {(string|void)} LIBRARIES - list of libraries (e.g., `-lpthread -lblas`) +# @param {(string|void)} LIBPATH - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} CC - C compiler +# @param {string} CFLAGS - C compiler flags +# @param {(string|void)} fPIC - compiler flag indicating whether to generate position independent code +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled benchmarks. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/c/benchmark.c b/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/c/benchmark.c new file mode 100644 index 000000000000..7e6e2b0d4862 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/c/benchmark.c @@ -0,0 +1,133 @@ +/** +* @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. +*/ + +#include "stdlib/ndarray/base/clip_index.h" +#include +#include +#include +#include +#include +#include + +#define NAME "clip-index" +#define ITERATIONS 1000000 +#define REPEATS 3 + +/** +* Prints the TAP version. +*/ +static void print_version( void ) { + printf( "TAP version 13\n" ); +} + +/** +* Prints the TAP summary. +* +* @param total total number of tests +* @param passing total number of passing tests +*/ +static void print_summary( int total, int passing ) { + printf( "#\n" ); + printf( "1..%d\n", total ); // TAP plan + printf( "# total %d\n", total ); + printf( "# pass %d\n", passing ); + printf( "#\n" ); + printf( "# ok\n" ); +} + +/** +* Prints benchmarks results. +* +* @param elapsed elapsed time in seconds +*/ +static void print_results( double elapsed ) { + double rate = (double)ITERATIONS / elapsed; + printf( " ---\n" ); + printf( " iterations: %d\n", ITERATIONS ); + printf( " elapsed: %0.9f\n", elapsed ); + printf( " rate: %0.9f\n", rate ); + printf( " ...\n" ); +} + +/** +* Returns a clock time. +* +* @return clock time +*/ +static double tic( void ) { + struct timeval now; + gettimeofday( &now, NULL ); + return (double)now.tv_sec + (double)now.tv_usec/1.0e6; +} + +/** +* Generates a random number on the interval [0,1). +* +* @return random number +*/ +static double rand_double( void ) { + int r = rand(); + return (double)r / ( (double)RAND_MAX + 1.0 ); +} + +/** +* Runs a benchmark. +* +* @return elapsed time in seconds +*/ +static double benchmark( void ) { + double elapsed; + int64_t idx; + double t; + int i; + + t = tic(); + for ( i = 0; i < ITERATIONS; i++ ) { + idx = (int64_t)( rand_double()*200.0 ) - 100; + idx = stdlib_ndarray_clip_index( idx, 99 ); + if ( idx < 0 ) { + printf( "unexpected result\n" ); + break; + } + } + elapsed = tic() - t; + if ( idx < 0 ) { + printf( "unexpected result\n" ); + } + return elapsed; +} + +/** +* Main execution sequence. +*/ +int main( void ) { + double elapsed; + int i; + + // Use the current time to seed the random number generator: + srand( time( NULL ) ); + + print_version(); + for ( i = 0; i < REPEATS; i++ ) { + printf( "# c::%s\n", NAME ); + elapsed = benchmark(); + print_results( elapsed ); + printf( "ok %d benchmark finished\n", i+1 ); + } + print_summary( REPEATS, REPEATS ); +} diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/clip-index/docs/repl.txt new file mode 100644 index 000000000000..da44505a335d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( idx, max ) + Clips an index to the interval [0,max]. + + Negative indices are normalized according to `max + idx`, and + out-of-bounds indices are clipped to the nearest interval bound. + + Parameters + ---------- + idx: integer + Index to clip. + + max: integer + Maximum index value. + + Returns + ------- + out: integer + Clipped index. + + Examples + -------- + > var idx = {{alias}}( 2, 10 ) + 2 + > idx = {{alias}}( -5, 10 ) + 5 + > idx = {{alias}}( 15, 10 ) + 10 + > idx = {{alias}}( -15, 10 ) + 0 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/clip-index/docs/types/index.d.ts new file mode 100644 index 000000000000..2ad7acb62897 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/docs/types/index.d.ts @@ -0,0 +1,43 @@ +/* +* @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. +*/ + +// TypeScript Version: 4.1 + +/** +* Clips an index to the interval `[0,max]`. +* +* @param idx - index +* @param max - maximum index +* @returns index +* +* @example +* var idx = clipIndex( -2, 10 ); +* // returns 8 +* +* idx = clipIndex( 15, 10 ); +* // returns 10 +* +* idx = clipIndex( 5, 10 ); +* // returns 5 +*/ +declare function clipIndex( idx: number, max: number ): number; + + +// EXPORTS // + +export = clipIndex; diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/clip-index/docs/types/test.ts new file mode 100644 index 000000000000..779ca2b57703 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/docs/types/test.ts @@ -0,0 +1,56 @@ +/* +* @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. +*/ + +import clipIndex = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + clipIndex( 15, 10 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided values other than two numbers... +{ + clipIndex( true, 3 ); // $ExpectError + clipIndex( false, 2 ); // $ExpectError + clipIndex( '5', 1 ); // $ExpectError + clipIndex( [], 1 ); // $ExpectError + clipIndex( {}, 2 ); // $ExpectError + clipIndex( ( x: number ): number => x, 2 ); // $ExpectError + + clipIndex( 9, true ); // $ExpectError + clipIndex( 9, false ); // $ExpectError + clipIndex( 5, '5' ); // $ExpectError + clipIndex( 8, [] ); // $ExpectError + clipIndex( 9, {} ); // $ExpectError + clipIndex( 8, ( x: number ): number => x ); // $ExpectError + + clipIndex( [], true ); // $ExpectError + clipIndex( {}, false ); // $ExpectError + clipIndex( false, '5' ); // $ExpectError + clipIndex( {}, [] ); // $ExpectError + clipIndex( '5', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + clipIndex(); // $ExpectError + clipIndex( 3 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/clip-index/examples/index.js new file mode 100644 index 000000000000..0b794ddede02 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/examples/index.js @@ -0,0 +1,32 @@ +/** +* @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'; + +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var clipIndex = require( './../lib' ); + +var idx; +var out; +var i; + +for ( i = 0; i < 100; i++ ) { + idx = discreteUniform( -20, 20 ); + out = clipIndex( idx, 10 ); + console.log( '%d => [%d,%d] => %d', idx, 0, 10, out ); +} diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/include/stdlib/ndarray/base/clip_index.h b/lib/node_modules/@stdlib/ndarray/base/clip-index/include/stdlib/ndarray/base/clip_index.h new file mode 100644 index 000000000000..5b13818f6853 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/include/stdlib/ndarray/base/clip_index.h @@ -0,0 +1,40 @@ +/** +* @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. +*/ + +#ifndef STDLIB_NDARRAY_BASE_CLIP_INDEX_H +#define STDLIB_NDARRAY_BASE_CLIP_INDEX_H + +#include + +/* +* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. +*/ +#ifdef __cplusplus +extern "C" { +#endif + +/** +* Clips an index to the interval `[0,max]`. +*/ +int64_t stdlib_ndarray_clip_index( int64_t idx, int64_t max ); + +#ifdef __cplusplus +} +#endif + +#endif // !STDLIB_NDARRAY_BASE_CLIP_INDEX_H diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/clip-index/lib/index.js new file mode 100644 index 000000000000..7ce19d714304 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/lib/index.js @@ -0,0 +1,46 @@ +/** +* @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'; + +/** +* Clip an index to the interval `[0,max]`. +* +* @module @stdlib/ndarray/base/clip-index +* +* @example +* var clipIndex = require( '@stdlib/ndarray/base/clip-index' ); +* +* var idx = clipIndex( -2, 10 ); +* // returns 8 +* +* idx = clipIndex( 15, 10 ); +* // returns 10 +* +* idx = clipIndex( 5, 10 ); +* // returns 5 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/clip-index/lib/main.js new file mode 100644 index 000000000000..16a901a92dad --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/lib/main.js @@ -0,0 +1,57 @@ +/** +* @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'; + +// MAIN // + +/** +* Clips an index to the interval `[0,max]`. +* +* @param {integer} idx - index +* @param {NonNegativeInteger} max - maximum index +* @returns {NonNegativeInteger} index +* +* @example +* var idx = clipIndex( -2, 10 ); +* // returns 8 +* +* idx = clipIndex( 15, 10 ); +* // returns 10 +* +* idx = clipIndex( 5, 10 ); +* // returns 5 +*/ +function clipIndex( idx, max ) { + if ( idx < 0 ) { + idx += max; + if ( idx < 0 ) { + return 0; + } + return idx; + } + if ( idx > max ) { + return max; + } + return idx; +} + + +// EXPORTS // + +module.exports = clipIndex; diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/manifest.json b/lib/node_modules/@stdlib/ndarray/base/clip-index/manifest.json new file mode 100644 index 000000000000..04e61e361caa --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/manifest.json @@ -0,0 +1,38 @@ +{ + "options": {}, + "fields": [ + { + "field": "src", + "resolve": true, + "relative": true + }, + { + "field": "include", + "resolve": true, + "relative": true + }, + { + "field": "libraries", + "resolve": false, + "relative": false + }, + { + "field": "libpath", + "resolve": true, + "relative": false + } + ], + "confs": [ + { + "src": [ + "./src/main.c" + ], + "include": [ + "./include" + ], + "libraries": [], + "libpath": [], + "dependencies": [] + } + ] +} diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/package.json b/lib/node_modules/@stdlib/ndarray/base/clip-index/package.json new file mode 100644 index 000000000000..fea7f8aad742 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/ndarray/base/clip-index", + "version": "0.0.0", + "description": "Clip an index to the interval [0,max].", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "include": "./include", + "lib": "./lib", + "src": "./src", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "base", + "ndarray", + "index", + "idx", + "ind", + "subscript", + "clip", + "multidimensional", + "array", + "utilities", + "utility", + "utils", + "util" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/src/main.c b/lib/node_modules/@stdlib/ndarray/base/clip-index/src/main.c new file mode 100644 index 000000000000..bf758ebbfca1 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/src/main.c @@ -0,0 +1,47 @@ +/** +* @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. +*/ + +#include "stdlib/ndarray/base/clip_index.h" +#include + +/** +* Clips an index to the interval `[0,max]`. +* +* @param idx index +* @param max maximum index (should be nonnegative) +* @return index +* +* @example +* #include "stdlib/ndarray/base/clip_index.h" +* +* int64_t idx = stdlib_ndarray_clip_index( -2, 8 ); +* // returns 6 +*/ +int64_t stdlib_ndarray_clip_index( int64_t idx, int64_t max ) { + if ( idx < 0 ) { + idx += max; + if ( idx < 0 ) { + return 0; + } + return idx; + } + if ( idx > max ) { + return max; + } + return idx; +} diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/test/test.js b/lib/node_modules/@stdlib/ndarray/base/clip-index/test/test.js new file mode 100644 index 000000000000..38e45793991a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/test/test.js @@ -0,0 +1,53 @@ +/** +* @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 tape = require( 'tape' ); +var clipIndex = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof clipIndex, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function clips an index to the interval `[0,max]`', function test( t ) { + t.strictEqual( clipIndex( 2, 10 ), 2, 'returns expected value' ); + t.strictEqual( clipIndex( 5, 10 ), 5, 'returns expected value' ); + t.strictEqual( clipIndex( 0, 10 ), 0, 'returns expected value' ); + t.strictEqual( clipIndex( 10, 10 ), 10, 'returns expected value' ); + t.strictEqual( clipIndex( -1, 10 ), 9, 'returns expected value' ); + t.strictEqual( clipIndex( -2, 10 ), 8, 'returns expected value' ); + t.strictEqual( clipIndex( -5, 10 ), 5, 'returns expected value' ); + t.strictEqual( clipIndex( -10, 10 ), 0, 'returns expected value' ); + t.end(); +}); + +tape( 'if provided an out-of-bounds index, the function clips the index to the nearest interval bound', function test( t ) { + t.strictEqual( clipIndex( 15, 10 ), 10, 'returns expected value' ); + t.strictEqual( clipIndex( 11, 10 ), 10, 'returns expected value' ); + t.strictEqual( clipIndex( -11, 10 ), 0, 'returns expected value' ); + t.strictEqual( clipIndex( -15, 10 ), 0, 'returns expected value' ); + t.end(); +}); From 0141d71b5f0b14ed6726e371d38792c31385c970 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Thu, 20 Aug 2026 10:12:50 +0500 Subject: [PATCH 2/4] fix: copyright year --- .../@stdlib/ndarray/base/clip-index/benchmark/c/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/c/Makefile b/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/c/Makefile index 218178c81266..e790d7bffa7f 100644 --- a/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/c/Makefile +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/c/Makefile @@ -1,7 +1,7 @@ #/ # @license Apache-2.0 # -# Copyright (c) 2023 The Stdlib Authors. +# 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. From 3b2e8a29b3651d42c16836e1368d03e2fdccd633 Mon Sep 17 00:00:00 2001 From: headlessNode Date: Thu, 20 Aug 2026 16:16:31 +0500 Subject: [PATCH 3/4] fix: apply suggestions from code review --- .../@stdlib/ndarray/base/clip-index/README.md | 104 +++++++++++-- .../base/clip-index/benchmark/benchmark.js | 12 +- .../base/clip-index/benchmark/c/benchmark.c | 14 +- .../ndarray/base/clip-index/docs/repl.txt | 3 +- .../base/clip-index/examples/c/Makefile | 146 ++++++++++++++++++ .../base/clip-index/examples/c/example.c | 32 ++++ .../ndarray/base/clip-index/examples/index.js | 15 +- .../include/stdlib/ndarray/base/clip_index.h | 2 +- .../ndarray/base/clip-index/lib/main.js | 13 +- .../ndarray/base/clip-index/src/main.c | 15 +- 10 files changed, 312 insertions(+), 44 deletions(-) create mode 100644 lib/node_modules/@stdlib/ndarray/base/clip-index/examples/c/Makefile create mode 100644 lib/node_modules/@stdlib/ndarray/base/clip-index/examples/c/example.c diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/README.md b/lib/node_modules/@stdlib/ndarray/base/clip-index/README.md index 74dd51360548..d5adffe4641e 100644 --- a/lib/node_modules/@stdlib/ndarray/base/clip-index/README.md +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/README.md @@ -72,7 +72,7 @@ idx = clipIndex( -15, 10 ); ## Notes -- Negative indices are normalized according to `max + idx`, and out-of-bounds indices are clipped to the nearest interval bound. +- Negative indices are normalized according to `max + idx`. @@ -87,17 +87,99 @@ idx = clipIndex( -15, 10 ); ```javascript -var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +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; -var out; -var i; +var idx = discreteUniform( 100, -20, 20, { + 'dtype': 'generic' +}); -for ( i = 0; i < 100; i++ ) { - idx = discreteUniform( -20, 20 ); - out = clipIndex( idx, 10 ); - console.log( '%d => [%d,%d] => %d', idx, 0, 10, out ); +logEachMap( '%d => [0,%d] => %d', idx, 10, clipIndex ); +``` + + + + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### 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 + +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 ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/ndarray/base/clip_index.h" +#include +#include + +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 ); + } } ``` @@ -105,6 +187,10 @@ for ( i = 0; i < 100; i++ ) { +
+ + +
diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/benchmark.js index 52551b70762f..340608a2257b 100644 --- a/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/benchmark.js +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/benchmark.js @@ -21,8 +21,7 @@ // MODULES // var bench = require( '@stdlib/bench' ); -var randu = require( '@stdlib/random/base/randu' ); -var floor = require( '@stdlib/math/base/special/floor' ); +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' ); @@ -31,14 +30,17 @@ var clipIndex = require( './../lib' ); // MAIN // bench( pkg, function benchmark( b ) { + var values; var out; - var idx; var i; + values = discreteUniform( 100, -20, 20, { + 'dtype': 'generic' + }); + b.tic(); for ( i = 0; i < b.iterations; i++ ) { - idx = floor( randu()*40.0 ) - 20.0; - out = clipIndex( idx, 10 ); + out = clipIndex( values[ i%values.length ], 10 ); if ( out !== out ) { b.fail( 'should not return NaN' ); } diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/c/benchmark.c b/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/c/benchmark.c index 7e6e2b0d4862..a496e339779f 100644 --- a/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/c/benchmark.c +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/c/benchmark.c @@ -91,22 +91,26 @@ static double rand_double( void ) { * @return elapsed time in seconds */ static double benchmark( void ) { + int64_t values[ 100 ]; double elapsed; - int64_t idx; + int64_t out; double t; int i; + for ( i = 0; i < 100; i++ ) { + values[ i ] = (int64_t)( rand_double()*200.0 ) - 100; + } + out = 0; t = tic(); for ( i = 0; i < ITERATIONS; i++ ) { - idx = (int64_t)( rand_double()*200.0 ) - 100; - idx = stdlib_ndarray_clip_index( idx, 99 ); - if ( idx < 0 ) { + out = stdlib_ndarray_clip_index( values[ i%100 ], 99 ); + if ( out < 0 ) { printf( "unexpected result\n" ); break; } } elapsed = tic() - t; - if ( idx < 0 ) { + if ( out < 0 ) { printf( "unexpected result\n" ); } return elapsed; diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/clip-index/docs/repl.txt index da44505a335d..98476ffe6309 100644 --- a/lib/node_modules/@stdlib/ndarray/base/clip-index/docs/repl.txt +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/docs/repl.txt @@ -2,8 +2,7 @@ {{alias}}( idx, max ) Clips an index to the interval [0,max]. - Negative indices are normalized according to `max + idx`, and - out-of-bounds indices are clipped to the nearest interval bound. + Negative indices are normalized according to `max + idx`. Parameters ---------- diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/examples/c/Makefile b/lib/node_modules/@stdlib/ndarray/base/clip-index/examples/c/Makefile new file mode 100644 index 000000000000..c8f8e9a1517b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/examples/c/Makefile @@ -0,0 +1,146 @@ +#/ +# @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. +#/ + +# VARIABLES # + +ifndef VERBOSE + QUIET := @ +else + QUIET := +endif + +# Determine the OS ([1][1], [2][2]). +# +# [1]: https://en.wikipedia.org/wiki/Uname#Examples +# [2]: http://stackoverflow.com/a/27776822/2225624 +OS ?= $(shell uname) +ifneq (, $(findstring MINGW,$(OS))) + OS := WINNT +else +ifneq (, $(findstring MSYS,$(OS))) + OS := WINNT +else +ifneq (, $(findstring CYGWIN,$(OS))) + OS := WINNT +else +ifneq (, $(findstring Windows_NT,$(OS))) + OS := WINNT +endif +endif +endif +endif + +# Define the program used for compiling C source files: +ifdef C_COMPILER + CC := $(C_COMPILER) +else + CC := gcc +endif + +# Define the command-line options when compiling C files: +CFLAGS ?= \ + -std=c99 \ + -O3 \ + -Wall \ + -pedantic + +# Determine whether to generate position independent code ([1][1], [2][2]). +# +# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options +# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option +ifeq ($(OS), WINNT) + fPIC ?= +else + fPIC ?= -fPIC +endif + +# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`): +INCLUDE ?= + +# List of source files: +SOURCE_FILES ?= + +# List of libraries (e.g., `-lopenblas -lpthread`): +LIBRARIES ?= + +# List of library paths (e.g., `-L /foo/bar -L /beep/boop`): +LIBPATH ?= + +# List of C targets: +c_targets := example.out + + +# RULES # + +#/ +# Compiles source files. +# +# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`) +# @param {string} [CFLAGS] - C compiler options +# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`) +# @param {string} [SOURCE_FILES] - list of source files +# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`) +# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`) +# +# @example +# make +# +# @example +# make all +#/ +all: $(c_targets) + +.PHONY: all + +#/ +# Compiles C source files. +# +# @private +# @param {string} CC - C compiler (e.g., `gcc`) +# @param {string} CFLAGS - C compiler options +# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`) +# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`) +# @param {string} SOURCE_FILES - list of source files +# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`) +# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`) +#/ +$(c_targets): %.out: %.c + $(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES) + +#/ +# Runs compiled examples. +# +# @example +# make run +#/ +run: $(c_targets) + $(QUIET) ./$< + +.PHONY: run + +#/ +# Removes generated files. +# +# @example +# make clean +#/ +clean: + $(QUIET) -rm -f *.o *.out + +.PHONY: clean diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/examples/c/example.c b/lib/node_modules/@stdlib/ndarray/base/clip-index/examples/c/example.c new file mode 100644 index 000000000000..1268dbcf174a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/examples/c/example.c @@ -0,0 +1,32 @@ +/** +* @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. +*/ + +#include "stdlib/ndarray/base/clip_index.h" +#include +#include + +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 ); + } +} diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/clip-index/examples/index.js index 0b794ddede02..073ca6a906ea 100644 --- a/lib/node_modules/@stdlib/ndarray/base/clip-index/examples/index.js +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/examples/index.js @@ -18,15 +18,12 @@ 'use strict'; -var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var logEachMap = require( '@stdlib/console/log-each-map' ); var clipIndex = require( './../lib' ); -var idx; -var out; -var i; +var idx = discreteUniform( 100, -20, 20, { + 'dtype': 'generic' +}); -for ( i = 0; i < 100; i++ ) { - idx = discreteUniform( -20, 20 ); - out = clipIndex( idx, 10 ); - console.log( '%d => [%d,%d] => %d', idx, 0, 10, out ); -} +logEachMap( '%d => [0,%d] => %d', idx, 10, clipIndex ); diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/include/stdlib/ndarray/base/clip_index.h b/lib/node_modules/@stdlib/ndarray/base/clip-index/include/stdlib/ndarray/base/clip_index.h index 5b13818f6853..208b3f104dc0 100644 --- a/lib/node_modules/@stdlib/ndarray/base/clip-index/include/stdlib/ndarray/base/clip_index.h +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/include/stdlib/ndarray/base/clip_index.h @@ -31,7 +31,7 @@ extern "C" { /** * Clips an index to the interval `[0,max]`. */ -int64_t stdlib_ndarray_clip_index( int64_t idx, int64_t max ); +int64_t stdlib_ndarray_clip_index( const int64_t idx, const int64_t max ); #ifdef __cplusplus } diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/clip-index/lib/main.js index 16a901a92dad..5df0eb6efc0e 100644 --- a/lib/node_modules/@stdlib/ndarray/base/clip-index/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/lib/main.js @@ -38,17 +38,18 @@ * // returns 5 */ function clipIndex( idx, max ) { - if ( idx < 0 ) { - idx += max; - if ( idx < 0 ) { + var v = idx; + if ( v < 0 ) { + v += max; + if ( v < 0 ) { return 0; } - return idx; + return v; } - if ( idx > max ) { + if ( v > max ) { return max; } - return idx; + return v; } diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/src/main.c b/lib/node_modules/@stdlib/ndarray/base/clip-index/src/main.c index bf758ebbfca1..6a695ff88734 100644 --- a/lib/node_modules/@stdlib/ndarray/base/clip-index/src/main.c +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/src/main.c @@ -32,16 +32,17 @@ * int64_t idx = stdlib_ndarray_clip_index( -2, 8 ); * // returns 6 */ -int64_t stdlib_ndarray_clip_index( int64_t idx, int64_t max ) { - if ( idx < 0 ) { - idx += max; - if ( idx < 0 ) { +int64_t stdlib_ndarray_clip_index( const int64_t idx, const int64_t max ) { + int64_t v = idx; + if ( v < 0 ) { + v += max; + if ( v < 0 ) { return 0; } - return idx; + return v; } - if ( idx > max ) { + if ( v > max ) { return max; } - return idx; + return v; } From a0abcacdc16b15731f41694ccf2b2f85110dbf0c Mon Sep 17 00:00:00 2001 From: Athan Date: Thu, 20 Aug 2026 17:57:54 -0700 Subject: [PATCH 4/4] Apply suggestions from code review Co-authored-by: Athan Signed-off-by: Athan --- .../@stdlib/ndarray/base/clip-index/lib/main.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-index/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/clip-index/lib/main.js index 5df0eb6efc0e..16a901a92dad 100644 --- a/lib/node_modules/@stdlib/ndarray/base/clip-index/lib/main.js +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/lib/main.js @@ -38,18 +38,17 @@ * // returns 5 */ function clipIndex( idx, max ) { - var v = idx; - if ( v < 0 ) { - v += max; - if ( v < 0 ) { + if ( idx < 0 ) { + idx += max; + if ( idx < 0 ) { return 0; } - return v; + return idx; } - if ( v > max ) { + if ( idx > max ) { return max; } - return v; + return idx; }