diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/README.md b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/README.md new file mode 100644 index 000000000000..7b1a0151c095 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/README.md @@ -0,0 +1,216 @@ + + +# clipUpperIndex + +> Clip an index to the interval `[-1,max]`. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var clipUpperIndex = require( '@stdlib/ndarray/base/clip-upper-index' ); +``` + +#### clipUpperIndex( idx, max ) + +Clips an index to the interval `[-1,max]`. + +```javascript +var idx = clipUpperIndex( 2, 10 ); +// returns 2 + +idx = clipUpperIndex( -5, 10 ); +// returns 5 +``` + +If provided an out-of-bounds index, the function clips the index to the nearest interval bound. + +```javascript +var idx = clipUpperIndex( 15, 10 ); +// returns 10 + +idx = clipUpperIndex( -15, 10 ); +// returns -1 +``` + +
+ + + + + +
+ +## 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 clipUpperIndex = require( '@stdlib/ndarray/base/clip-upper-index' ); + +var idx = discreteUniform( 100, -20, 20, { + 'dtype': 'generic' +}); + +logEachMap( '%d => [-1,%d] => %d', idx, 10, clipUpperIndex ); +``` + +
+ + + + + +* * * + +
+ +## C APIs + + + +
+ +
+ + + + + +
+ +### Usage + +```c +#include "stdlib/ndarray/base/clip_upper_index.h" +``` + +#### stdlib_ndarray_clip_upper_index( idx, max ) + +Clips an index to the interval `[-1,max]`. + +```c +#include + +int64_t idx = stdlib_ndarray_clip_upper_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_upper_index( const int64_t idx, const int64_t max ); +``` + +
+ + + + + +
+ +
+ + + + + +
+ +### Examples + +```c +#include "stdlib/ndarray/base/clip_upper_index.h" +#include +#include + +int main( void ) { + const int64_t idx[] = { -15, 8, 11, 4 }; + + int64_t out; + int i; + for ( i = 0; i < 4; i++ ) { + out = stdlib_ndarray_clip_upper_index( idx[ i ], 10 ); + printf( "clip_upper_index(%" PRId64 ", 10) => %" PRId64 "\n", idx[ i ], out ); + } +} +``` + +
+ + + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/benchmark/benchmark.js new file mode 100644 index 000000000000..028dca572bf2 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-upper-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 isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive; +var pkg = require( './../package.json' ).name; +var clipUpperIndex = 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 = clipUpperIndex( values[ i%values.length ], 10 ); + if ( out !== out ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isInteger( out ) ) { + b.fail( 'should return an integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/benchmark/c/Makefile b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/benchmark/c/Makefile new file mode 100644 index 000000000000..e790d7bffa7f --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-upper-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-upper-index/benchmark/c/benchmark.c b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/benchmark/c/benchmark.c new file mode 100644 index 000000000000..15b7bbf89e8c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-upper-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_upper_index.h" +#include +#include +#include +#include +#include +#include + +#define NAME "clip-upper-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_upper_index( values[ i%100 ], 99 ); + if ( out < -1 ) { + printf( "unexpected result\n" ); + break; + } + } + elapsed = tic() - t; + if ( out < -1 ) { + 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-upper-index/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/docs/repl.txt new file mode 100644 index 000000000000..5c2908086f2d --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/docs/repl.txt @@ -0,0 +1,33 @@ + +{{alias}}( idx, max ) + Clips an index to the interval [-1,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 ) + -1 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/docs/types/index.d.ts new file mode 100644 index 000000000000..e9ad4f70460c --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-upper-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 `[-1,max]`. +* +* @param idx - index +* @param max - maximum index +* @returns index +* +* @example +* var idx = clipUpperIndex( -2, 10 ); +* // returns 8 +* +* idx = clipUpperIndex( 15, 10 ); +* // returns 10 +* +* idx = clipUpperIndex( 5, 10 ); +* // returns 5 +*/ +declare function clipUpperIndex( idx: number, max: number ): number; + + +// EXPORTS // + +export = clipUpperIndex; diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/docs/types/test.ts new file mode 100644 index 000000000000..4ce27f11eb76 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-upper-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 clipUpperIndex = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + clipUpperIndex( 15, 10 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided values other than two numbers... +{ + clipUpperIndex( true, 3 ); // $ExpectError + clipUpperIndex( false, 2 ); // $ExpectError + clipUpperIndex( '5', 1 ); // $ExpectError + clipUpperIndex( [], 1 ); // $ExpectError + clipUpperIndex( {}, 2 ); // $ExpectError + clipUpperIndex( ( x: number ): number => x, 2 ); // $ExpectError + + clipUpperIndex( 9, true ); // $ExpectError + clipUpperIndex( 9, false ); // $ExpectError + clipUpperIndex( 5, '5' ); // $ExpectError + clipUpperIndex( 8, [] ); // $ExpectError + clipUpperIndex( 9, {} ); // $ExpectError + clipUpperIndex( 8, ( x: number ): number => x ); // $ExpectError + + clipUpperIndex( [], true ); // $ExpectError + clipUpperIndex( {}, false ); // $ExpectError + clipUpperIndex( false, '5' ); // $ExpectError + clipUpperIndex( {}, [] ); // $ExpectError + clipUpperIndex( '5', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided insufficient arguments... +{ + clipUpperIndex(); // $ExpectError + clipUpperIndex( 3 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/examples/c/Makefile b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/examples/c/Makefile new file mode 100644 index 000000000000..c8f8e9a1517b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-upper-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-upper-index/examples/c/example.c b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/examples/c/example.c new file mode 100644 index 000000000000..b1da93a56471 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-upper-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_upper_index.h" +#include +#include + +int main( void ) { + const int64_t idx[] = { -15, 8, 11, 4 }; + + int64_t out; + int i; + for ( i = 0; i < 4; i++ ) { + out = stdlib_ndarray_clip_upper_index( idx[ i ], 10 ); + printf( "clip_upper_index(%" PRId64 ", 10) => %" PRId64 "\n", idx[ i ], out ); + } +} diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/examples/index.js new file mode 100644 index 000000000000..517d41541ec5 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-upper-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 clipUpperIndex = require( './../lib' ); + +var idx = discreteUniform( 100, -20, 20, { + 'dtype': 'generic' +}); + +logEachMap( '%d => [-1,%d] => %d', idx, 10, clipUpperIndex ); diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/include/stdlib/ndarray/base/clip_upper_index.h b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/include/stdlib/ndarray/base/clip_upper_index.h new file mode 100644 index 000000000000..1ff43ebe0e27 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/include/stdlib/ndarray/base/clip_upper_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_UPPER_INDEX_H +#define STDLIB_NDARRAY_BASE_CLIP_UPPER_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 `[-1,max]`. +*/ +int64_t stdlib_ndarray_clip_upper_index( const int64_t idx, const int64_t max ); + +#ifdef __cplusplus +} +#endif + +#endif // !STDLIB_NDARRAY_BASE_CLIP_UPPER_INDEX_H diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/lib/index.js new file mode 100644 index 000000000000..4ff9c3d69325 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-upper-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 `[-1,max]`. +* +* @module @stdlib/ndarray/base/clip-upper-index +* +* @example +* var clipUpperIndex = require( '@stdlib/ndarray/base/clip-upper-index' ); +* +* var idx = clipUpperIndex( -2, 10 ); +* // returns 8 +* +* idx = clipUpperIndex( 15, 10 ); +* // returns 10 +* +* idx = clipUpperIndex( 5, 10 ); +* // returns 5 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/lib/main.js new file mode 100644 index 000000000000..1307e0580bf2 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-upper-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 `[-1,max]`. +* +* @param {integer} idx - index +* @param {NonNegativeInteger} max - maximum index +* @returns {integer} index +* +* @example +* var idx = clipUpperIndex( -2, 10 ); +* // returns 8 +* +* idx = clipUpperIndex( 15, 10 ); +* // returns 10 +* +* idx = clipUpperIndex( 5, 10 ); +* // returns 5 +*/ +function clipUpperIndex( idx, max ) { + if ( idx < 0 ) { + idx += max; + if ( idx < 0 ) { + return -1; + } + return idx; + } + if ( idx > max ) { + return max; + } + return idx; +} + + +// EXPORTS // + +module.exports = clipUpperIndex; diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/manifest.json b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/manifest.json new file mode 100644 index 000000000000..04e61e361caa --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-upper-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-upper-index/package.json b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/package.json new file mode 100644 index 000000000000..60daa2ccca00 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/ndarray/base/clip-upper-index", + "version": "0.0.0", + "description": "Clip an index to the interval [-1,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-upper-index/src/main.c b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/src/main.c new file mode 100644 index 000000000000..a853621850d9 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-upper-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_upper_index.h" +#include + +/** +* Clips an index to the interval `[-1,max]`. +* +* @param idx index +* @param max maximum index (should be nonnegative) +* @return index +* +* @example +* #include "stdlib/ndarray/base/clip_upper_index.h" +* +* int64_t idx = stdlib_ndarray_clip_upper_index( -2, 8 ); +* // returns 6 +*/ +int64_t stdlib_ndarray_clip_upper_index( const int64_t idx, const int64_t max ) { + int64_t v = idx; + if ( v < 0 ) { + v += max; + if ( v < 0 ) { + return -1; + } + return v; + } + if ( v > max ) { + return max; + } + return v; +} diff --git a/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/test/test.js b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/test/test.js new file mode 100644 index 000000000000..7b66d47c02fd --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/clip-upper-index/test/test.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 tape = require( 'tape' ); +var clipUpperIndex = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof clipUpperIndex, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function clips an index to the interval `[-1,max]`', function test( t ) { + t.strictEqual( clipUpperIndex( 2, 10 ), 2, 'returns expected value' ); + t.strictEqual( clipUpperIndex( 5, 10 ), 5, 'returns expected value' ); + t.strictEqual( clipUpperIndex( 0, 10 ), 0, 'returns expected value' ); + t.strictEqual( clipUpperIndex( 10, 10 ), 10, 'returns expected value' ); + t.strictEqual( clipUpperIndex( -1, 10 ), 9, 'returns expected value' ); + t.strictEqual( clipUpperIndex( -2, 10 ), 8, 'returns expected value' ); + t.strictEqual( clipUpperIndex( -5, 10 ), 5, 'returns expected value' ); + t.strictEqual( clipUpperIndex( -10, 10 ), 0, 'returns expected value' ); + t.strictEqual( clipUpperIndex( -11, 10 ), -1, '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( clipUpperIndex( 15, 10 ), 10, 'returns expected value' ); + t.strictEqual( clipUpperIndex( 11, 10 ), 10, 'returns expected value' ); + t.strictEqual( clipUpperIndex( -11, 10 ), -1, 'returns expected value' ); + t.strictEqual( clipUpperIndex( -15, 10 ), -1, 'returns expected value' ); + t.end(); +});