Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Uint64Array

64-bit unsigned integer array.

Usage

var Uint64Array = require( '@stdlib/array/uint64' );

Uint64Array()

Creates a 64-bit unsigned integer array.

var arr = new Uint64Array();
// returns <Uint64Array>

Uint64Array( length )

Creates a 64-bit unsigned integer array having a specified length.

var arr = new Uint64Array( 5 );
// returns <Uint64Array>[ 0n, 0n, 0n, 0n, 0n ]

Uint64Array( typedarray )

Creates a 64-bit unsigned integer array from another typed array.

var Uint32Array = require( '@stdlib/array/uint32' );

var arr1 = new Uint32Array( [ 5, 5, 5 ] );
var arr2 = new Uint64Array( arr1 );
// returns <Uint64Array>[ 5n, 5n, 5n ]

Uint64Array( obj )

Creates a 64-bit unsigned integer array from an array-like object or iterable.

var arr = new Uint64Array( [ 5.0, 5.0, 5.0 ] );
// returns <Uint64Array>[ 5n, 5n, 5n ]

Uint64Array( buffer[, byteOffset[, length]] )

Returns a 64-bit unsigned integer array view of an ArrayBuffer.

var ArrayBuffer = require( '@stdlib/array/buffer' );

var buf = new ArrayBuffer( 32 );
var arr = new Uint64Array( buf, 0, 4 );
// returns <Uint64Array>[ 0n, 0n, 0n, 0n ]

Properties

Uint64Array.BYTES_PER_ELEMENT

Static property returning the size (in bytes) of each array element.

var nbytes = Uint64Array.BYTES_PER_ELEMENT;
// returns 8

Uint64Array.name

Static property returning the constructor name.

var str = Uint64Array.name;
// returns 'Uint64Array'

Uint64Array.prototype.buffer

Read-only property which returns the underlying ArrayBuffer referenced by array instance.

var arr = new Uint64Array( 5 );

var buf = arr.buffer;
// returns <ArrayBuffer>

Uint64Array.prototype.byteLength

Read-only property which returns the length (in bytes) of the array instance.

var arr = new Uint64Array( 5 );

var byteLength = arr.byteLength;
// returns 40

Uint64Array.prototype.byteOffset

Read-only property which returns the offset (in bytes) of the array instance from the start of its underlying ArrayBuffer.

var arr = new Uint64Array( 5 );

var byteOffset = arr.byteOffset;
// returns 0

Uint64Array.prototype.BYTES_PER_ELEMENT

Size (in bytes) of each array element.

var arr = new Uint64Array( 5 );

var nbytes = arr.BYTES_PER_ELEMENT;
// returns 8

Uint64Array.prototype.length

Read-only property which returns the number of array elements.

var arr = new Uint64Array( 5 );

var len = arr.length;
// returns 5

Methods

Uint64Array.from( src[, map[, thisArg]] )

Creates a new 64-bit unsigned integer array from an array-like object or an iterable.

var arr = Uint64Array.from( [ 1, 2 ] );
// returns <Uint64Array>[ 1n, 2n ]

To invoke a function for each src value, provide a callback function.

function mapFcn( v ) {
    return v * 2;
}

var arr = Uint64Array.from( [ 1, 2 ], mapFcn );
// returns <Uint64Array>[ 2n, 4n ]

A callback function is provided two arguments:

  • value: source value.
  • index: source index.

To set the callback execution context, provide a thisArg.

function mapFcn( v ) {
    this.count += 1;
    return v * 2;
}

var ctx = {
    'count': 0
};

var arr = Uint64Array.from( [ 1, 2 ], mapFcn, ctx );
// returns <Uint64Array>[ 2n, 4n ]

var n = ctx.count;
// returns 2

Uint64Array.of( element0[, element1[, ...elementN]] )

Creates a new 64-bit unsigned integer array from a variable number of arguments.

var arr = Uint64Array.of( 1, 2 );
// returns <Uint64Array>[ 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.

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 <Uint64>[ 1n ]

// Get the last element:
z = arr.at( -1 );
// returns <Uint64>[ 9n ]

If provided an out-of-bounds index, the method returns undefined.

var arr = new Uint64Array( 10 );

var z = arr.at( 100 );
// returns undefined

z = arr.at( -100 );
// returns undefined

Uint64Array.prototype.entries()

Returns an iterator for iterating over array key-value pairs.

var Uint64 = require( '@stdlib/number/uint64/ctor' );

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

// Create an iterator:
var it = arr.entries();

// Iterate over the key-value pairs...
var v = it.next().value;
// returns [ 0, <Uint64>[ 1n ] ]

