diff --git a/lib/node_modules/@stdlib/array/uint64/README.md b/lib/node_modules/@stdlib/array/uint64/README.md index 3c0a21f8c0fa..135bbba2b154 100644 --- a/lib/node_modules/@stdlib/array/uint64/README.md +++ b/lib/node_modules/@stdlib/array/uint64/README.md @@ -211,7 +211,7 @@ var arr = Uint64Array.from( [ 1, 2 ], mapFcn ); A callback function is provided two arguments: - **value**: source value. -- **index**: source index.. +- **index**: source index. To set the callback execution context, provide a `thisArg`. @@ -243,6 +243,41 @@ var arr = Uint64Array.of( 1, 2 ); // returns [ 1n, 2n ] ``` + + +#### Uint64Array.prototype.at( i ) + +Returns an array element located at integer position (index) `i`, with support for both nonnegative and negative integer positions. + +```javascript +var arr = new Uint64Array( 10 ); + +// Set the first, second, and last elements: +arr.set( 1, 0 ); +arr.set( 2, 1 ); +arr.set( 9, 9 ); + +// Get the first element: +var z = arr.at( 0 ); +// returns [ 1n ] + +// Get the last element: +z = arr.at( -1 ); +// returns [ 9n ] +``` + +If provided an out-of-bounds index, the method returns `undefined`. + +```javascript +var arr = new Uint64Array( 10 ); + +var z = arr.at( 100 ); +// returns undefined + +z = arr.at( -100 ); +// returns undefined +``` + #### Uint64Array.prototype.get( i ) @@ -391,7 +426,7 @@ var arr = [ out = new Uint64Array( arr ); logEach( '%s', out ); -// Create a 64-bit unsigned integer array from an typed array: +// Create a 64-bit unsigned integer array from a typed array: arr = new Uint32Array( [ 1, 2, 3, 4 ] ); out = new Uint64Array( arr ); logEach( '%s', out ); diff --git a/lib/node_modules/@stdlib/array/uint64/benchmark/benchmark.at.js b/lib/node_modules/@stdlib/array/uint64/benchmark/benchmark.at.js new file mode 100644 index 000000000000..eed6eb4aef14 --- /dev/null +++ b/lib/node_modules/@stdlib/array/uint64/benchmark/benchmark.at.js @@ -0,0 +1,87 @@ +/** +* @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 format = require( '@stdlib/string/format' ); +var isUint64 = require( '@stdlib/assert/is-uint64' ); +var Uint64 = require( '@stdlib/number/uint64/ctor' ); +var pkg = require( './../package.json' ).name; +var Uint64Array = require( './../lib' ); + + +// MAIN // + +bench( format( '%s::nonnegative_indices:at', pkg ), function benchmark( b ) { + var arr; + var N; + var u; + var i; + + arr = []; + for ( i = 0; i < 10; i++ ) { + arr.push( new Uint64( i ) ); + } + arr = new Uint64Array( arr ); + N = arr.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + u = arr.at( i%N ); + if ( typeof u !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isUint64( u ) ) { + b.fail( 'should return a 64-bit unsigned integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( format( '%s::negative_indices:at', pkg ), function benchmark( b ) { + var arr; + var N; + var u; + var i; + + arr = []; + for ( i = 0; i < 10; i++ ) { + arr.push( new Uint64( i ) ); + } + arr = new Uint64Array( arr ); + N = arr.length; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + u = arr.at( -(i%N)-1 ); + if ( typeof u !== 'object' ) { + b.fail( 'should return an object' ); + } + } + b.toc(); + if ( !isUint64( u ) ) { + b.fail( 'should return a 64-bit unsigned integer' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/uint64/docs/repl.txt b/lib/node_modules/@stdlib/array/uint64/docs/repl.txt index 64ba000628aa..aad3a9918af8 100644 --- a/lib/node_modules/@stdlib/array/uint64/docs/repl.txt +++ b/lib/node_modules/@stdlib/array/uint64/docs/repl.txt @@ -227,6 +227,31 @@ 5 +{{alias}}.prototype.at( i ) + Returns an array element located at integer position (index) `i`, with + support for both nonnegative and negative integer positions. + + If provided an index outside the array index range, the method returns + `undefined`. + + Parameters + ---------- + i: integer + Element index. + + Returns + ------- + out: Uint64|void + Array element or `undefined`. + + Examples + -------- + > var arr = new {{alias}}( [ 1, 2 ] ) + [ 1n, 2n ] + > var z = arr.at( 1 ) + [ 2n ] + + {{alias}}.prototype.get( i ) Returns an array element located at integer position (index) `i`. diff --git a/lib/node_modules/@stdlib/array/uint64/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/uint64/docs/types/index.d.ts index fe4f08640876..5e66f513e8bb 100644 --- a/lib/node_modules/@stdlib/array/uint64/docs/types/index.d.ts +++ b/lib/node_modules/@stdlib/array/uint64/docs/types/index.d.ts @@ -399,6 +399,33 @@ declare class Uint64Array implements Uint64ArrayInterface { */ readonly length: number; + /** + * Returns an array element located at integer position (index) `i`, with support for both nonnegative and negative integer indices. + * + * @param i - element index + * @throws index argument must be an integer + * @returns array element + * + * @example + * var arr = new Uint64Array( 10 ); + * + * var u = arr.at( 0 ); + * // returns [ 0n ] + * + * arr.set( 1, 0 ); + * arr.set( 9, 9 ); + * + * u = arr.at( -1 ); + * // returns [ 9n ] + * + * u = arr.at( 100 ); + * // returns undefined + * + * u = arr.at( -100 ); + * // returns undefined + */ + at( i: number ): Uint64 | void; + /** * Returns an array element. * diff --git a/lib/node_modules/@stdlib/array/uint64/examples/index.js b/lib/node_modules/@stdlib/array/uint64/examples/index.js index 6ba020a5bf4b..8d4ed3642c3c 100644 --- a/lib/node_modules/@stdlib/array/uint64/examples/index.js +++ b/lib/node_modules/@stdlib/array/uint64/examples/index.js @@ -36,7 +36,7 @@ var arr = [ out = new Uint64Array( arr ); logEach( '%s', out ); -// Create a 64-bit unsigned integer array from an typed array: +// Create a 64-bit unsigned integer array from a typed array: arr = new Uint32Array( [ 1, 2, 3, 4 ] ); out = new Uint64Array( arr ); logEach( '%s', out ); diff --git a/lib/node_modules/@stdlib/array/uint64/lib/main.js b/lib/node_modules/@stdlib/array/uint64/lib/main.js index a35e815ab144..f04c7a9c2cc5 100644 --- a/lib/node_modules/@stdlib/array/uint64/lib/main.js +++ b/lib/node_modules/@stdlib/array/uint64/lib/main.js @@ -477,6 +477,55 @@ setReadOnly( Uint64Array, 'of', function of() { return new this( args ); }); +/** +* Returns an array element located at integer position (index) `i`, with support for both nonnegative and negative integer indices. +* +* @name at +* @memberof Uint64Array.prototype +* @type {Function} +* @param {integer} idx - element index +* @throws {TypeError} `this` must be a 64-bit unsigned integer array +* @throws {TypeError} must provide an integer +* @returns {(Uint64|void)} array element +* +* @example +* var arr = new Uint64Array( 10 ); +* +* var u = arr.at( 0 ); +* // returns [ 0n ] +* +* arr.set( 1, 0 ); +* arr.set( 2, 1 ); +* arr.set( 9, 9 ); +* +* u = arr.at( 0 ); +* // returns [ 1n ] +* +* u = arr.at( -1 ); +* // returns [ 9n ] +* +* u = arr.at( 100 ); +* // returns undefined +* +* u = arr.at( -100 ); +* // returns undefined +*/ +setReadOnly( Uint64Array.prototype, 'at', function at( idx ) { + if ( !isUint64Array( this ) ) { + throw new TypeError( 'invalid invocation. `this` is not a 64-bit unsigned integer array.' ); + } + if ( !isInteger( idx ) ) { + throw new TypeError( format( 'invalid argument. Must provide an integer. Value: `%s`.', idx ) ); + } + if ( idx < 0 ) { + idx += this._length; + } + if ( idx < 0 || idx >= this._length ) { + return; + } + return getUint64( this._buffer, idx ); +}); + /** * Pointer to the underlying data buffer. * diff --git a/lib/node_modules/@stdlib/array/uint64/test/test.at.js b/lib/node_modules/@stdlib/array/uint64/test/test.at.js new file mode 100644 index 000000000000..f8218e5f13a6 --- /dev/null +++ b/lib/node_modules/@stdlib/array/uint64/test/test.at.js @@ -0,0 +1,144 @@ +/** +* @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 hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var isEqual = require( '@stdlib/number/uint64/base/assert/is-equal' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var Uint64 = require( '@stdlib/number/uint64/ctor' ); +var Uint64Array = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof Uint64Array, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the prototype of the main export is an `at` method for returning an array element', function test( t ) { + t.strictEqual( hasOwnProp( Uint64Array.prototype, 'at' ), true, 'has property' ); + t.strictEqual( isFunction( Uint64Array.prototype.at ), true, 'has method' ); + t.end(); +}); + +tape( 'the method throws an error if invoked with a `this` context which is not a 64-bit unsigned integer array instance', function test( t ) { + var values; + var arr; + var i; + + arr = new Uint64Array( 5 ); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + {}, + [] + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.at.call( value, 0 ); + }; + } +}); + +tape( 'the method throws an error if provided an index argument which is not an integer', function test( t ) { + var values; + var arr; + var i; + + arr = new Uint64Array( 10 ); + + values = [ + '5', + 3.14, + NaN, + true, + false, + null, + void 0, + {}, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + return arr.at( value ); + }; + } +}); + +tape( 'the method returns `undefined` if provided an index which exceeds array dimensions', function test( t ) { + var arr; + var v; + var i; + + arr = new Uint64Array( 10 ); + for ( i = -arr.length; i < arr.length; i++ ) { + if ( i < 0 ) { + v = arr.at( i-arr.length ); + t.strictEqual( v, void 0, 'returns expected value for index '+(i-arr.length) ); + } else { + v = arr.at( arr.length+i ); + t.strictEqual( v, void 0, 'returns expected value for index '+(arr.length+i) ); + } + } + t.end(); +}); + +tape( 'the method returns an array element', function test( t ) { + var arr; + var v; + var i; + + arr = []; + for ( i = 0; i < 10; i++ ) { + arr.push( new Uint64( i ) ); + } + arr = new Uint64Array( arr ); + + for ( i = -arr.length; i < arr.length; i++ ) { + v = arr.at( i ); + if ( i < 0 ) { + t.strictEqual( isEqual( v, new Uint64( arr.length+i ) ), true, 'returns expected value for index '+i ); + } else { + t.strictEqual( isEqual( v, new Uint64( i ) ), true, 'returns expected value for index '+i ); + } + } + t.end(); +});