Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions lib/node_modules/@stdlib/array/uint64/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,44 @@ var u = arr.get( 100 );
// returns undefined
```

<a name="method-reduce"></a>

#### Uint64Array.prototype.reduce( reducerFn\[, initialValue] )

Applies a provided callback function to each element of the array, in order, passing in the return value from the calculation on the preceding element and returning the accumulated result upon completion.

```javascript
var add = require( '@stdlib/number/uint64/base/add' );

var arr = new Uint64Array( [ 1, 2, 3 ] );

var u = arr.reduce( add );
// returns <Uint64>[ 6n ]
```

The reducer function is provided four arguments:

- **acc**: accumulated result.
- **value**: current array element.
- **index**: current array element index.
- **arr**: the array on which this method was called.

By default, the function initializes the accumulated result to the first element in the array and passes the second array element as `value` during the first invocation of the provided callback. To begin accumulation from a different starting value and pass in the first array element as `value` during the first invocation of the provided callback, provide an `initialValue` argument.

```javascript
var Number = require( '@stdlib/number/ctor' );

function reducer( acc, v ) {
acc += Number( v );
return acc;
}

var arr = new Uint64Array( [ 1, 2, 3 ] );

var sum = arr.reduce( reducer, 2 );
// returns 8
```

<a name="method-set"></a>

#### Uint64Array.prototype.set( value\[, i] )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* @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 isUint64 = require( '@stdlib/assert/is-uint64' );
var add = require( '@stdlib/number/uint64/base/add' );
var Uint64 = require( '@stdlib/number/uint64/ctor' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var Uint64Array = require( './../lib' );


// MAIN //

bench( format( '%s:reduce', pkg ), function benchmark( b ) {
var out;
var arr;
var i;

arr = new Uint64Array( 10 );

// Benchmark worst case scenario (i.e., every element is visited and passed to the reducer)...
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = arr.reduce( add );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isUint64( out ) ) {
b.fail( 'should return a 64-bit unsigned integer' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( format( '%s::initial_value:reduce', pkg ), function benchmark( b ) {
var out;
var arr;
var v;
var i;

arr = new Uint64Array( 10 );

v = new Uint64( 0 );

// Benchmark worst case scenario (i.e., every element is visited and passed to the reducer)...
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = arr.reduce( add, v );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isUint64( out ) ) {
b.fail( 'should return a 64-bit unsigned integer' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* @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 pow = require( '@stdlib/math/base/special/pow' );
var isUint64 = require( '@stdlib/assert/is-uint64' );
var add = require( '@stdlib/number/uint64/base/add' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var Uint64Array = require( './../lib' );


// FUNCTIONS //

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var arr = new Uint64Array( len );
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var out;
var i;

// Benchmark worst case scenario (i.e., every element is visited and passed to the reducer)...
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
out = arr.reduce( add );
if ( typeof out !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isUint64( out ) ) {
b.fail( 'should return a 64-bit unsigned integer' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( format( '%s:reduce:len=%d', pkg, len ), f );
}
}

main();
41 changes: 41 additions & 0 deletions lib/node_modules/@stdlib/array/uint64/docs/repl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,47 @@
<Uint64>[ 2n ]


{{alias}}.prototype.reduce( reducerFn[, initialValue] )
Applies a provided function to each element of the array, in order, passing
in the return value from the calculation on the preceding element and
returning the accumulated result upon completion.

A reducer function is provided the following arguments:

- acc: accumulated result.
- value: current array element.
- index: current array element index.
- arr: the array on which the method was called.

If provided an initial value, the method invokes a provided function with
the initial value as the first argument and the first array element as the
second argument.

If not provided an initial value, the method invokes a provided function
with the first array element as the first argument and the second array
element as the second argument.

Parameters
----------
reducerFn: Function
Function to apply to each array element.

initialValue: any (optional)
Initial accumulation value.

Returns
-------
out: any
Accumulated result.

Examples
--------
> var arr = new {{alias}}( [ 1, 2, 3 ] )
<Uint64Array>[ 1n, 2n, 3n ]
> var u = arr.reduce( {{alias:@stdlib/number/uint64/base/add}} )
<Uint64>[ 6n ]


{{alias}}.prototype.set( v[, i] )
Sets one or more array elements.

Expand Down
17 changes: 17 additions & 0 deletions lib/node_modules/@stdlib/array/uint64/docs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import ArrayBuffer = require( '@stdlib/array/buffer' );

// Define a union type representing both iterable and non-iterable iterators:
type Iterator = Iter | IterableIterator;

Check failure on line 31 in lib/node_modules/@stdlib/array/uint64/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

'Iterator' is defined but never used

// Define a type representing a value from which a 64-bit unsigned integer can be derived:
type Uint = Uint64 | bigint | number;
Expand All @@ -36,7 +36,7 @@
/**
* Locale-specific configuration options.
*/
interface LocaleOptions<T = unknown> {

Check failure on line 39 in lib/node_modules/@stdlib/array/uint64/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

'LocaleOptions' is defined but never used
/**
* Configuration property.
*/
Expand Down Expand Up @@ -118,7 +118,7 @@
* @param arr - array on which the method was called
* @returns boolean indicating whether an element in an array passes a test
*/
type Predicate<U> = NullaryPredicate<U> | UnaryPredicate<U> | BinaryPredicate<U> | TernaryPredicate<U>;

Check failure on line 121 in lib/node_modules/@stdlib/array/uint64/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

'Predicate' is defined but never used

/**
* Callback invoked for each element in an array.
Expand Down Expand Up @@ -162,7 +162,7 @@
* @param arr - array on which the method was called
* @returns undefined
*/
type Callback<U> = NullaryCallback<U> | UnaryCallback<U> | BinaryCallback<U> | TernaryCallback<U>;

Check failure on line 165 in lib/node_modules/@stdlib/array/uint64/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

'Callback' is defined but never used

/**
* Callback invoked for each element in an array.
Expand Down Expand Up @@ -206,7 +206,7 @@
* @param arr - array on which the method was called
* @returns transformed value
*/
type MapFcn<U> = NullaryMapFcn<U> | UnaryMapFcn<U> | BinaryMapFcn<U> | TernaryMapFcn<U>;

Check failure on line 209 in lib/node_modules/@stdlib/array/uint64/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

'MapFcn' is defined but never used

/**
* Reducer function invoked for each element in an array.
Expand Down Expand Up @@ -269,7 +269,7 @@
* @param b - second value for comparison
* @returns number indicating comparison result
*/
type CompareFcn = ( a: Uint64, b: Uint64 ) => number;

Check failure on line 272 in lib/node_modules/@stdlib/array/uint64/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

'CompareFcn' is defined but never used

/**
* Class for creating a 64-bit unsigned integer array.
Expand Down Expand Up @@ -477,6 +477,23 @@
*/
get( i: number ): Uint64 | void;

/**
* Applies a provided callback function to each element of the array, in order, passing in the return value from the calculation on the preceding element and returning the accumulated result upon completion.
*
* @param reducer - callback function
* @param initialValue - initial value
* @returns accumulated result
*
* @example
* var add = require( '@stdlib/number/uint64/base/add' );
*
* var arr = new Uint64Array( [ 1, 2, 3 ] );
*
* var u = arr.reduce( add );
* // returns <Uint64>[ 6n ]
*/
reduce<U = unknown>( reducer: Reducer<U>, initialValue?: U ): U;

/**
* Sets an array element.
*
Expand Down
52 changes: 52 additions & 0 deletions lib/node_modules/@stdlib/array/uint64/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,58 @@
return this._length;
});

/**
* Applies a provided callback function to each element of the array, in order, passing in the return value from the calculation on the preceding element and returning the accumulated result upon completion.
*
* @name reduce
* @memberof Uint64Array.prototype
* @type {Function}
* @param {Function} reducer - callback function
* @param {*} [initialValue] - initial value
* @throws {TypeError} `this` must be a 64-bit unsigned integer array
* @throws {TypeError} first argument must be a function
* @throws {Error} if not provided an initial value, the array must have at least one element
* @returns {*} accumulated result
*
* @example
* var add = require( '@stdlib/number/uint64/base/add' );
*
* var arr = new Uint64Array( [ 1, 2, 3 ] );
*
* var u = arr.reduce( add );
* // returns <Uint64>[ 6n ]
*/
setReadOnly( Uint64Array.prototype, 'reduce', function reduce( reducer, initialValue ) {
var buf;
var acc;
var len;
var v;
var i;
if ( !isUint64Array( this ) ) {
throw new TypeError( 'invalid invocation. `this` is not a 64-bit unsigned integer array.' );
}
if ( !isFunction( reducer ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', reducer ) );
}
buf = this._buffer;
len = this._length;
if ( arguments.length > 1 ) {
acc = initialValue;
i = 0;
} else {
if ( len === 0 ) {
throw new Error( 'invalid operation. If not provided an initial value, an array must contain at least one element.' );
}
acc = getUint64( buf, 0 );
i = 1;
}
for ( ; i < len; i++ ) {
v = getUint64( buf, i );
acc = reducer( acc, v, i, this );
}
return acc;
});

/**
* Sets an array element.
*
Expand Down Expand Up @@ -839,7 +891,7 @@
buf[ idx+indices.LOW ] = words[ 1 ];
return;
}
if ( isNonNegativeInteger( value ) || ( isBigInt( value ) && isNonNegativeInteger( Number( value ) ) ) ) {

Check warning on line 894 in lib/node_modules/@stdlib/array/uint64/lib/main.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

This line has a length of 110. Maximum allowed is 80
if ( idx >= this._length ) {
throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%u`.', idx ) );
}
Expand Down
Loading
Loading