v = it.next().value;
// returns [ 1, <Uint64>[ 2n ] ]

v = it.next().value;
// returns [ 2, <Uint64>[ 3n ] ]

var bool = it.next().done;
// returns true

The returned iterator protocol-compliant object has the following properties:

  • next: function which returns an iterator protocol-compliant object containing the next iterated value (if one exists) assigned to a value property and a done property having a boolean value indicating whether the iterator is finished.
  • return: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object.

Uint64Array.prototype.get( i )

Returns an array element located at position (index) i.

var arr = new Uint64Array( 10 );

// Set the first element:
arr.set( 1, 0 );

// Get the first element:
var z = arr.get( 0 );
// returns <Uint64>[ 1n ]

If provided an out-of-bounds index, the method returns undefined.

var arr = new Uint64Array( 10 );

var z = arr.get( 100 );
// returns undefined

Uint64Array.prototype.set( value[, i] )

Sets one or more array elements.

var Uint64 = require( '@stdlib/number/uint64/ctor' );

var arr = new Uint64Array( [ 1, 2, 3 ] );
// returns <Uint64Array>[ 1n, 2n, 3n ]

// Get the first element:
var z = arr.get( 0 );
// returns <Uint64>[ 1n ]

// Set the first element:
arr.set( new Uint64( 5 ) );

// Get the first element:
z = arr.get( 0 );
// returns <Uint64>[ 5n ]

By default, the method sets array elements starting at position (index) i = 0. To set elements starting elsewhere in the array, provide an index argument i.

var Uint64 = require( '@stdlib/number/uint64/ctor' );

var arr = new Uint64Array( [ 1, 2, 3 ] );
// returns <Uint64Array>[ 1n, 2n, 3n ]

// Get the third element:
var z = arr.get( 2 );
// returns <Uint64>[ 3n ]

// Set the third element:
arr.set( new Uint64( 5 ), 2 );

// Get the third element:
z = arr.get( 2 );
// returns <Uint64>[ 5n ]

In addition to providing a scalar value (e.g., nonnegative integer, bigint, or Uint64), to set one or more array elements, provide an array-like object containing scalar values

var Uint64 = require( '@stdlib/number/uint64/ctor' );

var arr = new Uint64Array( [ 1, 2, 3 ] );
// returns <Uint64Array>[ 1n, 2n, 3n ]

// Set the first two array elements:
arr.set( [ 4, 5 ] );

var z = arr.get( 0 );
// returns <Uint64>[ 4n ]

z = arr.get( 1 );
// returns <Uint64>[ 5n ]

A few notes:

  • If i is out-of-bounds, the method throws an error.
  • If a target array cannot accommodate all values (i.e., the length of source array plus i exceeds the target array length), the method throws an error.
  • If provided a typed array which shares an ArrayBuffer with the target array, the method will intelligently copy the source range to the destination range.


Notes

  • While a Uint64Array strives to maintain (but does not guarantee) consistency with typed arrays, significant deviations from ECMAScript-defined typed array behavior are as follows:

    • The constructor does not require the new operator.
    • The constructor and associated methods support a broader variety of input argument types in order to better accommodate unsigned integer input.
    • Accessing array elements using bracket syntax (e.g., X[i]) is not supported. Instead, one must use the .get() method.
    • The set method has extended behavior in order to support 64-bit unsigned integer instances.

Examples

var Uint64 = require( '@stdlib/number/uint64/ctor' );
var logEach = require( '@stdlib/console/log-each' );
var Uint32Array = require( '@stdlib/array/uint32' );
var Uint64Array = require( '@stdlib/array/uint64' );

// Create a 64-bit unsigned integer array by specifying a length:
var out = new Uint64Array( 3 );
logEach( '%s', out );

// Create a 64-bit unsigned integer array from an array of 64-bit unsigned integer numbers:
var arr = [
    new Uint64( 1 ),
    new Uint64( 2 ),
    new Uint64( 3 )
];
out = new Uint64Array( arr );
logEach( '%s', out );

// 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 );

// Create a 64-bit unsigned integer array from an array buffer, where underlying storage consists of interleaved high and low 32-bit words:
arr = new Uint32Array( [ 1, 2, 3, 4 ] );
out = new Uint64Array( arr.buffer );
logEach( '%s', out );

// Create a 64-bit unsigned integer array from an array buffer view, where underlying storage represents each 64-bit integer as interleaved high and low 32-bit words:
arr = new Uint32Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] );
out = new Uint64Array( arr.buffer, 16, 2 );
logEach( '%s', out );