Skip to content
Open
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
76 changes: 76 additions & 0 deletions lib/node_modules/@stdlib/array/uint64/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,82 @@ var count = context.count;
// returns 3
```

<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-reduce-right"></a>

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

Applies a provided callback function to each element of the array, in reverse order, passing in the return value from the calculation on the following 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.reduceRight( 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 last element in the array and passes the second-last array element as `value` during the first invocation of the provided callback. To begin accumulation from a different starting value and pass in the last 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.reduceRight( 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,80 @@
/**
* @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();
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* @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:reduceRight', 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.reduceRight( 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:reduceRight', 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.reduceRight( 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();
});
Loading
Loading