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..d5adffe4641e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/README.md @@ -0,0 +1,216 @@ + + +# 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`. + +
+ + + + + +
+ +## Examples + + + +```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 ); +``` + +
+ + + + + +* * * + +
+ +## 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 ); + } +} +``` + +
+ + + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + 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..340608a2257b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/benchmark.js @@ -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(); +}); 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..e790d7bffa7f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/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 := 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..a496e339779f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/benchmark/c/benchmark.c @@ -0,0 +1,137 @@ +/** +* @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 ) { + int64_t values[ 100 ]; + double elapsed; + 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++ ) { + out = stdlib_ndarray_clip_index( values[ i%100 ], 99 ); + if ( out < 0 ) { + printf( "unexpected result\n" ); + break; + } + } + elapsed = tic() - t; + if ( out < 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..98476ffe6309 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/docs/repl.txt @@ -0,0 +1,33 @@ + +{{alias}}( idx, max ) + Clips an index to the interval [0,max]. + + Negative indices are normalized according to `max + idx`. + + 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/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 new file mode 100644 index 000000000000..073ca6a906ea --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/examples/index.js @@ -0,0 +1,29 @@ +/** +* @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/array/discrete-uniform' ); +var logEachMap = require( '@stdlib/console/log-each-map' ); +var clipIndex = require( './../lib' ); + +var idx = discreteUniform( 100, -20, 20, { + 'dtype': 'generic' +}); + +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 new file mode 100644 index 000000000000..208b3f104dc0 --- /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( const int64_t idx, const 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..6a695ff88734 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-index/src/main.c @@ -0,0 +1,48 @@ +/** +* @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( const int64_t idx, const int64_t max ) { + int64_t v = idx; + if ( v < 0 ) { + v += max; + if ( v < 0 ) { + return 0; + } + return v; + } + if ( v > max ) { + return max; + } + return v; +} 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(); +});