UNPKG

@stdlib/array

Version:
1,740 lines (1,680 loc) 82.3 kB
/* eslint-disable no-restricted-syntax, max-lines, no-invalid-this */ /** * @license Apache-2.0 * * Copyright (c) 2018 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 isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; var isArrayLikeObject = require( '@stdlib/assert/is-array-like-object' ); var isCollection = require( '@stdlib/assert/is-collection' ); var isArrayBuffer = require( '@stdlib/assert/is-arraybuffer' ); var isObject = require( '@stdlib/assert/is-object' ); var isArray = require( '@stdlib/assert/is-array' ); var isStringArray = require( '@stdlib/assert/is-string-array' ).primitives; var isString = require( '@stdlib/assert/is-string' ).isPrimitive; var isFunction = require( '@stdlib/assert/is-function' ); var isComplexLike = require( '@stdlib/assert/is-complex-like' ); var isEven = require( '@stdlib/math/base/assert/is-even' ); var isInteger = require( '@stdlib/math/base/assert/is-integer' ); var isComplex64Array = require( './../../base/assert/is-complex64array' ); var isComplex128Array = require( './../../base/assert/is-complex128array' ); var hasIteratorSymbolSupport = require( '@stdlib/assert/has-iterator-symbol-support' ); var ITERATOR_SYMBOL = require( '@stdlib/symbol/iterator' ); var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' ); var Float32Array = require( './../../float32' ); var Complex64 = require( '@stdlib/complex/float32/ctor' ); var format = require( '@stdlib/string/format' ); var realf = require( '@stdlib/complex/float32/real' ); var imagf = require( '@stdlib/complex/float32/imag' ); var floor = require( '@stdlib/math/base/special/floor' ); var reinterpret64 = require( '@stdlib/strided/base/reinterpret-complex64' ); var reinterpret128 = require( '@stdlib/strided/base/reinterpret-complex128' ); var getter = require( './../../base/getter' ); var accessorGetter = require( './../../base/accessor-getter' ); var fromIterator = require( './from_iterator.js' ); var fromIteratorMap = require( './from_iterator_map.js' ); var fromArray = require( './from_array.js' ); // VARIABLES // var BYTES_PER_ELEMENT = Float32Array.BYTES_PER_ELEMENT * 2; var HAS_ITERATOR_SYMBOL = hasIteratorSymbolSupport(); // FUNCTIONS // /** * Returns a boolean indicating if a value is a complex typed array. * * @private * @param {*} value - value to test * @returns {boolean} boolean indicating if a value is a complex typed array */ function isComplexArray( value ) { return ( value instanceof Complex64Array || ( typeof value === 'object' && value !== null && ( value.constructor.name === 'Complex64Array' || value.constructor.name === 'Complex128Array' ) && typeof value._length === 'number' && // eslint-disable-line no-underscore-dangle // NOTE: we don't perform a more rigorous test here for a typed array for performance reasons, as robustly checking for a typed array instance could require walking the prototype tree and performing relatively expensive constructor checks... typeof value._buffer === 'object' // eslint-disable-line no-underscore-dangle ) ); } /** * Returns a boolean indicating if a value is a complex typed array constructor. * * @private * @param {*} value - value to test * @returns {boolean} boolean indicating if a value is a complex typed array constructor */ function isComplexArrayConstructor( value ) { return ( value === Complex64Array || // NOTE: weaker test in order to avoid a circular dependency with Complex128Array... value.name === 'Complex128Array' ); } /** * Retrieves a complex number from a complex number array buffer. * * @private * @param {Float32Array} buf - array buffer * @param {NonNegativeInteger} idx - element index * @returns {Complex64} complex number */ function getComplex64( buf, idx ) { idx *= 2; return new Complex64( buf[ idx ], buf[ idx+1 ] ); } // MAIN // /** * 64-bit complex number array constructor. * * @constructor * @param {(NonNegativeInteger|Collection|ArrayBuffer|Iterable)} [arg] - length, typed array, array-like object, buffer, or an iterable * @param {NonNegativeInteger} [byteOffset=0] - byte offset * @param {NonNegativeInteger} [length] - view length * @throws {RangeError} ArrayBuffer byte length must be a multiple of `8` * @throws {RangeError} array-like object and typed array input arguments must have a length which is a multiple of two * @throws {TypeError} if provided only a single argument, must provide a valid argument * @throws {TypeError} byte offset must be a nonnegative integer * @throws {RangeError} byte offset must be a multiple of `8` * @throws {TypeError} view length must be a positive multiple of `8` * @throws {RangeError} must provide sufficient memory to accommodate byte offset and view length requirements * @throws {TypeError} an iterator must return either a two element array containing real and imaginary components or a complex number * @returns {Complex64Array} complex number array * * @example * var arr = new Complex64Array(); * // returns <Complex64Array> * * var len = arr.length; * // returns 0 * * @example * var arr = new Complex64Array( 2 ); * // returns <Complex64Array> * * var len = arr.length; * // returns 2 * * @example * var arr = new Complex64Array( [ 1.0, -1.0 ] ); * // returns <Complex64Array> * * var len = arr.length; * // returns 1 * * @example * var ArrayBuffer = require( '@stdlib/array/buffer' ); * * var buf = new ArrayBuffer( 16 ); * var arr = new Complex64Array( buf ); * // returns <Complex64Array> * * var len = arr.length; * // returns 2 * * @example * var ArrayBuffer = require( '@stdlib/array/buffer' ); * * var buf = new ArrayBuffer( 16 ); * var arr = new Complex64Array( buf, 8 ); * // returns <Complex64Array> * * var len = arr.length; * // returns 1 * * @example * var ArrayBuffer = require( '@stdlib/array/buffer' ); * * var buf = new ArrayBuffer( 32 ); * var arr = new Complex64Array( buf, 8, 2 ); * // returns <Complex64Array> * * var len = arr.length; * // returns 2 */ function Complex64Array() { var byteOffset; var nargs; var buf; var len; nargs = arguments.length; if ( !(this instanceof Complex64Array) ) { if ( nargs === 0 ) { return new Complex64Array(); } if ( nargs === 1 ) { return new Complex64Array( arguments[0] ); } if ( nargs === 2 ) { return new Complex64Array( arguments[0], arguments[1] ); } return new Complex64Array( arguments[0], arguments[1], arguments[2] ); } // Create the underlying data buffer... if ( nargs === 0 ) { buf = new Float32Array( 0 ); // backward-compatibility } else if ( nargs === 1 ) { if ( isNonNegativeInteger( arguments[0] ) ) { buf = new Float32Array( arguments[0]*2 ); } else if ( isCollection( arguments[0] ) ) { buf = arguments[ 0 ]; len = buf.length; // If provided a "generic" array, peak at the first value, and, if the value is a complex number, try to process as an array of complex numbers, falling back to "normal" typed array initialization if we fail and ensuring consistency if the first value had not been a complex number... if ( len && isArray( buf ) && isComplexLike( buf[0] ) ) { buf = fromArray( new Float32Array( len*2 ), buf ); if ( buf === null ) { // We failed and we are now forced to allocate a new array :-( if ( !isEven( len ) ) { throw new RangeError( format( 'invalid argument. Array-like object arguments must have a length which is a multiple of two. Length: `%u`.', len ) ); } // We failed, so fall back to directly setting values... buf = new Float32Array( arguments[0] ); } } else { if ( isComplex64Array( buf ) ) { buf = reinterpret64( buf, 0 ); } else if ( isComplex128Array( buf ) ) { buf = reinterpret128( buf, 0 ); } else if ( !isEven( len ) ) { throw new RangeError( format( 'invalid argument. Array-like object and typed array arguments must have a length which is a multiple of two. Length: `%u`.', len ) ); } buf = new Float32Array( buf ); } } else if ( isArrayBuffer( arguments[0] ) ) { buf = arguments[ 0 ]; if ( !isInteger( buf.byteLength/BYTES_PER_ELEMENT ) ) { throw new RangeError( format( 'invalid argument. ArrayBuffer byte length must be a multiple of %u. Byte length: `%u`.', BYTES_PER_ELEMENT, buf.byteLength ) ); } buf = new Float32Array( buf ); } else if ( isObject( arguments[0] ) ) { buf = arguments[ 0 ]; if ( HAS_ITERATOR_SYMBOL === false ) { throw new TypeError( format( 'invalid argument. Environment lacks Symbol.iterator support. Must provide a length, ArrayBuffer, typed array, or array-like object. Value: `%s`.', buf ) ); } if ( !isFunction( buf[ ITERATOR_SYMBOL ] ) ) { throw new TypeError( format( 'invalid argument. Must provide a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', buf ) ); } buf = buf[ ITERATOR_SYMBOL ](); if ( !isFunction( buf.next ) ) { throw new TypeError( format( 'invalid argument. Must provide a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', buf ) ); // FIXME: `buf` is what is returned from above, NOT the original value } buf = fromIterator( buf ); if ( buf instanceof Error ) { throw buf; } buf = new Float32Array( buf ); } else { throw new TypeError( format( 'invalid argument. Must provide a length, ArrayBuffer, typed array, array-like object, or an iterable. Value: `%s`.', arguments[0] ) ); } } else { buf = arguments[ 0 ]; if ( !isArrayBuffer( buf ) ) { throw new TypeError( format( 'invalid argument. First argument must be an ArrayBuffer. Value: `%s`.', buf ) ); } byteOffset = arguments[ 1 ]; if ( !isNonNegativeInteger( byteOffset ) ) { throw new TypeError( format( 'invalid argument. Byte offset must be a nonnegative integer. Value: `%s`.', byteOffset ) ); } if ( !isInteger( byteOffset/BYTES_PER_ELEMENT ) ) { throw new RangeError( format( 'invalid argument. Byte offset must be a multiple of %u. Value: `%u`.', BYTES_PER_ELEMENT, byteOffset ) ); } if ( nargs === 2 ) { len = buf.byteLength - byteOffset; if ( !isInteger( len/BYTES_PER_ELEMENT ) ) { throw new RangeError( format( 'invalid arguments. ArrayBuffer view byte length must be a multiple of %u. View byte length: `%u`.', BYTES_PER_ELEMENT, len ) ); } buf = new Float32Array( buf, byteOffset ); } else { len = arguments[ 2 ]; if ( !isNonNegativeInteger( len ) ) { throw new TypeError( format( 'invalid argument. Length must be a nonnegative integer. Value: `%s`.', len ) ); } if ( (len*BYTES_PER_ELEMENT) > (buf.byteLength-byteOffset) ) { throw new RangeError( format( 'invalid arguments. ArrayBuffer has insufficient capacity. Either decrease the array length or provide a bigger buffer. Minimum capacity: `%u`.', len*BYTES_PER_ELEMENT ) ); } buf = new Float32Array( buf, byteOffset, len*2 ); } } setReadOnly( this, '_buffer', buf ); setReadOnly( this, '_length', buf.length/2 ); return this; } /** * Size (in bytes) of each array element. * * @name BYTES_PER_ELEMENT * @memberof Complex64Array * @readonly * @type {PositiveInteger} * @default 8 * * @example * var nbytes = Complex64Array.BYTES_PER_ELEMENT; * // returns 8 */ setReadOnly( Complex64Array, 'BYTES_PER_ELEMENT', BYTES_PER_ELEMENT ); /** * Constructor name. * * @name name * @memberof Complex64Array * @readonly * @type {string} * @default 'Complex64Array' * * @example * var str = Complex64Array.name; * // returns 'Complex64Array' */ setReadOnly( Complex64Array, 'name', 'Complex64Array' ); /** * Creates a new 64-bit complex number array from an array-like object or an iterable. * * @name from * @memberof Complex64Array * @type {Function} * @param {(Collection|Iterable)} src - array-like object or iterable * @param {Function} [clbk] - callback to invoke for each source element * @param {*} [thisArg] - context * @throws {TypeError} `this` context must be a constructor * @throws {TypeError} `this` must be a complex number array * @throws {TypeError} first argument must be an array-like object or an iterable * @throws {TypeError} second argument must be a function * @throws {RangeError} array-like objects must have a length which is a multiple of two * @throws {TypeError} an iterator must return either a two element array containing real and imaginary components or a complex number * @throws {TypeError} when provided an iterator, a callback must return either a two element array containing real and imaginary components or a complex number * @returns {Complex64Array} 64-bit complex number array * * @example * var arr = Complex64Array.from( [ 1.0, -1.0 ] ); * // returns <Complex64Array> * * var len = arr.length; * // returns 1 * * @example * var Complex64 = require( '@stdlib/complex/float32/ctor' ); * * var arr = Complex64Array.from( [ new Complex64( 1.0, 1.0 ) ] ); * // returns <Complex64Array> * * var len = arr.length; * // returns 1 * * @example * var Complex64 = require( '@stdlib/complex/float32/ctor' ); * var realf = require( '@stdlib/complex/float32/real' ); * var imagf = require( '@stdlib/complex/float32/imag' ); * * function clbk( v ) { * return new Complex64( realf(v)*2.0, imagf(v)*2.0 ); * } * * var arr = Complex64Array.from( [ new Complex64( 1.0, 1.0 ) ], clbk ); * // returns <Complex64Array> * * var len = arr.length; * // returns 1 */ setReadOnly( Complex64Array, 'from', function from( src ) { var thisArg; var nargs; var clbk; var out; var buf; var tmp; var get; var len; var flg; var v; var i; var j; if ( !isFunction( this ) ) { throw new TypeError( 'invalid invocation. `this` context must be a constructor.' ); } if ( !isComplexArrayConstructor( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } nargs = arguments.length; if ( nargs > 1 ) { clbk = arguments[ 1 ]; if ( !isFunction( clbk ) ) { throw new TypeError( format( 'invalid argument. Second argument must be a function. Value: `%s`.', clbk ) ); } if ( nargs > 2 ) { thisArg = arguments[ 2 ]; } } if ( isComplexArray( src ) ) { len = src.length; if ( clbk ) { out = new this( len ); buf = out._buffer; // eslint-disable-line no-underscore-dangle j = 0; for ( i = 0; i < len; i++ ) { v = clbk.call( thisArg, src.get( i ), i ); if ( isComplexLike( v ) ) { buf[ j ] = realf( v ); buf[ j+1 ] = imagf( v ); } else if ( isArrayLikeObject( v ) && v.length >= 2 ) { buf[ j ] = v[ 0 ]; buf[ j+1 ] = v[ 1 ]; } else { throw new TypeError( format( 'invalid argument. Callback must return either a two-element array containing real and imaginary components or a complex number. Value: `%s`.', v ) ); } j += 2; // stride } return out; } return new this( src ); } if ( isCollection( src ) ) { if ( clbk ) { // Note: array contents affect how we iterate over a provided data source. If only complex number objects, we can extract real and imaginary components. Otherwise, for non-complex number arrays (e.g., `Float64Array`, etc), we assume a strided array where real and imaginary components are interleaved. In the former case, we expect a callback to return real and imaginary components (possibly as a complex number). In the latter case, we expect a callback to return *either* a real or imaginary component. len = src.length; if ( src.get && src.set ) { get = accessorGetter( 'default' ); } else { get = getter( 'default' ); } // Detect whether we've been provided an array which returns complex number objects... for ( i = 0; i < len; i++ ) { if ( !isComplexLike( get( src, i ) ) ) { flg = true; break; } } // If an array does not contain only complex number objects, then we assume interleaved real and imaginary components... if ( flg ) { if ( !isEven( len ) ) { throw new RangeError( format( 'invalid argument. First argument must have a length which is a multiple of %u. Length: `%u`.', 2, len ) ); } out = new this( len/2 ); buf = out._buffer; // eslint-disable-line no-underscore-dangle for ( i = 0; i < len; i++ ) { buf[ i ] = clbk.call( thisArg, get( src, i ), i ); } return out; } // If an array contains only complex number objects, then we need to extract real and imaginary components... out = new this( len ); buf = out._buffer; // eslint-disable-line no-underscore-dangle j = 0; for ( i = 0; i < len; i++ ) { v = clbk.call( thisArg, get( src, i ), i ); if ( isComplexLike( v ) ) { buf[ j ] = realf( v ); buf[ j+1 ] = imagf( v ); } else if ( isArrayLikeObject( v ) && v.length >= 2 ) { buf[ j ] = v[ 0 ]; buf[ j+1 ] = v[ 1 ]; } else { throw new TypeError( format( 'invalid argument. Callback must return either a two-element array containing real and imaginary components or a complex number. Value: `%s`.', v ) ); } j += 2; // stride } return out; } return new this( src ); } if ( isObject( src ) && HAS_ITERATOR_SYMBOL && isFunction( src[ ITERATOR_SYMBOL ] ) ) { // eslint-disable-line max-len buf = src[ ITERATOR_SYMBOL ](); if ( !isFunction( buf.next ) ) { throw new TypeError( format( 'invalid argument. First argument must be an array-like object or an iterable. Value: `%s`.', src ) ); } if ( clbk ) { tmp = fromIteratorMap( buf, clbk, thisArg ); } else { tmp = fromIterator( buf ); } if ( tmp instanceof Error ) { throw tmp; } len = tmp.length / 2; out = new this( len ); buf = out._buffer; // eslint-disable-line no-underscore-dangle for ( i = 0; i < len; i++ ) { buf[ i ] = tmp[ i ]; } return out; } throw new TypeError( format( 'invalid argument. First argument must be an array-like object or an iterable. Value: `%s`.', src ) ); }); /** * Creates a new 64-bit complex number array from a variable number of arguments. * * @name of * @memberof Complex64Array * @type {Function} * @param {...*} element - array elements * @throws {TypeError} `this` context must be a constructor * @throws {TypeError} `this` must be a complex number array * @returns {Complex64Array} 64-bit complex number array * * @example * var arr = Complex64Array.of( 1.0, 1.0, 1.0, 1.0 ); * // returns <Complex64Array> * * var len = arr.length; * // returns 2 */ setReadOnly( Complex64Array, 'of', function of() { var args; var i; if ( !isFunction( this ) ) { throw new TypeError( 'invalid invocation. `this` context must be a constructor.' ); } if ( !isComplexArrayConstructor( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } args = []; for ( i = 0; i < arguments.length; i++ ) { args.push( arguments[ i ] ); } return new this( args ); }); /** * Returns an array element with support for both nonnegative and negative integer indices. * * @name at * @memberof Complex64Array.prototype * @type {Function} * @param {integer} idx - element index * @throws {TypeError} `this` must be a complex number array * @throws {TypeError} must provide an integer * @returns {(Complex64|void)} array element * * @example * var arr = new Complex64Array( 10 ); * var realf = require( '@stdlib/complex/float32/real' ); * var imagf = require( '@stdlib/complex/float32/imag' ); * * var z = arr.at( 0 ); * // returns <Complex64> * * var re = realf( z ); * // returns 0.0 * * var im = imagf( z ); * // returns 0.0 * * arr.set( [ 1.0, -1.0 ], 0 ); * arr.set( [ 2.0, -2.0 ], 1 ); * arr.set( [ 9.0, -9.0 ], 9 ); * * z = arr.at( 0 ); * // returns <Complex64> * * re = realf( z ); * // returns 1.0 * * im = imagf( z ); * // returns -1.0 * * z = arr.at( -1 ); * // returns <Complex64> * * re = realf( z ); * // returns 9.0 * * im = imagf( z ); * // returns -9.0 * * z = arr.at( 100 ); * // returns undefined * * z = arr.at( -100 ); * // returns undefined */ setReadOnly( Complex64Array.prototype, 'at', function at( idx ) { if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number 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 getComplex64( this._buffer, idx ); }); /** * Pointer to the underlying data buffer. * * @name buffer * @memberof Complex64Array.prototype * @readonly * @type {ArrayBuffer} * * @example * var arr = new Complex64Array( 10 ); * * var buf = arr.buffer; * // returns <ArrayBuffer> */ setReadOnlyAccessor( Complex64Array.prototype, 'buffer', function get() { return this._buffer.buffer; }); /** * Size (in bytes) of the array. * * @name byteLength * @memberof Complex64Array.prototype * @readonly * @type {NonNegativeInteger} * * @example * var arr = new Complex64Array( 10 ); * * var byteLength = arr.byteLength; * // returns 80 */ setReadOnlyAccessor( Complex64Array.prototype, 'byteLength', function get() { return this._buffer.byteLength; }); /** * Offset (in bytes) of the array from the start of its underlying `ArrayBuffer`. * * @name byteOffset * @memberof Complex64Array.prototype * @readonly * @type {NonNegativeInteger} * * @example * var arr = new Complex64Array( 10 ); * * var byteOffset = arr.byteOffset; * // returns 0 */ setReadOnlyAccessor( Complex64Array.prototype, 'byteOffset', function get() { return this._buffer.byteOffset; }); /** * Size (in bytes) of each array element. * * @name BYTES_PER_ELEMENT * @memberof Complex64Array.prototype * @readonly * @type {PositiveInteger} * @default 8 * * @example * var arr = new Complex64Array( 10 ); * * var nbytes = arr.BYTES_PER_ELEMENT; * // returns 8 */ setReadOnly( Complex64Array.prototype, 'BYTES_PER_ELEMENT', Complex64Array.BYTES_PER_ELEMENT ); /** * Copies a sequence of elements within the array to the position starting at `target`. * * @name copyWithin * @memberof Complex64Array.prototype * @type {Function} * @param {integer} target - index at which to start copying elements * @param {integer} start - source index at which to copy elements from * @param {integer} [end] - source index at which to stop copying elements from * @throws {TypeError} `this` must be a complex number array * @returns {Complex64Array} modified array * * @example * var Complex64 = require( '@stdlib/complex/float32/ctor' ); * var realf = require( '@stdlib/complex/float32/real' ); * var imagf = require( '@stdlib/complex/float32/imag' ); * * var arr = new Complex64Array( 4 ); * * // Set the array elements: * arr.set( new Complex64( 1.0, 1.0 ), 0 ); * arr.set( new Complex64( 2.0, 2.0 ), 1 ); * arr.set( new Complex64( 3.0, 3.0 ), 2 ); * arr.set( new Complex64( 4.0, 4.0 ), 3 ); * * // Copy the first two elements to the last two elements: * arr.copyWithin( 2, 0, 2 ); * * // Get the last array element: * var z = arr.get( 3 ); * * var re = realf( z ); * // returns 2.0 * * var im = imagf( z ); * // returns 2.0 */ setReadOnly( Complex64Array.prototype, 'copyWithin', function copyWithin( target, start ) { if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } // FIXME: prefer a functional `copyWithin` implementation which addresses lack of universal browser support (e.g., IE11 and Safari) or ensure that typed arrays are polyfilled if ( arguments.length === 2 ) { this._buffer.copyWithin( target*2, start*2 ); } else { this._buffer.copyWithin( target*2, start*2, arguments[2]*2 ); } return this; }); /** * Returns an iterator for iterating over array key-value pairs. * * @name entries * @memberof Complex64Array.prototype * @type {Function} * @throws {TypeError} `this` must be a complex number array * @returns {Iterator} iterator * * @example * var Complex64 = require( '@stdlib/complex/float32/ctor' ); * * var arr = [ * new Complex64( 1.0, 1.0 ), * new Complex64( 2.0, 2.0 ), * new Complex64( 3.0, 3.0 ) * ]; * arr = new Complex64Array( arr ); * * // Create an iterator: * var it = arr.entries(); * * // Iterate over the key-value pairs... * var v = it.next().value; * // returns [ 0, <Complex64> ] * * v = it.next().value; * // returns [ 1, <Complex64> ] * * v = it.next().value; * // returns [ 2, <Complex64> ] * * var bool = it.next().done; * // returns true */ setReadOnly( Complex64Array.prototype, 'entries', function entries() { var self; var iter; var len; var buf; var FLG; var i; if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } self = this; buf = this._buffer; len = this._length; // Initialize an iteration index: i = -1; // Create an iterator protocol-compliant object: iter = {}; setReadOnly( iter, 'next', next ); setReadOnly( iter, 'return', end ); if ( ITERATOR_SYMBOL ) { setReadOnly( iter, ITERATOR_SYMBOL, factory ); } return iter; /** * Returns an iterator protocol-compliant object containing the next iterated value. * * @private * @returns {Object} iterator protocol-compliant object */ function next() { i += 1; if ( FLG || i >= len ) { return { 'done': true }; } return { 'value': [ i, getComplex64( buf, i ) ], 'done': false }; } /** * Finishes an iterator. * * @private * @param {*} [value] - value to return * @returns {Object} iterator protocol-compliant object */ function end( value ) { FLG = true; if ( arguments.length ) { return { 'value': value, 'done': true }; } return { 'done': true }; } /** * Returns a new iterator. * * @private * @returns {Iterator} iterator */ function factory() { return self.entries(); } }); /** * Tests whether all elements in an array pass a test implemented by a predicate function. * * @name every * @memberof Complex64Array.prototype * @type {Function} * @param {Function} predicate - test function * @param {*} [thisArg] - predicate function execution context * @throws {TypeError} `this` must be a complex number array * @throws {TypeError} first argument must be a function * @returns {boolean} boolean indicating whether all elements pass a test * * @example * var realf = require( '@stdlib/complex/float32/real' ); * var imagf = require( '@stdlib/complex/float32/imag' ); * * function predicate( v ) { * return ( realf( v ) === imagf( v ) ); * } * * var arr = new Complex64Array( 3 ); * * arr.set( [ 1.0, 1.0 ], 0 ); * arr.set( [ 2.0, 2.0 ], 1 ); * arr.set( [ 3.0, 3.0 ], 2 ); * * var bool = arr.every( predicate ); * // returns true */ setReadOnly( Complex64Array.prototype, 'every', function every( predicate, thisArg ) { var buf; var i; if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } if ( !isFunction( predicate ) ) { throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); } buf = this._buffer; for ( i = 0; i < this._length; i++ ) { if ( !predicate.call( thisArg, getComplex64( buf, i ), i, this ) ) { return false; } } return true; }); /** * Returns a modified typed array filled with a fill value. * * @name fill * @memberof Complex64Array.prototype * @type {Function} * @param {ComplexLike} value - fill value * @param {integer} [start=0] - starting index (inclusive) * @param {integer} [end] - ending index (exclusive) * @throws {TypeError} `this` must be a complex number array * @throws {TypeError} first argument must be a complex number * @throws {TypeError} second argument must be an integer * @throws {TypeError} third argument must be an integer * @returns {Complex64Array} modified array * * @example * var realf = require( '@stdlib/complex/float32/real' ); * var imagf = require( '@stdlib/complex/float32/imag' ); * * var arr = new Complex64Array( 3 ); * * arr.fill( new Complex64( 1.0, 1.0 ), 1 ); * * var z = arr.get( 1 ); * // returns <Complex64> * * var re = realf( z ); * // returns 1.0 * * var im = imagf( z ); * // returns 1.0 * * z = arr.get( 2 ); * // returns <Complex64> * * re = realf( z ); * // returns 1.0 * * im = imagf( z ); * // returns 1.0 */ setReadOnly( Complex64Array.prototype, 'fill', function fill( value, start, end ) { var buf; var len; var idx; var re; var im; var i; if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } if ( !isComplexLike( value ) ) { throw new TypeError( format( 'invalid argument. First argument must be a complex number. Value: `%s`.', value ) ); } buf = this._buffer; len = this._length; if ( arguments.length > 1 ) { if ( !isInteger( start ) ) { throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', start ) ); } if ( start < 0 ) { start += len; if ( start < 0 ) { start = 0; } } if ( arguments.length > 2 ) { if ( !isInteger( end ) ) { throw new TypeError( format( 'invalid argument. Third argument must be an integer. Value: `%s`.', end ) ); } if ( end < 0 ) { end += len; if ( end < 0 ) { end = 0; } } if ( end > len ) { end = len; } } else { end = len; } } else { start = 0; end = len; } re = realf( value ); im = imagf( value ); for ( i = start; i < end; i++ ) { idx = 2*i; buf[ idx ] = re; buf[ idx+1 ] = im; } return this; }); /** * Returns a new array containing the elements of an array which pass a test implemented by a predicate function. * * @name filter * @memberof Complex64Array.prototype * @type {Function} * @param {Function} predicate - test function * @param {*} [thisArg] - predicate function execution context * @throws {TypeError} `this` must be a complex number array * @throws {TypeError} first argument must be a function * @returns {Complex64Array} complex number array * * @example * var realf = require( '@stdlib/complex/float32/real' ); * var imagf = require( '@stdlib/complex/float32/imag' ); * * function predicate( v ) { * return ( realf( v ) === imagf( v ) ); * } * * var arr = new Complex64Array( 3 ); * * arr.set( [ 1.0, -1.0 ], 0 ); * arr.set( [ 2.0, 2.0 ], 1 ); * arr.set( [ 3.0, -3.0 ], 2 ); * * var out = arr.filter( predicate ); * // returns <Complex64Array> * * var len = out.length; * // returns 1 * * var z = out.get( 0 ); * // returns <Complex64> * * var re = realf( z ); * // returns 2.0 * * var im = imagf( z ); * // returns 2.0 */ setReadOnly( Complex64Array.prototype, 'filter', function filter( predicate, thisArg ) { var buf; var out; var i; var z; if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } if ( !isFunction( predicate ) ) { throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); } buf = this._buffer; out = []; for ( i = 0; i < this._length; i++ ) { z = getComplex64( buf, i ); if ( predicate.call( thisArg, z, i, this ) ) { out.push( z ); } } return new this.constructor( out ); }); /** * Returns the first element in an array for which a predicate function returns a truthy value. * * @name find * @memberof Complex64Array.prototype * @type {Function} * @param {Function} predicate - test function * @param {*} [thisArg] - predicate function execution context * @throws {TypeError} `this` must be a complex number array * @throws {TypeError} first argument must be a function * @returns {(Complex64|void)} array element or undefined * * @example * var realf = require( '@stdlib/complex/float32/real' ); * var imagf = require( '@stdlib/complex/float32/imag' ); * var Complex64 = require( '@stdlib/complex/float32/ctor' ); * * function predicate( v ) { * return ( realf( v ) === imagf( v ) ); * } * * var arr = new Complex64Array( 3 ); * * arr.set( [ 1.0, 1.0 ], 0 ); * arr.set( [ 2.0, 2.0 ], 1 ); * arr.set( [ 3.0, 3.0 ], 2 ); * * var z = arr.find( predicate ); * // returns <Complex64> * * var re = realf( z ); * // returns 1.0 * * var im = imagf( z ); * // returns 1.0 */ setReadOnly( Complex64Array.prototype, 'find', function find( predicate, thisArg ) { var buf; var i; var z; if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } if ( !isFunction( predicate ) ) { throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); } buf = this._buffer; for ( i = 0; i < this._length; i++ ) { z = getComplex64( buf, i ); if ( predicate.call( thisArg, z, i, this ) ) { return z; } } }); /** * Returns the index of the first element in an array for which a predicate function returns a truthy value. * * @name findIndex * @memberof Complex64Array.prototype * @type {Function} * @param {Function} predicate - test function * @param {*} [thisArg] - predicate function execution context * @throws {TypeError} `this` must be a complex number array * @throws {TypeError} first argument must be a function * @returns {integer} index or -1 * * @example * var Complex64 = require( '@stdlib/complex/float32/ctor' ); * var realf = require( '@stdlib/complex/float32/real' ); * var imagf = require( '@stdlib/complex/float32/imag' ); * * function predicate( v ) { * return ( realf( v ) === imagf( v ) ); * } * * var arr = new Complex64Array( 3 ); * * arr.set( [ 1.0, -1.0 ], 0 ); * arr.set( [ 2.0, -2.0 ], 1 ); * arr.set( [ 3.0, 3.0 ], 2 ); * * var idx = arr.findIndex( predicate ); * // returns 2 */ setReadOnly( Complex64Array.prototype, 'findIndex', function findIndex( predicate, thisArg ) { var buf; var i; var z; if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } if ( !isFunction( predicate ) ) { throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); } buf = this._buffer; for ( i = 0; i < this._length; i++ ) { z = getComplex64( buf, i ); if ( predicate.call( thisArg, z, i, this ) ) { return i; } } return -1; }); /** * Returns the last element in an array for which a predicate function returns a truthy value. * * @name findLast * @memberof Complex64Array.prototype * @type {Function} * @param {Function} predicate - test function * @param {*} [thisArg] - predicate function execution context * @throws {TypeError} `this` must be a complex number array * @throws {TypeError} first argument must be a function * @returns {(Complex64|void)} array element or undefined * * @example * var realf = require( '@stdlib/complex/float32/real' ); * var imagf = require( '@stdlib/complex/float32/imag' ); * var Complex64 = require( '@stdlib/complex/float32/ctor' ); * * function predicate( v ) { * return ( realf( v ) === imagf( v ) ); * } * * var arr = new Complex64Array( 3 ); * * arr.set( [ 1.0, 1.0 ], 0 ); * arr.set( [ 2.0, 2.0 ], 1 ); * arr.set( [ 3.0, 3.0 ], 2 ); * * var z = arr.findLast( predicate ); * // returns <Complex64> * * var re = realf( z ); * // returns 3.0 * * var im = imagf( z ); * // returns 3.0 */ setReadOnly( Complex64Array.prototype, 'findLast', function findLast( predicate, thisArg ) { var buf; var i; var z; if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } if ( !isFunction( predicate ) ) { throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); } buf = this._buffer; for ( i = this._length-1; i >= 0; i-- ) { z = getComplex64( buf, i ); if ( predicate.call( thisArg, z, i, this ) ) { return z; } } }); /** * Returns the index of the last element in an array for which a predicate function returns a truthy value. * * @name findLastIndex * @memberof Complex64Array.prototype * @type {Function} * @param {Function} predicate - test function * @param {*} [thisArg] - predicate function execution context * @throws {TypeError} `this` must be a complex number array * @throws {TypeError} first argument must be a function * @returns {integer} index or -1 * * @example * var Complex64 = require( '@stdlib/complex/float32/ctor' ); * var realf = require( '@stdlib/complex/float32/real' ); * var imagf = require( '@stdlib/complex/float32/imag' ); * * function predicate( v ) { * return ( realf( v ) === imagf( v ) ); * } * * var arr = new Complex64Array( 3 ); * * arr.set( [ 1.0, 1.0 ], 0 ); * arr.set( [ 2.0, 2.0 ], 1 ); * arr.set( [ 3.0, -3.0 ], 2 ); * * var idx = arr.findLastIndex( predicate ); * // returns 1 */ setReadOnly( Complex64Array.prototype, 'findLastIndex', function findLastIndex( predicate, thisArg ) { var buf; var i; var z; if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } if ( !isFunction( predicate ) ) { throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', predicate ) ); } buf = this._buffer; for ( i = this._length-1; i >= 0; i-- ) { z = getComplex64( buf, i ); if ( predicate.call( thisArg, z, i, this ) ) { return i; } } return -1; }); /** * Invokes a function once for each array element. * * @name forEach * @memberof Complex64Array.prototype * @type {Function} * @param {Function} fcn - function to invoke * @param {*} [thisArg] - function invocation context * @throws {TypeError} `this` must be a complex number array * @throws {TypeError} first argument must be a function * * @example * var Complex64 = require( '@stdlib/complex/float32/ctor' ); * * function log( v, i ) { * console.log( '%s: %s', i, v.toString() ); * } * * var arr = new Complex64Array( 3 ); * * arr.set( [ 1.0, 1.0 ], 0 ); * arr.set( [ 2.0, 2.0 ], 1 ); * arr.set( [ 3.0, 3.0 ], 2 ); * * arr.forEach( log ); */ setReadOnly( Complex64Array.prototype, 'forEach', function forEach( fcn, thisArg ) { var buf; var i; var z; if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } if ( !isFunction( fcn ) ) { throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) ); } buf = this._buffer; for ( i = 0; i < this._length; i++ ) { z = getComplex64( buf, i ); fcn.call( thisArg, z, i, this ); } }); /** * Returns an array element. * * @name get * @memberof Complex64Array.prototype * @type {Function} * @param {NonNegativeInteger} idx - element index * @throws {TypeError} `this` must be a complex number array * @throws {TypeError} must provide a nonnegative integer * @returns {(Complex64|void)} array element * * @example * var arr = new Complex64Array( 10 ); * var realf = require( '@stdlib/complex/float32/real' ); * var imagf = require( '@stdlib/complex/float32/imag' ); * * var z = arr.get( 0 ); * // returns <Complex64> * * var re = realf( z ); * // returns 0.0 * * var im = imagf( z ); * // returns 0.0 * * arr.set( [ 1.0, -1.0 ], 0 ); * * z = arr.get( 0 ); * // returns <Complex64> * * re = realf( z ); * // returns 1.0 * * im = imagf( z ); * // returns -1.0 * * z = arr.get( 100 ); * // returns undefined */ setReadOnly( Complex64Array.prototype, 'get', function get( idx ) { if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } if ( !isNonNegativeInteger( idx ) ) { throw new TypeError( format( 'invalid argument. Must provide a nonnegative integer. Value: `%s`.', idx ) ); } if ( idx >= this._length ) { return; } return getComplex64( this._buffer, idx ); }); /** * Returns a boolean indicating whether an array includes a provided value. * * @name includes * @memberof Complex64Array.prototype * @type {Function} * @param {ComplexLike} searchElement - search element * @param {integer} [fromIndex=0] - starting index (inclusive) * @throws {TypeError} `this` must be a complex number array * @throws {TypeError} first argument must be a complex number * @throws {TypeError} second argument must be an integer * @returns {boolean} boolean indicating whether an array includes a provided value * * @example * var Complex64 = require( '@stdlib/complex/float32/ctor' ); * * var arr = new Complex64Array( 5 ); * * arr.set( [ 1.0, -1.0 ], 0 ); * arr.set( [ 2.0, -2.0 ], 1 ); * arr.set( [ 3.0, -3.0 ], 2 ); * arr.set( [ 4.0, -4.0 ], 3 ); * arr.set( [ 5.0, -5.0 ], 4 ); * * var bool = arr.includes( new Complex64( 3.0, -3.0 ) ); * // returns true * * bool = arr.includes( new Complex64( 3.0, -3.0 ), 3 ); * // returns false * * bool = arr.includes( new Complex64( 4.0, -4.0 ), -3 ); * // returns true */ setReadOnly( Complex64Array.prototype, 'includes', function includes( searchElement, fromIndex ) { var buf; var idx; var re; var im; var i; if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } if ( !isComplexLike( searchElement ) ) { throw new TypeError( format( 'invalid argument. First argument must be a complex number. Value: `%s`.', searchElement ) ); } if ( arguments.length > 1 ) { if ( !isInteger( fromIndex ) ) { throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) ); } if ( fromIndex < 0 ) { fromIndex += this._length; if ( fromIndex < 0 ) { fromIndex = 0; } } } else { fromIndex = 0; } re = realf( searchElement ); im = imagf( searchElement ); buf = this._buffer; for ( i = fromIndex; i < this._length; i++ ) { idx = 2 * i; if ( re === buf[ idx ] && im === buf[ idx+1 ] ) { return true; } } return false; }); /** * Returns the first index at which a given element can be found. * * @name indexOf * @memberof Complex64Array.prototype * @type {Function} * @param {ComplexLike} searchElement - element to find * @param {integer} [fromIndex=0] - starting index (inclusive) * @throws {TypeError} `this` must be a complex number array * @throws {TypeError} first argument must be a complex number * @throws {TypeError} second argument must be an integer * @returns {integer} index or -1 * * @example * var Complex64 = require( '@stdlib/complex/float32/ctor' ); * * var arr = new Complex64Array( 10 ); * * arr.set( [ 1.0, -1.0 ], 0 ); * arr.set( [ 2.0, -2.0 ], 1 ); * arr.set( [ 3.0, -3.0 ], 2 ); * arr.set( [ 4.0, -4.0 ], 3 ); * arr.set( [ 5.0, -5.0 ], 4 ); * * var idx = arr.indexOf( new Complex64( 3.0, -3.0 ) ); * // returns 2 * * idx = arr.indexOf( new Complex64( 3.0, -3.0 ), 3 ); * // returns -1 * * idx = arr.indexOf( new Complex64( 4.0, -4.0 ), -3 ); * // returns -1 */ setReadOnly( Complex64Array.prototype, 'indexOf', function indexOf( searchElement, fromIndex ) { var buf; var idx; var re; var im; var i; if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } if ( !isComplexLike( searchElement ) ) { throw new TypeError( format( 'invalid argument. First argument must be a complex number. Value: `%s`.', searchElement ) ); } if ( arguments.length > 1 ) { if ( !isInteger( fromIndex ) ) { throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) ); } if ( fromIndex < 0 ) { fromIndex += this._length; if ( fromIndex < 0 ) { fromIndex = 0; } } } else { fromIndex = 0; } re = realf( searchElement ); im = imagf( searchElement ); buf = this._buffer; for ( i = fromIndex; i < this._length; i++ ) { idx = 2 * i; if ( re === buf[ idx ] && im === buf[ idx+1 ] ) { return i; } } return -1; }); /** * Returns a new string by concatenating all array elements. * * @name join * @memberof Complex64Array.prototype * @type {Function} * @param {string} [separator=','] - element separator * @throws {TypeError} `this` must be a complex number array * @throws {TypeError} first argument must be a string * @returns {string} string representation * * @example * var arr = new Complex64Array( 2 ); * * arr.set( [ 1.0, 1.0 ], 0 ); * arr.set( [ 2.0, 2.0 ], 1 ); * * var str = arr.join(); * // returns '1 + 1i,2 + 2i' * * str = arr.join( '/' ); * // returns '1 + 1i/2 + 2i' */ setReadOnly( Complex64Array.prototype, 'join', function join( separator ) { var out; var buf; var sep; var i; if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } if ( arguments.length === 0 ) { sep = ','; } else if ( isString( separator ) ) { sep = separator; } else { throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', separator ) ); } out = []; buf = this._buffer; for ( i = 0; i < this._length; i++ ) { out.push( getComplex64( buf, i ).toString() ); } return out.join( sep ); }); /** * Returns an iterator for iterating over each index key in a typed array. * * @name keys * @memberof Complex64Array.prototype * @type {Function} * @throws {TypeError} `this` must be a complex number array * @returns {Iterator} iterator * * @example * var arr = new Complex64Array( 2 ); * * arr.set( [ 1.0, 1.0 ], 0 ); * arr.set( [ 2.0, 2.0 ], 1 ); * * var iter = arr.keys(); * * var v = iter.next().value; * // returns 0 * * v = iter.next().value; * // returns 1 * * var bool = iter.next().done; * // returns true */ setReadOnly( Complex64Array.prototype, 'keys', function keys() { var self; var iter; var len; var FLG; var i; if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } self = this; len = this._length; // Initialize an iteration index: i = -1; // Create an iterator protocol-compliant object: iter = {}; setReadOnly( iter, 'next', next ); setReadOnly( iter, 'return', end ); if ( ITERATOR_SYMBOL ) { setReadOnly( iter, ITERATOR_SYMBOL, factory ); } return iter; /** * Returns an iterator protocol-compliant object containing the next iterated value. * * @private * @returns {Object} iterator protocol-compliant object */ function next() { i += 1; if ( FLG || i >= len ) { return { 'done': true }; } return { 'value': i, 'done': false }; } /** * Finishes an iterator. * * @private * @param {*} [value] - value to return * @returns {Object} iterator protocol-compliant object */ function end( value ) { FLG = true; if ( arguments.length ) { return { 'value': value, 'done': true }; } return { 'done': true }; } /** * Returns a new iterator. * * @private * @returns {Iterator} iterator */ function factory() { return self.keys(); } }); /** * Returns the last index at which a given element can be found. * * @name lastIndexOf * @memberof Complex64Array.prototype * @type {Function} * @param {ComplexLike} searchElement - element to find * @param {integer} [fromIndex] - index at which to start searching backward (inclusive) * @throws {TypeError} `this` must be a complex number array * @throws {TypeError} first argument must be a complex number * @throws {TypeError} second argument must be an integer * @returns {integer} index or -1 * * @example * var Complex64 = require( '@stdlib/complex/float32/ctor' ); * * var arr = new Complex64Array( 5 ); * * arr.set( [ 1.0, -1.0 ], 0 ); * arr.set( [ 2.0, -2.0 ], 1 ); * arr.set( [ 3.0, -3.0 ], 2 ); * arr.set( [ 4.0, -4.0 ], 3 ); * arr.set( [ 3.0, -3.0 ], 4 ); * * var idx = arr.lastIndexOf( new Complex64( 3.0, -3.0 ) ); * // returns 4 * * idx = arr.lastIndexOf( new Complex64( 3.0, -3.0 ), 3 ); * // returns 2 * * idx = arr.lastIndexOf( new Complex64( 5.0, -5.0 ), 3 ); * // returns -1 * * idx = arr.lastIndexOf( new Complex64( 2.0, -2.0 ), -3 ); * // returns 1 */ setReadOnly( Complex64Array.prototype, 'lastIndexOf', function lastIndexOf( searchElement, fromIndex ) { var buf; var idx; var re; var im; var i; if ( !isComplexArray( this ) ) { throw new TypeError( 'invalid invocation. `this` is not a complex number array.' ); } if ( !isComplexLike( searchElement ) ) { throw new TypeError( format( 'invalid argument. First argument must be a complex number. Value: `%s`.', searchElement ) ); } if ( arguments.length > 1 ) { if ( !isInteger( fromIndex ) ) { throw new TypeError( format( 'invalid argument. Second argument must be an integer. Value: `%s`.', fromIndex ) ); } if ( fromIndex >= this._length ) { fromIndex = this._length - 1; } else if ( fromIndex < 0 ) { fromIndex += this._length; } } else { fromIndex = this._length - 1; } re = realf( searchElement ); im = imagf( searchElement ); buf = this._buffer; for ( i = fromIndex; i >= 0; i-- ) { idx = 2 * i; if ( re === buf[ idx ] && im === buf[ idx+1 ] ) { return i; } } return -1; }); /** * Number of array elements. * * @name length * @memberof Complex64Array.prototype * @readonly * @type {NonNegativeInteger} * * @example * var arr = new Complex64Array( 10 ); * * var len = arr.length; * // returns 10 */ setReadOnlyAccessor( Complex64Array.prototype, 'length', function get() { return this._length; }); /** * Returns a new array with each element being the result of a provided callback function. * * @name map * @memberof Complex64Array.prototype * @type {Function} * @param {Function} fcn - callback function * @param {*} [thisArg] - callback function execution context * @throws {TypeError} `this` must be a complex number array * @throws {TypeError} first argument m